64 lines
1.6 KiB
C++
64 lines
1.6 KiB
C++
/*
|
|
* UDP interface for the bunshin RNS shim, adapted from microReticulum's
|
|
* examples/common/udp_interface (Apache-2.0). The example version hardcodes
|
|
* compile-time defaults; this one takes its endpoints from the caller, and
|
|
* with no forward target configured it sends to the source address of the
|
|
* last datagram received, so a listen-only server can answer any client.
|
|
*/
|
|
#pragma once
|
|
|
|
#include <microReticulum/Interface.h>
|
|
#include <microReticulum/Bytes.h>
|
|
#include <microReticulum/Type.h>
|
|
|
|
#ifdef ARDUINO
|
|
#include <WiFi.h>
|
|
#include <WiFiUdp.h>
|
|
#else
|
|
#include <netinet/in.h>
|
|
#endif
|
|
|
|
#include <stdint.h>
|
|
#include <string>
|
|
|
|
class UDPInterface : public RNS::InterfaceImpl {
|
|
|
|
public:
|
|
static const uint32_t BITRATE_GUESS = 10*1000*1000;
|
|
|
|
UDPInterface(const char* name,
|
|
const std::string& local_host, int local_port,
|
|
const std::string& remote_host, int remote_port);
|
|
virtual ~UDPInterface();
|
|
|
|
virtual bool start();
|
|
virtual void stop();
|
|
virtual void loop();
|
|
|
|
virtual inline std::string toString() const { return "UDPInterface[" + _name + "/" + _local_host + ":" + std::to_string(_local_port) + "]"; }
|
|
|
|
protected:
|
|
virtual bool send_outgoing(const RNS::Bytes& data);
|
|
void on_incoming(const RNS::Bytes& data);
|
|
|
|
private:
|
|
RNS::Bytes _buffer;
|
|
|
|
std::string _local_host;
|
|
int _local_port;
|
|
std::string _remote_host;
|
|
int _remote_port;
|
|
|
|
bool _forward_configured = false;
|
|
|
|
#ifdef ARDUINO
|
|
WiFiUDP udp;
|
|
#else
|
|
int _socket = -1;
|
|
in_addr_t _local_address = INADDR_ANY;
|
|
in_addr_t _remote_address = INADDR_NONE;
|
|
sockaddr_in _last_src_addr = {};
|
|
bool _have_src = false;
|
|
#endif
|
|
|
|
};
|