-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcsocket.cpp
327 lines (257 loc) · 6.59 KB
/
csocket.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#include "csocket.h"
#ifdef _WIN32
#include <io.h>
#pragma comment(lib, "Ws2_32.lib")
#else
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/errno.h>
#include <unistd.h>
#endif
#if __APPLE__
#include <netdb.h>
#include <arpa/inet.h>
#endif
#undef INVALID_SOCKET
#define INVALID_SOCKET (unsigned int)(~0)
#define BLOCK_RETRY_INTERVAL_MSECS 1
#define ERROR_INAPPROPRIATE_STATE -1
static bool s_bSocketsInitialized = false;;
csocket::csocket() : m_socketState(CLOSED),
m_remotePort(0)
{
if ( ! s_bSocketsInitialized )
{
#ifdef _WIN32
WORD socketVersion;
WSADATA socketData;
int err;
socketVersion = MAKEWORD(2, 0);
err = WSAStartup(socketVersion, &socketData);
#endif
}
m_socket = 0;
}
csocket::~csocket()
{
if ( m_socketState != csocket::CLOSED )
{
#ifdef _WIN32
closesocket(m_socket);
#else
::close(m_socket);
#endif
}
}
int csocket::resolveHost(const std::string& szRemoteHostName, struct hostent** pHostEnt)
{
if (szRemoteHostName.length() == 0)
{
return FAILURE;
}
if ( ! s_bSocketsInitialized )
{
#ifdef _WIN32
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(2, 0);
err = WSAStartup(wVersionRequested, &wsaData);
#endif
}
*pHostEnt = gethostbyname( szRemoteHostName.c_str() );
if (*pHostEnt == NULL)
{
unsigned long uhostname = inet_addr ( szRemoteHostName.c_str() );
*pHostEnt = gethostbyaddr( reinterpret_cast<char *>(&uhostname),
sizeof(unsigned long),
AF_INET);
if (*pHostEnt == NULL)
{
return FAILURE;
}
}
return SUCCESS;
}
int csocket::connect( const char* remoteHost, unsigned int remotePort )
{
if ( m_socketState != CLOSED )
{
return ERROR_INAPPROPRIATE_STATE;
}
struct hostent *pHostEnt = NULL;
int status = resolveHost(remoteHost, &pHostEnt);
if (status == FAILURE || !pHostEnt)
{
return FAILURE;
}
m_strRemoteHost = remoteHost;
m_remotePort = remotePort;
m_remoteSocketAddr.sin_family = pHostEnt->h_addrtype;
memcpy((char *) &m_remoteSocketAddr.sin_addr.s_addr,
pHostEnt->h_addr_list[0],
pHostEnt->h_length);
m_remoteSocketAddr.sin_port = htons(m_remotePort);
#ifdef _WIN32
m_socket = WSASocket(AF_INET, SOCK_STREAM,
IPPROTO_TCP, 0 , 0 , 0);
if (m_socket == INVALID_SOCKET)
{
return FAILURE;
}
#else
m_socket = socket(AF_INET, SOCK_STREAM, 0);
#endif
if (m_socket < 0)
{
return FAILURE;
}
m_localSocketAddr.sin_family = AF_INET;
m_localSocketAddr.sin_addr.s_addr = htonl(INADDR_ANY);
int iRecvTimeout = 1;
setsockopt(m_socket, SOL_SOCKET, SO_RCVTIMEO,
(char*) &iRecvTimeout, sizeof(iRecvTimeout) );
m_localSocketAddr.sin_port = htons(0);
status = bind( m_socket,
(const sockaddr*)&(m_localSocketAddr),
sizeof(struct sockaddr) );
if (status < 0)
{
return FAILURE;
}
#ifdef _WIN32
int set = 1;
setsockopt(m_socket, IPPROTO_TCP, TCP_NODELAY, (char*) &set, sizeof(set) );
#endif
m_remoteSocketAddr.sin_family = pHostEnt->h_addrtype;
memcpy(&(m_remoteSocketAddr.sin_addr),
pHostEnt->h_addr,
pHostEnt->h_length);
m_remoteSocketAddr.sin_port = htons( m_remotePort );
status = ::connect(m_socket,
(const sockaddr*)&(m_remoteSocketAddr),
sizeof(m_remoteSocketAddr) );
if (status < 0)
{
return FAILURE;
}
m_socketState = CONNECTED;
return SUCCESS;
}
int csocket::canRead( bool* readyToRead, float waitTime )
{
if (m_socketState != CONNECTED )
{
return ERROR_INAPPROPRIATE_STATE;
}
fd_set fds;
FD_ZERO(&fds);
FD_SET(m_socket, &fds);
int nfds = 0;
timeval timeout;
if ( waitTime <= 0.0f )
{
timeout.tv_sec = 0;
timeout.tv_usec = 0;
}
else
{
timeout.tv_sec = (int)(waitTime);
timeout.tv_usec = (int)(1000000.0f *
(waitTime - (float)timeout.tv_sec));
}
#ifdef _WIN32
nfds = m_socket+1;
#endif
int n = select(nfds, &fds, NULL, NULL, &timeout);
if ( n < 0 )
{
m_socketState = ERRORED;
return FAILURE;
}
if (n == 1)
{
*readyToRead = true;
return SUCCESS;
}
if (n == 0)
{
*readyToRead = false;
}
return SUCCESS;
}
int csocket::read( char* pDataBuffer, unsigned int numBytesToRead, bool bReadAll )
{
if (m_socketState != CONNECTED )
{
return ERROR_INAPPROPRIATE_STATE;
}
int numBytesRemaining = numBytesToRead;
int numBytesRead = 0;
do
{
#ifdef _WIN32
numBytesRead = recv( m_socket, pDataBuffer, numBytesRemaining, 0 );
#else
numBytesRead = static_cast<int>(::read( m_socket, pDataBuffer, numBytesRemaining));
#endif
if (numBytesRead < 0)
{
if ( bReadAll )
{
#ifdef _WIN32
Sleep(BLOCK_RETRY_INTERVAL_MSECS);
#else
usleep(BLOCK_RETRY_INTERVAL_MSECS * 1000);
#endif
}
else
{
return numBytesRead;
}
}
if ( numBytesRead > 0 )
{
numBytesRemaining -= numBytesRead;
pDataBuffer += numBytesRead;
}
if (numBytesRead == 0)
{
break;
}
} while ((numBytesRemaining > 0) && (bReadAll) );
return(numBytesToRead - numBytesRemaining);
}
int csocket::write( const char* pDataBuffer, unsigned int numBytesToWrite )
{
if (m_socketState != CONNECTED )
{
return ERROR_INAPPROPRIATE_STATE;
}
int numBytesRemaining = numBytesToWrite;
int numBytestWritten = 0;
while (numBytesRemaining > 0)
{
#ifdef _WIN32
numBytestWritten = send( m_socket, pDataBuffer, numBytesRemaining, 0 );
#else
numBytestWritten= ::write( m_socket, pDataBuffer, numBytesRemaining );
#endif
if (numBytestWritten < 0)
{
m_socketState = ERRORED;
return numBytestWritten;
}
numBytesRemaining -= numBytestWritten;
pDataBuffer += numBytestWritten;
}
return(numBytesToWrite - numBytesRemaining);
}
csocket::SocketState csocket::getState( void ) const
{
return m_socketState;
}