Debian Codegraph @

plugins/dm.gameconnection/clsocket/ActiveSocket.cpp

raw
1 /*---------------------------------------------------------------------------*/
2 /* */
3 /* CActiveSocket.cpp - Active Socket Implementation */
4 /* */
5 /* Author : Mark Carrier ([email protected]) */
6 /* */
7 /*---------------------------------------------------------------------------*/
8 /* Copyright (c) 2007-2009 CarrierLabs, LLC. All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 *
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 *
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in
19 * the documentation and/or other materials provided with the
20 * distribution.
21 *
22 * 3. The name of the author may not be used to endorse or promote products
23 * derived from this software without specific prior written permission.
24 *
25 * 4. The name "CarrierLabs" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * [email protected].
29 *
30 * THIS SOFTWARE IS PROVIDED BY MARK CARRIER ``AS IS'' AND ANY
31 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
33 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MARK CARRIER OR
34 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
41 * OF THE POSSIBILITY OF SUCH DAMAGE.
42 *----------------------------------------------------------------------------*/
43 #include "ActiveSocket.h"
44
45 CActiveSocket::CActiveSocket(CSocketType nType) : CSimpleSocket(nType)
46 {
47 }
48
49 //------------------------------------------------------------------------------
50 //
51 // ConnectTCP() -
52 //
53 //------------------------------------------------------------------------------
54 bool CActiveSocket::ConnectTCP(const char *pAddr, uint16 nPort)
55 {
56 bool bRetVal = false;
57 struct in_addr stIpAddress;
58
59 //------------------------------------------------------------------
60 // Preconnection setup that must be preformed
61 //------------------------------------------------------------------
62 memset(&m_stServerSockaddr, 0, sizeof(m_stServerSockaddr));
63 m_stServerSockaddr.sin_family = AF_INET;
64
65 if ((m_pHE = GETHOSTBYNAME(pAddr)) == NULL)
66 {
67 #ifdef WIN32
68 TranslateSocketError();
69 #else
70 if (h_errno == HOST_NOT_FOUND)
71 {
72 SetSocketError(SocketInvalidAddress);
73 }
74 #endif
75 return bRetVal;
76 }
77
78 memcpy(&stIpAddress, m_pHE->h_addr_list[0], m_pHE->h_length);
79 m_stServerSockaddr.sin_addr.s_addr = stIpAddress.s_addr;
80
81 if ((int32)m_stServerSockaddr.sin_addr.s_addr == CSimpleSocket::SocketError)
82 {
83 TranslateSocketError();
84 return bRetVal;
85 }
86
87 m_stServerSockaddr.sin_port = htons(nPort);
88
89 //------------------------------------------------------------------
90 // Connect to address "xxx.xxx.xxx.xxx" (IPv4) address only.
91 //
92 //------------------------------------------------------------------
93 m_timer.Initialize();
94 m_timer.SetStartTime();
95
96 if (connect(m_socket, (struct sockaddr*)&m_stServerSockaddr, sizeof(m_stServerSockaddr)) ==
97 CSimpleSocket::SocketError)
98 {
99 //--------------------------------------------------------------
100 // Get error value this might be a non-blocking socket so we
101 // must first check.
102 //--------------------------------------------------------------
103 TranslateSocketError();
104
105 //--------------------------------------------------------------
106 // If the socket is non-blocking and the current socket error
107 // is SocketEinprogress or SocketEwouldblock then poll connection
108 // with select for designated timeout period.
109 // Linux returns EINPROGRESS and Windows returns WSAEWOULDBLOCK.
110 //--------------------------------------------------------------
111 if ((IsNonblocking()) &&
112 ((GetSocketError() == CSimpleSocket::SocketEwouldblock) ||
113 (GetSocketError() == CSimpleSocket::SocketEinprogress)))
114 {
115 bRetVal = Select(GetConnectTimeoutSec(), GetConnectTimeoutUSec());
116 }
117 }
118 else
119 {
120 TranslateSocketError();
121 bRetVal = true;
122 }
123
124 m_timer.SetEndTime();
125
126 return bRetVal;
127 }
128
129 //------------------------------------------------------------------------------
130 //
131 // ConnectUDP() -
132 //
133 //------------------------------------------------------------------------------
134 bool CActiveSocket::ConnectUDP(const char *pAddr, uint16 nPort)
135 {
136 bool bRetVal = false;
137 struct in_addr stIpAddress;
138
139 //------------------------------------------------------------------
140 // Pre-connection setup that must be preformed
141 //------------------------------------------------------------------
142 memset(&m_stServerSockaddr, 0, sizeof(m_stServerSockaddr));
143 m_stServerSockaddr.sin_family = AF_INET;
144
145 if ((m_pHE = GETHOSTBYNAME(pAddr)) == NULL)
146 {
147 #ifdef WIN32
148 TranslateSocketError();
149 #else
150 if (h_errno == HOST_NOT_FOUND)
151 {
152 SetSocketError(SocketInvalidAddress);
153 }
154 #endif
155 return bRetVal;
156 }
157
158 memcpy(&stIpAddress, m_pHE->h_addr_list[0], m_pHE->h_length);
159 m_stServerSockaddr.sin_addr.s_addr = stIpAddress.s_addr;
160
161 if ((int32)m_stServerSockaddr.sin_addr.s_addr == CSimpleSocket::SocketError)
162 {
163 TranslateSocketError();
164 return bRetVal;
165 }
166
167 m_stServerSockaddr.sin_port = htons(nPort);
168
169 //------------------------------------------------------------------
170 // Connect to address "xxx.xxx.xxx.xxx" (IPv4) address only.
171 //
172 //------------------------------------------------------------------
173 m_timer.Initialize();
174 m_timer.SetStartTime();
175
176 if (connect(m_socket, (struct sockaddr*)&m_stServerSockaddr, sizeof(m_stServerSockaddr)) != CSimpleSocket::SocketError)
177 {
178 bRetVal = true;
179 }
180
181 TranslateSocketError();
182
183 m_timer.SetEndTime();
184
185 return bRetVal;
186 }
187
188 //------------------------------------------------------------------------------
189 //
190 // ConnectRAW() -
191 //
192 //------------------------------------------------------------------------------
193 bool CActiveSocket::ConnectRAW(const char *pAddr, uint16 nPort)
194 {
195 bool bRetVal = false;
196 struct in_addr stIpAddress;
197 //------------------------------------------------------------------
198 // Pre-connection setup that must be preformed
199 //------------------------------------------------------------------
200 memset(&m_stServerSockaddr, 0, sizeof(m_stServerSockaddr));
201 m_stServerSockaddr.sin_family = AF_INET;
202
203 if ((m_pHE = GETHOSTBYNAME(pAddr)) == NULL)
204 {
205 #ifdef WIN32
206 TranslateSocketError();
207 #else
208 if (h_errno == HOST_NOT_FOUND)
209 {
210 SetSocketError(SocketInvalidAddress);
211 }
212 #endif
213 return bRetVal;
214 }
215
216 memcpy(&stIpAddress, m_pHE->h_addr_list[0], m_pHE->h_length);
217 m_stServerSockaddr.sin_addr.s_addr = stIpAddress.s_addr;
218
219 if ((int32)m_stServerSockaddr.sin_addr.s_addr == CSimpleSocket::SocketError)
220 {
221 TranslateSocketError();
222 return bRetVal;
223 }
224
225 m_stServerSockaddr.sin_port = htons(nPort);
226
227 //------------------------------------------------------------------
228 // Connect to address "xxx.xxx.xxx.xxx" (IPv4) address only.
229 //
230 //------------------------------------------------------------------
231 m_timer.Initialize();
232 m_timer.SetStartTime();
233
234 if (connect(m_socket, (struct sockaddr*)&m_stServerSockaddr, sizeof(m_stServerSockaddr)) != CSimpleSocket::SocketError)
235 {
236 bRetVal = true;
237 }
238
239 TranslateSocketError();
240
241 m_timer.SetEndTime();
242
243 return bRetVal;
244 }
245
246
247 //------------------------------------------------------------------------------
248 //
249 // Open() - Create a connection to a specified address on a specified port
250 //
251 //------------------------------------------------------------------------------
252 bool CActiveSocket::Open(const char *pAddr, uint16 nPort)
253 {
254 bool bRetVal = false;
255
256 if (IsSocketValid() == false)
257 {
258 SetSocketError(CSimpleSocket::SocketInvalidSocket);
259 return bRetVal;
260 }
261
262 if (pAddr == NULL)
263 {
264 SetSocketError(CSimpleSocket::SocketInvalidAddress);
265 return bRetVal;
266 }
267
268 if (nPort == 0)
269 {
270 SetSocketError(CSimpleSocket::SocketInvalidPort);
271 return bRetVal;
272 }
273
274 switch (m_nSocketType)
275 {
276 case CSimpleSocket::SocketTypeTcp :
277 {
278 bRetVal = ConnectTCP(pAddr, nPort);
279 break;
280 }
281 case CSimpleSocket::SocketTypeUdp :
282 {
283 bRetVal = ConnectUDP(pAddr, nPort);
284 break;
285 }
286 case CSimpleSocket::SocketTypeRaw :
287 break;
288 default:
289 break;
290 }
291
292 //--------------------------------------------------------------------------
293 // If successful then create a local copy of the address and port
294 //--------------------------------------------------------------------------
295 if (bRetVal)
296 {
297 socklen_t nSockLen = sizeof(struct sockaddr);
298
299 memset(&m_stServerSockaddr, 0, nSockLen);
300 getpeername(m_socket, (struct sockaddr *)&m_stServerSockaddr, &nSockLen);
301
302 nSockLen = sizeof(struct sockaddr);
303 memset(&m_stClientSockaddr, 0, nSockLen);
304 getsockname(m_socket, (struct sockaddr *)&m_stClientSockaddr, &nSockLen);
305
306 SetSocketError(SocketSuccess);
307 }
308
309 return bRetVal;
310 }