Skip to content

Commit 75b73ce

Browse files
committed
feat: BridgeTCPClient constructor for established connections
feat: tcp_client getId method feat: server-side client connection/disconnection examples: server.ino with python test script
1 parent 88057f3 commit 75b73ce

File tree

4 files changed

+94
-5
lines changed

4 files changed

+94
-5
lines changed

examples/server/python/main.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# A python sketch that uses RPC Bridge to test the server.ino
2+
3+
import time
4+
from arduino.app_utils import *
5+
6+
7+
def log(msg):
8+
with open("./log.log", "a") as f:
9+
f.write(str(msg) + "\n")
10+
11+
def main():
12+
res = Bridge.call("tcp/connect", "127.0.0.1", 5678)
13+
log(f"Connection attempt id: {res}")
14+
15+
written = Bridge.call("tcp/write", res, "Hello friend")
16+
log(f"Written msg of len: {written}")
17+
18+
time.sleep(1)
19+
20+
ok = Bridge.call("tcp/close", res)
21+
log(f"Closed connection: {ok}")
22+
23+
if __name__ == "__main__":
24+
25+
while True:
26+
main()
27+
time.sleep(1)

examples/server/server.ino

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <Arduino_RouterBridge.h>
2+
3+
IPAddress localhost(127, 0, 0, 1);
4+
BridgeTCPServer<> server(Bridge, localhost, 5678);
5+
6+
void setup() {
7+
Serial.begin(115200);
8+
9+
if (!Bridge.begin()) {
10+
Serial.println("cannot setup Bridge");
11+
while (true) {}
12+
}
13+
14+
server.begin();
15+
16+
}
17+
18+
void loop() {
19+
Serial.println("loop");
20+
21+
BridgeTCPClient<> client = server.accept();
22+
23+
if (client.connected() == 1){
24+
Serial.print("client ");
25+
Serial.print(client.getId());
26+
Serial.println(" connected");
27+
}
28+
29+
if (client) {
30+
Serial.println("A client established a connection");
31+
}
32+
33+
while (client.connected()) {
34+
size_t len = client.available();
35+
if (len) {
36+
Serial.println("Message received from client");
37+
uint8_t buff[len];
38+
client.read(buff, len);
39+
Serial.write(buff, len);
40+
}
41+
}
42+
43+
server.disconnect(); // Disconnects the client server-side
44+
45+
}

src/tcp_client.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class BridgeTCPClient : public Client {
3939
public:
4040
explicit BridgeTCPClient(BridgeClass& bridge): bridge(&bridge) {}
4141

42+
BridgeTCPClient(BridgeClass& bridge, uint32_t connection_id, bool connected=true): bridge(&bridge), connection_id(connection_id), _connected {connected} {}
43+
4244
bool begin() {
4345
k_mutex_init(&client_mutex);
4446
if (!(*bridge)) {
@@ -95,6 +97,10 @@ class BridgeTCPClient : public Client {
9597
return 0;
9698
}
9799

100+
uint32_t getId() const {
101+
return connection_id;
102+
}
103+
98104
size_t write(uint8_t c) override {
99105
return write(&c, 1);
100106
}

src/tcp_server.h

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class BridgeTCPServer final: public Server {
3535
bool _listening = false;
3636
uint32_t listener_id = 0;
3737
uint32_t connection_id = 0;
38+
bool _connected = false;
3839
struct k_mutex server_mutex{};
3940

4041
public:
@@ -60,7 +61,7 @@ class BridgeTCPServer final: public Server {
6061

6162
BridgeTCPClient<BufferSize> accept() {
6263

63-
if (connection_id != 0) {
64+
if (_connected) {
6465
return BridgeTCPClient<BufferSize>(*bridge, connection_id);
6566
}
6667

@@ -70,12 +71,15 @@ class BridgeTCPServer final: public Server {
7071

7172
k_mutex_unlock(&server_mutex);
7273

73-
if (ret && connection_id != 0) { // connection_id 0 marks an invalid connection
74+
if (ret) { // connection_id 0 marks an invalid connection
75+
_connected = true;
7476
return BridgeTCPClient<BufferSize>(*bridge, connection_id);
77+
} else {
78+
_connected = false;
79+
// Return invalid client
80+
return BridgeTCPClient<BufferSize>(*bridge, 0, false);
7581
}
7682

77-
// Return invalid client
78-
return BridgeTCPClient<BufferSize>(*bridge, 0);
7983
}
8084

8185
size_t write(uint8_t c) override {
@@ -86,7 +90,7 @@ class BridgeTCPServer final: public Server {
8690

8791
BridgeTCPClient<BufferSize> client = accept();
8892

89-
if (client) {
93+
if (client && _connected) {
9094
return client.write(buf, size);
9195
}
9296

@@ -106,6 +110,13 @@ class BridgeTCPServer final: public Server {
106110
k_mutex_unlock(&server_mutex);
107111
}
108112

113+
void disconnect() {
114+
k_mutex_lock(&server_mutex, K_FOREVER);
115+
_connected = false;
116+
connection_id = 0;
117+
k_mutex_unlock(&server_mutex);
118+
}
119+
109120
bool is_listening() const {
110121
return _listening;
111122
}

0 commit comments

Comments
 (0)