Skip to content

Commit

Permalink
Merge pull request #337 from facchinm/udp_socketbuffer_fix
Browse files Browse the repository at this point in the history
SocketWrapper: UDP: send packet on endPacket(), not on write()
  • Loading branch information
facchinm committed Sep 29, 2021
2 parents 51bf25a + 32b4cfc commit 5746714
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
36 changes: 24 additions & 12 deletions libraries/SocketWrapper/src/MbedUdp.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
#include "MbedUdp.h"

#ifndef WIFI_UDP_BUFFER_SIZE
#define WIFI_UDP_BUFFER_SIZE 508
#endif

arduino::MbedUDP::MbedUDP() {
_packet_buffer = new uint8_t[WIFI_UDP_BUFFER_SIZE];
_current_packet = NULL;
Expand Down Expand Up @@ -60,18 +56,35 @@ void arduino::MbedUDP::stop() {
int arduino::MbedUDP::beginPacket(IPAddress ip, uint16_t port) {
_host = SocketHelpers::socketAddressFromIpAddress(ip, port);
//If IP is null and port is 0 the initialization failed
txBuffer.clear();
return (_host.get_ip_address() == nullptr && _host.get_port() == 0) ? 0 : 1;
}

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

int arduino::MbedUDP::endPacket() {
return 1;
_socket.set_blocking(true);
_socket.set_timeout(1000);

size_t size = txBuffer.available();
uint8_t buffer[size];
for (int i = 0; i < size; i++) {
buffer[i] = txBuffer.read_char();
}

nsapi_size_or_error_t ret = _socket.sendto(_host, buffer, size);
_socket.set_blocking(false);
_socket.set_timeout(0);
if (ret < 0) {
return 0;
}
return size;
}

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

// Write size bytes from buffer into the packet
size_t arduino::MbedUDP::write(const uint8_t *buffer, size_t size) {
_socket.set_blocking(true);
_socket.set_timeout(1000);
nsapi_size_or_error_t ret = _socket.sendto(_host, buffer, size);
_socket.set_blocking(false);
_socket.set_timeout(0);
if (ret < 0) {
return 0;
for (int i = 0; i<size; i++) {
if (txBuffer.availableForStore()) {
txBuffer.store_char(buffer[i]);
} else {
return 0;
}
}
return size;
}
Expand Down
6 changes: 5 additions & 1 deletion libraries/SocketWrapper/src/MbedUdp.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
#include "netsocket/SocketAddress.h"
#include "netsocket/UDPSocket.h"

#define UDP_TX_PACKET_MAX_SIZE 24
#ifndef WIFI_UDP_BUFFER_SIZE
#define WIFI_UDP_BUFFER_SIZE 508
#endif

namespace arduino {

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

RingBufferN<WIFI_UDP_BUFFER_SIZE> txBuffer;

protected:
virtual NetworkInterface* getNetwork() = 0;

Expand Down

0 comments on commit 5746714

Please sign in to comment.