-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWSClient.cpp
307 lines (270 loc) · 7.24 KB
/
WSClient.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
#include <WSClient.h>
char handshake_line1a[] PROGMEM = "GET ";
char handshake_line1b[] PROGMEM = " HTTP/1.1";
char handshake_line2[] PROGMEM = "Upgrade: websocket";
char handshake_line3[] PROGMEM = "Connection: Upgrade";
char handshake_line4[] PROGMEM = "Host: ";
char handshake_line5[] PROGMEM = "Origin: GalileoWSClient";
char handshake_line6[] PROGMEM = "Sec-WebSocket-Version: 13";
char handshake_line7[] PROGMEM = "Sec-WebSocket-Key: ";
char handshake_success_response[] PROGMEM = "HTTP/1.1 101";
PROGMEM const char *WSClientStringTable[] =
{
handshake_line1a,
handshake_line1b,
handshake_line2,
handshake_line3,
handshake_line4,
handshake_line5,
handshake_line6,
handshake_line7,
handshake_success_response
};
void getStringTableItem(char* buffer, int index) {
strcpy(buffer, WSClientStringTable[index]);
}
void WSClient::readLine(char* buffer) {
char character;
int i = 0;
while(_socket.available() > 0 && (character = _socket.read()) != '\n') {
if(character != '\r' && character != -1) {
buffer[i++] = character;
}
}
buffer[i] = 0x0;
}
bool WSClient::handshake(const char host[], const int port, const char path[]){
char buffer[45];
getStringTableItem(buffer, 0);
_socket.print(buffer);
_socket.print(path);
getStringTableItem(buffer, 1);
_socket.println(buffer);
getStringTableItem(buffer, 2);
_socket.println(buffer);
getStringTableItem(buffer, 3);
_socket.println(buffer);
getStringTableItem(buffer, 4);
_socket.print(buffer);
_socket.println(host);
getStringTableItem(buffer, 5);
_socket.println(buffer);
getStringTableItem(buffer, 6);
_socket.println(buffer);
byte bytes[16];
for(int i = 0; i < 16; i++) {
bytes[i] = 255 * random();
}
getStringTableItem(buffer, 7);
_socket.print(buffer);
base64Encode(bytes, 16, buffer, 45);
_socket.println(buffer);
_socket.println("");
bool result = false;
int maxAttempts = 300, attempts = 0;
char line[128];
char response[12];
getStringTableItem(response, 8);
while(_socket.available() == 0 && attempts < maxAttempts) {
delay(100);
attempts++;
}
while(true) {
readLine(line);
if(strcmp(line, "") == 0) { break; }
if(strncmp(line, response, 12) == 0) { result = true; }
}
return result;
}
byte WSClient::getNext() {
while(_socket.available() == 0);
byte b = _socket.read();
return b;
}
void WSClient::connect(const char host[], const int port, const char path[]){
_socket.flush();
_socket.stop();
if(_socket.connected()){
Serial.println("Disconnecting");
disconnect();
}
_host = host;
_port = port;
_path = path;
if (_socket.connect(host, port)){
if(!handshake(host, port, path)){
disconnect();
}
}
}
void WSClient::reconnect(){
if(_socket.connected()){
disconnect();
}
connect(_host, _port, _path);
}
void WSClient::disconnect(){
_socket.write((uint8_t) 0x87);
_socket.write((uint8_t) 0x00);
_socket.flush();
_socket.stop();
//if(_on_close_cb != NULL)(*_on_close_cb)(NULL);
}
bool WSClient::connected(){
return _socket.connected();
}
void WSClient::send(char* data){
int len = strlen(data);
_socket.write(0x81);
if(len > 125) {
_socket.write(0xFE);
_socket.write(byte(len >> 8));
_socket.write(byte(len & 0xFF));
} else {
_socket.write(0x80 | byte(len));
}
for(int i = 0; i < 4; i++) {
_socket.write((byte)0x00);
}
_socket.print(data);
}
void WSClient::addEventListener(char* event_type, Delegate<void, char*> *callback){
if(strcmp(event_type, "on_message")==0){
_on_message_cb = callback;
} else if(strcmp(event_type, "close")==0){
_on_close_cb = callback;
}
}
void WSClient::getNextPacket() {
if(!_socket.connected()){
return;
}
if(_socket.available() > 0) {
byte hdr = getNext();
bool fin = hdr & 0x80;
int opCode = hdr & 0x0F;
hdr = getNext();
bool mask = hdr & 0x80;
int len = hdr & 0x7F;
if(len == 126) {
len = getNext();
len <<= 8;
len += getNext();
} else if (len == 127) {
len = getNext();
for(int i = 0; i < 7; i++) {
len <<= 8;
len += getNext();
}
}
if(mask) {
for(int i = 0; i < 4; i++) { getNext(); }
}
if(mask) {
free(_packet);
return;
}
if(!fin) {
if(_packet == NULL) {
_packet = (char*) malloc(len);
for(int i = 0; i < len; i++) { _packet[i] = getNext(); }
_packetLength = len;
_opCode = opCode;
} else {
int copyLen = _packetLength;
_packetLength += len;
char *temp = _packet;
_packet = (char*)malloc(_packetLength);
for(int i = 0; i < _packetLength; i++) {
if(i < copyLen) {
_packet[i] = temp[i];
} else {
_packet[i] = getNext();
}
}
free(temp);
}
return;
}
if(_packet == NULL) {
_packet = (char*) malloc(len + 1);
for(int i = 0; i < len; i++) { _packet[i] = getNext(); }
_packet[len] = 0x0;
} else {
int copyLen = _packetLength;
_packetLength += len;
char *temp = _packet;
_packet = (char*) malloc(_packetLength + 1);
for(int i = 0; i < _packetLength; i++) {
if(i < copyLen) {
_packet[i] = temp[i];
} else {
_packet[i] = getNext();
}
}
_packet[_packetLength] = 0x0;
free(temp);
}
if(opCode == 0 && _opCode > 0) {
opCode = _opCode;
_opCode = 0;
}
switch(opCode) {
case 0x01:
if (_on_message_cb != NULL) {
(*_on_message_cb)(_packet);
}
break;
case 0x09:
_socket.write(0x8A);
_socket.write(byte(0x00));
break;
case 0x08:
unsigned int code = ((byte)_packet[0] << 8) + (byte)_packet[1];
disconnect();
break;
}
free(_packet);
_packet = NULL;
}
}
size_t WSClient::base64Encode(byte* src, size_t srclength, char* target, size_t targsize) {
size_t datalength = 0;
char input[3];
char output[4];
size_t i;
while (2 < srclength) {
input[0] = *src++;
input[1] = *src++;
input[2] = *src++;
srclength -= 3;
output[0] = input[0] >> 2;
output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);
output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);
output[3] = input[2] & 0x3f;
if(datalength + 4 > targsize) { return (-1); }
target[datalength++] = b64Alphabet[output[0]];
target[datalength++] = b64Alphabet[output[1]];
target[datalength++] = b64Alphabet[output[2]];
target[datalength++] = b64Alphabet[output[3]];
}
if(0 != srclength) {
input[0] = input[1] = input[2] = '\0';
for (i = 0; i < srclength; i++) { input[i] = *src++; }
output[0] = input[0] >> 2;
output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);
output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);
if(datalength + 4 > targsize) { return (-1); }
target[datalength++] = b64Alphabet[output[0]];
target[datalength++] = b64Alphabet[output[1]];
if(srclength == 1) {
target[datalength++] = '=';
} else {
target[datalength++] = b64Alphabet[output[2]];
}
target[datalength++] = '=';
}
if(datalength >= targsize) { return (-1); }
target[datalength] = '\0';
return (datalength);
}