Skip to content

Commit 32b4cfc

Browse files
committed
SocketWrapper: UDP: send packet on endPacket(), not on write()
1 parent a5644ce commit 32b4cfc

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

libraries/SocketWrapper/src/MbedUdp.cpp

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
#include "MbedUdp.h"
22

3-
#ifndef WIFI_UDP_BUFFER_SIZE
4-
#define WIFI_UDP_BUFFER_SIZE 508
5-
#endif
6-
73
arduino::MbedUDP::MbedUDP() {
84
_packet_buffer = new uint8_t[WIFI_UDP_BUFFER_SIZE];
95
_current_packet = NULL;
@@ -60,18 +56,35 @@ void arduino::MbedUDP::stop() {
6056
int arduino::MbedUDP::beginPacket(IPAddress ip, uint16_t port) {
6157
_host = SocketHelpers::socketAddressFromIpAddress(ip, port);
6258
//If IP is null and port is 0 the initialization failed
59+
txBuffer.clear();
6360
return (_host.get_ip_address() == nullptr && _host.get_port() == 0) ? 0 : 1;
6461
}
6562

6663
int arduino::MbedUDP::beginPacket(const char *host, uint16_t port) {
6764
_host = SocketAddress(host, port);
65+
txBuffer.clear();
6866
getNetwork()->gethostbyname(host, &_host);
6967
//If IP is null and port is 0 the initialization failed
7068
return (_host.get_ip_address() == nullptr && _host.get_port() == 0) ? 0 : 1;
7169
}
7270

7371
int arduino::MbedUDP::endPacket() {
74-
return 1;
72+
_socket.set_blocking(true);
73+
_socket.set_timeout(1000);
74+
75+
size_t size = txBuffer.available();
76+
uint8_t buffer[size];
77+
for (int i = 0; i < size; i++) {
78+
buffer[i] = txBuffer.read_char();
79+
}
80+
81+
nsapi_size_or_error_t ret = _socket.sendto(_host, buffer, size);
82+
_socket.set_blocking(false);
83+
_socket.set_timeout(0);
84+
if (ret < 0) {
85+
return 0;
86+
}
87+
return size;
7588
}
7689

7790
// Write a single byte into the packet
@@ -81,13 +94,12 @@ size_t arduino::MbedUDP::write(uint8_t byte) {
8194

8295
// Write size bytes from buffer into the packet
8396
size_t arduino::MbedUDP::write(const uint8_t *buffer, size_t size) {
84-
_socket.set_blocking(true);
85-
_socket.set_timeout(1000);
86-
nsapi_size_or_error_t ret = _socket.sendto(_host, buffer, size);
87-
_socket.set_blocking(false);
88-
_socket.set_timeout(0);
89-
if (ret < 0) {
90-
return 0;
97+
for (int i = 0; i<size; i++) {
98+
if (txBuffer.availableForStore()) {
99+
txBuffer.store_char(buffer[i]);
100+
} else {
101+
return 0;
102+
}
91103
}
92104
return size;
93105
}

libraries/SocketWrapper/src/MbedUdp.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
#include "netsocket/SocketAddress.h"
2828
#include "netsocket/UDPSocket.h"
2929

30-
#define UDP_TX_PACKET_MAX_SIZE 24
30+
#ifndef WIFI_UDP_BUFFER_SIZE
31+
#define WIFI_UDP_BUFFER_SIZE 508
32+
#endif
3133

3234
namespace arduino {
3335

@@ -44,6 +46,8 @@ class MbedUDP : public UDP {
4446
uint8_t* _current_packet;
4547
size_t _current_packet_size;
4648

49+
RingBufferN<WIFI_UDP_BUFFER_SIZE> txBuffer;
50+
4751
protected:
4852
virtual NetworkInterface* getNetwork() = 0;
4953

0 commit comments

Comments
 (0)