-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathEPosixClientSocket.cpp
308 lines (239 loc) · 5.79 KB
/
EPosixClientSocket.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/* Copyright (C) 2013 Interactive Brokers LLC. All rights reserved. This code is subject to the terms
* and conditions of the IB API Non-Commercial License or the IB API Commercial License, as applicable. */
#include "stdafx.h"
// 2014/08/24 added this line
#include <WinSock2.h>
#include "TWS/EPosixClientSocket.h"
#include "TWS/EPosixClientSocketPlatform.h"
#include "TWS/TwsSocketClientErrors.h"
#include "TWS/EWrapper.h"
#include <string.h>
std::map<UINT_PTR, EPosixClientSocket*> EPosixClientSocket::socketMap;
///////////////////////////////////////////////////////////
// member funcs
EPosixClientSocket::EPosixClientSocket( EWrapper *ptr) : EClientSocketBase( ptr)
{
m_fd = -1;
}
EPosixClientSocket::~EPosixClientSocket()
{
}
void CALLBACK EPosixClientSocket::socketTimerProc( _In_ HWND hwnd, _In_ UINT uMsg, _In_ UINT_PTR idEvent, _In_ DWORD dwTime) {
auto evIt = socketMap.find(idEvent);
if (evIt != socketMap.end())
evIt->second->processMessages();
}
void EPosixClientSocket::processMessages()
{
fd_set readSet, writeSet, errorSet;
struct timeval tval;
tval.tv_usec = 0;
tval.tv_sec = 0;
time_t now = time(NULL);
if( m_sleepDeadline > 0) {
// initialize timeout with m_sleepDeadline - now
tval.tv_sec = m_sleepDeadline - now;
}
if( fd() >= 0 ) {
FD_ZERO( &readSet);
errorSet = writeSet = readSet;
FD_SET( fd(), &readSet);
if( !isOutBufferEmpty())
FD_SET( fd(), &writeSet);
FD_SET( fd(), &errorSet);
int ret = select( fd() + 1, &readSet, &writeSet, &errorSet, &tval);
if( ret == 0) { // timeout
return;
}
if( ret < 0) { // error
onError();
return;
}
if( fd() < 0)
return;
if( FD_ISSET( fd(), &errorSet)) {
// error on socket
onError();
}
if( fd() < 0)
return;
if( FD_ISSET( fd(), &writeSet)) {
// socket is ready for writing
onSend();
}
if( fd() < 0)
return;
if( FD_ISSET( fd(), &readSet)) {
// socket is ready for reading
onReceive();
}
}
}
bool EPosixClientSocket::eConnect( const char *host, unsigned int port, int clientId, bool extraAuth)
{
// reset errno
errno = 0;
// already connected?
if( m_fd >= 0) {
errno = EISCONN;
getWrapper()->error( NO_VALID_ID, ALREADY_CONNECTED.code(), ALREADY_CONNECTED.msg());
return false;
}
// initialize Winsock DLL (only for Windows)
if ( !SocketsInit()) {
return false;
}
// create socket
m_fd = socket(AF_INET, SOCK_STREAM, 0);
// cannot create socket
if( m_fd < 0) {
// uninitialize Winsock DLL (only for Windows)
SocketsDestroy();
getWrapper()->error( NO_VALID_ID, FAIL_CREATE_SOCK.code(), FAIL_CREATE_SOCK.msg());
return false;
}
// use local machine if no host passed in
if ( !( host && *host)) {
host = "127.0.0.1";
}
// starting to connect to server
struct sockaddr_in sa;
memset( &sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_port = htons( port);
sa.sin_addr.s_addr = inet_addr( host);
// try to connect
if( (connect( m_fd, (struct sockaddr *) &sa, sizeof( sa))) < 0) {
// error connecting
SocketClose( m_fd);
m_fd = -1;
// uninitialize Winsock DLL (only for Windows)
SocketsDestroy();
getWrapper()->error( NO_VALID_ID, CONNECT_FAIL.code(), CONNECT_FAIL.msg());
return false;
}
// set client id
setClientId( clientId);
setExtraAuth( extraAuth);
onConnectBase();
while( isSocketOK() && !isConnected()) {
if ( !checkMessages()) {
// uninitialize Winsock DLL (only for Windows)
SocketsDestroy();
getWrapper()->error( NO_VALID_ID, CONNECT_FAIL.code(), CONNECT_FAIL.msg());
return false;
}
}
// set socket to non-blocking state
if ( !SetSocketNonBlocking(m_fd)){
// error setting socket to non-blocking
SocketsDestroy();
getWrapper()->error( NO_VALID_ID, CONNECT_FAIL.code(), CONNECT_FAIL.msg());
return false;
}
socketMap[SetTimer(0, 0, USER_TIMER_MINIMUM, socketTimerProc)] = this;
// successfully connected
return true;
}
void EPosixClientSocket::eDisconnect()
{
if ( m_fd >= 0 )
// close socket
SocketClose( m_fd);
m_fd = -1;
// uninitialize Winsock DLL (only for Windows)
SocketsDestroy();
eDisconnectBase();
}
bool EPosixClientSocket::isSocketOK() const
{
return ( m_fd >= 0);
}
int EPosixClientSocket::fd() const
{
return m_fd;
}
int EPosixClientSocket::send(const char* buf, size_t sz)
{
if( sz <= 0)
return 0;
int nResult = ::send( m_fd, buf, sz, 0);
if( nResult == -1 && !handleSocketError()) {
return -1;
}
if( nResult <= 0) {
return 0;
}
return nResult;
}
int EPosixClientSocket::receive(char* buf, size_t sz)
{
if( sz <= 0)
return 0;
int nResult = ::recv( m_fd, buf, sz, 0);
if( nResult == -1 && !handleSocketError()) {
return -1;
}
if (nResult == 0)
onClose();
if( nResult <= 0) {
return 0;
}
return nResult;
}
///////////////////////////////////////////////////////////
// callbacks from socket
void EPosixClientSocket::onConnect()
{
if( !handleSocketError())
return;
onConnectBase();
}
void EPosixClientSocket::onReceive()
{
if( !handleSocketError())
return;
checkMessages();
}
void EPosixClientSocket::onSend()
{
if( !handleSocketError())
return;
sendBufferedData();
}
void EPosixClientSocket::onClose()
{
if( !handleSocketError())
return;
eDisconnect();
getWrapper()->connectionClosed();
}
void EPosixClientSocket::onError()
{
handleSocketError();
}
///////////////////////////////////////////////////////////
// helper
bool EPosixClientSocket::handleSocketError()
{
// no error
if( errno == 0)
return true;
// Socket is already connected
if( errno == EISCONN) {
return true;
}
if( errno == EWOULDBLOCK)
return false;
if( errno == ECONNREFUSED) {
getWrapper()->error( NO_VALID_ID, CONNECT_FAIL.code(), CONNECT_FAIL.msg());
}
else {
getWrapper()->error( NO_VALID_ID, SOCKET_EXCEPTION.code(),
SOCKET_EXCEPTION.msg() + strerror(errno));
}
// reset errno
errno = 0;
eDisconnect();
return false;
}