Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/libraries/AP_DDS/AP_DDS_UDP.cpp
Views: 1798
#include "AP_DDS_Client.h"12#if AP_DDS_UDP_ENABLED34#include <errno.h>56/*7open connection on UDP8*/9bool AP_DDS_Client::udp_transport_open(uxrCustomTransport *t)10{11AP_DDS_Client *dds = (AP_DDS_Client *)t->args;12auto *sock = NEW_NOTHROW SocketAPM(true);13if (sock == nullptr) {14return false;15}16if (!sock->connect(dds->udp.ip.get_str(), dds->udp.port.get())) {17return false;18}19dds->udp.socket = sock;20return true;21}2223/*24close UDP connection25*/26bool AP_DDS_Client::udp_transport_close(uxrCustomTransport *t)27{28AP_DDS_Client *dds = (AP_DDS_Client *)t->args;29delete dds->udp.socket;30dds->udp.socket = nullptr;31return true;32}3334/*35write on UDP36*/37size_t AP_DDS_Client::udp_transport_write(uxrCustomTransport *t, const uint8_t* buf, size_t len, uint8_t* error)38{39AP_DDS_Client *dds = (AP_DDS_Client *)t->args;40if (dds->udp.socket == nullptr) {41*error = EINVAL;42return 0;43}44const ssize_t ret = dds->udp.socket->send(buf, len);45if (ret <= 0) {46*error = errno;47return 0;48}49return ret;50}5152/*53read from UDP54*/55size_t AP_DDS_Client::udp_transport_read(uxrCustomTransport *t, uint8_t* buf, size_t len, int timeout_ms, uint8_t* error)56{57AP_DDS_Client *dds = (AP_DDS_Client *)t->args;58if (dds->udp.socket == nullptr) {59*error = EINVAL;60return 0;61}62const ssize_t ret = dds->udp.socket->recv(buf, len, timeout_ms);63if (ret <= 0) {64*error = errno;65return 0;66}67return ret;68}6970/*71initialise UDP connection72*/73bool AP_DDS_Client::ddsUdpInit()74{75// setup a non-framed transport for UDP76uxr_set_custom_transport_callbacks(&udp.transport, false,77udp_transport_open,78udp_transport_close,79udp_transport_write,80udp_transport_read);8182if (!uxr_init_custom_transport(&udp.transport, (void*)this)) {83return false;84}85comm = &udp.transport.comm;86return true;87}88#endif // AP_DDS_UDP_ENABLED899091