(HCore) AsyncUDPDevice

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

HCore has another two UDP endpoint device classes. They are built on OS asynchronous socket functions. These devices implement packet communication using UDP protocol. They are similar in usage to the UDPDevice classes.

AsyncUDPEndpointDevice

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


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

  public:
   
   // constructors
  
   static const ulen DefaultMaxPackets = 500 ;
  
   AsyncUDPEndpointDevice(UDPort udport,UDPoint dst,ulen max_packets=DefaultMaxPackets);
   
   virtual ~AsyncUDPEndpointDevice();
   
   // methods
   
   UDPort getPort() const;
   
   UDPoint getDst() const;
   
   enum Event
    {
     Event_read,
     Event_sent,
     Event_tick,
    
     Event_extra_timeout,
    
     Event_cancel_on_put,
     Event_cancel_on_get,
     Event_cancel,
     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(AsyncUDPEndpointDevice &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.

AsyncUDPMultipointDevice

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


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

  public: 

   // constructors
  
   static const ulen DefaultMaxPackets = 500 ;
  
   explicit AsyncUDPMultipointDevice(UDPort udport,ulen max_packets=DefaultMaxPackets);
   
   virtual ~AsyncUDPMultipointDevice();
   
   // methods
   
   UDPort getPort() const;
   
   enum Event
    {
     Event_read,
     Event_sent,
     Event_tick,
    
     Event_extra_timeout,
    
     Event_cancel_on_put,
     Event_cancel_on_get,
     Event_cancel,
     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(AsyncUDPMultipointDevice &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::AsyncUDPSocket

Both async UDP device classes are implemented using Sys::AsyncUDPSocket and Sys::AsyncUDPSocketWait classes, provided by the target. They are declared in the header sys/SysNet.h.


struct Sys::AsyncUDPSocket
 {
  // private

  ....
  
  // public
  
  struct AsyncState;
  
  using Async = AsyncState * ;
  
  struct OutResult
   {
    bool pending;
    Sys::ErrorType error;
   };
  
  struct InResult
   {
    Sys::ErrorType error;
    Net::UDPoint src;
    ulen len;
   };
  
  struct StartInResult
   {
    bool pending;
    InResult result;
   };
  
  Sys::ErrorType open(Net::UDPort udport) noexcept;
  
  Sys::ErrorType close() noexcept;
  
  OutResult startOutbound(Async async,Net::UDPoint dst,PtrLen<const uint8> data) noexcept;
  
  Sys::ErrorType completeOutbound(Async async) noexcept;
  
  StartInResult startInbound(Async async,PtrLen<uint8> buf) noexcept;
  
  InResult completeInbound(Async async) noexcept;
 };


struct Sys::AsyncUDPSocketWait
 {
  // private

  ....
  
  // public
  
  static const ulen MaxAsyncs = 50 ;
  
  Sys::ErrorType init(ulen async_count) noexcept;
  
  void exit() noexcept;
  
  AsyncUDPSocket::Async getAsync(ulen index) noexcept;
  
  bool addWait(ulen index) noexcept;
  
  bool delWait(ulen index) noexcept;
  
  Sys::WaitResult wait(MSec timeout) noexcept;
  
  Sys::WaitResult wait(TimeScope time_scope) noexcept;
  
  void interrupt() noexcept; // async , semaphore
  
  Sys::WaitResult waitAll(MSec timeout) noexcept;
  
  Sys::WaitResult waitAll(TimeScope time_scope) noexcept;
 };