source: cheroxy/trunk/src/CRXSocket.cpp@ 58

Last change on this file since 58 was 58, checked in by cheese, 11 years ago

#1 revert missed code for windows

File size: 10.2 KB
RevLine 
[6]1/**
2 * CRXSocket.cpp
3 */
4#include "CRXSocket.h"
5
6#include <string.h>
7
8#ifdef _WIN32
9# pragma comment (lib, "ws2_32.lib")
10# define close(__socket) closesocket(__socket)
11#endif
12
13bool CRXSocket::mInitialized = false;
14
15CRXSocket::CRXSocket (int aSocket)
16 : mSocket (aSocket)
17{
18 /*----------------------------------------------------------------*/
[24]19 CRXSocket::Initialize ();
20
[14]21 memset ((void *)&mAddress, 0x0, sizeof (struct sockaddr_in));
[6]22 /*----------------------------------------------------------------*/
23}
24
25CRXSocket::~CRXSocket (void)
26{
27 /*----------------------------------------------------------------*/
28 Close ();
29 /*----------------------------------------------------------------*/
30}
31
32void
33CRXSocket::Attach (int aSocket)
34{
35 /*----------------------------------------------------------------*/
36 mSocket = aSocket;
37 /*----------------------------------------------------------------*/
38}
39
40int
41CRXSocket::Detach (void)
42{
43 int aSocket = mSocket;
44
45 /*----------------------------------------------------------------*/
46 mSocket = 0;
47
48 /*----------------------------------------------------------------*/
49 return aSocket;
50}
51
52void
53CRXSocket::Close (void)
54{
55 /*----------------------------------------------------------------*/
56 if (mSocket <= 0)
57 return ;
58
59 close (mSocket);
60 mSocket = 0;
61 /*----------------------------------------------------------------*/
62}
63
64CRXSocket::operator int (void) const
65{
66 /*----------------------------------------------------------------*/
67 /*----------------------------------------------------------------*/
68 return mSocket;
69}
70
71CRXSocket &
72CRXSocket::operator = (int aSocket)
73{
74 /*----------------------------------------------------------------*/
75 Attach (aSocket);
76 /*----------------------------------------------------------------*/
77 return *this;
78}
79
80int
[24]81CRXSocket::SetTimeout (const int aTimeout)
82{
83 int aResult = 0;
84
85#ifndef _WIN32
86 struct timeval aTimeVal;
87 aTimeVal.tv_sec = aTimeout;
88 aTimeVal.tv_usec= 0;
89#else
90 int aTimeMilliSec = aTimeout * 1000;
91#endif
92
93 /*----------------------------------------------------------------*/
94 if (!IsReady ())
95 return ERROR_TCPSOCKET_NOT_READY;
96
97 if (aTimeout == TCPSOCKET_NO_TIMEOUT)
98 return 0;
99
100 aResult = setsockopt (mSocket,
101 SOL_SOCKET,
102 SO_RCVTIMEO,
103#ifdef _WIN32
104 (char *) &aTimeMilliSec,
[58]105 sizeof (aTimeMilliSec));
[24]106#else
107 &aTimeVal,
[58]108 (socklen_t) sizeof (aTimeVal));
[57]109#endif
[24]110 if (aResult < 0)
111 {
[57]112 aResult = ERROR_TCPSOCKET_FAILED_TO_SET_SOCKOPT;
[35]113 CRX_ERROR_SET (aResult, "Failed to set socket option.");
[24]114 }
115 /*----------------------------------------------------------------*/
116
117 return aResult;
118}
119
120int
[6]121CRXSocket::Initialize (void)
122{
123 int aResult = 0;
124
125 /*----------------------------------------------------------------*/
126 if (CRXSocket::IsInitialized ())
127 return aResult;
128
129#ifdef WIN32
130 WSADATA aWinSockData;
131
132 aResult = WSAStartup (MAKEWORD (2, 0), &aWinSockData);
133 if (aResult != 0)
134 {
[18]135 aResult = ERROR_TCPSOCKET_FAILED_TO_INITIALIZE;
136 return aResult;
[6]137 }
138#endif
139
140 mInitialized = true;
141
142 /*----------------------------------------------------------------*/
143 return aResult;
144}
145
146void
147CRXSocket::Finalize (void)
148{
149 /*----------------------------------------------------------------*/
150#ifdef WIN32
151 WSACleanup ();
152#endif
153 /*----------------------------------------------------------------*/
154}
155
156bool
157CRXSocket::IsInitialized (void)
158{
159 /*----------------------------------------------------------------*/
160 /*----------------------------------------------------------------*/
161 return mInitialized;
162}
163
164bool
165CRXSocket::IsReady (void) const
166{
167 /*----------------------------------------------------------------*/
168 /*----------------------------------------------------------------*/
169 return IsInitialized () && IsCreated ();
170}
171
172bool
173CRXSocket::IsCreated (void) const
174{
175 /*----------------------------------------------------------------*/
176 /*----------------------------------------------------------------*/
177 return mSocket > 0 ? true : false;
178}
179
180int
181CRXSocket::CreateSocket (void)
182{
183 int aResult = -1;
184
185 /*----------------------------------------------------------------*/
186 if (!IsInitialized () || IsCreated ())
[18]187 {
188 aResult = ERROR_TCPSOCKET_ALREADY_IN_USE;
[35]189 CRX_ERROR_SET (aResult, "Already in use.");
[18]190 return aResult;
191 }
[6]192
193 aResult = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
194 if (aResult < 0)
195 {
[18]196 aResult = ERROR_TCPSOCKET_FAILED_TO_CREATE_SOCKET;
[35]197 CRX_ERROR_SET (aResult, "Failed to create socket(%d).", GetSystemErrorCode ());
[18]198 return aResult;
[6]199 }
200
201 mSocket = aResult;
202
203 /*----------------------------------------------------------------*/
204 return aResult;
205}
206
207int
[14]208CRXSocket::Connect (const std::string aUrl,
209 const unsigned short aPort,
210 const int aTimeout)
[6]211{
212 int aResult = -1;
213 struct hostent * aHostEnt;
[57]214 struct linger aLinger;
[6]215
216 /*----------------------------------------------------------------*/
217 aResult = CreateSocket ();
218 if (aResult < 0)
219 {
[24]220 aResult = ERROR_TCPSOCKET_FAILED_TO_CREATE_SOCKET;
[35]221 CRX_ERROR_SET (aResult, "Failed to create socket(%d).", GetSystemErrorCode ());
[6]222 return aResult;
223 }
224
225 mAddress.sin_family = AF_INET;
226 mAddress.sin_port = htons (aPort);
227 mAddress.sin_addr.s_addr = inet_addr (aUrl.c_str ());
228
229 if (mAddress.sin_addr.s_addr == (unsigned int)-1)
230 {
231 aHostEnt = gethostbyname (aUrl.c_str ());
232 if (aHostEnt == NULL)
233 {
[18]234 aResult = ERROR_TCPSOCKET_FAILED_TO_GET_HOSTNAME;
[35]235 CRX_ERROR_SET (aResult, "Failed to get hostname.");
[18]236 return aResult;
[6]237 }
238 mAddress.sin_family = aHostEnt->h_addrtype;
239 memcpy (&(mAddress.sin_addr.s_addr), aHostEnt->h_addr, aHostEnt->h_length);
240 }
241
[57]242 aLinger.l_onoff = 1;
243 aLinger.l_linger = 0;
244 aResult = setsockopt (mSocket,
245 SOL_SOCKET,
246 SO_LINGER,
247#ifdef _WIN32
248 (char *) &aLinger,
249#else
250 &aLinger,
251#endif
252 (socklen_t) sizeof (aLinger));
253 if (aResult < 0)
254 {
255 aResult = ERROR_TCPSOCKET_FAILED_TO_SET_SOCKOPT;
256 CRX_ERROR_SET (aResult, "Failed to set socket option (linger).");
257 }
258
[24]259 aResult = SetTimeout (aTimeout);
260 if (aResult < 0)
[14]261 {
[57]262 aResult = ERROR_TCPSOCKET_FAILED_TO_SET_TIMEOUT;
[35]263 CRX_ERROR_SET (aResult, "Failed to set timeout (%d).", GetSystemErrorCode ());
[24]264 return aResult;
[14]265 }
266
267 aResult = connect (mSocket, (struct sockaddr*) &mAddress, sizeof (mAddress));
[6]268 if (aResult < 0)
269 {
[18]270 aResult = ERROR_TCPSOCKET_FAILED_TO_CONNECT;
[35]271 CRX_ERROR_SET (aResult, "Failed to connect (%d).", GetSystemErrorCode ());
[6]272 }
273
274 /*----------------------------------------------------------------*/
275 return aResult;
276}
277
278int
[14]279CRXSocket::CreateServer (const unsigned short aPort,
280 const int aBacklog,
281 struct sockaddr_in * aAddress)
[6]282{
283 int aResult = -1;
284
[57]285 int aReuseAddress = 1;
286
[6]287 /*----------------------------------------------------------------*/
288 aResult = CreateSocket ();
289 if (aResult < 0)
290 {
[24]291 aResult = ERROR_TCPSOCKET_FAILED_TO_CREATE_SOCKET;
[35]292 CRX_ERROR_SET (aResult, "Failed to create socket(%d).", GetSystemErrorCode ());
[6]293 return aResult;
294 }
295
[57]296 aResult = setsockopt (mSocket,
297 SOL_SOCKET,
298 SO_REUSEADDR,
299#ifdef _WIN32
300 (char *) &aReuseAddress,
301#else
302 &aReuseAddress,
303#endif
304 (socklen_t) sizeof (aReuseAddress));
305 if (aResult < 0)
306 {
307 aResult = ERROR_TCPSOCKET_FAILED_TO_SET_SOCKOPT;
308 CRX_ERROR_SET (aResult, "Failed to set socket option (reuseaddr).");
309 }
310
[6]311 mAddress.sin_family = AF_INET;
312 mAddress.sin_addr.s_addr= htonl (INADDR_ANY);
313 mAddress.sin_port = htons (aPort);
314
[14]315 aResult = bind (mSocket, (struct sockaddr *)&mAddress, sizeof (struct sockaddr));
[6]316 if (aResult < 0)
317 {
[18]318 aResult = ERROR_TCPSOCKET_FAILED_TO_BIND;
[35]319 CRX_ERROR_SET (aResult, "Failed to bind(%d).", GetSystemErrorCode ());
[18]320 return aResult;
[6]321 }
322
323 aResult = listen (mSocket, aBacklog);
324 if (aResult < 0)
325 {
[18]326 aResult = ERROR_TCPSOCKET_FAILED_TO_LISTEN;
[35]327 CRX_ERROR_SET (aResult, "Failed to listen(%d).", GetSystemErrorCode ());
[18]328 return aResult;
[6]329 }
330
331 if (aAddress != NULL)
332 {
[14]333 memset ((void *)aAddress, 0x0, sizeof (struct sockaddr_in));
334 memcpy ((void *)aAddress, (void *)&mAddress, sizeof (struct sockaddr_in));
[6]335 }
336
337 /*----------------------------------------------------------------*/
338 return aResult;
339}
340
341int
[14]342CRXSocket::Accept (struct sockaddr_in * aRemoteAddress,
343 int * aAddressLength)
[6]344{
[14]345 int aResult = -1;
[6]346
[14]347 struct sockaddr_in aAddress;
348 socklen_t aLength = sizeof (aAddress);
[6]349
350 /*----------------------------------------------------------------*/
[24]351 if (!IsReady ())
352 return ERROR_TCPSOCKET_NOT_READY;
[6]353
[14]354 aResult = accept (mSocket, (struct sockaddr *) &aAddress, &aLength);
[6]355 if (aResult < 0)
356 {
[18]357 aResult = ERROR_TCPSOCKET_FAILED_TO_ACCEPT;
[35]358 CRX_ERROR_SET (aResult, "Failed to accept(%d).", GetSystemErrorCode ());
[6]359 return aResult;
360 }
361
362 if (aRemoteAddress != NULL)
363 {
[14]364 memset ((void *)aRemoteAddress, 0x0, sizeof (struct sockaddr_in));
365 memcpy ((void *)aRemoteAddress, (void *)&aAddress, sizeof (struct sockaddr_in));
[6]366 }
367
368 if (aAddressLength != NULL)
369 *aAddressLength = aLength;
370
371 /*----------------------------------------------------------------*/
372 return aResult;
373}
374
375int
[24]376CRXSocket::Send (const char * aBuffer,
377 int aSize)
[6]378{
379 int aResult = -1;
380
381 /*----------------------------------------------------------------*/
[24]382 if (!IsReady ())
383 return ERROR_TCPSOCKET_NOT_READY;
[6]384
385 aResult = send (mSocket, aBuffer, aSize, 0);
386 if (aResult != aSize)
387 {
[18]388 aResult = ERROR_TCPSOCKET_FAILED_TO_SEND;
[35]389 CRX_ERROR_SET (aResult, "Failed to send(%d).", GetSystemErrorCode ());
[18]390 return aResult;
[6]391 }
392
393 /*----------------------------------------------------------------*/
394 return aResult;
395}
396
397int
398CRXSocket::Receive (char * aBuffer,
399 int aSize)
400{
401 int aResult = -1;
402
403 /*----------------------------------------------------------------*/
[24]404 if (!IsReady ())
405 return ERROR_TCPSOCKET_NOT_READY;
[6]406
407 aResult = recv (mSocket, aBuffer, aSize, 0);
408 if (aResult < 0)
409 {
[18]410 aResult = ERROR_TCPSOCKET_FAILED_TO_RECEIVE;
[35]411 CRX_ERROR_SET (aResult, "Failed to receive(%d).", GetSystemErrorCode ());
[18]412 return aResult;
[6]413 }
414
415 /*----------------------------------------------------------------*/
416 return aResult;
417}
Note: See TracBrowser for help on using the repository browser.