forked from maxicus/ib-tws-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocol-bytes.js
192 lines (136 loc) · 4.01 KB
/
protocol-bytes.js
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
import EventEmitter from 'events';
import net from 'net';
import util from 'util';
const debuglog = util.debuglog('ib-tws-api-bytes');
/* Protocol's byte-level handing */
class ProtocolBytes {
constructor() {
this._emitter = new EventEmitter();
}
/* p: {host, port, clientId} */
connect(p = {}) {
p.host = p.host || '127.0.0.1';
this._clientId = p.clientId;
this._socket = new net.Socket();
this._socket.on('data', (data) => {
this._onData(data);
});
this._socket.on('close', (e) => {
debuglog('ProtocolBytes: close');
this._emitter.emit('close');
});
this._socket.on('end', (e) => {
debuglog('ProtocolBytes: end');
this._emitter.emit('close');
});
this._socket.on('error', (e) => {
debuglog('ProtocolBytes: error');
debuglog(e);
this._emitter.emit('error', e);
});
return this._promiseBoundToSocketError((resolve) => {
this._socket.connect({
host: p.host,
port: p.port
}, resolve);
});
}
on(eventName, listener) {
this._emitter.on(eventName, listener);
}
off(eventName, listener) {
this._emitter.off(eventName, listener);
}
removeAllListeners() {
this._emitter.removeAllListeners();
}
disconnect() {
this._socket.end();
}
sendFieldset(fields) {
debuglog('sending fieldset');
debuglog(fields);
let fieldsStrings = fields.map((i) => {
if (i == null) {
return '';
} else if (typeof i === 'boolean') {
return i ? '1' : '0';
} else if (typeof i === 'string') {
return i;
} else if (typeof i === 'number') {
return i.toString();
} else {
let output = '';
for (let key in i) {
output += key + '=' + i[key] + ';';
}
return output;
}
});
let string = fieldsStrings.join('\0') + '\0';
let message = this._serializeString(string);
debuglog('sending (serialized)');
debuglog(fieldsStrings);
debuglog(string);
debuglog(message);
this._socket.write(message);
}
sendHandshake() {
const MIN_CLIENT_VERSION = 100;
const MAX_CLIENT_VERSION = 151;
let v100prefix = "API\0";
this._socket.write(v100prefix);
let v100version = util.format('v%d..%d', MIN_CLIENT_VERSION, MAX_CLIENT_VERSION);
let msg = this._serializeString(v100version);
this._socket.write(msg);
debuglog('connection handshake sent');
}
_promiseBoundToSocketError(workload) {
return new Promise((resolve, reject) => {
const onError = (e) => {
reject(e);
};
const onSuccess = () => {
this._socket.off('error', onError);
resolve();
};
this._socket.once('error', onError);
workload(onSuccess);
});
}
_serializeString(s) {
let content = Buffer.from(s);
let header = Buffer.alloc(4);
header.writeInt32BE(content.length);
return Buffer.concat([header, content]);
}
_onData(data) {
debuglog('received packet');
debuglog(data);
debuglog(data.toString('ascii'));
if (this._dataPending) {
this._dataPending = Buffer.concat([this._dataPending, data]);
} else {
this._dataPending = data;
}
while (this._dataPending && this._dataPending.length >= 4) {
let stringLength = this._dataPending.readInt32BE(0);
if (this._dataPending.length < stringLength + 4) {
debuglog('not full message received, waiting for remainder');
return;
}
let messageString = this._dataPending.toString('ascii', 4, stringLength + 4);
if (this._dataPending.length == stringLength + 4) {
this._dataPending = null;
} else {
this._dataPending = this._dataPending.slice(stringLength + 4);
}
let messageFields = messageString.split("\0");
messageFields.pop(); // last item is always empty
debuglog('received message fields');
debuglog(messageFields);
this._emitter.emit('message_fieldset', messageFields);
}
}
}
export default ProtocolBytes;