22 #include "TcpComm.h" |
22 #include "TcpComm.h" |
23 //#include "pn_const.h" |
23 //#include "pn_const.h" |
24 //#include "OSTConstants.h" |
24 //#include "OSTConstants.h" |
25 #include "Connection.h" |
25 #include "Connection.h" |
26 |
26 |
27 #ifdef _DEBUG |
|
28 static char sTcpLogMsg[3000]; |
|
29 #endif |
|
30 ////////////////////////////////////////////////////////////////////// |
27 ////////////////////////////////////////////////////////////////////// |
31 // Construction/Destruction |
28 // Construction/Destruction |
32 ////////////////////////////////////////////////////////////////////// |
29 ////////////////////////////////////////////////////////////////////// |
33 CTcpComm::CTcpComm() |
30 CTcpComm::CTcpComm() |
34 { |
31 { |
35 #ifdef _DEBUG |
32 #ifdef _DEBUG |
36 if (gDoLogging) |
33 if (gDoLogging) |
37 { |
34 { |
38 FILE* f = fopen("c:\\tcf\\tcpcommlog.txt", "at"); |
35 // FILE* f = fopen("c:\\tcf\\tcpcommlog.txt", "at"); |
39 fprintf(f, "CTcpComm::CTcpComm() (default constructor)\n"); |
36 // fprintf(f, "CTcpComm::CTcpComm() (default constructor)\n"); |
40 fclose(f); |
37 // fclose(f); |
41 } |
38 } |
42 #endif |
39 #endif |
43 m_socket = INVALID_SOCKET; |
40 m_socket = INVALID_SOCKET; |
44 m_timeOut.tv_sec = TIMEOUT_SEC(DEFAULT_SOCKET_TIMEOUT); |
41 m_timeOut.tv_sec = TIMEOUT_SEC(DEFAULT_SOCKET_TIMEOUT); |
45 m_timeOut.tv_usec = TIMEOUT_USEC(DEFAULT_SOCKET_TIMEOUT); |
42 m_timeOut.tv_usec = TIMEOUT_USEC(DEFAULT_SOCKET_TIMEOUT); |
46 |
43 |
47 m_hSocketEvent = WSA_INVALID_EVENT; |
44 m_hSocketEvent = WSA_INVALID_EVENT; |
|
45 |
48 } |
46 } |
49 |
47 |
50 CTcpComm::CTcpComm(ConnectData* connectSettings, DWORD connectionId, CBaseProtocol* protocol) |
48 CTcpComm::CTcpComm(ConnectData* connectSettings, DWORD connectionId, CBaseProtocol* protocol) |
51 { |
49 { |
52 #ifdef _DEBUG |
50 #ifdef _DEBUG |
53 if (gDoLogging) |
51 if (gDoLogging) |
54 { |
52 { |
55 FILE* f = fopen("c:\\tcf\\tcpcommlog.txt", "at"); |
53 // FILE* f = fopen("c:\\tcf\\tcpcommlog.txt", "at"); |
56 fprintf(f, "connectSettings=%x connectionId=%d, protocol=%x\n", connectSettings, connectionId, protocol); |
54 // fprintf(f, "connectSettings=%x connectionId=%d, protocol=%x\n", connectSettings, connectionId, protocol); |
57 fclose(f); |
55 // fclose(f); |
58 } |
56 } |
59 #endif |
57 #endif |
60 m_connId = connectionId; |
58 m_connId = connectionId; |
61 m_Protocol = protocol; |
59 m_Protocol = protocol; |
62 |
60 |
73 m_socket = INVALID_SOCKET; |
71 m_socket = INVALID_SOCKET; |
74 m_timeOut.tv_sec = TIMEOUT_SEC(DEFAULT_SOCKET_TIMEOUT); |
72 m_timeOut.tv_sec = TIMEOUT_SEC(DEFAULT_SOCKET_TIMEOUT); |
75 m_timeOut.tv_usec = TIMEOUT_USEC(DEFAULT_SOCKET_TIMEOUT); |
73 m_timeOut.tv_usec = TIMEOUT_USEC(DEFAULT_SOCKET_TIMEOUT); |
76 |
74 |
77 m_hSocketEvent = WSA_INVALID_EVENT; |
75 m_hSocketEvent = WSA_INVALID_EVENT; |
|
76 |
78 } |
77 } |
79 CTcpComm::~CTcpComm() |
78 CTcpComm::~CTcpComm() |
80 { |
79 { |
81 #ifdef _DEBUG |
80 #ifdef _DEBUG |
82 if (gDoLogging) |
81 if (gDoLogging) |
83 { |
82 { |
84 FILE* f = fopen("c:\\tcf\\tcpcommlog.txt", "at"); |
83 // FILE* f = fopen("c:\\tcf\\tcpcommlog.txt", "at"); |
85 fprintf(f, "CTcpComm::~CTcpComm()\n"); |
84 // fprintf(f, "CTcpComm::~CTcpComm()\n"); |
86 fclose(f); |
85 // fclose(f); |
87 } |
86 } |
88 #endif |
87 #endif |
89 if (IsConnected()) |
88 if (IsConnected()) |
90 { |
89 { |
91 shutdown(m_socket, SD_BOTH); |
90 if (m_socket != INVALID_SOCKET) |
92 closesocket(m_socket); |
91 { |
93 WSACleanup(); |
92 shutdown(m_socket, SD_BOTH); |
|
93 closesocket(m_socket); |
|
94 WSAClose(); |
|
95 } |
|
96 m_isConnected = false; |
94 } |
97 } |
95 if (m_pBuffer) |
98 if (m_pBuffer) |
96 delete[] m_pBuffer; |
99 delete[] m_pBuffer; |
97 |
100 |
98 if (m_hSocketEvent != WSA_INVALID_EVENT) |
101 if (m_hSocketEvent != WSA_INVALID_EVENT) |
114 // DWORD nonblock = 1; // non-blocking |
117 // DWORD nonblock = 1; // non-blocking |
115 DWORD nonblock = 0; // blocking |
118 DWORD nonblock = 0; // blocking |
116 |
119 |
117 COMMLOGA2("CTcpComm::OpenPort ipAddress=%s ipPort=%s\n", ipAddress, ipPort); |
120 COMMLOGA2("CTcpComm::OpenPort ipAddress=%s ipPort=%s\n", ipAddress, ipPort); |
118 |
121 |
119 WSADATA wsaData; |
122 int wsaErr = 0; |
120 int wsaErr = WSAStartup(MAKEWORD(2,2), &wsaData); |
123 wsaErr = WSAInit(); |
121 if (wsaErr != 0) |
124 if (wsaErr != 0) |
122 { |
125 { |
123 err = TCAPI_ERR_WHILE_CONFIGURING_MEDIA; |
126 err = TCAPI_ERR_WHILE_CONFIGURING_MEDIA; |
124 // err = -1; |
127 // err = -1; |
125 } |
128 } |
126 else |
129 else |
127 { |
130 { |
128 m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
131 m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
|
132 COMMLOGA1("CTcpComm::OpenPort socket=%x\n", m_socket); |
129 if (m_socket == INVALID_SOCKET) |
133 if (m_socket == INVALID_SOCKET) |
130 { |
134 { |
131 m_lastCommError = WSAGetLastError(); |
135 m_lastCommError = WSAGetLastError(); |
132 WSACleanup(); |
136 WSASetLastError(0); |
|
137 WSAClose(); |
133 err = TCAPI_ERR_WHILE_CONFIGURING_MEDIA; |
138 err = TCAPI_ERR_WHILE_CONFIGURING_MEDIA; |
134 } |
139 } |
135 else |
140 else |
136 { |
141 { |
137 if (ioctlsocket(m_socket, FIONBIO, &nonblock) == SOCKET_ERROR) |
142 if (ioctlsocket(m_socket, FIONBIO, &nonblock) == SOCKET_ERROR) |
138 { |
143 { |
139 m_lastCommError = WSAGetLastError(); |
144 m_lastCommError = WSAGetLastError(); |
|
145 WSASetLastError(0); |
140 closesocket(m_socket); |
146 closesocket(m_socket); |
141 m_socket = INVALID_SOCKET; |
147 m_socket = INVALID_SOCKET; |
142 WSACleanup(); |
148 WSAClose(); |
143 err = TCAPI_ERR_WHILE_CONFIGURING_MEDIA; |
149 err = TCAPI_ERR_WHILE_CONFIGURING_MEDIA; |
144 } |
150 } |
145 else |
151 else |
146 { |
152 { |
147 int i = SO_MAX_MSG_SIZE; |
153 int i = SO_MAX_MSG_SIZE; |
148 // set socket options |
154 // set socket options |
149 BOOL keepAlive = TRUE; |
155 BOOL keepAlive = FALSE; |
150 setsockopt(m_socket, SOL_SOCKET, SO_KEEPALIVE, (const char*)&keepAlive, sizeof(BOOL)); |
156 setsockopt(m_socket, SOL_SOCKET, SO_KEEPALIVE, (const char*)&keepAlive, sizeof(BOOL)); |
|
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 |
151 int sockRecvSize = MAX_TCP_MESSAGE_BUFFER_LENGTH;//(256*1024L); |
162 int sockRecvSize = MAX_TCP_MESSAGE_BUFFER_LENGTH;//(256*1024L); |
152 setsockopt(m_socket, SOL_SOCKET, SO_RCVBUF, (const char*)&sockRecvSize, sizeof(int)); |
163 setsockopt(m_socket, SOL_SOCKET, SO_RCVBUF, (const char*)&sockRecvSize, sizeof(int)); |
153 int sockSendSize = (64*1024L); |
164 int sockSendSize = (64*1024L); |
154 setsockopt(m_socket, SOL_SOCKET, SO_SNDBUF, (const char*)&sockSendSize, sizeof(int)); |
165 setsockopt(m_socket, SOL_SOCKET, SO_SNDBUF, (const char*)&sockSendSize, sizeof(int)); |
155 WSAGetLastError(); // ignore error for now |
166 WSAGetLastError(); // ignore error for now |
156 int gotsockRecvSize, optLen=sizeof(int); |
167 WSASetLastError(0); |
157 getsockopt(m_socket, SOL_SOCKET, SO_RCVBUF, (char*)&gotsockRecvSize, &optLen); |
168 |
158 WSAGetLastError(); // ignore error for now |
|
159 // connect |
169 // connect |
160 WORD wPort = atoi(ipPort); |
170 WORD wPort = atoi(ipPort); |
161 m_clientService.sin_family = AF_INET; |
171 m_clientService.sin_family = AF_INET; |
162 m_clientService.sin_addr.S_un.S_addr = inet_addr(ipAddress); |
172 m_clientService.sin_addr.S_un.S_addr = inet_addr(ipAddress); |
163 m_clientService.sin_port = htons(wPort); |
173 m_clientService.sin_port = htons(wPort); |
164 if (connect(m_socket, (SOCKADDR*)&m_clientService, sizeof(m_clientService)) == SOCKET_ERROR) |
174 if (connect(m_socket, (SOCKADDR*)&m_clientService, sizeof(m_clientService)) == SOCKET_ERROR) |
165 { |
175 { |
166 int wsaErr = WSAGetLastError(); |
176 int wsaErr = WSAGetLastError(); |
|
177 WSASetLastError(0); |
|
178 COMMLOGA1("CTcpComm::OpenPort connect=wsaErr=%d\n", wsaErr); |
|
179 |
167 // socket is non-blocking |
180 // socket is non-blocking |
168 if (wsaErr != WSAEWOULDBLOCK) |
181 if (wsaErr != WSAEWOULDBLOCK) |
169 { |
182 { |
170 m_lastCommError = wsaErr; |
183 m_lastCommError = wsaErr; |
171 |
184 |
172 closesocket(m_socket); |
185 closesocket(m_socket); |
173 m_socket = INVALID_SOCKET; |
186 m_socket = INVALID_SOCKET; |
174 WSACleanup(); |
187 WSAClose(); |
175 err = TCAPI_ERR_WHILE_CONFIGURING_MEDIA; |
188 err = TCAPI_ERR_WHILE_CONFIGURING_MEDIA; |
176 } |
189 } |
177 else // WSAEWOULDBLOCK error returned |
190 else // WSAEWOULDBLOCK error returned |
178 { |
191 { |
179 // WSAEWOULDBLOCK use select now |
192 // WSAEWOULDBLOCK use select now |
190 { |
203 { |
191 selRes = select(0, &readfds, &writefds, &exceptfds, &m_timeOut); |
204 selRes = select(0, &readfds, &writefds, &exceptfds, &m_timeOut); |
192 if (selRes == SOCKET_ERROR) |
205 if (selRes == SOCKET_ERROR) |
193 { |
206 { |
194 wsaErr = WSAGetLastError(); |
207 wsaErr = WSAGetLastError(); |
|
208 WSASetLastError(0); |
195 if (wsaErr != WSAEWOULDBLOCK) |
209 if (wsaErr != WSAEWOULDBLOCK) |
196 { |
210 { |
197 // real error |
211 // real error |
198 m_lastCommError = wsaErr; |
212 m_lastCommError = wsaErr; |
199 shutdown(m_socket, SD_BOTH); |
213 shutdown(m_socket, SD_BOTH); |
200 closesocket(m_socket); |
214 closesocket(m_socket); |
201 m_socket = INVALID_SOCKET; |
215 m_socket = INVALID_SOCKET; |
202 WSACleanup(); |
216 WSAClose(); |
203 err = TCAPI_ERR_WHILE_CONFIGURING_MEDIA; |
217 err = TCAPI_ERR_WHILE_CONFIGURING_MEDIA; |
204 } |
218 } |
205 // else do another select |
219 // else do another select |
206 } |
220 } |
207 else if (selRes > 0)// select OK |
221 else if (selRes > 0)// select OK |