Skip to content

Commit feebff6

Browse files
committed
mod: udp new methods beginPacket endPacket awaitPacket dropPacket
impr: write packet opt
1 parent 1037bd9 commit feebff6

File tree

1 file changed

+46
-15
lines changed

1 file changed

+46
-15
lines changed

src/udp_bridge.h

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ This file is part of the Arduino_RouterBridge library.
1717
#define UDP_CONNECT_METHOD "udp/connect"
1818
#define UDP_CONNECT_MULTI_METHOD "udp/connectMulticast"
1919
#define UDP_CLOSE_METHOD "udp/close"
20+
#define UDP_BEGIN_PACKET_METHOD "udp/beginPacket"
2021
#define UDP_WRITE_METHOD "udp/write"
21-
#define UDP_AWAIT_READ_METHOD "udp/awaitRead"
22+
#define UDP_END_PACKET_METHOD "udp/endPacket"
23+
#define UDP_AWAIT_PACKET_METHOD "udp/awaitPacket"
2224
#define UDP_READ_METHOD "udp/read"
25+
#define UDP_DROP_PACKET_METHOD "udp/dropPacket"
2326

2427
#include <api/Udp.h>
2528

@@ -128,24 +131,37 @@ class BridgeUDP final: public UDP {
128131
}
129132

130133
int beginPacket(const char *host, uint16_t port) override {
134+
if (!connected()) return 0;
135+
bool ok = false;
136+
131137
k_mutex_lock(&udp_mutex, K_FOREVER);
132138

133139
_targetHost = host;
134140
_targetPort = port;
141+
bool res = false;
142+
ok = bridge->call(UDP_BEGIN_PACKET_METHOD, connection_id, _targetHost, _targetPort).result(res) && res;
135143

136144
k_mutex_unlock(&udp_mutex);
137145

138-
return 1;
146+
return ok? 1 : 0;
139147
}
140148

141149
int endPacket() override {
150+
if (!connected()) return 0;
151+
bool ok = false;
152+
142153
k_mutex_lock(&udp_mutex, K_FOREVER);
154+
int transmitted = 0;
155+
ok = bridge->call(UDP_END_PACKET_METHOD, connection_id).result(transmitted);
143156

144-
_targetHost = "";
145-
_targetPort = 0;
157+
if (ok) {
158+
_targetHost = "";
159+
_targetPort = 0;
160+
}
146161

147162
k_mutex_unlock(&udp_mutex);
148-
return 1;
163+
164+
return ok? 1 : 0;
149165
}
150166

151167
size_t write(uint8_t c) override {
@@ -162,7 +178,10 @@ class BridgeUDP final: public UDP {
162178
}
163179

164180
size_t written;
165-
const bool ok = bridge->call(UDP_WRITE_METHOD, connection_id, _targetHost, _targetPort, payload).result(written);
181+
k_mutex_lock(&udp_mutex, K_FOREVER);
182+
const bool ok = bridge->call(UDP_WRITE_METHOD, connection_id, payload).result(written);
183+
k_mutex_unlock(&udp_mutex);
184+
166185
return ok? written : 0;
167186
}
168187

@@ -171,12 +190,11 @@ class BridgeUDP final: public UDP {
171190
int parsePacket() override {
172191
k_mutex_lock(&udp_mutex, K_FOREVER);
173192

174-
while (_remaining) read(); // ensure previous packet is read
193+
dropPacket(); // ensure previous packet is read
175194

176195
int out = 0;
177196

178-
RpcResult async_res = bridge->call(UDP_AWAIT_READ_METHOD, connection_id, read_timeout);
179-
const bool ret = _connected && async_res.result(packet_meta);
197+
const bool ret = _connected && bridge->call(UDP_AWAIT_PACKET_METHOD, connection_id, read_timeout).result(packet_meta);
180198

181199
if (ret) {
182200
if (!_remoteIP.fromString(packet_meta.host)) {
@@ -192,6 +210,24 @@ class BridgeUDP final: public UDP {
192210
return out;
193211
}
194212

213+
int dropPacket() {
214+
if (!connected()) return 0;
215+
216+
bool ok=false;
217+
218+
k_mutex_lock(&udp_mutex, K_FOREVER);
219+
if (_remaining > temp_buffer.available()) {
220+
bool res = false;
221+
ok = bridge->call(UDP_DROP_PACKET_METHOD, connection_id).result(res) && res;
222+
}
223+
224+
_remaining = 0;
225+
temp_buffer.clear();
226+
k_mutex_unlock(&udp_mutex);
227+
228+
return ok? 1 : 0;
229+
}
230+
195231
int available() override {
196232
k_mutex_lock(&udp_mutex, K_FOREVER);
197233
const int size = temp_buffer.availableForStore();
@@ -289,19 +325,14 @@ class BridgeUDP final: public UDP {
289325
k_mutex_lock(&udp_mutex, K_FOREVER);
290326

291327
MsgPack::arr_t<uint8_t> message;
292-
RpcResult async_res = bridge->call(UDP_READ_METHOD, connection_id, size, read_timeout);
293-
const bool ret = _connected && async_res.result(message);
328+
const bool ret = _connected && bridge->call(UDP_READ_METHOD, connection_id, size, read_timeout).result(message);
294329

295330
if (ret) {
296331
for (size_t i = 0; i < message.size(); ++i) {
297332
temp_buffer.store_char(static_cast<char>(message[i]));
298333
}
299334
}
300335

301-
// if (async_res.error.code > NO_ERR) {
302-
// _connected = false;
303-
// }
304-
305336
k_mutex_unlock(&udp_mutex);
306337
}
307338

0 commit comments

Comments
 (0)