@@ -22,7 +22,7 @@ This file is part of the Arduino_RouterBridge library.
2222
2323#include < api/Udp.h>
2424
25- #define DEFAULT_UDP_BUF_SIZE 65507
25+ #define DEFAULT_UDP_BUF_SIZE 4096
2626
2727
2828template <size_t BufferSize=DEFAULT_UDP_BUF_SIZE>
@@ -57,15 +57,11 @@ class BridgeUDP final: public UDP {
5757
5858 k_mutex_lock (&udp_mutex, K_FOREVER);
5959
60- if (_connected) {
61- k_mutex_unlock (&udp_mutex);
62- return 1 ;
63- }
64-
6560 String hostname = " 0.0.0.0" ;
66- const bool ok = bridge->call (UDP_CONNECT_METHOD, hostname, port).result (connection_id);
61+ const bool ok = _connected || bridge->call (UDP_CONNECT_METHOD, hostname, port).result (connection_id);
6762 _connected = ok;
6863 if (_connected) _port = port;
64+
6965 k_mutex_unlock (&udp_mutex);
7066
7167 return ok? 1 : 0 ;
@@ -79,15 +75,11 @@ class BridgeUDP final: public UDP {
7975
8076 k_mutex_lock (&udp_mutex, K_FOREVER);
8177
82- if (_connected) {
83- k_mutex_unlock (&udp_mutex);
84- return 1 ;
85- }
86-
8778 String hostname = ip.toString ();
88- const bool ok = bridge->call (UDP_CONNECT_MULTI_METHOD, hostname, port).result (connection_id);
79+ const bool ok = _connected || bridge->call (UDP_CONNECT_MULTI_METHOD, hostname, port).result (connection_id);
8980 _connected = ok;
9081 if (_connected) _port = port;
82+
9183 k_mutex_unlock (&udp_mutex);
9284
9385 return ok? 1 : 0 ;
@@ -97,7 +89,9 @@ class BridgeUDP final: public UDP {
9789 k_mutex_lock (&udp_mutex, K_FOREVER);
9890
9991 String msg;
100- _connected = !bridge->call (UDP_CLOSE_METHOD, connection_id).result (msg);
92+ if (_connected) {
93+ _connected = !bridge->call (UDP_CLOSE_METHOD, connection_id).result (msg);
94+ }
10195
10296 k_mutex_unlock (&udp_mutex);
10397 }
@@ -152,7 +146,7 @@ class BridgeUDP final: public UDP {
152146 int parsePacket () override {
153147 k_mutex_lock (&udp_mutex, K_FOREVER);
154148
155- while (_remaining) read ();
149+ while (_remaining) read (); // ensure previous packet is read
156150
157151 int out = 0 ;
158152 if (available () >= 8 ) {
@@ -188,10 +182,11 @@ class BridgeUDP final: public UDP {
188182 return c;
189183 }
190184
185+ // reading stops when the UDP package has been read completely (_remaining = 0)
191186 int read (unsigned char *buffer, size_t len) override {
192187 k_mutex_lock (&udp_mutex, K_FOREVER);
193188 int i = 0 ;
194- while (temp_buffer. available () && i < len) {
189+ while (_remaining && i < len && available () ) {
195190 buffer[i++] = temp_buffer.read_char ();
196191 _remaining--;
197192 }
@@ -202,7 +197,7 @@ class BridgeUDP final: public UDP {
202197 int read (char *buffer, size_t len) override {
203198 k_mutex_lock (&udp_mutex, K_FOREVER);
204199 int i = 0 ;
205- while (temp_buffer. available () && i < len) {
200+ while (_remaining && i < len && available () ) {
206201 buffer[i++] = static_cast <char >(temp_buffer.read_char ());
207202 _remaining--;
208203 }
@@ -212,9 +207,10 @@ class BridgeUDP final: public UDP {
212207
213208 int peek () override {
214209 k_mutex_lock (&udp_mutex, K_FOREVER);
215- int out = 0 ;
216- if (!_connected || _remaining == 0 ) out = -1 ;
217- out = temp_buffer.peek ();
210+ int out = -1 ;
211+ if (_remaining && temp_buffer.available ()) {
212+ out = temp_buffer.peek ();
213+ }
218214 k_mutex_unlock (&udp_mutex);
219215 return out;
220216 }
@@ -256,13 +252,13 @@ class BridgeUDP final: public UDP {
256252
257253 void _read (size_t size) {
258254
259- k_mutex_lock (&udp_mutex, K_FOREVER) ;
255+ if (size == 0 ) return ;
260256
261- if (size == 0 || !_connected) return ;
257+ k_mutex_lock (&udp_mutex, K_FOREVER) ;
262258
263259 MsgPack::arr_t <uint8_t > message;
264260 RpcResult async_res = bridge->call (TCP_READ_METHOD, connection_id, size);
265- const bool ret = async_res.result (message);
261+ const bool ret = _connected && async_res.result (message);
266262
267263 if (ret) {
268264 for (size_t i = 0 ; i < message.size (); ++i) {
0 commit comments