1
- #ifdef _MSC_VER
2
- // _CRT_SECURE_NO_WARNINGS for sscanf errors in MSVC2013 Express
3
- #define _CRT_SECURE_NO_WARNINGS
4
- #endif
5
-
6
- #include " easywsclient.hpp"
7
1
8
2
#ifdef _WIN32
3
+ #if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
4
+ #define _CRT_SECURE_NO_WARNINGS // _CRT_SECURE_NO_WARNINGS for sscanf errors in MSVC2013 Express
5
+ #endif
9
6
#ifndef WIN32_LEAN_AND_MEAN
10
7
#define WIN32_LEAN_AND_MEAN
11
8
#endif
62
59
#ifndef SOCKET_ERROR
63
60
#define SOCKET_ERROR (-1 )
64
61
#endif
62
+ #define closesocket (s ) ::close(s)
65
63
#endif
66
64
67
65
#include < vector>
68
66
#include < string>
69
67
68
+ #include " easywsclient.hpp"
69
+
70
70
namespace { // private module-only namespace
71
71
72
72
socket_t hostname_connect (const std::string& hostname, int port) {
@@ -92,11 +92,7 @@ socket_t hostname_connect(const std::string& hostname, int port) {
92
92
if (connect (sockfd, p->ai_addr , p->ai_addrlen ) != SOCKET_ERROR) {
93
93
break ;
94
94
}
95
- #ifdef _WIN32
96
95
closesocket (sockfd);
97
- #else
98
- close (sockfd);
99
- #endif
100
96
sockfd = INVALID_SOCKET;
101
97
}
102
98
freeaddrinfo (result);
@@ -156,14 +152,6 @@ class _RealWebSocket : public easywsclient::WebSocket
156
152
uint8_t masking_key[4 ];
157
153
};
158
154
159
- inline int close (socket_t sockfd) {
160
- #ifdef _WIN32
161
- return ::closesocket (sockfd);
162
- #else
163
- return ::close (sockfd);
164
- #endif
165
- }
166
-
167
155
std::vector<uint8_t > rxbuf;
168
156
std::vector<uint8_t > txbuf;
169
157
@@ -194,30 +182,22 @@ class _RealWebSocket : public easywsclient::WebSocket
194
182
FD_ZERO (&wfds);
195
183
FD_SET (sockfd, &rfds);
196
184
if (txbuf.size ()) { FD_SET (sockfd, &wfds); }
197
- #ifdef _WIN32
198
- select (0 , &rfds, &wfds, NULL , &tv);
199
- #else
200
185
select (sockfd + 1 , &rfds, &wfds, NULL , &tv);
201
- #endif
202
186
}
203
187
while (true ) {
204
188
// FD_ISSET(0, &rfds) will be true
205
189
int N = rxbuf.size ();
206
190
ssize_t ret;
207
191
rxbuf.resize (N + 1500 );
208
- #ifdef _WIN32
209
192
ret = recv (sockfd, (char *)&rxbuf[0 ] + N, 1500 , 0 );
210
- #else
211
- ret = recv (sockfd, &rxbuf[0 ] + N, 1500 , 0 );
212
- #endif
213
193
if (false ) { }
214
194
else if (ret < 0 ) {
215
195
rxbuf.resize (N);
216
196
break ;
217
197
}
218
198
else if (ret == 0 ) {
219
199
rxbuf.resize (N);
220
- close (sockfd);
200
+ closesocket (sockfd);
221
201
readyState = CLOSED;
222
202
fprintf (stderr, " Connection closed!\n " );
223
203
break ;
@@ -227,17 +207,12 @@ class _RealWebSocket : public easywsclient::WebSocket
227
207
}
228
208
}
229
209
while (txbuf.size ()) {
230
- int ret;
231
- #ifdef _WIN32
232
- ret = ::send (sockfd, (char *)&txbuf[0 ], txbuf.size (), 0 );
233
- #else
234
- ret = ::send (sockfd, &txbuf[0 ], txbuf.size (), 0 );
235
- #endif
210
+ int ret = ::send (sockfd, (char *)&txbuf[0 ], txbuf.size (), 0 );
236
211
if (ret > 0 ) { txbuf.erase (txbuf.begin (), txbuf.begin () + ret); }
237
212
else { break ; }
238
213
}
239
214
if (!txbuf.size () && readyState == CLOSING) {
240
- close (sockfd);
215
+ closesocket (sockfd);
241
216
readyState = CLOSED;
242
217
}
243
218
}
@@ -300,15 +275,15 @@ class _RealWebSocket : public easywsclient::WebSocket
300
275
if (false ) { }
301
276
else if (ws.opcode == wsheader_type::TEXT_FRAME && ws.fin ) {
302
277
if (ws.mask ) { for (size_t i = 0 ; i != ws.N ; ++i) { rxbuf[i+ws.header_size ] ^= ws.masking_key [i&0x3 ]; } }
303
- std::string data (rxbuf.begin ()+ws.header_size , rxbuf.begin ()+ws.header_size +ws.N );
278
+ std::string data (rxbuf.begin ()+ws.header_size , rxbuf.begin ()+ws.header_size +( size_t ) ws.N );
304
279
callable ((const std::string) data);
305
280
}
306
281
else if (ws.opcode == wsheader_type::PING) { }
307
282
else if (ws.opcode == wsheader_type::PONG) { }
308
283
else if (ws.opcode == wsheader_type::CLOSE) { close (); }
309
284
else { fprintf (stderr, " ERROR: Got unexpected WebSocket message.\n " ); close (); }
310
285
311
- rxbuf.erase (rxbuf.begin (), rxbuf.begin () + ws.header_size +ws.N );
286
+ rxbuf.erase (rxbuf.begin (), rxbuf.begin () + ws.header_size +( size_t ) ws.N );
312
287
}
313
288
}
314
289
0 commit comments