-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsocket.hpp
More file actions
647 lines (535 loc) · 18.9 KB
/
Copy pathsocket.hpp
File metadata and controls
647 lines (535 loc) · 18.9 KB
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
// socket.hpp - Socket cross-platform Enterprise Production-Ready
#ifndef OUR_SOCKET_HPP
#define OUR_SOCKET_HPP
// Configurazione per Windows
#ifdef _WIN32
#ifndef NOMINMAX
#define NOMINMAX
#endif
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
#include <mstcpip.h>
#pragma comment(lib, "ws2_32.lib")
typedef SOCKET socket_t;
typedef int socklen_t;
#define INVALID_SOCKET_VALUE INVALID_SOCKET
#define SOCKET_ERROR_VALUE SOCKET_ERROR
#define CLOSE_SOCKET closesocket
#define LAST_ERROR WSAGetLastError()
#ifndef SIO_KEEPALIVE_VALS
#define SIO_KEEPALIVE_VALS _WSAIOW(IOC_VENDOR, 4)
#endif
#else
// Linux/Unix
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <netdb.h>
#include <sys/ioctl.h>
#include <poll.h>
typedef int socket_t;
#define INVALID_SOCKET_VALUE -1
#define SOCKET_ERROR_VALUE -1
#define CLOSE_SOCKET close
#define LAST_ERROR errno
#endif
#include <cstring>
#include <string>
#include <stdexcept>
#include <memory>
#include <vector>
#include <chrono>
#include <mutex>
#include <atomic>
namespace ourmqtt {
// Forward declarations
class TLSSocket;
struct TLSConfig;
// Socket system initialization
class SocketSystem {
private:
static std::atomic<bool> initialized_;
static std::mutex init_mutex_;
public:
static bool initialize() {
std::lock_guard<std::mutex> lock(init_mutex_);
#ifdef _WIN32
if (!initialized_.load()) {
WSADATA wsaData;
int result = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (result == 0) {
initialized_ = true;
}
return result == 0;
}
return true;
#else
initialized_ = true;
return true;
#endif
}
static void cleanup() {
std::lock_guard<std::mutex> lock(init_mutex_);
#ifdef _WIN32
if (initialized_.load()) {
WSACleanup();
initialized_ = false;
}
#else
initialized_ = false;
#endif
}
static bool is_initialized() {
return initialized_.load();
}
};
// Main Socket class
class Socket {
protected:
socket_t fd_;
std::atomic<bool> is_connected_;
mutable std::mutex socket_mutex_;
// Internal close without lock
void close_internal() {
if (fd_ != INVALID_SOCKET_VALUE) {
#ifdef _WIN32
::shutdown(fd_, SD_BOTH);
#else
::shutdown(fd_, SHUT_RDWR);
#endif
CLOSE_SOCKET(fd_);
fd_ = INVALID_SOCKET_VALUE;
is_connected_ = false;
}
}
public:
static int get_last_error() {
#ifdef _WIN32
return WSAGetLastError();
#else
return errno;
#endif
}
Socket() : fd_(INVALID_SOCKET_VALUE), is_connected_(false) {
if (!SocketSystem::is_initialized()) {
SocketSystem::initialize();
}
}
explicit Socket(socket_t fd) : fd_(fd), is_connected_(true) {
if (!SocketSystem::is_initialized()) {
SocketSystem::initialize();
}
}
virtual ~Socket() {
close();
}
// Move constructor
Socket(Socket&& other) noexcept
: fd_(INVALID_SOCKET_VALUE), is_connected_(false) {
std::lock_guard<std::mutex> lock(other.socket_mutex_);
fd_ = other.fd_;
is_connected_ = other.is_connected_.load();
other.fd_ = INVALID_SOCKET_VALUE;
other.is_connected_ = false;
}
// Move assignment - CORRECTED to avoid deadlock
Socket& operator=(Socket&& other) noexcept {
if (this != &other) {
// Use std::lock to avoid deadlock
std::unique_lock<std::mutex> lock1(socket_mutex_, std::defer_lock);
std::unique_lock<std::mutex> lock2(other.socket_mutex_, std::defer_lock);
std::lock(lock1, lock2);
close_internal();
fd_ = other.fd_;
is_connected_ = other.is_connected_.load();
other.fd_ = INVALID_SOCKET_VALUE;
other.is_connected_ = false;
}
return *this;
}
// Delete copy operations
Socket(const Socket&) = delete;
Socket& operator=(const Socket&) = delete;
// Factory method
static std::unique_ptr<Socket> create(bool use_tls, const TLSConfig* config = nullptr);
// Socket creation
bool create() {
std::lock_guard<std::mutex> lock(socket_mutex_);
if (fd_ != INVALID_SOCKET_VALUE) {
return false;
}
fd_ = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (fd_ == INVALID_SOCKET_VALUE) {
return false;
}
return true;
}
// Set non-blocking mode
bool set_non_blocking(bool enable) {
std::lock_guard<std::mutex> lock(socket_mutex_);
if (fd_ == INVALID_SOCKET_VALUE) return false;
#ifdef _WIN32
u_long mode = enable ? 1 : 0;
return ioctlsocket(fd_, FIONBIO, &mode) == 0;
#else
int flags = fcntl(fd_, F_GETFL, 0);
if (flags == -1) return false;
if (enable) {
flags |= O_NONBLOCK;
}
else {
flags &= ~O_NONBLOCK;
}
return fcntl(fd_, F_SETFL, flags) != -1;
#endif
}
// Set reuse address
bool set_reuse_address(bool enable) {
std::lock_guard<std::mutex> lock(socket_mutex_);
if (fd_ == INVALID_SOCKET_VALUE) return false;
int flag = enable ? 1 : 0;
#ifdef _WIN32
return setsockopt(fd_, SOL_SOCKET, SO_REUSEADDR,
(const char*)&flag, sizeof(flag)) == 0;
#else
return setsockopt(fd_, SOL_SOCKET, SO_REUSEADDR,
&flag, sizeof(flag)) == 0;
#endif
}
// Set TCP nodelay
bool set_tcp_nodelay(bool enable) {
std::lock_guard<std::mutex> lock(socket_mutex_);
if (fd_ == INVALID_SOCKET_VALUE) return false;
int flag = enable ? 1 : 0;
#ifdef _WIN32
return setsockopt(fd_, IPPROTO_TCP, TCP_NODELAY,
(const char*)&flag, sizeof(flag)) == 0;
#else
return setsockopt(fd_, IPPROTO_TCP, TCP_NODELAY,
&flag, sizeof(flag)) == 0;
#endif
}
bool set_nodelay(bool enable) {
return set_tcp_nodelay(enable);
}
// Set buffer sizes
bool set_send_buffer_size(int size) {
std::lock_guard<std::mutex> lock(socket_mutex_);
if (fd_ == INVALID_SOCKET_VALUE) return false;
#ifdef _WIN32
return setsockopt(fd_, SOL_SOCKET, SO_SNDBUF,
(const char*)&size, sizeof(size)) == 0;
#else
return setsockopt(fd_, SOL_SOCKET, SO_SNDBUF,
&size, sizeof(size)) == 0;
#endif
}
bool set_receive_buffer_size(int size) {
std::lock_guard<std::mutex> lock(socket_mutex_);
if (fd_ == INVALID_SOCKET_VALUE) return false;
#ifdef _WIN32
return setsockopt(fd_, SOL_SOCKET, SO_RCVBUF,
(const char*)&size, sizeof(size)) == 0;
#else
return setsockopt(fd_, SOL_SOCKET, SO_RCVBUF,
&size, sizeof(size)) == 0;
#endif
}
// Set timeouts
bool set_receive_timeout(int timeout_ms) {
std::lock_guard<std::mutex> lock(socket_mutex_);
if (fd_ == INVALID_SOCKET_VALUE) return false;
#ifdef _WIN32
DWORD timeout = timeout_ms;
return setsockopt(fd_, SOL_SOCKET, SO_RCVTIMEO,
(const char*)&timeout, sizeof(timeout)) == 0;
#else
struct timeval tv;
tv.tv_sec = timeout_ms / 1000;
tv.tv_usec = (timeout_ms % 1000) * 1000;
return setsockopt(fd_, SOL_SOCKET, SO_RCVTIMEO,
&tv, sizeof(tv)) == 0;
#endif
}
bool set_send_timeout(int timeout_ms) {
std::lock_guard<std::mutex> lock(socket_mutex_);
if (fd_ == INVALID_SOCKET_VALUE) return false;
#ifdef _WIN32
DWORD timeout = timeout_ms;
return setsockopt(fd_, SOL_SOCKET, SO_SNDTIMEO,
(const char*)&timeout, sizeof(timeout)) == 0;
#else
struct timeval tv;
tv.tv_sec = timeout_ms / 1000;
tv.tv_usec = (timeout_ms % 1000) * 1000;
return setsockopt(fd_, SOL_SOCKET, SO_SNDTIMEO,
&tv, sizeof(tv)) == 0;
#endif
}
// Server operations
bool bind(uint16_t port, const std::string& address = "") {
std::lock_guard<std::mutex> lock(socket_mutex_);
if (fd_ == INVALID_SOCKET_VALUE) return false;
sockaddr_in addr{};
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
if (address.empty()) {
addr.sin_addr.s_addr = INADDR_ANY;
}
else {
#ifdef _WIN32
if (InetPtonA(AF_INET, address.c_str(), &addr.sin_addr) != 1) {
return false;
}
#else
if (inet_pton(AF_INET, address.c_str(), &addr.sin_addr) <= 0) {
return false;
}
#endif
}
return ::bind(fd_, (sockaddr*)&addr, sizeof(addr)) == 0;
}
bool listen(int backlog = 128) {
std::lock_guard<std::mutex> lock(socket_mutex_);
if (fd_ == INVALID_SOCKET_VALUE) return false;
return ::listen(fd_, backlog) == 0;
}
Socket accept() {
std::lock_guard<std::mutex> lock(socket_mutex_);
if (fd_ == INVALID_SOCKET_VALUE) {
return Socket();
}
sockaddr_in client_addr{};
socklen_t len = sizeof(client_addr);
socket_t client_fd = ::accept(fd_, (sockaddr*)&client_addr, &len);
if (client_fd != INVALID_SOCKET_VALUE) {
return Socket(client_fd);
}
return Socket();
}
// Client operations
bool connect(const std::string& host, uint16_t port) {
std::lock_guard<std::mutex> lock(socket_mutex_);
if (fd_ == INVALID_SOCKET_VALUE) return false;
sockaddr_in addr{};
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
// Try to parse as IP address first
#ifdef _WIN32
if (InetPtonA(AF_INET, host.c_str(), &addr.sin_addr) != 1) {
// Not an IP address, try DNS resolution
struct addrinfo hints {}, * result = nullptr;
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
std::string port_str = std::to_string(port);
if (getaddrinfo(host.c_str(), port_str.c_str(), &hints, &result) == 0) {
sockaddr_in* addr_in = (sockaddr_in*)result->ai_addr;
addr = *addr_in;
freeaddrinfo(result);
}
else {
return false;
}
}
#else
if (inet_pton(AF_INET, host.c_str(), &addr.sin_addr) <= 0) {
// Not an IP address, try DNS resolution
struct hostent* he = gethostbyname(host.c_str());
if (he == nullptr) {
return false;
}
addr.sin_addr = *((struct in_addr*)he->h_addr);
}
#endif
int result = ::connect(fd_, (sockaddr*)&addr, sizeof(addr));
#ifdef _WIN32
if (result == SOCKET_ERROR) {
int error = WSAGetLastError();
if (error == WSAEWOULDBLOCK || error == WSAEINPROGRESS) {
is_connected_ = true;
return true;
}
return false;
}
#else
if (result < 0) {
if (errno == EINPROGRESS || errno == EWOULDBLOCK) {
is_connected_ = true;
return true;
}
return false;
}
#endif
is_connected_ = true;
return true;
}
bool wait_for_connect(int timeout_ms) {
std::lock_guard<std::mutex> lock(socket_mutex_);
if (fd_ == INVALID_SOCKET_VALUE) return false;
#ifdef _WIN32
fd_set write_set, error_set;
FD_ZERO(&write_set);
FD_ZERO(&error_set);
FD_SET(fd_, &write_set);
FD_SET(fd_, &error_set);
struct timeval tv;
tv.tv_sec = timeout_ms / 1000;
tv.tv_usec = (timeout_ms % 1000) * 1000;
int result = select(static_cast<int>(fd_) + 1, nullptr, &write_set, &error_set, &tv);
if (result > 0) {
if (FD_ISSET(fd_, &error_set)) {
return false;
}
if (FD_ISSET(fd_, &write_set)) {
int error = 0;
socklen_t len = sizeof(error);
if (getsockopt(fd_, SOL_SOCKET, SO_ERROR,
(char*)&error, &len) == 0) {
if (error == 0) {
is_connected_ = true;
return true;
}
}
}
}
#else
struct pollfd pfd;
pfd.fd = fd_;
pfd.events = POLLOUT;
int result = poll(&pfd, 1, timeout_ms);
if (result > 0) {
int error = 0;
socklen_t len = sizeof(error);
if (getsockopt(fd_, SOL_SOCKET, SO_ERROR, &error, &len) == 0) {
if (error == 0) {
is_connected_ = true;
return true;
}
}
}
#endif
return false;
}
// Data transfer
virtual int send(const void* data, size_t len) {
return send(static_cast<const uint8_t*>(data), len);
}
virtual int send(const uint8_t* data, size_t len) {
std::lock_guard<std::mutex> lock(socket_mutex_);
if (fd_ == INVALID_SOCKET_VALUE || !data || len == 0) return -1;
#ifdef _WIN32
return ::send(fd_, (const char*)data, static_cast<int>(len), 0);
#else
return ::send(fd_, data, len, MSG_NOSIGNAL);
#endif
}
int send(const std::string& data) {
return send(data.c_str(), data.length());
}
int send(const char* data, size_t len) {
return send(static_cast<const void*>(data), len);
}
virtual int receive(void* buffer, size_t max_len) {
return receive(static_cast<uint8_t*>(buffer), max_len);
}
virtual int receive(uint8_t* buffer, size_t max_len) {
std::lock_guard<std::mutex> lock(socket_mutex_);
if (fd_ == INVALID_SOCKET_VALUE || !buffer || max_len == 0) return -1;
#ifdef _WIN32
return ::recv(fd_, (char*)buffer, static_cast<int>(max_len), 0);
#else
return ::recv(fd_, buffer, max_len, 0);
#endif
}
int receive(char* buffer, size_t max_len) {
return receive(static_cast<void*>(buffer), max_len);
}
// Socket state
void close() {
std::lock_guard<std::mutex> lock(socket_mutex_);
close_internal();
}
bool is_valid() const {
std::lock_guard<std::mutex> lock(socket_mutex_);
return fd_ != INVALID_SOCKET_VALUE;
}
bool is_connected() const {
return is_connected_.load() && is_valid();
}
socket_t get_handle() const {
std::lock_guard<std::mutex> lock(socket_mutex_);
return fd_;
}
virtual bool is_tls() const {
return false;
}
bool would_block() const {
#ifdef _WIN32
int error = WSAGetLastError();
return (error == WSAEWOULDBLOCK || error == WSAEINPROGRESS);
#else
return (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINPROGRESS);
#endif
}
// Get peer address
std::string get_peer_address() const {
std::lock_guard<std::mutex> lock(socket_mutex_);
if (fd_ == INVALID_SOCKET_VALUE) return "";
sockaddr_in addr{};
socklen_t len = sizeof(addr);
if (getpeername(fd_, (sockaddr*)&addr, &len) == 0) {
char ip_str[INET_ADDRSTRLEN];
#ifdef _WIN32
InetNtopA(AF_INET, &addr.sin_addr, ip_str, INET_ADDRSTRLEN);
#else
inet_ntop(AF_INET, &addr.sin_addr, ip_str, INET_ADDRSTRLEN);
#endif
return std::string(ip_str) + ":" + std::to_string(ntohs(addr.sin_port));
}
return "";
}
// Error handling
bool set_reuse_port(bool enable) {
#ifdef SO_REUSEPORT
int opt = enable ? 1 : 0;
return setsockopt(sock_, SOL_SOCKET, SO_REUSEPORT,
reinterpret_cast<const char*>(&opt), sizeof(opt)) == 0;
#else
return true; // Not supported on Windows
#endif
}
static std::string get_last_error_string() {
#ifdef _WIN32
int error = WSAGetLastError();
char buffer[256];
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr, error,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
buffer, sizeof(buffer), nullptr);
return std::string(buffer);
#else
return std::string(strerror(errno));
#endif
}
static int get_last_error_code() {
#ifdef _WIN32
return WSAGetLastError();
#else
return errno;
#endif
}
};
// Static member initialization
inline std::atomic<bool> SocketSystem::initialized_{ false };
inline std::mutex SocketSystem::init_mutex_;
} // namespace ourmqtt
#endif // OUR_SOCKET_HPP