-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRetiTransportServer.cpp
More file actions
149 lines (130 loc) · 5.74 KB
/
Copy pathRetiTransportServer.cpp
File metadata and controls
149 lines (130 loc) · 5.74 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
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (C) 2026 Dobrev IT Ltd
//
// This file is part of RetiMesh Node.
//
// RetiMesh Node is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation, either version 3 of the License, or (at your
// option) any later version.
//
// RetiMesh Node is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
// Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with RetiMesh Node. If not, see <https://www.gnu.org/licenses/>.
// ============================================================================
// RetiTransportServer.cpp — see RetiTransportServer.h for the data flow.
// ============================================================================
#include "RetiTransportServer.h"
#include "RnsTransport.h"
RetiTransportServer transportServer;
// ---------------------------------------------------------------------------
void RetiTransportServer::begin(RingbufHandle_t tcpInRing) {
_tcpInRing = tcpInRing;
_lock = xSemaphoreCreateMutex();
_server = new AsyncServer(RNS_TCP_PORT);
_server->setNoDelay(true);
_server->onClient([](void* self, AsyncClient* client) {
static_cast<RetiTransportServer*>(self)->onClient(client);
}, this);
_server->begin();
log_i("Reticulum transport listening on 0.0.0.0:%d", RNS_TCP_PORT);
}
size_t RetiTransportServer::clientCount() {
xSemaphoreTake(_lock, portMAX_DELAY);
size_t n = _clients.size();
xSemaphoreGive(_lock);
return n;
}
// ---------------------------------------------------------------------------
// Connection lifecycle (all callbacks run on the AsyncTCP event task).
// ---------------------------------------------------------------------------
void RetiTransportServer::onClient(AsyncClient* client) {
if (client == nullptr) return;
if (clientCount() >= RNS_MAX_CLIENTS) {
log_w("Rejecting %s: client limit (%d) reached",
client->remoteIP().toString().c_str(), RNS_MAX_CLIENTS);
client->close();
return;
}
auto* ctx = new ClientCtx{ _nextId++, client, {} };
client->setNoDelay(true); // packets are latency-sensitive
client->setKeepAlive(10000, 3); // detect vanished phones
client->onData([](void* arg, AsyncClient*, void* data, size_t len) {
auto* c = static_cast<ClientCtx*>(arg);
transportServer.onData(c, (const uint8_t*)data, len);
}, ctx);
client->onDisconnect([](void* arg, AsyncClient*) {
transportServer.onDisconnect(static_cast<ClientCtx*>(arg));
}, ctx);
client->onError([](void* arg, AsyncClient*, int8_t error) {
log_w("TCP client error %d", error);
}, ctx);
xSemaphoreTake(_lock, portMAX_DELAY);
_clients.push_back(ctx);
g_stats.tcpClients = _clients.size();
xSemaphoreGive(_lock);
String ip = client->remoteIP().toString();
RnsTransport::clientConnected(ctx->id, ip.c_str());
log_i("Reticulum peer connected: %s (#%lu, %d total)", ip.c_str(),
(unsigned long)ctx->id, (int)g_stats.tcpClients);
}
void RetiTransportServer::onDisconnect(ClientCtx* ctx) {
xSemaphoreTake(_lock, portMAX_DELAY);
for (auto it = _clients.begin(); it != _clients.end(); ++it) {
if (*it == ctx) { _clients.erase(it); break; }
}
g_stats.tcpClients = _clients.size();
xSemaphoreGive(_lock);
RnsTransport::clientDisconnected(ctx->id);
AsyncClient* client = ctx->client;
delete ctx;
delete client; // server-accepted clients are ours
log_i("Reticulum peer disconnected (%d left)", (int)g_stats.tcpClients);
}
// ---------------------------------------------------------------------------
// Inbound: TCP bytes -> HDLC deframer -> [client id | packet] -> RNS task.
// Runs on the AsyncTCP task (core 0); it only copies bytes.
// ---------------------------------------------------------------------------
void RetiTransportServer::onData(ClientCtx* ctx, const uint8_t* data, size_t len) {
for (size_t i = 0; i < len; i++) {
ctx->deframer.feed(data[i], [this, ctx](const uint8_t* pkt, size_t pktLen) {
g_stats.tcpRxPackets++;
uint8_t item[sizeof(RnsTransport::TcpItemHeader) + RNS_MTU];
RnsTransport::TcpItemHeader h{ ctx->id };
memcpy(item, &h, sizeof(h));
memcpy(item + sizeof(h), pkt, pktLen);
// Drop rather than back-pressure the socket: a stalled AsyncTCP task
// takes the web server down with it, and Reticulum tolerates loss.
if (xRingbufferSend(_tcpInRing, item, sizeof(h) + pktLen, pdMS_TO_TICKS(20)) != pdTRUE)
log_w("TCP-in ring full, dropping %u-byte packet", (unsigned)pktLen);
});
}
}
// ---------------------------------------------------------------------------
// Outbound: one packet to one client. Called from the RNS task; _lock
// serialises the client list and the framing scratch buffer.
// ---------------------------------------------------------------------------
bool RetiTransportServer::sendTo(uint32_t clientId, const uint8_t* packet, size_t len) {
bool ok = false;
xSemaphoreTake(_lock, portMAX_DELAY);
size_t frameLen = HDLC::frame(packet, len, _frameBuf, sizeof(_frameBuf));
if (frameLen > 0) {
for (auto* ctx : _clients) {
if (ctx->id != clientId) continue;
AsyncClient* c = ctx->client;
// Slow-consumer policy: if the socket's window can't take the whole
// frame right now, drop it for that client instead of blocking.
if (c->connected() && c->canSend() && c->space() >= frameLen) {
c->write((const char*)_frameBuf, frameLen);
ok = true;
}
break;
}
}
xSemaphoreGive(_lock);
return ok;
}