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
Line 
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 /*----------------------------------------------------------------*/
19 CRXSocket::Initialize ();
20
21 memset ((void *)&mAddress, 0x0, sizeof (struct sockaddr_in));
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
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,
105 sizeof (aTimeMilliSec));
106#else
107 &aTimeVal,
108 (socklen_t) sizeof (aTimeVal));
109#endif
110 if (aResult < 0)
111 {
112 aResult = ERROR_TCPSOCKET_FAILED_TO_SET_SOCKOPT;
113 CRX_ERROR_SET (aResult, "Failed to set socket option.");
114 }
115 /*----------------------------------------------------------------*/
116
117 return aResult;
118}
119
120int
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 {
135 aResult = ERROR_TCPSOCKET_FAILED_TO_INITIALIZE;
136 return aResult;
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 ())
187 {
188 aResult = ERROR_TCPSOCKET_ALREADY_IN_USE;
189 CRX_ERROR_SET (aResult, "Already in use.");
190 return aResult;
191 }
192
193 aResult = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
194 if (aResult < 0)
195 {
196 aResult = ERROR_TCPSOCKET_FAILED_TO_CREATE_SOCKET;
197 CRX_ERROR_SET (aResult, "Failed to create socket(%d).", GetSystemErrorCode ());
198 return aResult;
199 }
200
201 mSocket = aResult;
202
203 /*----------------------------------------------------------------*/
204 return aResult;
205}
206
207int
208CRXSocket::Connect (const std::string aUrl,
209 const unsigned short aPort,
210 const int aTimeout)
211{
212 int aResult = -1;
213 struct hostent * aHostEnt;
214 struct linger aLinger;
215
216 /*----------------------------------------------------------------*/
217 aResult = CreateSocket ();
218 if (aResult < 0)
219 {
220 aResult = ERROR_TCPSOCKET_FAILED_TO_CREATE_SOCKET;
221 CRX_ERROR_SET (aResult, "Failed to create socket(%d).", GetSystemErrorCode ());
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 {
234 aResult = ERROR_TCPSOCKET_FAILED_TO_GET_HOSTNAME;
235 CRX_ERROR_SET (aResult, "Failed to get hostname.");
236 return aResult;
237 }
238 mAddress.sin_family = aHostEnt->h_addrtype;
239 memcpy (&(mAddress.sin_addr.s_addr), aHostEnt->h_addr, aHostEnt->h_length);
240 }
241
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
259 aResult = SetTimeout (aTimeout);
260 if (aResult < 0)
261 {
262 aResult = ERROR_TCPSOCKET_FAILED_TO_SET_TIMEOUT;
263 CRX_ERROR_SET (aResult, "Failed to set timeout (%d).", GetSystemErrorCode ());
264 return aResult;
265 }
266
267 aResult = connect (mSocket, (struct sockaddr*) &mAddress, sizeof (mAddress));
268 if (aResult < 0)
269 {
270 aResult = ERROR_TCPSOCKET_FAILED_TO_CONNECT;
271 CRX_ERROR_SET (aResult, "Failed to connect (%d).", GetSystemErrorCode ());
272 }
273
274 /*----------------------------------------------------------------*/
275 return aResult;
276}
277
278int
279CRXSocket::CreateServer (const unsigned short aPort,
280 const int aBacklog,
281 struct sockaddr_in * aAddress)
282{
283 int aResult = -1;
284
285 int aReuseAddress = 1;
286
287 /*----------------------------------------------------------------*/
288 aResult = CreateSocket ();
289 if (aResult < 0)
290 {
291 aResult = ERROR_TCPSOCKET_FAILED_TO_CREATE_SOCKET;
292 CRX_ERROR_SET (aResult, "Failed to create socket(%d).", GetSystemErrorCode ());
293 return aResult;
294 }
295
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
311 mAddress.sin_family = AF_INET;
312 mAddress.sin_addr.s_addr= htonl (INADDR_ANY);
313 mAddress.sin_port = htons (aPort);
314
315 aResult = bind (mSocket, (struct sockaddr *)&mAddress, sizeof (struct sockaddr));
316 if (aResult < 0)
317 {
318 aResult = ERROR_TCPSOCKET_FAILED_TO_BIND;
319 CRX_ERROR_SET (aResult, "Failed to bind(%d).", GetSystemErrorCode ());
320 return aResult;
321 }
322
323 aResult = listen (mSocket, aBacklog);
324 if (aResult < 0)
325 {
326 aResult = ERROR_TCPSOCKET_FAILED_TO_LISTEN;
327 CRX_ERROR_SET (aResult, "Failed to listen(%d).", GetSystemErrorCode ());
328 return aResult;
329 }
330
331 if (aAddress != NULL)
332 {
333 memset ((void *)aAddress, 0x0, sizeof (struct sockaddr_in));
334 memcpy ((void *)aAddress, (void *)&mAddress, sizeof (struct sockaddr_in));
335 }
336
337 /*----------------------------------------------------------------*/
338 return aResult;
339}
340
341int
342CRXSocket::Accept (struct sockaddr_in * aRemoteAddress,
343 int * aAddressLength)
344{
345 int aResult = -1;
346
347 struct sockaddr_in aAddress;
348 socklen_t aLength = sizeof (aAddress);
349
350 /*----------------------------------------------------------------*/
351 if (!IsReady ())
352 return ERROR_TCPSOCKET_NOT_READY;
353
354 aResult = accept (mSocket, (struct sockaddr *) &aAddress, &aLength);
355 if (aResult < 0)
356 {
357 aResult = ERROR_TCPSOCKET_FAILED_TO_ACCEPT;
358 CRX_ERROR_SET (aResult, "Failed to accept(%d).", GetSystemErrorCode ());
359 return aResult;
360 }
361
362 if (aRemoteAddress != NULL)
363 {
364 memset ((void *)aRemoteAddress, 0x0, sizeof (struct sockaddr_in));
365 memcpy ((void *)aRemoteAddress, (void *)&aAddress, sizeof (struct sockaddr_in));
366 }
367
368 if (aAddressLength != NULL)
369 *aAddressLength = aLength;
370
371 /*----------------------------------------------------------------*/
372 return aResult;
373}
374
375int
376CRXSocket::Send (const char * aBuffer,
377 int aSize)
378{
379 int aResult = -1;
380
381 /*----------------------------------------------------------------*/
382 if (!IsReady ())
383 return ERROR_TCPSOCKET_NOT_READY;
384
385 aResult = send (mSocket, aBuffer, aSize, 0);
386 if (aResult != aSize)
387 {
388 aResult = ERROR_TCPSOCKET_FAILED_TO_SEND;
389 CRX_ERROR_SET (aResult, "Failed to send(%d).", GetSystemErrorCode ());
390 return aResult;
391 }
392
393 /*----------------------------------------------------------------*/
394 return aResult;
395}
396
397int
398CRXSocket::Receive (char * aBuffer,
399 int aSize)
400{
401 int aResult = -1;
402
403 /*----------------------------------------------------------------*/
404 if (!IsReady ())
405 return ERROR_TCPSOCKET_NOT_READY;
406
407 aResult = recv (mSocket, aBuffer, aSize, 0);
408 if (aResult < 0)
409 {
410 aResult = ERROR_TCPSOCKET_FAILED_TO_RECEIVE;
411 CRX_ERROR_SET (aResult, "Failed to receive(%d).", GetSystemErrorCode ());
412 return aResult;
413 }
414
415 /*----------------------------------------------------------------*/
416 return aResult;
417}
Note: See TracBrowser for help on using the repository browser.