Skip to content

Commit cbfa74c

Browse files
committed
Avoid most in-code #ifdefs
1 parent 80d5c25 commit cbfa74c

File tree

1 file changed

+11
-36
lines changed

1 file changed

+11
-36
lines changed

easywsclient.cpp

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
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"
71

82
#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
96
#ifndef WIN32_LEAN_AND_MEAN
107
#define WIN32_LEAN_AND_MEAN
118
#endif
@@ -62,11 +59,14 @@
6259
#ifndef SOCKET_ERROR
6360
#define SOCKET_ERROR (-1)
6461
#endif
62+
#define closesocket(s) ::close(s)
6563
#endif
6664

6765
#include <vector>
6866
#include <string>
6967

68+
#include "easywsclient.hpp"
69+
7070
namespace { // private module-only namespace
7171

7272
socket_t hostname_connect(const std::string& hostname, int port) {
@@ -92,11 +92,7 @@ socket_t hostname_connect(const std::string& hostname, int port) {
9292
if (connect(sockfd, p->ai_addr, p->ai_addrlen) != SOCKET_ERROR) {
9393
break;
9494
}
95-
#ifdef _WIN32
9695
closesocket(sockfd);
97-
#else
98-
close(sockfd);
99-
#endif
10096
sockfd = INVALID_SOCKET;
10197
}
10298
freeaddrinfo(result);
@@ -156,14 +152,6 @@ class _RealWebSocket : public easywsclient::WebSocket
156152
uint8_t masking_key[4];
157153
};
158154

159-
inline int close(socket_t sockfd) {
160-
#ifdef _WIN32
161-
return ::closesocket(sockfd);
162-
#else
163-
return ::close(sockfd);
164-
#endif
165-
}
166-
167155
std::vector<uint8_t> rxbuf;
168156
std::vector<uint8_t> txbuf;
169157

@@ -194,30 +182,22 @@ class _RealWebSocket : public easywsclient::WebSocket
194182
FD_ZERO(&wfds);
195183
FD_SET(sockfd, &rfds);
196184
if (txbuf.size()) { FD_SET(sockfd, &wfds); }
197-
#ifdef _WIN32
198-
select(0, &rfds, &wfds, NULL, &tv);
199-
#else
200185
select(sockfd + 1, &rfds, &wfds, NULL, &tv);
201-
#endif
202186
}
203187
while (true) {
204188
// FD_ISSET(0, &rfds) will be true
205189
int N = rxbuf.size();
206190
ssize_t ret;
207191
rxbuf.resize(N + 1500);
208-
#ifdef _WIN32
209192
ret = recv(sockfd, (char*)&rxbuf[0] + N, 1500, 0);
210-
#else
211-
ret = recv(sockfd, &rxbuf[0] + N, 1500, 0);
212-
#endif
213193
if (false) { }
214194
else if (ret < 0) {
215195
rxbuf.resize(N);
216196
break;
217197
}
218198
else if (ret == 0) {
219199
rxbuf.resize(N);
220-
close(sockfd);
200+
closesocket(sockfd);
221201
readyState = CLOSED;
222202
fprintf(stderr, "Connection closed!\n");
223203
break;
@@ -227,17 +207,12 @@ class _RealWebSocket : public easywsclient::WebSocket
227207
}
228208
}
229209
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);
236211
if (ret > 0) { txbuf.erase(txbuf.begin(), txbuf.begin() + ret); }
237212
else { break; }
238213
}
239214
if (!txbuf.size() && readyState == CLOSING) {
240-
close(sockfd);
215+
closesocket(sockfd);
241216
readyState = CLOSED;
242217
}
243218
}
@@ -300,15 +275,15 @@ class _RealWebSocket : public easywsclient::WebSocket
300275
if (false) { }
301276
else if (ws.opcode == wsheader_type::TEXT_FRAME && ws.fin) {
302277
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);
304279
callable((const std::string) data);
305280
}
306281
else if (ws.opcode == wsheader_type::PING) { }
307282
else if (ws.opcode == wsheader_type::PONG) { }
308283
else if (ws.opcode == wsheader_type::CLOSE) { close(); }
309284
else { fprintf(stderr, "ERROR: Got unexpected WebSocket message.\n"); close(); }
310285

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);
312287
}
313288
}
314289

0 commit comments

Comments
 (0)