Skip to content

SocketWrapper: UDP: send packet on endPacket(), not on write() #337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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