(HCore) UDPDevice

Files CCore/inc/net/UDPDevice.h CCore/src/net/UDPDevice.cpp

HCore has two UDP endpoint device classes. They are built on OS socket functions. These devices implement packet communication using UDP protocol.

UDPEndpointDevice

The class UDPEndpointDevice implements the PacketEndpointDevice interface and the PortManager interface.


class UDPEndpointDevice : public ObjBase , public PacketEndpointDevice , public PortManager
 {
   ....

  public:
   
   // constructors
  
   static const ulen DefaultMaxPackets = 500 ;
  
   UDPEndpointDevice(UDPort udport,UDPoint dst,ulen max_packets=DefaultMaxPackets);
   
   virtual ~UDPEndpointDevice();
   
   // methods
   
   UDPort getPort() const;
   
   UDPoint getDst() const;
   
   enum Event
    {
     Event_read,
     Event_sent,
     Event_tick,
    
     Event_extra_timeout,
    
     Event_nosent,
     Event_nopacket,
     Event_noread,
     Event_nodst,
    
     EventLim
    };
   
   friend const char * GetTextDesc(Event ev); 
  
   using StatInfo = Counters<Event,EventLim> ;                                  

   void getStat(StatInfo &ret);
   
   // PacketEndpointDevice
   
   virtual PacketFormat getOutboundFormat();
   
   virtual void outbound(Packet<uint8> packet);
   
   virtual ulen getMaxInboundLen();
   
   virtual void attach(InboundProc *proc);
   
   virtual void detach();
   
   // PortManager
   
   virtual XPoint getDevicePort() const;
   
   virtual XPoint getPort(XPoint point) const;
   
   virtual XPoint changePort(XPoint point,XPoint port) const;
   
   // start/stop
   
   class StartStop : NoCopy 
    {
     public:

      template <class ... TT>
      StartStop(UDPEndpointDevice &obj,TT ... tt);
      
      ~StartStop();
    };
 };

The first constructor argument is the local UDP port, the second is the UDP address (IP address+UDP port) of the communication peer. max_packets limits the packet usage for the inbound processing.

getPort() returns the local port.

getDst() returns the peer UDP address.

getStat() returns the device statistic.

Other methods are the PacketEndpointDevice interface implementation and the PortManager interface implementation.

The inner class StartStop should be used to activate the object of this class. You cannot attach an inbound processor (or detach it) to the object while it is running. So attach first, then start. Additional constructor arguments are not used.

UDPMultipointDevice

The class UDPMultipointDevice implements the PacketMultipointDevice interface and the PortManager interface.


class UDPMultipointDevice : public ObjBase , public PacketMultipointDevice , public PortManager
 {
   ....

  public:
   
   // constructors
  
   static const ulen DefaultMaxPackets = 500 ;
  
   explicit UDPMultipointDevice(UDPort udport,ulen max_packets=DefaultMaxPackets);
   
   virtual ~UDPMultipointDevice();
   
   // methods
   
   UDPort getPort() const;
   
   enum Event
    {
     Event_read,
     Event_sent,
     Event_tick,
    
     Event_extra_timeout,
    
     Event_nosent,
     Event_nopacket,
     Event_noread,
     
     EventLim
    };
   
   friend const char * GetTextDesc(Event ev); 
  
   using StatInfo = Counters<Event,EventLim> ;                                  

   void getStat(StatInfo &ret);
   
   // PacketMultipointDevice
   
   virtual StrLen toText(XPoint point,PtrLen<char> buf);
   
   virtual PacketFormat getOutboundFormat();
   
   virtual void outbound(XPoint point,Packet<uint8> packet);
   
   virtual ulen getMaxInboundLen();
   
   virtual void attach(InboundProc *proc);
   
   virtual void detach();
   
   // PortManager
   
   virtual XPoint getDevicePort() const;
   
   virtual XPoint getPort(XPoint point) const;
   
   virtual XPoint changePort(XPoint point,XPoint port) const;
   
   // start/stop
   
   class StartStop : NoCopy
    {
     public:

      template <class ... TT>
      StartStop(UDPMultipointDevice &obj,TT ... tt);
      
      ~StartStop();
    };
 };

The first constructor argument is the local UDP port. max_packets limits the packet usage for the inbound processing.

getPort() returns the local port.

getStat() returns the device statistic.

Other methods are the PacketEndpointDevice interface implementation and the PortManager interface implementation.

The inner class StartStop should be used to activate the object of this class. Additional constructor arguments are not used.

Sys::UDPSocket

Both UDP device classes are implemented using the class Sys::UDPSocket, declared in the target header sys/SysNet.h.


struct Sys::UDPSocket
 {
  // private

  ....

  // public
  
  struct InResult
   {
    Net::UDPoint src;
    ulen len;
    Sys::ErrorType error;
   };
  
  Sys::ErrorType open(Net::UDPort udport) noexcept;
  
  Sys::ErrorType close() noexcept;
  
  Sys::ErrorType outbound(Net::UDPoint dst,PtrLen<const uint8> data) noexcept;
  
  Sys::WaitResult wait_in(MSec timeout) noexcept;
  
  Sys::WaitResult wait_in(TimeScope time_scope) noexcept;
  
  InResult inbound(PtrLen<uint8> buf) noexcept;
 };