forked from qubic/core-bob
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQubicServer.cpp
More file actions
395 lines (331 loc) · 13.9 KB
/
Copy pathQubicServer.cpp
File metadata and controls
395 lines (331 loc) · 13.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
#include <atomic>
#include <thread>
#include <vector>
#include <memory>
#include <mutex>
#include <cstring>
#include <unordered_map>
#include <chrono>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <unistd.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include "Logger.h"
#include "connection/connection.h"
#include "shim.h"
// Forward declaration from IOProcessor.cpp
void connReceiver(QCPtr conn, const bool isTrustedNode);
namespace {
// Simple connection limiter with global and per-IP limits
class ConnectionLimiter {
public:
ConnectionLimiter(size_t max_global, size_t max_per_ip)
: max_global_(max_global), max_per_ip_(max_per_ip) {}
// Try to accept a new connection from the given IP
// Returns true if allowed, false if limits exceeded
bool tryAccept(const std::string& ip) {
std::lock_guard<std::mutex> lk(mutex_);
// Check global limit
if (total_connections_ >= max_global_) {
Logger::get()->warn("ConnectionLimiter: Global limit reached ({}/{})",
total_connections_, max_global_);
return false;
}
// Check per-IP limit
auto current_ip_count = ip_connections_[ip];
if (current_ip_count >= max_per_ip_) {
Logger::get()->warn("ConnectionLimiter: IP {} limit reached ({}/{})",
ip, current_ip_count, max_per_ip_);
return false;
}
// Accept the connection
ip_connections_[ip]++;
total_connections_++;
Logger::get()->debug("ConnectionLimiter: Accepted {} (IP: {}/{}, Global: {}/{})",
ip, ip_connections_[ip], max_per_ip_,
total_connections_, max_global_);
return true;
}
// Release a connection from the given IP
void release(const std::string& ip) {
std::lock_guard<std::mutex> lk(mutex_);
auto it = ip_connections_.find(ip);
if (it != ip_connections_.end()) {
if (--it->second == 0) {
ip_connections_.erase(it);
}
}
if (total_connections_ > 0) {
total_connections_--;
}
Logger::get()->debug("ConnectionLimiter: Released {} (Global: {}/{})",
ip, total_connections_, max_global_);
}
// Get current statistics
size_t getTotalConnections() const {
std::lock_guard<std::mutex> lk(mutex_);
return total_connections_;
}
size_t getIpConnectionCount(const std::string& ip) const {
std::lock_guard<std::mutex> lk(mutex_);
auto it = ip_connections_.find(ip);
return (it != ip_connections_.end()) ? it->second : 0;
}
private:
mutable std::mutex mutex_;
std::unordered_map<std::string, size_t> ip_connections_;
size_t total_connections_ = 0;
const size_t max_global_;
const size_t max_per_ip_;
};
class QubicServer {
public:
static QubicServer& instance() {
static QubicServer inst;
return inst;
}
bool start(uint16_t port = 21842, size_t max_connections = 64, size_t max_per_ip = 5) {
std::lock_guard<std::mutex> lk(m_);
if (running_) return true;
// Initialize connection limiter
limiter_ = std::make_unique<ConnectionLimiter>(max_connections, max_per_ip);
listen_fd_ = ::socket(AF_INET, SOCK_STREAM, 0);
if (listen_fd_ < 0) {
Logger::get()->critical("QubicServer: socket() failed (errno={})", errno);
return false;
}
int yes = 1;
::setsockopt(listen_fd_, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
#ifdef SO_REUSEPORT
::setsockopt(listen_fd_, SOL_SOCKET, SO_REUSEPORT, &yes, sizeof(yes));
#endif
sockaddr_in addr{};
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(port);
if (::bind(listen_fd_, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) < 0) {
Logger::get()->critical("QubicServer: bind() failed on port {} (errno={})", port, errno);
::close(listen_fd_);
listen_fd_ = -1;
return false;
}
if (::listen(listen_fd_, max_connections) < 0) {
Logger::get()->critical("QubicServer: listen() failed (errno={})", errno);
::close(listen_fd_);
listen_fd_ = -1;
return false;
}
running_ = true;
accept_thread_ = std::thread(&QubicServer::acceptLoop, this);
cleanup_thread_ = std::thread(&QubicServer::cleanupThreadFunc, this);
Logger::get()->info("QubicServer: listening on port {} (max {} connections, {} per IP)",
port, max_connections, max_per_ip);
return true;
}
void stop() {
std::lock_guard<std::mutex> lk(m_);
if (!running_) return;
running_ = false;
if (listen_fd_ >= 0) {
::shutdown(listen_fd_, SHUT_RDWR);
::close(listen_fd_);
listen_fd_ = -1;
}
if (accept_thread_.joinable()) {
accept_thread_.join();
}
if (cleanup_thread_.joinable()) {
cleanup_thread_.join();
}
// Signal all client handlers to stop
std::vector<std::shared_ptr<ClientCtx>> local_clients;
{
std::lock_guard<std::mutex> lk2(clients_m_);
local_clients = clients_;
for (auto& c : local_clients) {
c->stopFlag.store(true, std::memory_order_relaxed);
if (c->conn) {
c->conn->disconnect();
}
}
}
// Wait for all client threads to finish
for (auto& c : local_clients) {
if (c->th.joinable()) {
c->th.join();
}
}
// Clear the list
{
std::lock_guard<std::mutex> lk2(clients_m_);
clients_.clear();
}
Logger::get()->info("QubicServer: stopped");
}
void setConnectionPool(ConnectionPool* ptr)
{
pConnPool = ptr;
}
ConnectionPool* getConnectionPool()
{
return pConnPool;
}
private:
struct ClientCtx {
std::atomic_bool stopFlag{false};
QCPtr conn;
std::thread th;
int fd{-1};
std::atomic_bool finished{false};
std::string client_ip;
std::chrono::steady_clock::time_point connected_at;
std::atomic<std::chrono::steady_clock::time_point> last_activity; // Track activity
};
QubicServer() = default;
~QubicServer() { stop(); }
void cleanupFinishedClients() {
std::lock_guard<std::mutex> lk(clients_m_);
size_t before = clients_.size();
clients_.erase(
std::remove_if(clients_.begin(), clients_.end(),
[this](const std::shared_ptr<ClientCtx>& ctx) {
if (ctx->finished.load(std::memory_order_acquire)) {
// Join the thread
if (ctx->th.joinable()) {
ctx->th.join();
}
// Release connection from limiter
if (limiter_) {
limiter_->release(ctx->client_ip);
}
return true; // Remove from vector
}
return false;
}),
clients_.end()
);
size_t after = clients_.size();
if (before != after) {
Logger::get()->debug("QubicServer: Cleaned up {} finished client(s), {} active",
before - after, after);
}
}
void cleanupThreadFunc() {
while (running_) {
std::this_thread::sleep_for(std::chrono::milliseconds(500));
cleanupFinishedClients();
pConnPool->removeDisconnectedClient();
}
}
void acceptLoop() {
while (running_) {
sockaddr_in cli{};
socklen_t len = sizeof(cli);
// Set accept timeout for periodic cleanup
struct timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 0;
::setsockopt(listen_fd_, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
int cfd = ::accept(listen_fd_, reinterpret_cast<sockaddr*>(&cli), &len);
if (cfd < 0) {
if (!running_) break;
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
// Timeout or interrupted - this is normal, continue
continue;
}
Logger::get()->warn("QubicServer: accept() failed with errno={}", errno);
continue;
}
// Get client IP address
char client_ip_str[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &cli.sin_addr, client_ip_str, INET_ADDRSTRLEN);
std::string client_ip(client_ip_str);
if (!limiter_->tryAccept(client_ip)) {
Logger::get()->warn("QubicServer: Rejecting connection from {} (limits exceeded)",
client_ip);
::shutdown(cfd, SHUT_RDWR);
::close(cfd);
continue;
}
// Set socket options
int one = 1;
::setsockopt(cfd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));
::setsockopt(cfd, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one));
// Create client context
auto ctx = std::make_shared<ClientCtx>();
ctx->fd = cfd;
ctx->client_ip = client_ip;
ctx->connected_at = std::chrono::steady_clock::now();
// Create QubicConnection (this allocates memory and spawns send thread)
try {
ctx->conn = make_qc_by_socket(cfd);
if (gAllowReceiveLogFromIncomingConnection)
{
// add to conn pool for data querying
pConnPool->add(ctx->conn);
}
} catch (const std::exception& e) {
Logger::get()->error("QubicServer: Failed to create connection for {}: {}",
client_ip, e.what());
::close(cfd);
limiter_->release(client_ip); // Release the slot
continue;
}
// Add to active clients list
{
std::lock_guard<std::mutex> lk(clients_m_);
clients_.push_back(ctx);
}
Logger::get()->debug("QubicServer: Accepted connection from {} (total active: {})",
client_ip, limiter_->getTotalConnections());
// Non-trusted connections
const bool isTrustedNode = false;
// Launch per-connection receiver thread
ctx->th = std::thread([this, ctx, isTrustedNode]() {
try {
ctx->conn->doHandshake();
// Run the main receiver loop
connReceiver(ctx->conn, isTrustedNode);
} catch (const std::exception& ex) {
Logger::get()->debug("QubicServer: Exception for {}: {}",
ctx->client_ip, ex.what());
} catch (...) {
Logger::get()->debug("QubicServer: Unknown exception for {}", ctx->client_ip);
}
// Cleanup when receiver exits
if (ctx->conn) {
ctx->conn->disconnect();
ctx->conn.reset();
}
ctx->fd = -1;
// Mark as finished for cleanup thread
ctx->finished.store(true, std::memory_order_release);
Logger::get()->debug("QubicServer: Client {} disconnected", ctx->client_ip);
});
}
}
private:
std::mutex m_;
std::atomic_bool running_{false};
int listen_fd_{-1};
std::thread accept_thread_;
std::thread cleanup_thread_;
std::mutex clients_m_;
std::vector<std::shared_ptr<ClientCtx>> clients_;
std::unique_ptr<ConnectionLimiter> limiter_;
ConnectionPool * pConnPool{nullptr};
};
} // namespace
// Public helpers to control the server
bool StartQubicServer(ConnectionPool* cp, uint16_t port = 21842)
{
QubicServer::instance().setConnectionPool(cp);
return QubicServer::instance().start(port, 64, 5); // 64 global, 5 per IP
}
void StopQubicServer() {
QubicServer::instance().stop();
Logger::get()->info("Stop qubic server");
}