-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
603 lines (541 loc) · 21.9 KB
/
main.cpp
File metadata and controls
603 lines (541 loc) · 21.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
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#include <ws2tcpip.h>
#endif
#include "logger.hpp"
#include <filesystem>
#include <iostream>
#include <sstream>
#include <string>
#include <thread>
#include <chrono>
#include <vector>
#include <mutex>
#include <random>
#include <algorithm>
#include <atomic>
#include <queue>
#include <condition_variable>
#include <cctype>
namespace fs = std::filesystem;
static std::string tolower_s(const std::string &s){ std::string r=s; for(char &c: r) c = (char)std::tolower((unsigned char)c); return r; }
static std::string stripq(const std::string &s){ if(s.size()>=2 && ((s.front()=='"'&&s.back()=='"')||(s.front()=='\''&&s.back()=='\''))) return s.substr(1,s.size()-2); return s; }
#ifdef _WIN32
#pragma warning(disable:4996)
#include <wincrypt.h>
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "advapi32.lib")
#pragma comment(lib, "crypt32.lib")
using sock_t = SOCKET;
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <poll.h>
#include <openssl/sha.h>
#include <openssl/bio.h>
#include <openssl/evp.h>
#include <openssl/buffer.h>
using sock_t = int;
#define INVALID_SOCKET (-1)
#endif
// ================================
// Base64 + SHA1
// ================================
#ifdef _WIN32
std::string base64_encode(const unsigned char* data, size_t len) {
DWORD outLen = 0;
CryptBinaryToStringA(data, (DWORD)len, CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF, NULL, &outLen);
if (outLen == 0) return {};
std::string out(outLen, '\0');
CryptBinaryToStringA(data, (DWORD)len, CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF, &out[0], &outLen);
if (!out.empty() && out.back() == '\0') out.pop_back();
return out;
}
std::string sha1_base64(const std::string& input) {
HCRYPTPROV prov = 0;
HCRYPTHASH hash = 0;
BYTE digest[20];
DWORD digestLen = sizeof(digest);
if (!CryptAcquireContext(&prov, NULL, NULL, PROV_RSA_AES, CRYPT_VERIFYCONTEXT)) {
CryptAcquireContext(&prov, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
}
CryptCreateHash(prov, CALG_SHA1, 0, 0, &hash);
CryptHashData(hash, reinterpret_cast<const BYTE*>(input.data()), (DWORD)input.size(), 0);
CryptGetHashParam(hash, HP_HASHVAL, digest, &digestLen, 0);
CryptDestroyHash(hash);
CryptReleaseContext(prov, 0);
return base64_encode(digest, digestLen);
}
#else
std::string base64_encode(const unsigned char* data, size_t len) {
BIO *bio, *b64;
BUF_MEM *bufferPtr;
b64 = BIO_new(BIO_f_base64());
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
bio = BIO_new(BIO_s_mem());
bio = BIO_push(b64, bio);
BIO_write(bio, data, (int)len);
BIO_flush(bio);
BIO_get_mem_ptr(bio, &bufferPtr);
std::string result(bufferPtr->data, bufferPtr->length);
BIO_free_all(bio);
return result;
}
std::string sha1_base64(const std::string& input) {
unsigned char hash[SHA_DIGEST_LENGTH];
SHA1(reinterpret_cast<const unsigned char*>(input.c_str()), input.size(), hash);
return base64_encode(hash, SHA_DIGEST_LENGTH);
}
#endif
static std::queue<std::string> g_cmd_queue; static std::mutex g_queue_mtx; static std::condition_variable g_queue_cv;
static std::atomic<int> g_delay_ms{0};
// server control globals
static std::atomic<bool> server_active{false};
static std::mutex server_mtx;
static sock_t server_sock = INVALID_SOCKET;
static std::thread server_thread;
static std::thread sender_thread;
static std::atomic<bool> app_running{true};
// Client and websocket helper declarations
struct Client {
sock_t sock = INVALID_SOCKET;
sockaddr_in addr{};
bool websocket = false;
std::string recvbuf;
};
static std::vector<Client> clients;
static std::mutex clients_mtx;
// Use implementation closely matching the working .old files
static std::string escape_json(const std::string& s) {
std::string out;
out.reserve(s.size());
for (char c : s) {
if (c == '\\') out += "\\\\";
else if (c == '"') out += "\\\"";
else if (c == '\n') out += "\\n";
else out += c;
}
return out;
}
static std::string makeCommandJSON(const std::string& command) {
static std::mt19937_64 rng{ std::random_device{}() };
unsigned long long id = rng();
std::ostringstream ss;
ss << "{"
<< "\"header\":{"
<< "\"version\":1,"
<< "\"requestId\":\"" << id << "\","
<< "\"messagePurpose\":\"commandRequest\""
<< "},"
<< "\"body\":{"
<< "\"version\":1,"
<< "\"commandLine\":\"" << escape_json(command) << "\""
<< "}"
<< "}";
return ss.str();
}
static void ws_send(sock_t s, const std::string& msg) {
std::vector<uint8_t> frame;
frame.push_back(0x81); // FIN + text
size_t len = msg.size();
if (len < 126) {
frame.push_back((uint8_t)len);
} else if (len <= 0xFFFF) {
frame.push_back(126);
frame.push_back((uint8_t)((len >> 8) & 0xFF));
frame.push_back((uint8_t)(len & 0xFF));
} else {
frame.push_back(127);
for (int i = 7; i >= 0; --i) frame.push_back((uint8_t)((len >> (i*8)) & 0xFF));
}
frame.insert(frame.end(), msg.begin(), msg.end());
send(s, reinterpret_cast<const char*>(frame.data()), (int)frame.size(), 0);
}
static void broadcast(const std::string& msg) {
std::lock_guard<std::mutex> lk(clients_mtx);
for (auto& c : clients) {
if (c.websocket) ws_send(c.sock, msg);
}
}
static void remove_client_by_socket_locked(sock_t s) {
clients.erase(std::remove_if(clients.begin(), clients.end(), [&](const Client& c){ return c.sock == s; }), clients.end());
}
static bool websocket_handshake(Client& c) {
std::istringstream ss(c.recvbuf);
std::string line;
std::string key;
while (std::getline(ss, line)) {
if (line.rfind("Sec-WebSocket-Key:", 0) == 0) {
size_t pos = line.find(':');
if (pos != std::string::npos) {
key = line.substr(pos + 1);
while (!key.empty() && (key.front() == ' ')) key.erase(key.begin());
while (!key.empty() && (key.back() == '\r' || key.back() == '\n')) key.pop_back();
}
}
}
if (key.empty()) return false;
const std::string GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
std::string accept = sha1_base64(key + GUID);
std::ostringstream resp;
resp << "HTTP/1.1 101 Switching Protocols\r\n"
<< "Upgrade: websocket\r\n"
<< "Connection: Upgrade\r\n"
<< "Sec-WebSocket-Accept: " << accept << "\r\n\r\n";
std::string r = resp.str();
send(c.sock, r.c_str(), (int)r.size(), 0);
c.websocket = true;
c.recvbuf.clear();
return true;
}
static bool handle_websocket_frames(Client& c) {
while (true) {
if (c.recvbuf.size() < 2) return false;
uint8_t b1 = (uint8_t)c.recvbuf[0];
uint8_t b2 = (uint8_t)c.recvbuf[1];
bool fin = (b1 & 0x80) != 0;
uint8_t opcode = b1 & 0x0F;
bool masked = (b2 & 0x80) != 0;
uint64_t payload_len = b2 & 0x7F;
size_t pos = 2;
if (payload_len == 126) {
if (c.recvbuf.size() < pos + 2) return false;
payload_len = ((uint8_t)c.recvbuf[pos] << 8) | (uint8_t)c.recvbuf[pos+1];
pos += 2;
} else if (payload_len == 127) {
if (c.recvbuf.size() < pos + 8) return false;
payload_len = 0;
for (int i = 0; i < 8; ++i) payload_len = (payload_len << 8) | (uint8_t)c.recvbuf[pos + i];
pos += 8;
}
uint8_t mask[4] = {0,0,0,0};
if (masked) {
if (c.recvbuf.size() < pos + 4) return false;
mask[0] = (uint8_t)c.recvbuf[pos];
mask[1] = (uint8_t)c.recvbuf[pos+1];
mask[2] = (uint8_t)c.recvbuf[pos+2];
mask[3] = (uint8_t)c.recvbuf[pos+3];
pos += 4;
}
if (c.recvbuf.size() < pos + payload_len) return false;
std::string payload;
if (payload_len) payload.assign(c.recvbuf.begin() + pos, c.recvbuf.begin() + pos + (size_t)payload_len);
c.recvbuf.erase(0, pos + (size_t)payload_len);
if (masked && payload_len) {
for (size_t i = 0; i < payload.size(); ++i) payload[i] ^= mask[i % 4];
}
if (opcode == 0x8) {
std::vector<uint8_t> close_frame = { 0x88, 0x00 };
send(c.sock, reinterpret_cast<const char*>(close_frame.data()), (int)close_frame.size(), 0);
return true;
} else if (opcode == 0x9) {
std::vector<uint8_t> pong;
pong.push_back(0x8A);
if (payload.size() < 126) {
pong.push_back((uint8_t)payload.size());
} else if (payload.size() <= 0xFFFF) {
pong.push_back(126);
pong.push_back((payload.size() >> 8) & 0xFF);
pong.push_back(payload.size() & 0xFF);
} else {
pong.push_back(127);
for (int i = 7; i >= 0; --i) pong.push_back((payload.size() >> (i*8)) & 0xFF);
}
pong.insert(pong.end(), payload.begin(), payload.end());
send(c.sock, reinterpret_cast<const char*>(pong.data()), (int)pong.size(), 0);
} else if (opcode == 0x1) {
logf(INF, "[WS][%s:%d] %s", inet_ntoa(c.addr.sin_addr), ntohs(c.addr.sin_port), payload.c_str());
} else {
// ignore other opcodes
}
}
return false;
}
void print_help() {
logf(INF, "Usage:\n app --port <port> [--delay <ms>]");
}
void enqueue_command(const std::string& cmd) { { std::lock_guard<std::mutex> lk(g_queue_mtx); g_cmd_queue.push(cmd); } g_queue_cv.notify_one(); logf(DBG, "[ENQUEUE] %s", cmd.c_str()); }
// run server loop on this thread
static void run_server(int p){
sock_t server = socket(AF_INET, SOCK_STREAM,
#ifdef _WIN32
IPPROTO_TCP
#else
0
#endif
);
if (server == INVALID_SOCKET) { logf(ERR, "socket failed"); server_active = false; return; }
#ifndef _WIN32
int opt = 1; setsockopt(server, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
#endif
sockaddr_in addr{}; addr.sin_family = AF_INET; addr.sin_port = htons((unsigned short)p); addr.sin_addr.s_addr = INADDR_ANY;
if (bind(server, (sockaddr*)&addr, sizeof(addr))
#ifdef _WIN32
== SOCKET_ERROR
#else
< 0
#endif
) { logf(ERR, "bind failed");
#ifdef _WIN32
closesocket(server);
#else
close(server);
#endif
server_active = false; return; }
if (listen(server, SOMAXCONN)
#ifdef _WIN32
== SOCKET_ERROR
#else
< 0
#endif
) { logf(ERR, "listen failed");
#ifdef _WIN32
closesocket(server);
#else
close(server);
#endif
server_active = false; return; }
logf(INF, "Server is ready on port %d", p);
sender_thread = std::thread([&]() {
while (true) {
std::unique_lock<std::mutex> lk(g_queue_mtx);
g_queue_cv.wait(lk, [&] {
return !g_cmd_queue.empty() || !server_active;
});
if (!server_active && g_cmd_queue.empty())
break;
std::string cmd = std::move(g_cmd_queue.front());
g_cmd_queue.pop();
lk.unlock();
const int delay_ms = g_delay_ms.load();
if (delay_ms > 0) {
std::this_thread::sleep_for(
std::chrono::milliseconds(delay_ms));
}
if (!server_active)
break;
broadcast(makeCommandJSON(cmd));
logf(INF, "[SENT] %s", cmd.c_str());
}
});
// main accept loop
server_sock = server;
while (server_active) {
#ifdef _WIN32
fd_set readfds; FD_ZERO(&readfds); FD_SET(server, &readfds);
SOCKET maxfd = server;
{ std::lock_guard<std::mutex> lk(clients_mtx); for (auto& c : clients) { FD_SET(c.sock, &readfds); if (c.sock > maxfd) maxfd = c.sock; } }
timeval tv; tv.tv_sec = 1; tv.tv_usec = 0; int sel = select((int)maxfd + 1, &readfds, NULL, NULL, &tv);
if (sel <= 0) continue;
if (FD_ISSET(server, &readfds)) {
sockaddr_in claddr; int len = sizeof(claddr);
SOCKET cs = accept(server, (sockaddr*)&claddr, &len);
if (cs != INVALID_SOCKET) { Client c; c.sock = cs; c.addr = claddr; c.websocket = false; std::lock_guard<std::mutex> lk(clients_mtx); clients.push_back(std::move(c)); logf(INF, "Client connected: %s:%d", inet_ntoa(claddr.sin_addr), ntohs(claddr.sin_port)); }
}
std::vector<sock_t> to_close;
{ std::lock_guard<std::mutex> lk(clients_mtx); for (auto& c : clients) {
if (!FD_ISSET(c.sock, &readfds)) continue;
char buf[4096]; int n = recv(c.sock, buf, (int)sizeof(buf), 0);
if (n <= 0) { logf(INF, "Client disconnected: %s:%d", inet_ntoa(c.addr.sin_addr), ntohs(c.addr.sin_port)); to_close.push_back(c.sock); continue; }
c.recvbuf.append(buf, buf + n);
if (!c.websocket) { if (c.recvbuf.find("\r\n\r\n") != std::string::npos) { if (websocket_handshake(c)) logf(INF, "WebSocket handshake OK! (%s:%d)", inet_ntoa(c.addr.sin_addr), ntohs(c.addr.sin_port)); else c.recvbuf.clear(); } }
else { if (handle_websocket_frames(c)) to_close.push_back(c.sock); }
} }
if (!to_close.empty()) { std::lock_guard<std::mutex> lk(clients_mtx); for (sock_t s : to_close) { closesocket(s); remove_client_by_socket_locked(s); } }
#else
std::vector<pollfd> pfds; pfds.push_back({server, POLLIN,0}); { std::lock_guard<std::mutex> lk(clients_mtx); for(auto &c:clients) pfds.push_back({c.sock,POLLIN,0}); }
int ret = poll(pfds.data(), pfds.size(), 1000); if(ret<=0) continue;
if(pfds[0].revents & POLLIN){ sockaddr_in claddr; socklen_t len=sizeof(claddr); int cs = accept(server,(sockaddr*)&claddr,&len); if(cs>=0){ Client c; c.sock=cs; c.addr=claddr; std::lock_guard<std::mutex> lk(clients_mtx); clients.push_back(c); logf(INF, "Client connected: %s:%d", inet_ntoa(claddr.sin_addr), ntohs(claddr.sin_port)); } }
std::vector<int> to_close;
{ std::lock_guard<std::mutex> lk(clients_mtx); for(size_t i=0;i<clients.size();++i){ Client &c=clients[i]; if(pfds[i+1].revents & POLLIN){ char buf[4096]; int n = recv(c.sock, buf, sizeof(buf),0); if(n<=0){ logf(INF, "Client disconnected: %s:%d", inet_ntoa(c.addr.sin_addr), ntohs(c.addr.sin_port)); to_close.push_back(c.sock); continue; } c.recvbuf.append(buf,buf+n); if(!c.websocket){ if(c.recvbuf.find("\r\n\r\n")!=std::string::npos){ if(websocket_handshake(c)) logf(INF, "WebSocket handshake OK! (%s:%d)", inet_ntoa(c.addr.sin_addr), ntohs(c.addr.sin_port)); else c.recvbuf.clear(); } } else { if(handle_websocket_frames(c)) to_close.push_back(c.sock); } } }
if(!to_close.empty()){ std::lock_guard<std::mutex> lk(clients_mtx); for(int s:to_close){ close(s); remove_client_by_socket_locked(s); } }}
#endif
}
// cleanup
{ std::lock_guard<std::mutex> lk(clients_mtx); for(auto &c:clients){ if (c.sock!=INVALID_SOCKET){
#ifdef _WIN32
closesocket(c.sock);
#else
close(c.sock);
#endif
} } clients.clear(); }
// stop sender
g_queue_cv.notify_one(); if(sender_thread.joinable())
//sender_thread.join();
#ifdef _WIN32
closesocket(server);
#else
close(server);
#endif
server_sock = INVALID_SOCKET;
server_active = false;
}
// interactive CLI from your example
static void interactive(){
// settings available before start
int setting_port = 19134;
int setting_delay = 10;
logf(INF,"Interactive mode. Commands: setting start stop exit quit help");
interactive_mode.store(true);
std::string line;
while (true) {
std::cout << "list2hosting> " << std::flush;
if (!std::getline(std::cin, line))
break;
if (line.empty())
continue;
// ユーザー入力の処理中は、logfによる自動再プロンプトを停止する
// これにより、コマンド実行中に出るログ([ENQUEUE]など)がプロンプトを重複させないようにする
interactive_mode.store(false);
std::istringstream ss(line); std::string cmd; ss>>cmd;
std::string lcmd = tolower_s(cmd);
if(lcmd=="exit"||lcmd=="quit"){
if(server_active){
server_active = false;
std::lock_guard<std::mutex> lk(server_mtx);
if(server_sock!=INVALID_SOCKET){
#ifdef _WIN32
closesocket(server_sock);
#else
close(server_sock);
#endif
server_sock = INVALID_SOCKET;
}
g_queue_cv.notify_one();
if(server_thread.joinable()) server_thread.join();
if(sender_thread.joinable()) sender_thread.join();
#ifdef _WIN32
WSACleanup();
#endif
}
break;
}
if(lcmd=="help"){
std::cout<<"setting <key> <value>\nstart [port]\nstop\nqueue\nexit\n<any other line> -> send as Minecraft command\n";
interactive_mode.store(true); // 次のループのために戻す
continue;
}
if(lcmd=="setting"){
std::string key, val; ss>>key>>val; if(key.empty()||val.empty()){ logf(WARN,"usage: setting <port|delay> <value>"); }
else if(key=="port"){ try{ setting_port = std::stoi(val); logf(INF,"port=%d",setting_port);}catch(...){ logf(WARN,"bad port"); } }
// Bug fix: Update g_delay_ms immediately
else if(key=="delay"){
try{
setting_delay = std::stoi(val);
g_delay_ms = setting_delay;
logf(INF,"delay=%d",setting_delay);
}catch(...){ logf(WARN,"bad delay"); }
}
else { logf(WARN,"unknown setting"); }
interactive_mode.store(true);
continue;
}
if(lcmd=="start"){
int p = setting_port; if(ss>>p) { /* if provided */ }
if(server_active){ logf(WARN,"server already running"); }
else {
g_delay_ms = setting_delay;
// initialize WinSock on Windows
#ifdef _WIN32
{
WSADATA wsa;
if (WSAStartup(MAKEWORD(2,2), &wsa) != 0) { logf(ERR, "WSAStartup failed"); }
else {
// start server in background
std::lock_guard<std::mutex> lk(server_mtx);
server_active = true;
server_thread = std::thread(run_server, p);
}
}
#else
{
std::lock_guard<std::mutex> lk(server_mtx);
server_active = true;
server_thread = std::thread(run_server, p);
}
#endif
}
interactive_mode.store(true);
continue;
}
if(lcmd=="stop"){
if(!server_active){ logf(WARN,"server not running"); }
else {
// stop server
server_active = false;
// close listening socket to break accept/poll
{
std::lock_guard<std::mutex> lk(server_mtx);
if(server_sock!=INVALID_SOCKET){
#ifdef _WIN32
closesocket(server_sock);
#else
close(server_sock);
#endif
server_sock = INVALID_SOCKET;
}
}
// wake sender
g_queue_cv.notify_one();
if(server_thread.joinable()) server_thread.join();
if(sender_thread.joinable()) sender_thread.join();
#ifdef _WIN32
WSACleanup();
#endif
logf(INF,"server stopped");
}
interactive_mode.store(true);
continue;
}
if(lcmd=="queue"){
enqueue_command("/say queue");
interactive_mode.store(true);
continue;
}
// any other line is treated as a raw command to send
{
std::string raw = line;
if(!raw.empty()) {
enqueue_command(raw);
interactive_mode.store(true);
continue;
}
}
logf(WARN,"unknown cmd");
interactive_mode.store(true);
}
interactive_mode.store(false);
}
int main(int argc, char** argv) {
// If no args, run the interactive CLI
if (argc <= 1) { interactive(); return 0; }
// strict parsing: only allow --port <port> and optional --delay <ms>
int port = -1;
for (int i = 1; i < argc; ++i) {
std::string arg = argv[i];
if (arg == "--port" && i + 1 < argc) { try { port = std::stoi(argv[++i]); } catch(...) { print_help(); return 1; } }
else if (arg == "--delay" && i + 1 < argc) { try { g_delay_ms = std::stoi(argv[++i]); } catch(...) { print_help(); return 1; } }
else { print_help(); return 1; }
}
if (port <= 0) { print_help(); return 1; }
#ifdef _WIN32
WSADATA wsa;
if (WSAStartup(MAKEWORD(2,2), &wsa) != 0) { logf(ERR, "WSAStartup failed"); return 1; }
#endif
// set delay from args already stored in g_delay_ms
g_delay_ms = g_delay_ms.load();
// start server on this thread
{
std::lock_guard<std::mutex> lk(server_mtx);
server_active = true;
}
run_server(port);
#ifdef _WIN32
WSACleanup();
#endif
return 0;
}