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

Last change on this file since 18 was 18, checked in by cheese, 12 years ago

#1 work for CRXProxy and CRXException

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