Skip to content

Commit 7a455d2

Browse files
committed
mod: various TBT
1 parent 8a75a47 commit 7a455d2

File tree

2 files changed

+71
-32
lines changed

2 files changed

+71
-32
lines changed

src/tcp_client.h

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#define BRIDGE_TCP_CLIENT_H
1616

1717
#define TCP_CONNECT_METHOD "tcp/connect"
18+
#define TCP_CONNECT_SSL_METHOD "tcp/connectSSL"
1819
#define TCP_CLOSE_METHOD "tcp/close"
1920
#define TCP_WRITE_METHOD "tcp/write"
2021
#define TCP_READ_METHOD "tcp/read"
@@ -27,7 +28,7 @@
2728

2829

2930
template<size_t BufferSize=DEFAULT_TCP_CLIENT_BUF_SIZE>
30-
class BridgeTCPClient final: public Client {
31+
class BridgeTCPClient : public Client {
3132

3233
BridgeClass* bridge;
3334
uint32_t connection_id{};
@@ -52,13 +53,13 @@ class BridgeTCPClient final: public Client {
5253

5354
int connect(const char *host, uint16_t port) override {
5455

55-
String send_buffer = host;
56-
send_buffer += ":";
57-
send_buffer += String(port);
56+
if (_connected) return 0;
57+
58+
String hostname = host;
5859

5960
k_mutex_lock(&client_mutex, K_FOREVER);
6061

61-
const bool resp = bridge->call(TCP_CONNECT_METHOD, connection_id, send_buffer);
62+
const bool resp = bridge->call(TCP_CONNECT_METHOD, connection_id, hostname, port);
6263

6364
if (!resp) {
6465
_connected = false;
@@ -72,19 +73,44 @@ class BridgeTCPClient final: public Client {
7273
return 0;
7374
}
7475

76+
int connectSSL(const char *host, uint16_t port, const char *ca_cert) {
77+
78+
if (_connected) return 0;
79+
80+
String hostname = host;
81+
String ca_cert_str = ca_cert;
82+
83+
k_mutex_lock(&client_mutex, K_FOREVER);
84+
85+
const bool resp = bridge->call(TCP_CONNECT_SSL_METHOD, connection_id, hostname, port, ca_cert_str);
86+
87+
if (!resp) {
88+
_connected = false;
89+
k_mutex_unlock(&client_mutex);
90+
return -1;
91+
}
92+
_connected = true;
93+
94+
k_mutex_unlock(&client_mutex);
95+
return 0;
96+
}
97+
7598
size_t write(uint8_t c) override {
7699
return write(&c, 1);
77100
}
78101

79102
size_t write(const uint8_t *buf, size_t size) override {
80-
String send_buffer;
103+
104+
if (!_connected) return 0;
105+
106+
MsgPack::arr_t<uint8_t> payload;
81107

82108
for (size_t i = 0; i < size; ++i) {
83-
send_buffer += static_cast<char>(buf[i]);
109+
payload.push_back(buf[i]);
84110
}
85111

86112
size_t written;
87-
const bool ret = bridge->call(TCP_WRITE_METHOD, written, send_buffer);
113+
const bool ret = bridge->call(TCP_WRITE_METHOD, written, connection_id, payload);
88114
if (ret) {
89115
return written;
90116
}
@@ -131,6 +157,10 @@ class BridgeTCPClient final: public Client {
131157
// No-op: flush is implemented for Client subclasses using an output buffer
132158
}
133159

160+
void close() {
161+
stop();
162+
}
163+
134164
void stop() override {
135165
k_mutex_lock(&client_mutex, K_FOREVER);
136166
String msg;

src/tcp_server.h

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,52 +31,44 @@ class BridgeTCPServer final: public Server {
3131
IPAddress _addr{};
3232
uint16_t _port;
3333
bool _listening = false;
34-
uint32_t listener_id;
34+
uint32_t listener_id = 0;
35+
uint32_t connection_id = 0;
3536
struct k_mutex server_mutex{};
3637

3738
public:
3839
explicit BridgeTCPServer(BridgeClass& bridge, const IPAddress& addr, uint16_t port): bridge(&bridge), _addr(addr), _port(port) {}
3940

40-
explicit BridgeTCPServer(BridgeClass& bridge, uint16_t port): bridge(&bridge), _addr(IP_ANY_TYPE), _port(port) {}
41+
// explicit BridgeTCPServer(BridgeClass& bridge, uint16_t port): bridge(&bridge), _addr(INADDR_NONE), _port(port) {}
4142

42-
bool begin() {
43+
void begin() override {
4344
k_mutex_init(&server_mutex);
45+
4446
if (!(*bridge)) {
45-
if (!bridge->begin()) {
46-
return false;
47-
}
47+
while (!bridge->begin());
4848
}
4949

5050
k_mutex_lock(&server_mutex, K_FOREVER);
5151

52-
String conn_str = addr.toString() + String(_port);
53-
const bool ret = bridge->call(TCP_LISTEN_METHOD, listener_id, conn_str);
54-
// TODO is listener_id one per server obj?
55-
56-
if (ret) {
57-
_listening = true;
58-
}
52+
String hostname = _addr.toString();
53+
_listening = bridge->call(TCP_LISTEN_METHOD, listener_id, hostname, _port);
5954

6055
k_mutex_unlock(&server_mutex);
6156

62-
return ret;
6357
}
6458

65-
void begin(uint16_t port) {
66-
_port = port;
67-
begin();
68-
}
59+
BridgeTCPClient<BufferSize> accept() {
6960

61+
if (connection_id != 0) {
62+
return BridgeTCPClient<BufferSize>(*bridge, connection_id);
63+
}
7064

71-
BridgeTCPClient<BufferSize> accept() {
7265
k_mutex_lock(&server_mutex, K_FOREVER);
7366

74-
uint32_t connection_id = 0;
7567
const bool ret = bridge->call(TCP_ACCEPT_METHOD, connection_id, listener_id);
7668

7769
k_mutex_unlock(&server_mutex);
7870

79-
if (ret && connection_id != 0) { // TODO is connection_id 0 acceptable???
71+
if (ret && connection_id != 0) { // connection_id 0 marks an invalid connection
8072
return BridgeTCPClient<BufferSize>(*bridge, connection_id);
8173
}
8274

@@ -89,12 +81,29 @@ class BridgeTCPServer final: public Server {
8981
}
9082

9183
size_t write(const uint8_t *buf, size_t size) override {
92-
// Broadcasting to all clients would require tracking them
93-
// For now, this is not implemented
94-
// TODO a logic to resolve which port-socket is the target of the write
84+
85+
BridgeTCPClient<BufferSize> client = accept();
86+
87+
if (client) {
88+
return client.write(buf, size);
89+
}
90+
9591
return 0;
9692
}
9793

94+
void close() {
95+
k_mutex_lock(&server_mutex, K_FOREVER);
96+
97+
String msg;
98+
const bool ret = bridge->call(TCP_CLOSE_METHOD, msg, listener_id);
99+
100+
if (ret) {
101+
_listening = false;
102+
}
103+
104+
k_mutex_unlock(&server_mutex);
105+
}
106+
98107
bool is_listening() const {
99108
return _listening;
100109
}

0 commit comments

Comments
 (0)