forked from alexander-sholohov/rtlmuxer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sink_list.cpp
152 lines (128 loc) · 3.89 KB
/
sink_list.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//
// Author: Alexander Sholohov <[email protected]>
//
// License: MIT
//
#include <stdio.h>
#include <unistd.h>
#include <netdb.h>
#include <assert.h>
#include "sink_list.h"
//---------------------------------------------------------------------------------------------------
void CSinkList::addSink(int socket, size_t bufferSize, bool backReadEnabled)
{
CTcpSink item(socket, bufferSize, backReadEnabled);
m_items.push_back(item);
}
//---------------------------------------------------------------------------------------------------
CTcpSink& CSinkList::getLastSink()
{
// we never call this method with empty list
assert(m_items.size() > 0);
return m_items[m_items.size() - 1];
}
//---------------------------------------------------------------------------------------------------
void CSinkList::putData(const char* buf, size_t len)
{
for( std::vector<CTcpSink>::iterator it=m_items.begin(); it!=m_items.end(); it++ )
{
CTcpSink& item = *it;
if( !item.isNeedDelete() )
{
item.putData(buf, len);
}
}
}
//---------------------------------------------------------------------------------------------------
void CSinkList::fillRdSet(fd_set& set) const
{
for( std::vector<CTcpSink>::const_iterator it=m_items.begin(); it!=m_items.end(); it++ )
{
CTcpSink const& item = *it;
if( !item.isNeedDelete() )
{
FD_SET( item.getSocket(), &set);
}
}
}
//---------------------------------------------------------------------------------------------------
void CSinkList::fillWrSet(fd_set& set) const
{
for( std::vector<CTcpSink>::const_iterator it=m_items.begin(); it!=m_items.end(); it++ )
{
CTcpSink const& item = *it;
if( !item.isNeedDelete() && item.isWritePending() )
{
FD_SET( item.getSocket(), &set);
}
}
}
//---------------------------------------------------------------------------------------------------
void CSinkList::processRead(fd_set& set, CBuffer& backBuffer)
{
for( std::vector<CTcpSink>::iterator it=m_items.begin(); it!=m_items.end(); it++ )
{
CTcpSink& item = *it;
if( FD_ISSET(item.getSocket(), &set))
{
char buf[8192];
int readed = ::read(item.getSocket(), buf, sizeof(buf));
if( readed <= 0 )
{
item.markForDelete();
}
else
{
// process read
if( item.isBackReadEnabled() )
{
backBuffer.put(buf, readed);
}
}
}
}
}
//---------------------------------------------------------------------------------------------------
void CSinkList::processWrite(fd_set& set)
{
for( std::vector<CTcpSink>::iterator it=m_items.begin(); it!=m_items.end(); it++ )
{
CTcpSink& item = *it;
if( FD_ISSET(item.getSocket(), &set) && !item.isNeedDelete() )
{
item.doWrite();
}
}
}
//---------------------------------------------------------------------------------------------------
void CSinkList::cleanupTask()
{
for( size_t i=0; i<m_items.size(); )
{
CTcpSink& item = m_items[i];
if( item.isNeedDelete() )
{
printf("will del sink %d\n", item.getSocket());
item.close();
m_items.erase( m_items.begin() + i );
}
else
{
i++;
}
}
}
//---------------------------------------------------------------------------------------------------
int CSinkList::calcMaxFd(int incomingMaxFd) const
{
int res = incomingMaxFd;
for( std::vector<CTcpSink>::const_iterator it=m_items.begin(); it!=m_items.end(); it++ )
{
CTcpSink const& item = *it;
if( item.getSocket() > res )
{
res = item.getSocket();
}
}
return res;
}