forked from alexander-sholohov/rtlmuxer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp_source.cpp
243 lines (190 loc) · 5.98 KB
/
tcp_source.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
//
// Author: Alexander Sholohov <[email protected]>
//
// License: MIT
//
#include <stdio.h>
#include <unistd.h>
#include <netdb.h>
#include <cstring>
#include <errno.h>
#include <sstream>
#include <errno.h>
#include "tcp_source.h"
#define IDENTIFIER_SIZE (12)
//---------------------------------------------------------------------------------------
CTcpSource::CTcpSource(size_t bufferSize, std::string const& address, unsigned port)
: m_socket(0)
, m_address( address )
, m_port( port )
, m_isConnected(false)
, m_identifierBuffer(IDENTIFIER_SIZE)
, m_identifierPresent(false)
, m_buffer(bufferSize)
{
}
//---------------------------------------------------------------------------------------
int CTcpSource::calcMaxFd(int incomingMaxFd) const
{
return (m_socket > incomingMaxFd)? m_socket : incomingMaxFd;
}
//---------------------------------------------------------------------------------------
bool CTcpSource::checkConnected()
{
int sock_result;
socklen_t result_len = sizeof(sock_result);
if (getsockopt(m_socket, SOL_SOCKET, SO_ERROR, &sock_result, &result_len) < 0) {
// error, fail somehow, close socket
return false;
}
return sock_result == 0;
}
//---------------------------------------------------------------------------------------
void CTcpSource::connectSync()
{
m_isConnected = false;
m_socket = ::socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
if (m_socket < 0)
{
fprintf(stderr,"ERROR opening socket\n");
return;
}
int rc;
hostent *server;
server = gethostbyname(m_address.c_str());
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
return;
}
struct sockaddr_in serv_addr;
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(m_port);
// connection procedure
// http://stackoverflow.com/questions/10187347/async-connect-and-disconnect-with-epoll-linux/10194883#10194883
rc = connect(m_socket,(struct sockaddr *) &serv_addr,sizeof(serv_addr));
if (rc < 0 && errno != EINPROGRESS) {
// error, fail somehow, close socket
fprintf(stderr,"ERROR connecting\n");
return;
}
if (rc == 0) {
// connection has succeeded immediately
m_isConnected = true;
return;
}
// connection attempt is in progress
fd_set wr_set;
FD_ZERO(&wr_set);
FD_SET(m_socket, &wr_set);
struct timeval timeout;
timeout.tv_sec = 20;
timeout.tv_usec = 0;
rc = ::select(m_socket + 1, NULL, &wr_set, NULL, &timeout);
int sock_result;
socklen_t result_len = sizeof(sock_result);
if (getsockopt(m_socket, SOL_SOCKET, SO_ERROR, &sock_result, &result_len) < 0) {
// error, fail somehow, close socket
close(m_socket);
return;
}
if (sock_result != 0) {
// connection failed; error code is in 'result'
close(m_socket);
return;
}
struct linger ling = {1,0};
setsockopt(m_socket, SOL_SOCKET, SO_LINGER, (char *)&ling, sizeof(ling));
m_identifierPresent = false;
m_identifierBuffer.reset();
m_isConnected = true;
}
//---------------------------------------------------------------------------------------
void CTcpSource::putData(const char* buf, size_t len)
{
m_buffer.put(buf, len);
doWrite();
}
//---------------------------------------------------------------------------------------
void CTcpSource::doWrite()
{
printf("in src doWrite (%d)\n", m_socket);
size_t len = m_buffer.bytesAvailable();
if( len == 0 )
return;
std::vector<char> tmpBuf(len);
m_buffer.peek(&tmpBuf[0], len);
// TODO: remove
printf("Data to write: ");
for( size_t i=0; i<len; i++ )
{
printf("%02X ", tmpBuf[i]);
}
printf("\n");fflush(stdout);
int written = ::send(m_socket, &tmpBuf[0], len, MSG_NOSIGNAL);
if( written < 0 )
{
if( errno != EWOULDBLOCK )
{
// some error happen
printf("src socket returns error on write. close.\n");fflush(stdout);
close( m_socket );
m_isConnected = false;
}
return;
}
m_buffer.consume( written );
}
//---------------------------------------------------------------------------------------
bool CTcpSource::isWritePending() const
{
return m_buffer.bytesAvailable() > 0;
}
//---------------------------------------------------------------------------------------
unsigned CTcpSource::doRead(char* buf, size_t maxLen)
{
int readed = ::read(m_socket, buf, maxLen );
if( readed <= 0 )
{
printf("readed = %d, close src socket.\n", readed);
m_isConnected = false;
close(m_socket);
return 0;
}
// read rls_tcp specific identifier
if( !m_identifierPresent && IDENTIFIER_SIZE > 0 )
{
int available = IDENTIFIER_SIZE - m_identifierBuffer.bytesAvailable();
int len = (readed > available) ? available : readed;
m_identifierBuffer.put( buf, len );
if( m_identifierBuffer.bytesAvailable() == IDENTIFIER_SIZE )
m_identifierPresent = true;
}
return readed;
}
//---------------------------------------------------------------------------------------
void CTcpSource::fillIdentifier( std::vector<char>& data ) const
{
data.resize(IDENTIFIER_SIZE);
m_identifierBuffer.peek(data, IDENTIFIER_SIZE);
}
//---------------------------------------------------------------------------------------
void CTcpSource::fillWrSet(fd_set& set) const
{
if( isWritePending() )
{
FD_SET(m_socket, &set);
}
}
//---------------------------------------------------------------------------------------
std::string CTcpSource::printableAddress() const
{
std::ostringstream out;
out << m_address;
out << ":";
out << m_port;
return out.str();
}