author | stechong |
Thu, 13 Aug 2009 12:52:54 -0500 | |
changeset 433 | 93ea4941aeb5 |
parent 425 | 33a58f7aa2f4 |
child 458 | 70467d598794 |
permissions | -rw-r--r-- |
60 | 1 |
/* |
2 |
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 |
* All rights reserved. |
|
4 |
* This component and the accompanying materials are made available |
|
5 |
* under the terms of the License "Eclipse Public License v1.0" |
|
6 |
* which accompanies this distribution, and is available |
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 |
* |
|
9 |
* Initial Contributors: |
|
10 |
* Nokia Corporation - initial contribution. |
|
11 |
* |
|
12 |
* Contributors: |
|
13 |
* |
|
14 |
* Description: |
|
15 |
* |
|
16 |
*/ |
|
17 |
// TcpComm.cpp: implementation of the CTcpComm class. |
|
18 |
// |
|
19 |
////////////////////////////////////////////////////////////////////// |
|
20 |
||
21 |
#include "stdafx.h" |
|
22 |
#include "TcpComm.h" |
|
23 |
//#include "pn_const.h" |
|
24 |
//#include "OSTConstants.h" |
|
25 |
#include "Connection.h" |
|
26 |
||
27 |
////////////////////////////////////////////////////////////////////// |
|
28 |
// Construction/Destruction |
|
29 |
////////////////////////////////////////////////////////////////////// |
|
30 |
CTcpComm::CTcpComm() |
|
31 |
{ |
|
32 |
#ifdef _DEBUG |
|
33 |
if (gDoLogging) |
|
34 |
{ |
|
366 | 35 |
// FILE* f = fopen("c:\\tcf\\tcpcommlog.txt", "at"); |
36 |
// fprintf(f, "CTcpComm::CTcpComm() (default constructor)\n"); |
|
37 |
// fclose(f); |
|
60 | 38 |
} |
39 |
#endif |
|
40 |
m_socket = INVALID_SOCKET; |
|
41 |
m_timeOut.tv_sec = TIMEOUT_SEC(DEFAULT_SOCKET_TIMEOUT); |
|
42 |
m_timeOut.tv_usec = TIMEOUT_USEC(DEFAULT_SOCKET_TIMEOUT); |
|
43 |
||
44 |
m_hSocketEvent = WSA_INVALID_EVENT; |
|
366 | 45 |
|
60 | 46 |
} |
47 |
||
48 |
CTcpComm::CTcpComm(ConnectData* connectSettings, DWORD connectionId, CBaseProtocol* protocol) |
|
49 |
{ |
|
50 |
#ifdef _DEBUG |
|
51 |
if (gDoLogging) |
|
52 |
{ |
|
366 | 53 |
// FILE* f = fopen("c:\\tcf\\tcpcommlog.txt", "at"); |
54 |
// fprintf(f, "connectSettings=%x connectionId=%d, protocol=%x\n", connectSettings, connectionId, protocol); |
|
55 |
// fclose(f); |
|
60 | 56 |
} |
57 |
#endif |
|
58 |
m_connId = connectionId; |
|
59 |
m_Protocol = protocol; |
|
60 |
||
61 |
m_ConnectSettings = new ConnectData(); |
|
62 |
memcpy(m_ConnectSettings, connectSettings, sizeof(ConnectData)); |
|
63 |
||
64 |
#if (defined(LOG_COMM) || defined(LOG_PROCCOMM)) && defined(_DEBUG) |
|
65 |
if (gDoLogging) |
|
66 |
{ |
|
67 |
m_CommDebugLog = new TCDebugLog("TCF_Comm", connectionId, 2000L); |
|
68 |
m_ProcDebugLog = new TCDebugLog("TCF_CommP", connectionId, 2000L); |
|
69 |
} |
|
70 |
#endif |
|
71 |
m_socket = INVALID_SOCKET; |
|
72 |
m_timeOut.tv_sec = TIMEOUT_SEC(DEFAULT_SOCKET_TIMEOUT); |
|
73 |
m_timeOut.tv_usec = TIMEOUT_USEC(DEFAULT_SOCKET_TIMEOUT); |
|
74 |
||
75 |
m_hSocketEvent = WSA_INVALID_EVENT; |
|
366 | 76 |
|
60 | 77 |
} |
78 |
CTcpComm::~CTcpComm() |
|
79 |
{ |
|
80 |
#ifdef _DEBUG |
|
81 |
if (gDoLogging) |
|
82 |
{ |
|
366 | 83 |
// FILE* f = fopen("c:\\tcf\\tcpcommlog.txt", "at"); |
84 |
// fprintf(f, "CTcpComm::~CTcpComm()\n"); |
|
85 |
// fclose(f); |
|
60 | 86 |
} |
87 |
#endif |
|
88 |
if (IsConnected()) |
|
89 |
{ |
|
366 | 90 |
if (m_socket != INVALID_SOCKET) |
91 |
{ |
|
92 |
shutdown(m_socket, SD_BOTH); |
|
93 |
closesocket(m_socket); |
|
94 |
WSAClose(); |
|
95 |
} |
|
96 |
m_isConnected = false; |
|
60 | 97 |
} |
98 |
if (m_pBuffer) |
|
99 |
delete[] m_pBuffer; |
|
100 |
||
101 |
if (m_hSocketEvent != WSA_INVALID_EVENT) |
|
102 |
WSACloseEvent(m_hSocketEvent); |
|
103 |
||
104 |
} |
|
105 |
||
106 |
//#define USE_EVENTS |
|
107 |
; |
|
108 |
long CTcpComm::OpenPort() |
|
109 |
{ |
|
110 |
COMMLOGOPEN(); |
|
111 |
COMMLOGS("CTcpComm::OpenPort\n"); |
|
112 |
||
113 |
long err = TCAPI_ERR_NONE; |
|
114 |
char* ipAddress = m_ConnectSettings->tcpSettings.ipAddress; |
|
115 |
char* ipPort = m_ConnectSettings->tcpSettings.ipPort; |
|
116 |
// set this to set socket to non-blocking |
|
117 |
// DWORD nonblock = 1; // non-blocking |
|
118 |
DWORD nonblock = 0; // blocking |
|
119 |
||
120 |
COMMLOGA2("CTcpComm::OpenPort ipAddress=%s ipPort=%s\n", ipAddress, ipPort); |
|
121 |
||
366 | 122 |
int wsaErr = 0; |
123 |
wsaErr = WSAInit(); |
|
60 | 124 |
if (wsaErr != 0) |
125 |
{ |
|
126 |
err = TCAPI_ERR_WHILE_CONFIGURING_MEDIA; |
|
127 |
// err = -1; |
|
128 |
} |
|
129 |
else |
|
130 |
{ |
|
131 |
m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
|
366 | 132 |
COMMLOGA1("CTcpComm::OpenPort socket=%x\n", m_socket); |
60 | 133 |
if (m_socket == INVALID_SOCKET) |
134 |
{ |
|
135 |
m_lastCommError = WSAGetLastError(); |
|
366 | 136 |
WSASetLastError(0); |
137 |
WSAClose(); |
|
60 | 138 |
err = TCAPI_ERR_WHILE_CONFIGURING_MEDIA; |
139 |
} |
|
140 |
else |
|
141 |
{ |
|
142 |
if (ioctlsocket(m_socket, FIONBIO, &nonblock) == SOCKET_ERROR) |
|
143 |
{ |
|
144 |
m_lastCommError = WSAGetLastError(); |
|
366 | 145 |
WSASetLastError(0); |
60 | 146 |
closesocket(m_socket); |
147 |
m_socket = INVALID_SOCKET; |
|
366 | 148 |
WSAClose(); |
60 | 149 |
err = TCAPI_ERR_WHILE_CONFIGURING_MEDIA; |
150 |
} |
|
151 |
else |
|
152 |
{ |
|
153 |
int i = SO_MAX_MSG_SIZE; |
|
154 |
// set socket options |
|
389
de80f483b7e6
Turn keepalive back on to be compatible with 2.0.x releases.
Chad Peckham <chad.peckham@nokia.com>
parents:
366
diff
changeset
|
155 |
BOOL keepAlive = TRUE; |
60 | 156 |
setsockopt(m_socket, SOL_SOCKET, SO_KEEPALIVE, (const char*)&keepAlive, sizeof(BOOL)); |
366 | 157 |
|
158 |
struct linger l; |
|
159 |
l.l_onoff = 0; l.l_linger = 0; |
|
160 |
setsockopt(m_socket, SOL_SOCKET, SO_LINGER, (const char*)&l, sizeof(l)); |
|
161 |
||
60 | 162 |
int sockRecvSize = MAX_TCP_MESSAGE_BUFFER_LENGTH;//(256*1024L); |
163 |
setsockopt(m_socket, SOL_SOCKET, SO_RCVBUF, (const char*)&sockRecvSize, sizeof(int)); |
|
164 |
int sockSendSize = (64*1024L); |
|
165 |
setsockopt(m_socket, SOL_SOCKET, SO_SNDBUF, (const char*)&sockSendSize, sizeof(int)); |
|
166 |
WSAGetLastError(); // ignore error for now |
|
366 | 167 |
WSASetLastError(0); |
168 |
||
60 | 169 |
// connect |
170 |
WORD wPort = atoi(ipPort); |
|
171 |
m_clientService.sin_family = AF_INET; |
|
172 |
m_clientService.sin_addr.S_un.S_addr = inet_addr(ipAddress); |
|
173 |
m_clientService.sin_port = htons(wPort); |
|
174 |
if (connect(m_socket, (SOCKADDR*)&m_clientService, sizeof(m_clientService)) == SOCKET_ERROR) |
|
175 |
{ |
|
176 |
int wsaErr = WSAGetLastError(); |
|
366 | 177 |
WSASetLastError(0); |
178 |
COMMLOGA1("CTcpComm::OpenPort connect=wsaErr=%d\n", wsaErr); |
|
179 |
||
60 | 180 |
// socket is non-blocking |
181 |
if (wsaErr != WSAEWOULDBLOCK) |
|
182 |
{ |
|
183 |
m_lastCommError = wsaErr; |
|
184 |
||
185 |
closesocket(m_socket); |
|
186 |
m_socket = INVALID_SOCKET; |
|
366 | 187 |
WSAClose(); |
60 | 188 |
err = TCAPI_ERR_WHILE_CONFIGURING_MEDIA; |
189 |
} |
|
190 |
else // WSAEWOULDBLOCK error returned |
|
191 |
{ |
|
192 |
// WSAEWOULDBLOCK use select now |
|
193 |
fd_set readfds, writefds, exceptfds; |
|
194 |
FD_ZERO(&readfds); |
|
195 |
FD_ZERO(&writefds); |
|
196 |
FD_ZERO(&exceptfds); |
|
197 |
FD_SET(m_socket, &readfds); |
|
198 |
FD_SET(m_socket, &writefds); |
|
199 |
FD_SET(m_socket, &exceptfds); |
|
200 |
||
201 |
int selRes = 0; |
|
202 |
while(1) |
|
203 |
{ |
|
204 |
selRes = select(0, &readfds, &writefds, &exceptfds, &m_timeOut); |
|
205 |
if (selRes == SOCKET_ERROR) |
|
206 |
{ |
|
207 |
wsaErr = WSAGetLastError(); |
|
366 | 208 |
WSASetLastError(0); |
60 | 209 |
if (wsaErr != WSAEWOULDBLOCK) |
210 |
{ |
|
211 |
// real error |
|
212 |
m_lastCommError = wsaErr; |
|
213 |
shutdown(m_socket, SD_BOTH); |
|
214 |
closesocket(m_socket); |
|
215 |
m_socket = INVALID_SOCKET; |
|
366 | 216 |
WSAClose(); |
60 | 217 |
err = TCAPI_ERR_WHILE_CONFIGURING_MEDIA; |
218 |
} |
|
219 |
// else do another select |
|
220 |
} |
|
221 |
else if (selRes > 0)// select OK |
|
222 |
{ |
|
223 |
m_lastCommError = 0; |
|
224 |
m_isConnected = true; |
|
225 |
break; // done |
|
226 |
} |
|
227 |
else |
|
228 |
{ |
|
229 |
// timed out |
|
230 |
m_lastCommError = WSAGetLastError(); |
|
366 | 231 |
WSASetLastError(0); |
60 | 232 |
shutdown(m_socket, SD_BOTH); |
233 |
closesocket(m_socket); |
|
234 |
m_socket = INVALID_SOCKET; |
|
366 | 235 |
WSAClose(); |
60 | 236 |
err = TCAPI_ERR_WHILE_CONFIGURING_MEDIA; |
237 |
} |
|
238 |
} |
|
239 |
} |
|
240 |
} |
|
241 |
else // connect return OK |
|
242 |
{ |
|
366 | 243 |
COMMLOGS("CTcpComm::OpenPort connect OK\n"); |
60 | 244 |
m_lastCommError = 0; |
245 |
m_isConnected = true; |
|
246 |
} |
|
247 |
} |
|
248 |
} |
|
249 |
} |
|
250 |
if (err == TCAPI_ERR_NONE) |
|
251 |
{ |
|
252 |
// we are connected |
|
253 |
m_numberBytes = 0; |
|
254 |
m_pBuffer = new BYTE[MAX_TCP_MESSAGE_BUFFER_LENGTH]; |
|
255 |
||
256 |
#ifdef USE_EVENTS |
|
257 |
// create an event for the socket closing |
|
258 |
m_hSocketEvent = WSACreateEvent(); |
|
259 |
::WSAEventSelect(m_socket, m_hSocketEvent, FD_CLOSE); |
|
260 |
// above call sets socket to non-blocking |
|
261 |
// cannot reset to blocking after using WSAEventSelect |
|
262 |
// thus this ioctlsocket call will fail |
|
263 |
ioctlsocket(m_socket, FIONBIO, &nonblock); |
|
264 |
#endif |
|
265 |
} |
|
266 |
||
366 | 267 |
COMMLOGA1("CTcpComm::OpenPort err=%d\n", err); |
60 | 268 |
COMMLOGCLOSE(); |
269 |
return err; |
|
270 |
} |
|
271 |
||
272 |
long CTcpComm::ClosePort() |
|
273 |
{ |
|
274 |
COMMLOGOPEN(); |
|
275 |
COMMLOGS("CTcpComm::ClosePort\n"); |
|
276 |
||
277 |
long err = TCAPI_ERR_NONE; |
|
278 |
||
279 |
if (!IsConnected()) |
|
280 |
{ |
|
281 |
err = TCAPI_ERR_MEDIA_NOT_OPEN; |
|
282 |
} |
|
283 |
else |
|
284 |
{ |
|
366 | 285 |
if (m_socket != INVALID_SOCKET) |
286 |
{ |
|
287 |
shutdown(m_socket, SD_BOTH); |
|
288 |
closesocket(m_socket); |
|
289 |
m_socket = INVALID_SOCKET; |
|
290 |
||
291 |
WSAClose(); |
|
292 |
} |
|
293 |
m_isConnected = false; |
|
60 | 294 |
|
295 |
delete[] m_pBuffer; |
|
296 |
m_pBuffer = NULL; |
|
297 |
||
298 |
if (m_hSocketEvent != WSA_INVALID_EVENT) |
|
299 |
{ |
|
300 |
WSACloseEvent(m_hSocketEvent); |
|
301 |
m_hSocketEvent = WSA_INVALID_EVENT; |
|
302 |
} |
|
303 |
} |
|
304 |
||
305 |
COMMLOGCLOSE(); |
|
306 |
return err; |
|
307 |
} |
|
308 |
||
309 |
long CTcpComm::PollPort(DWORD &outSize) |
|
310 |
{ |
|
311 |
long err = TCAPI_ERR_NONE; |
|
312 |
DWORD numBytes = 0; |
|
313 |
outSize = 0; |
|
314 |
||
315 |
if (!IsConnected()) |
|
316 |
return TCAPI_ERR_MEDIA_NOT_OPEN; |
|
317 |
||
318 |
#ifdef USE_EVENTS |
|
319 |
int ret = ::WSAWaitForMultipleEvents(1, &m_hSocketEvent, FALSE, 0, FALSE); |
|
320 |
if (ret == WSA_WAIT_EVENT_0) |
|
321 |
{ |
|
322 |
::WSAResetEvent(m_hSocketEvent); |
|
323 |
err = TCAPI_ERR_COMM_ERROR; |
|
324 |
m_lastCommError = WSAESHUTDOWN; |
|
325 |
return err; |
|
326 |
} |
|
327 |
#endif |
|
328 |
int sockErr = 0; int optLen = sizeof(int); |
|
329 |
int getErr = getsockopt(m_socket, SOL_SOCKET, SO_ERROR, (char*)&sockErr, &optLen); |
|
330 |
if (getErr == 0) |
|
331 |
{ |
|
332 |
if (sockErr) |
|
333 |
{ |
|
334 |
err = TCAPI_ERR_COMM_ERROR; |
|
335 |
m_lastCommError = sockErr; |
|
336 |
return err; |
|
337 |
} |
|
338 |
} |
|
339 |
||
340 |
fd_set readfds, writefds, exceptfds; |
|
341 |
FD_ZERO(&readfds); |
|
342 |
FD_ZERO(&writefds); |
|
343 |
FD_ZERO(&exceptfds); |
|
344 |
FD_SET(m_socket, &readfds); |
|
345 |
FD_SET(m_socket, &writefds); |
|
346 |
FD_SET(m_socket, &exceptfds); |
|
347 |
||
348 |
bool portReady = false; |
|
349 |
{ |
|
350 |
TIMEVAL pollTimeout = {0,0}; // just poll the status |
|
351 |
int selErr = select(0, &readfds, 0, 0, &pollTimeout); |
|
352 |
if (selErr > 0) |
|
353 |
{ |
|
354 |
if (FD_ISSET(m_socket, &readfds)) |
|
355 |
{ |
|
356 |
m_lastCommError = 0; |
|
357 |
portReady = true; |
|
358 |
} |
|
359 |
} |
|
360 |
else if (selErr == SOCKET_ERROR) |
|
361 |
{ |
|
362 |
m_lastCommError = WSAGetLastError(); |
|
366 | 363 |
WSASetLastError(0); |
60 | 364 |
err = TCAPI_ERR_COMM_ERROR; |
365 |
} |
|
366 |
} |
|
367 |
||
368 |
if (portReady) |
|
369 |
{ |
|
370 |
// read was signaled as ready |
|
371 |
int recvRet = recv(m_socket, (char*)&m_pPeekBuffer, sizeof(m_pPeekBuffer), MSG_PEEK); |
|
372 |
if (recvRet > 0) |
|
373 |
{ |
|
374 |
if (ioctlsocket(m_socket, FIONREAD, &numBytes) == 0) |
|
375 |
{ |
|
376 |
m_lastCommError = 0; |
|
377 |
outSize = numBytes; |
|
378 |
} |
|
379 |
else // SOCKET_ERROR |
|
380 |
{ |
|
381 |
m_lastCommError = WSAGetLastError(); |
|
366 | 382 |
WSASetLastError(0); |
60 | 383 |
err = TCAPI_ERR_COMM_ERROR; |
384 |
} |
|
385 |
} |
|
386 |
else if (recvRet == 0) |
|
387 |
{ |
|
388 |
// read was signalled as ready but recv=0 signals that remote shutdown |
|
389 |
m_lastCommError = WSAESHUTDOWN; |
|
390 |
err = TCAPI_ERR_COMM_ERROR; |
|
391 |
} |
|
392 |
else |
|
393 |
{ |
|
394 |
// SOCKET_ERROR: error on recv other than a shutdown |
|
395 |
m_lastCommError = WSAGetLastError(); |
|
366 | 396 |
WSASetLastError(0); |
60 | 397 |
err = TCAPI_ERR_COMM_ERROR; |
398 |
} |
|
399 |
} |
|
400 |
return err; |
|
401 |
} |
|
402 |
||
403 |
long CTcpComm::ReadPort(DWORD inSize, void *outData, DWORD &outSize) |
|
404 |
{ |
|
405 |
long err = TCAPI_ERR_NONE; |
|
406 |
DWORD numBytes = 0; |
|
407 |
outSize = 0; |
|
408 |
||
409 |
if (!IsConnected()) |
|
410 |
return TCAPI_ERR_MEDIA_NOT_OPEN; |
|
411 |
||
412 |
if (ioctlsocket(m_socket, FIONREAD, &numBytes) == 0) |
|
413 |
{ |
|
414 |
if (numBytes > inSize) |
|
415 |
numBytes = inSize; |
|
416 |
int res = recv(m_socket, (char*)outData, numBytes, 0); |
|
417 |
if (res == SOCKET_ERROR) |
|
418 |
{ |
|
419 |
long commErr = WSAGetLastError(); |
|
366 | 420 |
WSASetLastError(0); |
60 | 421 |
if ((DWORD)commErr != m_lastCommError) |
422 |
{ |
|
423 |
m_lastCommError = commErr; |
|
424 |
} |
|
425 |
err = TCAPI_ERR_COMM_ERROR; |
|
426 |
} |
|
427 |
else if (res == 0) |
|
428 |
{ |
|
429 |
// recv=0 --> connection closed |
|
430 |
m_lastCommError = WSAESHUTDOWN; |
|
431 |
err = TCAPI_ERR_COMM_ERROR; |
|
432 |
} |
|
433 |
else |
|
434 |
{ |
|
435 |
m_lastCommError = 0; |
|
436 |
outSize = numBytes; |
|
437 |
} |
|
438 |
} |
|
439 |
else |
|
440 |
{ |
|
441 |
// SOCKET_ERROR on ioctlsocket |
|
442 |
m_lastCommError = WSAGetLastError(); |
|
366 | 443 |
WSASetLastError(0); |
60 | 444 |
err = TCAPI_ERR_COMM_ERROR; |
445 |
} |
|
446 |
return err; |
|
447 |
} |
|
448 |
long CTcpComm::ProcessBuffer(CConnection* pConn, CRegistry* pRegistry, long& numberProcessed) |
|
449 |
{ |
|
450 |
||
451 |
long err = TCAPI_ERR_NONE; |
|
452 |
long routingErr = TCAPI_ERR_NONE; |
|
453 |
||
454 |
if (!IsConnected()) |
|
455 |
return TCAPI_ERR_MEDIA_NOT_OPEN; |
|
456 |
||
457 |
if (!m_Protocol) |
|
458 |
return TCAPI_ERR_UNKNOWN_MEDIA_TYPE; |
|
459 |
||
460 |
DWORD protocolHeaderLength = m_Protocol->GetHeaderLength(); |
|
461 |
||
462 |
// fill buffer |
|
463 |
if (m_numberBytes < MAX_TCP_MESSAGE_BUFFER_LENGTH) |
|
464 |
{ |
|
465 |
DWORD outLen = MAX_TCP_MESSAGE_BUFFER_LENGTH - m_numberBytes; |
|
466 |
BYTE* ptr = &m_pBuffer[m_numberBytes]; |
|
467 |
err = ReadPort(outLen, ptr, outLen); |
|
468 |
if (err == TCAPI_ERR_NONE && outLen > 0) |
|
469 |
{ |
|
470 |
m_numberBytes += outLen; |
|
471 |
} |
|
472 |
} |
|
473 |
||
474 |
// now process buffer but only for complete messages |
|
475 |
if (err == TCAPI_ERR_NONE) |
|
476 |
{ |
|
477 |
if (m_numberBytes >= protocolHeaderLength) |
|
478 |
{ |
|
479 |
BYTE* ptr = m_pBuffer; |
|
480 |
long bytesRemaining = m_numberBytes; |
|
481 |
long usedLen = 0; |
|
482 |
bool done = false; |
|
483 |
||
484 |
while (!done) |
|
485 |
{ |
|
486 |
DWORD fullMessageLength = bytesRemaining; |
|
487 |
DWORD rawLength = 0; |
|
488 |
BYTE* fullMessage = ptr; |
|
489 |
BYTE* rawMessage = ptr; |
|
490 |
BYTE msgId = 0; |
|
491 |
if (m_Protocol->DecodeMessage(fullMessage, fullMessageLength, msgId, rawMessage, rawLength)) |
|
492 |
{ |
|
493 |
err = PreProcessMessage(msgId, fullMessageLength, fullMessage); |
|
494 |
if (err != TCAPI_ERR_NONE) |
|
495 |
{ |
|
496 |
PROCLOGOPEN(); |
|
497 |
PROCLOGA1("CTcpComm::ProcessBuffer Notify err = %x\n", err); |
|
498 |
PROCLOGCLOSE(); |
|
499 |
// notify all clients right now |
|
500 |
pConn->NotifyClientsCommError(err, false, 0); |
|
501 |
err = TCAPI_ERR_NONE; |
|
502 |
} |
|
503 |
#ifdef _DEBUG |
|
504 |
int reallen = fullMessageLength; |
|
505 |
if (reallen > 50) reallen = 50; |
|
506 |
char msg[6]; |
|
507 |
msg[0] = '\0'; |
|
508 |
||
509 |
sTcpLogMsg[0] = '\0'; |
|
510 |
if (reallen > 0) |
|
511 |
{ |
|
512 |
sTcpLogMsg[0] = '\0'; |
|
513 |
for (int i = 0; i < reallen; i++) |
|
514 |
{ |
|
515 |
sprintf(msg, "%02.2x ", ptr[i]); |
|
516 |
strcat(sTcpLogMsg, msg); |
|
517 |
} |
|
518 |
} |
|
519 |
#endif |
|
520 |
PROCLOGOPEN(); |
|
521 |
PROCLOGA5("CTcpComm::ProcessBuffer - RouteMesssage pRegistry = %x id=%x len=%d len=%d\n msg=%s\n", pRegistry, msgId, fullMessageLength, rawLength, sTcpLogMsg); |
|
522 |
PROCLOGCLOSE(); |
|
523 |
||
524 |
err = pRegistry->RouteMessage(msgId, fullMessage, fullMessageLength, rawMessage, rawLength); |
|
525 |
if (err != TCAPI_ERR_NONE) routingErr = err; // saved for future |
|
526 |
||
527 |
numberProcessed++; |
|
528 |
usedLen += fullMessageLength; |
|
529 |
bytesRemaining -= fullMessageLength; |
|
530 |
ptr += fullMessageLength; |
|
531 |
if (bytesRemaining < protocolHeaderLength) |
|
532 |
done = true; |
|
533 |
} |
|
534 |
else |
|
535 |
{ |
|
425 | 536 |
numberProcessed++; |
537 |
usedLen += fullMessageLength; |
|
538 |
bytesRemaining -= fullMessageLength; |
|
539 |
ptr += fullMessageLength; |
|
540 |
if (bytesRemaining < protocolHeaderLength) |
|
541 |
done = true; |
|
60 | 542 |
} |
543 |
} |
|
544 |
DeleteMsg(usedLen); |
|
545 |
} |
|
546 |
} |
|
547 |
||
548 |
if (routingErr == TCAPI_ERR_NONE) |
|
549 |
return err; |
|
550 |
else |
|
551 |
return routingErr; |
|
552 |
} |
|
553 |
||
554 |
||
555 |
long CTcpComm::SendDataToPort(DWORD inSize, const void* inData) |
|
556 |
{ |
|
557 |
COMMLOGOPEN(); |
|
558 |
COMMLOGS("CTcpComm::SendDataToPort\n"); |
|
559 |
||
560 |
long err = TCAPI_ERR_NONE; |
|
561 |
||
562 |
if (!IsConnected()) |
|
563 |
{ |
|
564 |
COMMLOGCLOSE(); |
|
565 |
return TCAPI_ERR_MEDIA_NOT_OPEN; |
|
566 |
} |
|
567 |
||
568 |
#ifdef USE_EVENTS |
|
569 |
int ret = ::WSAWaitForMultipleEvents(1, &m_hSocketEvent, FALSE, 0, FALSE); |
|
570 |
if (ret == WSA_WAIT_EVENT_0) |
|
571 |
{ |
|
572 |
::WSAResetEvent(m_hSocketEvent); |
|
573 |
err = TCAPI_ERR_COMM_ERROR; |
|
574 |
m_lastCommError = WSAESHUTDOWN; |
|
575 |
COMMLOGCLOSE(); |
|
576 |
return err; |
|
577 |
} |
|
578 |
#endif |
|
579 |
int sockErr = 0; int optLen = sizeof(int); |
|
580 |
int getErr = getsockopt(m_socket, SOL_SOCKET, SO_ERROR, (char*)&sockErr, &optLen); |
|
581 |
if (getErr == 0) |
|
582 |
{ |
|
583 |
if (sockErr) |
|
584 |
{ |
|
585 |
err = TCAPI_ERR_COMM_ERROR; |
|
586 |
m_lastCommError = sockErr; |
|
366 | 587 |
COMMLOGA1("CTcpComm::SendDataToPort getsockopt=%d\n", sockErr); |
60 | 588 |
COMMLOGCLOSE(); |
589 |
return err; |
|
590 |
} |
|
591 |
} |
|
592 |
||
593 |
fd_set readfds, writefds, exceptfds; |
|
594 |
FD_ZERO(&readfds); |
|
595 |
FD_ZERO(&writefds); |
|
596 |
FD_ZERO(&exceptfds); |
|
597 |
FD_SET(m_socket, &readfds); |
|
598 |
FD_SET(m_socket, &writefds); |
|
599 |
FD_SET(m_socket, &exceptfds); |
|
600 |
||
601 |
COMMLOGS("CTcpComm::SendDataToPort select\n"); |
|
602 |
bool portReady = false; |
|
603 |
{ |
|
604 |
int selErr = select(0, &readfds, &writefds, &exceptfds, &m_timeOut); |
|
605 |
if (selErr > 0) |
|
606 |
{ |
|
607 |
if (FD_ISSET(m_socket, &writefds)) |
|
608 |
{ |
|
609 |
m_lastCommError = 0; |
|
610 |
portReady = true; |
|
611 |
} |
|
612 |
} |
|
613 |
else if (selErr == SOCKET_ERROR) |
|
614 |
{ |
|
615 |
m_lastCommError = WSAGetLastError(); |
|
366 | 616 |
WSASetLastError(0); |
617 |
COMMLOGA1("CTcpComm::SendDataToPort select(SOCKET_ERROR)=%d\n", m_lastCommError); |
|
60 | 618 |
err = TCAPI_ERR_COMM_ERROR; |
619 |
} |
|
620 |
else if (selErr == 0) // timeout |
|
621 |
{ |
|
622 |
m_lastCommError = WSAGetLastError(); |
|
366 | 623 |
WSASetLastError(0); |
624 |
COMMLOGA1("CTcpComm::SendDataToPort select(timeout)=%d\n", m_lastCommError); |
|
60 | 625 |
err = TCAPI_ERR_COMM_ERROR; |
626 |
} |
|
627 |
} |
|
628 |
COMMLOGA1("CTcpComm::SendDataToPort portReady=%d\n", portReady); |
|
629 |
if (portReady) |
|
630 |
{ |
|
631 |
COMMLOGS("CTcpComm::SendDataToPort send start\n"); |
|
632 |
// loop until all bytes are sent |
|
633 |
DWORD bytesRemaining = inSize; |
|
634 |
DWORD nSent = 0; |
|
635 |
char* unsent = (char*)inData; |
|
636 |
while (bytesRemaining) |
|
637 |
{ |
|
638 |
nSent = send(m_socket, unsent, bytesRemaining, 0); |
|
639 |
if (nSent == SOCKET_ERROR) |
|
640 |
{ |
|
641 |
int wsaErr = WSAGetLastError(); |
|
366 | 642 |
WSASetLastError(0); |
60 | 643 |
// ignore "would block" errors |
644 |
if (wsaErr != WSAEWOULDBLOCK) |
|
645 |
{ |
|
646 |
// TODO: error handling |
|
366 | 647 |
COMMLOGA1("CTcpComm::SendDataToPort send(SOCKET_ERROR)=%d\n", wsaErr); |
60 | 648 |
m_lastCommError = wsaErr; |
649 |
err = TCAPI_ERR_COMM_ERROR; |
|
650 |
break; |
|
651 |
} |
|
652 |
} |
|
653 |
else |
|
654 |
{ |
|
655 |
m_lastCommError = 0; |
|
656 |
unsent += nSent; |
|
657 |
bytesRemaining -= nSent; |
|
658 |
} |
|
659 |
} // end while |
|
660 |
COMMLOGS("CTcpComm::SendDataToPort send done\n"); |
|
661 |
#ifdef _DEBUG |
|
662 |
BYTE* ptr = (BYTE*)inData; |
|
663 |
long numBytes = (inSize > 20) ? 20 : inSize; |
|
664 |
char msg[200]; |
|
665 |
sprintf(msg, "CTcpComm::SendDataToPort data = "); |
|
666 |
for (int i = 0; i < numBytes; i++) |
|
667 |
{ |
|
668 |
sprintf(msg, "%s %02.2x", msg, ptr[i]); |
|
669 |
} |
|
670 |
sprintf(msg, "%s\n", msg); |
|
671 |
COMMLOGS(msg); |
|
672 |
#endif |
|
673 |
} |
|
674 |
||
675 |
COMMLOGCLOSE(); |
|
676 |
return err; |
|
677 |
} |
|
678 |
||
679 |
void CTcpComm::DeleteMsg(DWORD inMsgLength) |
|
680 |
{ |
|
681 |
// inMsgLength includes header |
|
682 |
// delete from beginning of buffer |
|
683 |
if (inMsgLength == 0) |
|
684 |
return; |
|
685 |
if (m_numberBytes > 0 && m_numberBytes >= inMsgLength) |
|
686 |
{ |
|
687 |
size_t moveLen = m_numberBytes - inMsgLength; |
|
688 |
if (moveLen > 0) |
|
689 |
memcpy(&m_pBuffer[0], &m_pBuffer[inMsgLength], moveLen); |
|
690 |
m_numberBytes -= inMsgLength; |
|
691 |
} |
|
692 |
} |
|
693 |
bool CTcpComm::IsConnectionEqual(ConnectData* pConn) |
|
694 |
{ |
|
366 | 695 |
if ((strcmp(pConn->tcpSettings.ipAddress, m_ConnectSettings->tcpSettings.ipAddress) == 0)) |
60 | 696 |
{ |
366 | 697 |
if ((strcmp(pConn->tcpSettings.ipPort, m_ConnectSettings->tcpSettings.ipPort) == 0)) |
698 |
{ |
|
699 |
// same port and same IP |
|
700 |
return true; |
|
701 |
} |
|
702 |
else |
|
703 |
{ |
|
704 |
// different port but same IP |
|
705 |
return false; |
|
706 |
} |
|
60 | 707 |
} |
708 |
else |
|
709 |
{ |
|
366 | 710 |
// different IP |
60 | 711 |
return false; |
712 |
} |
|
713 |
} |
|
714 |
||
366 | 715 |
int CTcpComm::WSAInit() |
716 |
{ |
|
717 |
int wsaErr = 0; |
|
718 |
||
719 |
COMMLOGOPEN(); |
|
720 |
COMMLOGS("CTcpComm::WSAInit\n"); |
|
721 |
||
722 |
WSADATA wsaData; |
|
723 |
wsaErr = WSAStartup(MAKEWORD(2,2), &wsaData); |
|
724 |
||
725 |
COMMLOGCLOSE(); |
|
726 |
return wsaErr; |
|
727 |
} |
|
728 |
||
729 |
void CTcpComm::WSAClose() |
|
730 |
{ |
|
731 |
COMMLOGOPEN(); |
|
732 |
COMMLOGS("CTcpComm::WSAClose\n"); |
|
733 |
||
734 |
WSACleanup(); |
|
735 |
||
736 |
COMMLOGCLOSE(); |
|
737 |
} |
|
738 |