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

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

#1 add debugging message

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