OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4NetIO.h
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
5  * Copyright (c) 2009-2016, The OpenClonk Team and contributors
6  *
7  * Distributed under the terms of the ISC license; see accompanying file
8  * "COPYING" for details.
9  *
10  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
11  * See accompanying file "TRADEMARK" for details.
12  *
13  * To redistribute this file separately, substitute the full license texts
14  * for the above references.
15  */
16 /* network i/o, featuring tcp, udp and multicast */
17 
18 #ifndef C4NETIO_H
19 #define C4NETIO_H
20 
21 #include "lib/StdCompiler.h"
22 #include "platform/StdScheduler.h"
23 #include "platform/StdSync.h"
24 
25 #ifdef _WIN32
27 #include <winsock2.h>
28 #include <ws2tcpip.h>
29 #ifndef WINSOCK_VERSION
30 #define WINSOCK_VERSION MAKEWORD(2,2)
31 #endif
32 // Events are Windows-specific
33 #define HAVE_WINSOCK
34 #else
35 #define SOCKET int
36 #define INVALID_SOCKET (-1)
37 #include <arpa/inet.h>
38 // for htons
39 #include <netinet/in.h>
40 #endif
41 
42 #ifdef HAVE_SYS_TYPES_H
43 #include <sys/types.h>
44 #endif
45 #ifdef HAVE_SYS_SOCKET_H
46 #include <sys/socket.h>
47 #endif
48 
49 #ifndef SOCK_CLOEXEC
50 #define SOCK_CLOEXEC 0
51 #endif
52 
53 #ifndef HAVE_CONFIG_H
54 // #define C4NETIO_DEBUG
55 #endif
56 
57 
58 // net i/o base class
59 class C4NetIO : public StdSchedulerProc
60 {
61 public:
62  C4NetIO();
63  ~C4NetIO() override;
64 
65  // *** constants / types
66  static const int TO_INF; // = -1;
67 
68  struct HostAddress
69  {
71  {
72  IPv6 = AF_INET6,
73  IPv4 = AF_INET,
75  };
77  {
78  Loopback, // IPv6 localhost (::1)
79  Any, // IPv6 any-address (::)
80  AnyIPv4 // IPv4 any-address (0.0.0.0)
81  };
82 
84  {
87  };
88 
89  HostAddress() { Clear(); }
90  HostAddress(const HostAddress &other) { SetHost(other); }
92  explicit HostAddress(uint32_t addr) { SetHost(addr); }
93  HostAddress(const StdStrBuf &addr) { SetHost(addr); }
94  HostAddress(const sockaddr *addr) { SetHost(addr); }
95 
96  AddressFamily GetFamily() const;
97 
98  void SetScopeId(int scopeId);
99  int GetScopeId() const;
100 
101  void Clear();
102  void SetHost(const sockaddr *addr);
103  void SetHost(const HostAddress &host);
104  void SetHost(SpecialAddress host);
105  void SetHost(const StdStrBuf &host, AddressFamily family = UnknownFamily);
106  void SetHost(uint32_t host);
107 
108  C4NetIO::HostAddress AsIPv6() const; // convert an IPv4 address to an IPv6-mapped IPv4 address
109  C4NetIO::HostAddress AsIPv4() const; // try to convert an IPv6-mapped IPv4 address to an IPv4 address (returns unchanged address if not possible)
110 
111  // General categories
112  bool IsNull() const;
113  bool IsMulticast() const;
114  bool IsLoopback() const;
115  bool IsLocal() const; // IPv6 link-local address
116  bool IsPrivate() const; // IPv6 ULA or IPv4 private address range
117 // bool IsBroadcast() const;
118 
119  StdStrBuf ToString(int flags = 0) const;
120 
121  bool operator ==(const HostAddress &rhs) const;
122  bool operator !=(const HostAddress &rhs) const { return !(*this == rhs); }
123 
124  protected:
125  // data
126  union
127  {
128  sockaddr gen;
129  sockaddr_in v4;
130  sockaddr_in6 v6;
131  };
132  };
133 
134  struct EndpointAddress : public HostAddress // Host and port
135  {
136  static const uint16_t IPPORT_NONE = 0;
137 
140  EndpointAddress(const HostAddress &host, uint16_t port = IPPORT_NONE) : HostAddress(host) { SetPort(port); }
141  EndpointAddress(HostAddress::SpecialAddress addr, uint16_t port = IPPORT_NONE) : HostAddress(addr) { SetPort(port); }
142  explicit EndpointAddress(const StdStrBuf &addr) { SetAddress(addr); }
143 
144  StdStrBuf ToString(int flags = 0) const;
145 
146  void Clear();
147 
148  void SetAddress(const sockaddr *addr);
149  void SetAddress(const EndpointAddress &other);
150  void SetAddress(HostAddress::SpecialAddress addr, uint16_t port = IPPORT_NONE);
151  void SetAddress(const HostAddress &host, uint16_t port = IPPORT_NONE);
152  void SetAddress(const StdStrBuf &addr, AddressFamily family = UnknownFamily);
153 
154  HostAddress GetHost() const { return *this; } // HostAddress copy ctor slices off port information
155  EndpointAddress AsIPv6() const; // convert an IPv4 address to an IPv6-mapped IPv4 address
156  EndpointAddress AsIPv4() const; // try to convert an IPv6-mapped IPv4 address to an IPv4 address (returns unchanged address if not possible)
157 
158  void SetPort(uint16_t port);
159  void SetDefaultPort(uint16_t port); // set a port only if there is none
160  uint16_t GetPort() const;
161 
162  bool IsNull() const;
163  bool IsNullHost() const { return HostAddress::IsNull(); }
164 
165  // Pointer wrapper to be able to implicitly convert to sockaddr*
166  class EndpointAddressPtr;
167  const EndpointAddressPtr operator &() const;
170  {
171  EndpointAddress * const p;
173  friend const EndpointAddressPtr EndpointAddress::operator &() const;
174  EndpointAddressPtr(EndpointAddress *p) : p(p) {}
175  public:
176  const EndpointAddress &operator *() const { return *p; }
177  EndpointAddress &operator *() { return *p; }
178 
179  const EndpointAddress &operator ->() const { return *p; }
180  EndpointAddress &operator ->() { return *p; }
181 
182  operator const EndpointAddress*() const { return p; }
183  operator EndpointAddress*() { return p; }
184 
185  operator const sockaddr*() const { return &p->gen; }
186  operator sockaddr*() { return &p->gen; }
187 
188  operator const sockaddr_in*() const { return &p->v4; }
189  operator sockaddr_in*() { return &p->v4; }
190 
191  operator const sockaddr_in6*() const { return &p->v6; }
192  operator sockaddr_in6*() { return &p->v6; }
193  };
194 
195  bool operator ==(const EndpointAddress &rhs) const;
196  bool operator !=(const EndpointAddress &rhs) const { return !(*this == rhs); }
197 
198  // conversions
199  operator sockaddr() const { return gen; }
200  operator sockaddr_in() const { assert(gen.sa_family == AF_INET); return v4; }
201  operator sockaddr_in6() const { assert(gen.sa_family == AF_INET6); return v6; }
202 
203  // StdCompiler
204  void CompileFunc(StdCompiler *comp);
205 
206  private:
207  bool SetAddressByString(const StdStrBuf &address, short family);
208 
209  friend class EndpointAddressPtr;
210  };
212 
213  static std::vector<HostAddress> GetLocalAddresses();
214 
215  // callback class
216  class CBClass
217  {
218  public:
219  virtual bool OnConn(const addr_t &AddrPeer, const addr_t &AddrConnect, const addr_t *pOwnAddr, C4NetIO *pNetIO) { return true; }
220  virtual void OnDisconn(const addr_t &AddrPeer, C4NetIO *pNetIO, const char *szReason) { }
221  virtual void OnPacket(const class C4NetIOPacket &rPacket, C4NetIO *pNetIO) = 0;
222  virtual ~CBClass() = default;
223  };
224 
225  // used to explicitly callback to a specific class
226  template <class T>
227  class CBProxy : public CBClass
228  {
229  T *pTarget;
230  public:
231  CBProxy *operator () (T *pnTarget) { pTarget = pnTarget; return this; }
232  bool OnConn(const addr_t &AddrPeer, const addr_t &AddrConnect, const addr_t *pOwnAddr, C4NetIO *pNetIO) override
233  { return pTarget->T::OnConn(AddrPeer, AddrConnect, pOwnAddr, pNetIO); }
234  void OnDisconn(const addr_t &AddrPeer, C4NetIO *pNetIO, const char *szReason) override
235  { pTarget->T::OnDisconn(AddrPeer, pNetIO, szReason); }
236  void OnPacket(const class C4NetIOPacket &rPacket, C4NetIO *pNetIO) override
237  { pTarget->T::OnPacket(rPacket, pNetIO); }
238  };
239 
240 #ifdef _MSC_VER
241 #define NETIO_CREATE_CALLBACK_PROXY(ForClass, ProxyName) \
242  typedef class C4NetIO::CBProxy<ForClass> CBProxyT; \
243  friend CBProxyT; \
244  CBProxyT ProxyName;
245 #else
246 #define NETIO_CREATE_CALLBACK_PROXY(ForClass, ProxyName) \
247  friend class C4NetIO::CBProxy<ForClass>; \
248  C4NetIO::CBProxy<ForClass> ProxyName;
249 #endif
250 
251  // *** interface
252 
253  // * not multithreading safe
254  virtual bool Init(uint16_t iPort = addr_t::IPPORT_NONE) = 0;
255  virtual bool Close() = 0;
256 
257  bool Execute(int iTimeout = -1, pollfd * = nullptr) override = 0; // (for StdSchedulerProc)
258  bool IsNotify() override { return true; }
259 
260  // * multithreading safe
261  virtual bool Connect(const addr_t &addr) = 0; // async!
262  virtual bool Close(const addr_t &addr) = 0;
263 
264  virtual bool Send(const class C4NetIOPacket &rPacket) = 0;
265  virtual bool SetBroadcast(const addr_t &addr, bool fSet = true) = 0;
266  virtual bool Broadcast(const class C4NetIOPacket &rPacket) = 0;
267 
268  // statistics
269  virtual bool GetStatistic(int *pBroadcastRate) = 0;
270  virtual bool GetConnStatistic(const addr_t &addr, int *pIRate, int *pORate, int *pLoss) = 0;
271  virtual void ClearStatistic() = 0;
272 
273 protected:
274 // virtual SOCKET CreateSocket() = 0;
275 
276  // Makes IPv4 connections from an IPv6 socket work.
277  bool InitIPv6Socket(SOCKET socket);
278 
279  // *** errors
280 protected:
282  void SetError(const char *strnError, bool fSockErr = false);
283 public:
284  virtual const char *GetError() const { return Error.getData(); }
285  void ResetError() { Error.Clear(); }
286 
287  // *** callbacks
288  virtual void SetCallback(CBClass *pnCallback) = 0;
289 
290 };
291 
292 // packet class
293 class C4NetIOPacket : public StdCopyBuf
294 {
295 public:
296 
297  C4NetIOPacket();
298 
299  // construct from memory (copies / references data)
300  C4NetIOPacket(const void *pnData, size_t inSize, bool fCopy = false, const C4NetIO::addr_t &naddr = C4NetIO::addr_t());
301  // construct from buffer (copies data)
302  explicit C4NetIOPacket(const StdBuf &Buf, const C4NetIO::addr_t &naddr);
303  // construct from status byte + buffer (copies data)
304  C4NetIOPacket(uint8_t cStatusByte, const char *pnData, size_t inSize, const C4NetIO::addr_t &naddr = C4NetIO::addr_t());
305 
306  ~C4NetIOPacket();
307 
308 protected:
309 
310  // address
312 
313 public:
314 
315  const C4NetIO::addr_t &getAddr() const { return addr; }
316 
317  uint8_t getStatus()const { return getSize() ? *getBufPtr<char>(*this) : 0; }
318  const char *getPData() const { return getSize() ? getBufPtr<char>(*this, 1) : nullptr; }
319  size_t getPSize() const { return getSize() ? getSize() - 1 : 0; }
320  StdBuf getPBuf() const { return getSize() ? getPart(1, getSize() - 1) : getRef(); }
321 
322  // Some overloads
323  C4NetIOPacket getRef() const { return C4NetIOPacket(StdBuf::getRef(), addr); }
325  // change addr
326  void SetAddr(const C4NetIO::addr_t &naddr) { addr = naddr; }
327 
328  // delete contents
329  void Clear();
330 };
331 
332 
333 // tcp network i/o
334 class C4NetIOTCP : public C4NetIO, protected CStdCSecExCallback
335 {
336 public:
337  C4NetIOTCP();
338  ~C4NetIOTCP() override;
339 
340  // *** interface
341 
342  // * not multithreading safe
343  bool Init(uint16_t iPort = addr_t::IPPORT_NONE) override;
344  virtual bool InitBroadcast(addr_t *pBroadcastAddr);
345  bool Close() override;
346  virtual bool CloseBroadcast();
347 
348  bool Execute(int iMaxTime = TO_INF, pollfd * readyfds = nullptr) override;
349 
350  // * multithreading safe
351  bool Connect(const addr_t &addr) override;
352  bool Close(const addr_t &addr) override;
353 
354  bool Send(const C4NetIOPacket &rPacket) override;
355  bool Broadcast(const C4NetIOPacket &rPacket) override;
356  bool SetBroadcast(const addr_t &addr, bool fSet = true) override;
357 
358  virtual void UnBlock();
359 #ifdef STDSCHEDULER_USE_EVENTS
360  HANDLE GetEvent() override;
361 #else
362  void GetFDs(std::vector<struct pollfd> & FDs) override;
363 #endif
364 
365  // statistics
366  bool GetStatistic(int *pBroadcastRate) override;
367  bool GetConnStatistic(const addr_t &addr, int *pIRate, int *pORate, int *pLoss) override;
368  void ClearStatistic() override;
369 
370 protected:
371 
372  // * overridables (packet format)
373 
374  // Append packet data to output buffer
375  virtual void PackPacket(const C4NetIOPacket &rPacket, StdBuf &rOutBuf);
376 
377  // Extract a packet from the start of the input buffer (if possible) and call OnPacket.
378  // Should return the numer of bytes used.
379  virtual size_t UnpackPacket(const StdBuf &rInBuf, const C4NetIO::addr_t &Addr);
380 
381  // *** data
382 
383  // peer class
384  class Peer
385  {
386  public:
387  Peer(const C4NetIO::addr_t &naddr, SOCKET nsock, C4NetIOTCP *pnParent);
388  ~Peer();
389  protected:
390  // constants
391  static const unsigned int iTCPHeaderSize; // = 28 + 24; // (bytes)
392  static const unsigned int iMinIBufSize; // = 8192; // (bytes)
393  // parent
395  // addr
397  // socket connected
399  // incoming & outgoing buffer
402  // statistics
404  // status (1 = open, 0 = closed)
405  bool fOpen;
406  // selected for broadcast?
408  // IO critical sections
410  public:
411  // data access
412  const C4NetIO::addr_t &GetAddr() const { return addr; }
413  SOCKET GetSocket() const { return sock; }
414  int GetIRate() const { return iIRate; }
415  int GetORate() const { return iORate; }
416  // send a packet to this peer
417  bool Send(const C4NetIOPacket &rPacket);
418  // send as much data of the interal outgoing buffer as possible
419  bool Send();
420  // request buffer space for new input. Must call OnRecv or NoRecv afterwards!
421  void *GetRecvBuf(int iSize);
422  // called after the buffer returned by GetRecvBuf has been filled with fresh data
423  void OnRecv(int iSize);
424  // close socket
425  void Close();
426  // test: open?
427  bool Open() const { return fOpen; }
428  // selected for broadcast?
429  bool doBroadcast() const { return fDoBroadcast; }
430  // outgoing data waiting?
431  bool hasWaitingData() const { return !OBuf.isNull(); }
432  // select/unselect peer
433  void SetBroadcast(bool fSet) { fDoBroadcast = fSet; }
434  // statistics
435  void ClearStatistics();
436  public:
437  // next peer
439  };
440  friend class Peer;
441  // peer list
442  Peer *pPeerList{nullptr};
443 
444  // small list for waited-for connections
445  struct ConnectWait
446  {
449 
451  }
452  *pConnectWaits{nullptr};
453 
456 
457  // initialized?
458  bool fInit{false};
459 
460  // listen socket
461  uint16_t iListenPort;
463 
464 #ifdef STDSCHEDULER_USE_EVENTS
465  // event indicating network activity
466  HANDLE Event{nullptr};
467 #else
468  // Pipe used for cancelling select
469  int Pipe[2];
470 #endif
471 
472  // *** implementation
473 
474  bool Listen(uint16_t inListenPort);
475 
476  Peer *Accept(SOCKET nsock = INVALID_SOCKET, const addr_t &ConnectAddr = addr_t());
477  Peer *GetPeer(const addr_t &addr);
478  void OnShareFree(CStdCSecEx *pCSec) override;
479 
480  void AddConnectWait(SOCKET sock, const addr_t &addr);
481  ConnectWait *GetConnectWait(const addr_t &addr);
482  void ClearConnectWaits();
483 
484  // *** callbacks
485 public:
486  void SetCallback(CBClass *pnCallback) override { pCB = pnCallback; };
487 private:
488  CBClass *pCB{nullptr};
489 
490 };
491 
492 // simple udp network i/o
493 // - No connections
494 // - Delivery not garantueed
495 // - Broadcast will multicast the packet to all clients with the same broadcast address.
496 class C4NetIOSimpleUDP : public C4NetIO
497 {
498 public:
500  ~C4NetIOSimpleUDP() override;
501 
502  bool Init(uint16_t iPort = addr_t::IPPORT_NONE) override;
503  virtual bool InitBroadcast(addr_t *pBroadcastAddr);
504  bool Close() override;
505  virtual bool CloseBroadcast();
506 
507  bool Execute(int iMaxTime = TO_INF, pollfd * = nullptr) override;
508 
509  bool Send(const C4NetIOPacket &rPacket) override;
510  bool Broadcast(const C4NetIOPacket &rPacket) override;
511 
512  virtual void UnBlock();
513 #ifdef STDSCHEDULER_USE_EVENTS
514  HANDLE GetEvent() override;
515 #else
516  void GetFDs(std::vector<struct pollfd> & FDs) override;
517 #endif
518 
519  // not implemented
520  bool Connect(const addr_t &addr) override { assert(false); return false; }
521  bool Close(const addr_t &addr) override { assert(false); return false; }
522 
523  bool SetBroadcast(const addr_t &addr, bool fSet = true) override { assert(false); return false; }
524 
525  bool GetStatistic(int *pBroadcastRate) override { assert(false); return false; }
526  bool GetConnStatistic(const addr_t &addr, int *pIRate, int *pORate, int *pLoss) override
527  { assert(false); return false; }
528  void ClearStatistic() override { assert(false); }
529 
530 private:
531  // status
532  bool fInit{false};
533  bool fMultiCast{false};
534  uint16_t iPort;
535 
536  // the socket and the associated event
537  SOCKET sock;
538 #ifdef STDSCHEDULER_USE_EVENTS
539  HANDLE hEvent{nullptr};
540 #else
541  int Pipe[2];
542 #endif
543 
544  // multicast
545  addr_t MCAddr; ipv6_mreq MCGrpInfo;
546  bool fMCLoopback;
547 
548  // multibind
549  int fAllowReUse{false};
550 
551 protected:
552 
553  // multicast address
554  const addr_t &getMCAddr() const { return MCAddr; }
555 
556  // (try to) control loopback
557  bool SetMCLoopback(int fLoopback);
558  bool getMCLoopback() const { return fMCLoopback; }
559 
560  // enable multi-bind (call before Init!)
561  void SetReUseAddress(bool fAllow);
562 
563 private:
564 
565  // socket wait (check for readability)
566  enum WaitResult { WR_Timeout, WR_Readable, WR_Cancelled, WR_Error = -1 };
567  WaitResult WaitForSocket(int iTimeout);
568 
569  // *** callbacks
570 public:
571  void SetCallback(CBClass *pnCallback) override { pCB = pnCallback; };
572 private:
573  CBClass *pCB;
574 
575 };
576 
577 // udp network i/o
578 // - Connection are emulated
579 // - Delivery garantueed
580 // - Broadcast will automatically be activated on one side if it's active on the other side.
581 // If the peer can't be reached through broadcasting, packets will be sent directly.
583 {
584 public:
585  C4NetIOUDP();
586  ~C4NetIOUDP() override;
587 
588  // *** interface
589 
590  bool Init(uint16_t iPort = addr_t::IPPORT_NONE) override;
591  bool InitBroadcast(addr_t *pBroadcastAddr) override;
592  bool Close() override;
593  bool CloseBroadcast() override;
594 
595  bool Execute(int iMaxTime = TO_INF, pollfd * = nullptr) override;
596 
597  bool Connect(const addr_t &addr) override;
598  bool Close(const addr_t &addr) override;
599 
600  bool Send(const C4NetIOPacket &rPacket) override;
601  bool SendDirect(C4NetIOPacket &&rPacket); // (mt-safe)
602  bool Broadcast(const C4NetIOPacket &rPacket) override;
603  bool SetBroadcast(const addr_t &addr, bool fSet = true) override;
604 
606 
607  bool GetStatistic(int *pBroadcastRate) override;
608  bool GetConnStatistic(const addr_t &addr, int *pIRate, int *pORate, int *pLoss) override;
609  void ClearStatistic() override;
610 
611 protected:
612 
613  // *** data
614 
615  // internal packet type ids
616  enum IPTypeID
617  {
626  };
627 
628  // packet structures
629  struct BinAddr;
630  struct PacketHdr; struct TestPacket; struct ConnPacket; struct ConnOKPacket; struct AddAddrPacket;
631  struct DataPacketHdr; struct CheckPacketHdr; struct ClosePacket;
632 
633  // constants
634  static const unsigned int iVersion; // = 2;
635 
636  static const unsigned int iStdTimeout, // = 1000, // (ms)
637  iCheckInterval; // = 1000 // (ms)
638 
639  static const unsigned int iMaxOPacketBacklog; // = 100;
640 
641  static const unsigned int iUDPHeaderSize; // = 8 + 24; // (bytes)
642 
643  // packet class
644  class PacketList;
645  class Packet
646  {
647  friend class PacketList;
648  public:
649 
650  // constants / structures
651  static const size_t MaxSize; // = 1024;
652  static const size_t MaxDataSize; // = MaxSize - sizeof(Header);
653 
654  // types used for packing
655  typedef uint32_t nr_t;
656 
657  // construction / destruction
658  Packet();
659  Packet(C4NetIOPacket &&rnData, nr_t inNr);
660  ~Packet();
661 
662  protected:
663  // data
664  nr_t iNr;
666  bool *pFragmentGot{nullptr};
667 
668  public:
669  // data access
670  C4NetIOPacket &GetData() { return Data; }
671  const C4NetIOPacket &GetData() const { return Data; }
672  nr_t GetNr() const { return iNr; }
673  bool Empty() const { return Data.isNull(); }
674  bool Multicast() const { return !!(Data.getStatus() & 0x80); }
675 
676  // fragmention
677  nr_t FragmentCnt() const;
678  C4NetIOPacket GetFragment(nr_t iFNr, bool fBroadcastFlag = false) const;
679  bool Complete() const;
680  bool FragmentPresent(nr_t iFNr) const;
681  bool AddFragment(const C4NetIOPacket &Packet, const C4NetIO::addr_t &addr);
682  protected:
683  ::size_t FragmentSize(nr_t iFNr) const;
684  // list
686  };
687  friend class Packet;
688 
690  {
691  public:
692  PacketList(unsigned int iMaxPacketCnt = ~0);
693  ~PacketList();
694 
695  protected:
696  // packet list
697  Packet *pFront{nullptr}, *pBack{nullptr};
698  // packet counts
699  unsigned int iPacketCnt{0}, iMaxPacketCnt;
700  // critical section
702 
703  public:
704  Packet *GetPacket(unsigned int iNr);
705  Packet *GetPacketFrgm(unsigned int iNr);
707  bool FragmentPresent(unsigned int iNr);
708 
709  bool AddPacket(Packet *pPacket);
710  bool DeletePacket(Packet *pPacket);
711  void ClearPackets(unsigned int iUntil);
712  void Clear();
713  };
714  friend class PacketList;
715 
716  // peer class
717  class Peer
718  {
719  public:
720 
721  // construction / destruction
722  Peer(const C4NetIO::addr_t &naddr, C4NetIOUDP *pnParent);
723  ~Peer();
724 
725  protected:
726 
727  // constants
728  static const unsigned int iConnectRetries; // = 5
729  static const unsigned int iReCheckInterval; // = 1000 (ms)
730 
731  // parent class
733 
734  // peer address
736  // alternate peer address
738  // the address used by the peer
740  // connection status
742  {
744  }
745  eStatus;
746  // does multicast work?
748  // selected for broadcast?
750  // do callback on connection timeout?
752 
753  // packet lists (outgoing, incoming, incoming multicast)
756 
757  // packet counters
758  unsigned int iOPacketCounter;
761 
762  unsigned int iMCAckPacketCounter;
763 
764  // output critical section
766 
767  // connection check time limit.
770 
771  // timeout time.
773  unsigned int iRetries;
774 
775  // statistics
778 
779  public:
780  // data access
781  const C4NetIO::addr_t &GetAddr() const { return addr; }
782  const C4NetIO::addr_t &GetAltAddr() const { return addr2; }
783 
784  // initiate connection
785  bool Connect(bool fFailCallback);
786 
787  // send something to this computer
788  bool Send(const C4NetIOPacket &rPacket);
789  // check for lost packets
790  bool Check(bool fForceCheck = true);
791 
792  // called if something from this peer was received
793  void OnRecv(const C4NetIOPacket &Packet);
794 
795  // close connection
796  void Close(const char *szReason);
797  // open?
798  bool Open() const { return eStatus == CS_Works; }
799  // closed?
800  bool Closed() const { return eStatus == CS_Closed; }
801  // multicast support?
802  bool MultiCast() const { return fMultiCast; }
803 
804  // acknowledgment check
805  unsigned int GetMCAckPacketCounter() const { return iMCAckPacketCounter; }
806 
807  // timeout checking
809  void CheckTimeout();
810 
811  // selected for broadcast?
812  bool doBroadcast() const { return fDoBroadcast; }
813  // select/unselect peer
814  void SetBroadcast(bool fSet) { fDoBroadcast = fSet; }
815 
816  // alternate address
817  void SetAltAddr(const C4NetIO::addr_t &naddr2) { addr2 = naddr2; }
818 
819  // statistics
820  int GetIRate() const { return iIRate; }
821  int GetORate() const { return iORate; }
822  int GetLoss() const { return iLoss; }
823  void ClearStatistics();
824 
825  protected:
826 
827  // helpers
828  bool DoConn(bool fMC);
829  bool DoCheck(int iAskCnt = 0, int iMCAskCnt = 0, unsigned int *pAskList = nullptr);
830 
831  // sending
832  bool SendDirect(const Packet &rPacket, unsigned int iNr = ~0);
833  bool SendDirect(C4NetIOPacket &&rPacket);
834 
835  // events
836  void OnConn();
837  void OnClose(const char *szReason);
838 
839  // incoming packet list
840  void CheckCompleteIPackets();
841 
842  // timeout
843  void SetTimeout(int iLength = iStdTimeout, int iRetryCnt = 0);
844  void OnTimeout();
845 
846  public:
847  // next peer
849  };
850  friend class Peer;
851 
852  // critical sections
856 
857  // status
858  bool fInit{false};
859  bool fMultiCast{false};
860  uint16_t iPort;
861 
862  // peer list
863  Peer *pPeerList{nullptr};
864 
865  // currently initializing - do not process packets, save them back instead
866  bool fSavePacket{false};
868 
869  // multicast support data
871  bool fDelayedLoopbackTest{false};
872 
873  // check timing.
875 
876  // outgoing packet list (for multicast)
878  unsigned int iOPacketCounter{0};
879 
880  // statistics
883 
884  // callback proxy
886 
887  // * helpers
888 
889  // sending
890  bool BroadcastDirect(const Packet &rPacket, unsigned int iNr = ~0u); // (mt-safe)
891 
892  // multicast related
893  bool DoLoopbackTest();
894  void ClearMCPackets();
895 
896  // peer list
897  void AddPeer(Peer *pPeer);
898  Peer *GetPeer(const addr_t &addr);
899  Peer *ConnectPeer(const addr_t &PeerAddr, bool fFailCallback);
900  void OnShareFree(CStdCSecEx *pCSec) override;
901 
902  // connection check
903  void DoCheck();
904 
905  // critical section: only one execute at a time
907 
908  // debug
909 #ifdef C4NETIO_DEBUG
910  int hDebugLog;
911  void OpenDebugLog();
912  void CloseDebugLog();
913  void DebugLogPkt(bool fOut, const C4NetIOPacket &Pkt);
914 #endif
915 
916  // *** callbacks
917 public:
918  void SetCallback(CBClass *pnCallback) override { pCB = pnCallback; };
919 private:
920  CBClass *pCB;
921 
922  // callback interface for C4NetIO
923  virtual bool OnConn(const addr_t &AddrPeer, const addr_t &AddrConnect, const addr_t *pOwnAddr, C4NetIO *pNetIO);
924  virtual void OnDisconn(const addr_t &AddrPeer, C4NetIO *pNetIO, const char *szReason);
925  virtual void OnPacket(const class C4NetIOPacket &rPacket, C4NetIO *pNetIO);
926 
927  void OnAddAddress(const addr_t &FromAddr, const AddAddrPacket &Packet);
928 };
929 
930 // net i/o management (e.g. thread support)
932 {
933 public:
934  C4NetIOMan();
935  ~C4NetIOMan() override;
936 
937  void Clear();
938 
939  void AddIO(C4NetIO *pNetIO, bool fSetCallback = true);
940  void RemoveIO(C4NetIO *pNetIO);
941 
942 protected:
943 
944  // net i/o list
946  C4NetIO **ppNetIO{nullptr};
947 
948  // overridables
949  virtual void OnError(const char *strError, C4NetIO *pNetIO) { };
950 
951 private:
952  void OnError(StdSchedulerProc *pProc) override;
953 
954  void EnlargeIO(int iBy);
955 };
956 
957 #ifdef HAVE_WINSOCK
958 bool AcquireWinSock();
959 void ReleaseWinSock();
960 #endif
961 
962 // Class that keeps winsock loaded
964 {
965 public:
967  {
968 #ifdef HAVE_WINSOCK
969  AcquireWinSock();
970 #endif
971  }
972 
974  {
975 #ifdef HAVE_WINSOCK
976  ReleaseWinSock();
977 #endif
978  }
979 };
980 
981 
982 #endif
bool Execute(int iMaxTime=TO_INF, pollfd *readyfds=nullptr) override
Definition: C4NetIO.cpp:914
const char * getData() const
Definition: StdBuf.h:442
unsigned int iMaxPacketCnt
Definition: C4NetIO.h:699
const C4NetIO::addr_t & GetAddr() const
Definition: C4NetIO.h:412
void OnPacket(const class C4NetIOPacket &rPacket, C4NetIO *pNetIO) override
Definition: C4NetIO.h:236
unsigned int iMCAckPacketCounter
Definition: C4NetIO.h:762
C4NetIO::addr_t addr
Definition: C4NetIO.h:735
void ResetError()
Definition: C4NetIO.h:285
bool IsMulticast() const
Definition: C4NetIO.cpp:242
bool Open() const
Definition: C4NetIO.h:798
nr_t GetNr() const
Definition: C4NetIO.h:672
void ClearConnectWaits()
Definition: C4NetIO.cpp:1615
bool Send(const C4NetIOPacket &rPacket)
Definition: C4NetIO.cpp:3121
void SetBroadcast(bool fSet)
Definition: C4NetIO.h:814
C4NetIO::HostAddress AsIPv4() const
Definition: C4NetIO.cpp:325
bool OnConn(const addr_t &AddrPeer, const addr_t &AddrConnect, const addr_t *pOwnAddr, C4NetIO *pNetIO) override
Definition: C4NetIO.h:232
bool Send(const C4NetIOPacket &rPacket) override
Definition: C4NetIO.cpp:1248
void CheckTimeout()
Definition: C4NetIO.cpp:3399
C4NetIOPacket Data
Definition: C4NetIO.h:665
C4TimeMilliseconds GetNextTick(C4TimeMilliseconds tNow) override
Definition: C4NetIO.cpp:2716
Definition: StdBuf.h:29
#define INVALID_SOCKET
Definition: C4NetIO.h:36
int GetIRate() const
Definition: C4NetIO.h:414
bool getMCLoopback() const
Definition: C4NetIO.h:558
bool doBroadcast() const
Definition: C4NetIO.h:812
void ClearStatistics()
Definition: C4NetIO.cpp:1817
bool DoLoopbackTest()
Definition: C4NetIO.cpp:3625
bool SetMCLoopback(int fLoopback)
Definition: C4NetIO.cpp:2228
C4NetIOPacket GetFragment(nr_t iFNr, bool fBroadcastFlag=false) const
Definition: C4NetIO.cpp:2885
C4TimeMilliseconds GetTimeout()
Definition: C4NetIO.h:808
virtual bool InitBroadcast(addr_t *pBroadcastAddr)
Definition: C4NetIO.cpp:1924
unsigned int iLastMCPacketAsked
Definition: C4NetIO.h:769
addr_t PeerAddr
Definition: C4NetIO.h:739
bool InitBroadcast(addr_t *pBroadcastAddr) override
Definition: C4NetIO.cpp:2434
void SetCallback(CBClass *pnCallback) override
Definition: C4NetIO.h:571
bool Close() override
Definition: C4NetIO.cpp:2572
void SetAltAddr(const C4NetIO::addr_t &naddr2)
Definition: C4NetIO.h:817
HostAddress(uint32_t addr)
Definition: C4NetIO.h:92
void Clear()
Definition: StdBuf.h:466
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:853
void SetError(const char *strnError, bool fSockErr=false)
Definition: C4NetIO.cpp:745
void ClearStatistic() override
Definition: C4NetIO.cpp:1357
int iBroadcastRate
Definition: C4NetIO.h:881
bool IsLocal() const
Definition: C4NetIO.cpp:260
friend class Packet
Definition: C4NetIO.h:687
virtual bool Broadcast(const class C4NetIOPacket &rPacket)=0
unsigned int iLastPacketAsked
Definition: C4NetIO.h:769
PacketList OPackets
Definition: C4NetIO.h:877
void OnClose(const char *szReason)
Definition: C4NetIO.cpp:3496
Packet * Next
Definition: C4NetIO.h:685
C4NetIOPacket & GetData()
Definition: C4NetIO.h:670
unsigned int iRetries
Definition: C4NetIO.h:773
StdBuf getPBuf() const
Definition: C4NetIO.h:320
unsigned int iIMCPacketCounter
Definition: C4NetIO.h:760
int GetORate() const
Definition: C4NetIO.h:821
static const unsigned int iReCheckInterval
Definition: C4NetIO.h:729
void SetCallback(CBClass *pnCallback) override
Definition: C4NetIO.h:486
~C4NetIOMan() override
Definition: C4NetIO.cpp:3903
HostAddress(SpecialAddress addr)
Definition: C4NetIO.h:91
StdStrBuf ToString(int flags=0) const
Definition: C4NetIO.cpp:583
virtual void OnError(const char *strError, C4NetIO *pNetIO)
Definition: C4NetIO.h:949
StdStrBuf ToString(int flags=0) const
Definition: C4NetIO.cpp:599
unsigned int GetMCAckPacketCounter() const
Definition: C4NetIO.h:805
bool Broadcast(const C4NetIOPacket &rPacket) override
Definition: C4NetIO.cpp:1270
CStdCSec OutCSec
Definition: C4NetIO.h:855
virtual const char * GetError() const
Definition: C4NetIO.h:284
static const unsigned int iMinIBufSize
Definition: C4NetIO.h:392
C4NetIOPacket getRef() const
Definition: C4NetIO.h:323
StdCopyStrBuf Error
Definition: C4NetIO.h:281
bool Close() override
Definition: C4NetIO.cpp:1979
void ClearStatistic() override
Definition: C4NetIO.h:528
bool AddPacket(Packet *pPacket)
Definition: C4NetIO.cpp:3027
void SetHost(const sockaddr *addr)
Definition: C4NetIO.cpp:346
static const unsigned int iUDPHeaderSize
Definition: C4NetIO.h:641
bool SetBroadcast(const addr_t &addr, bool fSet=true) override
Definition: C4NetIO.cpp:2705
bool GetStatistic(int *pBroadcastRate) override
Definition: C4NetIO.h:525
bool GetStatistic(int *pBroadcastRate) override
Definition: C4NetIO.cpp:1337
Peer * pPeerList
Definition: C4NetIO.h:863
bool fSavePacket
Definition: C4NetIO.h:866
C4NetIO::HostAddress AsIPv6() const
Definition: C4NetIO.cpp:303
Peer * GetPeer(const addr_t &addr)
Definition: C4NetIO.cpp:3719
virtual void SetCallback(CBClass *pnCallback)=0
virtual void PackPacket(const C4NetIOPacket &rPacket, StdBuf &rOutBuf)
Definition: C4NetIO.cpp:1626
void SetAddress(const sockaddr *addr)
Definition: C4NetIO.cpp:364
static const unsigned int iMaxOPacketBacklog
Definition: C4NetIO.h:639
bool isNull() const
Definition: StdBuf.h:98
void SetTimeout(int iLength=iStdTimeout, int iRetryCnt=0)
Definition: C4NetIO.cpp:3549
void ClearStatistic() override
Definition: C4NetIO.cpp:2751
virtual bool OnConn(const addr_t &AddrPeer, const addr_t &AddrConnect, const addr_t *pOwnAddr, C4NetIO *pNetIO)
Definition: C4NetIO.h:219
const addr_t & getMCAddr() const
Definition: C4NetIO.h:554
bool DoCheck(int iAskCnt=0, int iMCAskCnt=0, unsigned int *pAskList=nullptr)
Definition: C4NetIO.cpp:3432
bool IsNull() const
Definition: C4NetIO.cpp:508
Packet * GetFirstPacketComplete()
Definition: C4NetIO.cpp:3014
bool Execute(int iMaxTime=TO_INF, pollfd *=nullptr) override
Definition: C4NetIO.cpp:2612
bool Execute(int iMaxTime=TO_INF, pollfd *=nullptr) override
Definition: C4NetIO.cpp:2038
bool Listen(uint16_t inListenPort)
Definition: C4NetIO.cpp:1476
size_t getSize() const
Definition: StdBuf.h:101
StdBuf getPart(size_t iStart, size_t inSize) const
Definition: StdBuf.h:107
bool Close(const addr_t &addr) override
Definition: C4NetIO.h:521
bool InitIPv6Socket(SOCKET socket)
Definition: C4NetIO.cpp:725
unsigned int iRIPacketCounter
Definition: C4NetIO.h:759
C4NetIOPacket LastPacket
Definition: C4NetIO.h:867
bool IsLoopback() const
Definition: C4NetIO.cpp:251
bool Check(bool fForceCheck=true)
Definition: C4NetIO.cpp:3143
bool FragmentPresent(unsigned int iNr)
Definition: C4NetIO.cpp:3020
virtual bool InitBroadcast(addr_t *pBroadcastAddr)
Definition: C4NetIO.cpp:846
virtual void UnBlock()
Definition: C4NetIO.cpp:2181
void SetBroadcast(bool fSet)
Definition: C4NetIO.h:433
~WinSockHolder()
Definition: C4NetIO.h:973
void Clear()
Definition: C4NetIO.cpp:3908
Peer * pPeerList
Definition: C4NetIO.h:442
void SetCallback(CBClass *pnCallback) override
Definition: C4NetIO.h:918
void Close(const char *szReason)
Definition: C4NetIO.cpp:3384
unsigned int iIPacketCounter
Definition: C4NetIO.h:759
const C4NetIO::addr_t & getAddr() const
Definition: C4NetIO.h:315
void AddPeer(Peer *pPeer)
Definition: C4NetIO.cpp:3682
Peer * Accept(SOCKET nsock=INVALID_SOCKET, const addr_t &ConnectAddr=addr_t())
Definition: C4NetIO.cpp:1365
virtual void UnBlock()
Definition: C4NetIO.cpp:1281
void OnDisconn(const addr_t &AddrPeer, C4NetIO *pNetIO, const char *szReason) override
Definition: C4NetIO.h:234
static const size_t MaxDataSize
Definition: C4NetIO.h:652
~C4NetIOTCP() override
Definition: C4NetIO.cpp:801
PacketList IPackets
Definition: C4NetIO.h:755
bool DoConn(bool fMC)
Definition: C4NetIO.cpp:3413
int Pipe[2]
Definition: C4NetIO.h:469
bool operator==(const HostAddress &rhs) const
Definition: C4NetIO.cpp:553
void GetFDs(std::vector< struct pollfd > &FDs) override
Definition: C4NetIO.cpp:2190
int GetScopeId() const
Definition: C4NetIO.cpp:296
static const unsigned int iVersion
Definition: C4NetIO.h:631
bool GetStatistic(int *pBroadcastRate) override
Definition: C4NetIO.cpp:2731
EndpointAddress(const EndpointAddress &other)
Definition: C4NetIO.h:139
bool operator!=(const EndpointAddress &rhs) const
Definition: C4NetIO.h:196
bool CloseBroadcast() override
Definition: C4NetIO.cpp:2600
bool GetConnStatistic(const addr_t &addr, int *pIRate, int *pORate, int *pLoss) override
Definition: C4NetIO.cpp:2738
bool Multicast() const
Definition: C4NetIO.h:674
static std::vector< HostAddress > GetLocalAddresses()
Definition: C4NetIO.cpp:626
void ClearStatistics()
Definition: C4NetIO.cpp:3406
C4NetIO ** ppNetIO
Definition: C4NetIO.h:946
friend class EndpointAddressPtr
Definition: C4NetIO.h:209
static const unsigned int iTCPHeaderSize
Definition: C4NetIO.h:391
bool hasWaitingData() const
Definition: C4NetIO.h:431
void * GetRecvBuf(int iSize)
Definition: C4NetIO.cpp:1743
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:454
int GetORate() const
Definition: C4NetIO.h:415
bool fDelayedLoopbackTest
Definition: C4NetIO.h:871
EndpointAddress(const StdStrBuf &addr)
Definition: C4NetIO.h:142
C4NetIO::addr_t addr
Definition: C4NetIO.h:311
CBProxy * operator()(T *pnTarget)
Definition: C4NetIO.h:231
~C4NetIOSimpleUDP() override
Definition: C4NetIO.cpp:1831
bool IsNullHost() const
Definition: C4NetIO.h:163
virtual bool GetConnStatistic(const addr_t &addr, int *pIRate, int *pORate, int *pLoss)=0
uint16_t iPort
Definition: C4NetIO.h:860
HostAddress(const HostAddress &other)
Definition: C4NetIO.h:90
C4NetIOUDP *const pParent
Definition: C4NetIO.h:732
const EndpointAddress & operator*() const
Definition: C4NetIO.h:176
Peer * GetPeer(const addr_t &addr)
Definition: C4NetIO.cpp:1531
bool Connect(const addr_t &addr) override
Definition: C4NetIO.h:520
HostAddress GetHost() const
Definition: C4NetIO.h:154
const C4NetIO::addr_t & GetAddr() const
Definition: C4NetIO.h:781
CStdCSec ICSec
Definition: C4NetIO.h:409
void SetPort(uint16_t port)
Definition: C4NetIO.cpp:526
bool fConnFailCallback
Definition: C4NetIO.h:751
bool Send(const C4NetIOPacket &rPacket) override
Definition: C4NetIO.cpp:2667
const C4NetIO::addr_t & GetAltAddr() const
Definition: C4NetIO.h:782
bool Broadcast(const C4NetIOPacket &rPacket) override
Definition: C4NetIO.cpp:2138
void CompileFunc(StdCompiler *comp)
Definition: C4NetIO.cpp:613
C4TimeMilliseconds tTimeout
Definition: C4NetIO.h:772
int iNetIOCapacity
Definition: C4NetIO.h:945
bool Closed() const
Definition: C4NetIO.h:800
~C4NetIOUDP() override
Definition: C4NetIO.cpp:2400
int iNetIOCnt
Definition: C4NetIO.h:945
addr_t MCLoopbackAddr
Definition: C4NetIO.h:870
void AddConnectWait(SOCKET sock, const addr_t &addr)
Definition: C4NetIO.cpp:1589
virtual void ClearStatistic()=0
bool Empty() const
Definition: C4NetIO.h:673
unsigned int iOPacketCounter
Definition: C4NetIO.h:878
bool fInit
Definition: C4NetIO.h:858
void SetScopeId(int scopeId)
Definition: C4NetIO.cpp:289
ConnectWait * Next
Definition: C4NetIO.h:450
void SetReUseAddress(bool fAllow)
Definition: C4NetIO.cpp:2240
bool IsPrivate() const
Definition: C4NetIO.cpp:270
PacketList(unsigned int iMaxPacketCnt=~0)
Definition: C4NetIO.cpp:2981
#define SOCKET
Definition: C4NetIO.h:35
CStdCSecEx ListCSec
Definition: C4NetIO.h:701
void CheckCompleteIPackets()
Definition: C4NetIO.cpp:3507
AddressFamily GetFamily() const
Definition: C4NetIO.cpp:520
void GetFDs(std::vector< struct pollfd > &FDs) override
Definition: C4NetIO.cpp:1302
bool fDoBroadcast
Definition: C4NetIO.h:407
void OnRecv(int iSize)
Definition: C4NetIO.cpp:1756
bool doBroadcast() const
Definition: C4NetIO.h:429
bool GetConnStatistic(const addr_t &addr, int *pIRate, int *pORate, int *pLoss) override
Definition: C4NetIO.h:526
enum C4NetIOUDP::Peer::ConnStatus eStatus
bool BroadcastDirect(const Packet &rPacket, unsigned int iNr=~0u)
Definition: C4NetIO.cpp:3586
EndpointAddress addr_t
Definition: C4NetIO.h:211
CStdCSec PeerListAddCSec
Definition: C4NetIO.h:455
int GetIRate() const
Definition: C4NetIO.h:820
bool FragmentPresent(nr_t iFNr) const
Definition: C4NetIO.cpp:2913
Packet * GetPacket(unsigned int iNr)
Definition: C4NetIO.cpp:2992
bool Close() override
Definition: C4NetIO.cpp:852
bool fMultiCast
Definition: C4NetIO.h:859
const EndpointAddressPtr operator&() const
Definition: C4NetIO.cpp:228
static const int TO_INF
Definition: C4NetIO.h:66
EndpointAddress(const HostAddress &host, uint16_t port=IPPORT_NONE)
Definition: C4NetIO.h:140
void OnRecv(const C4NetIOPacket &Packet)
Definition: C4NetIO.cpp:3179
#define NETIO_CREATE_CALLBACK_PROXY(ForClass, ProxyName)
Definition: C4NetIO.h:246
HostAddress(const StdStrBuf &addr)
Definition: C4NetIO.h:93
bool operator==(const EndpointAddress &rhs) const
Definition: C4NetIO.cpp:567
virtual bool Close()=0
bool * pFragmentGot
Definition: C4NetIO.h:666
bool SetBroadcast(const addr_t &addr, bool fSet=true) override
Definition: C4NetIO.cpp:1259
uint32_t nr_t
Definition: C4NetIO.h:655
bool GetConnStatistic(const addr_t &addr, int *pIRate, int *pORate, int *pLoss) override
Definition: C4NetIO.cpp:1344
static const size_t MaxSize
Definition: C4NetIO.h:651
bool DeletePacket(Packet *pPacket)
Definition: C4NetIO.cpp:3051
bool Init(uint16_t iPort=addr_t::IPPORT_NONE) override
Definition: C4NetIO.cpp:806
virtual bool Send(const class C4NetIOPacket &rPacket)=0
bool Connect(const addr_t &addr) override
Definition: C4NetIO.cpp:2650
virtual bool CloseBroadcast()
Definition: C4NetIO.cpp:2020
void OnShareFree(CStdCSecEx *pCSec) override
Definition: C4NetIO.cpp:3693
bool Init(uint16_t iPort=addr_t::IPPORT_NONE) override
Definition: C4NetIO.cpp:1836
void ClearMCPackets()
Definition: C4NetIO.cpp:3664
bool Open() const
Definition: C4NetIO.h:427
bool MultiCast() const
Definition: C4NetIO.h:802
void OnShareFree(CStdCSecEx *pCSec) override
Definition: C4NetIO.cpp:1541
virtual size_t UnpackPacket(const StdBuf &rInBuf, const C4NetIO::addr_t &Addr)
Definition: C4NetIO.cpp:1643
StdBuf getRef() const
Definition: StdBuf.h:271
bool fInit
Definition: C4NetIO.h:458
unsigned int iOPacketCounter
Definition: C4NetIO.h:758
static const uint16_t IPPORT_NONE
Definition: C4NetIO.h:136
void DoCheck()
Definition: C4NetIO.cpp:3749
bool Execute(int iTimeout=-1, pollfd *=nullptr) override=0
bool operator!=(const HostAddress &rhs) const
Definition: C4NetIO.h:122
Packet * GetPacketFrgm(unsigned int iNr)
Definition: C4NetIO.cpp:3003
SOCKET lsock
Definition: C4NetIO.h:462
bool Complete() const
Definition: C4NetIO.cpp:2904
uint8_t getStatus() const
Definition: C4NetIO.h:317
Peer * ConnectPeer(const addr_t &PeerAddr, bool fFailCallback)
Definition: C4NetIO.cpp:3729
static const unsigned int iCheckInterval
Definition: C4NetIO.h:636
::size_t FragmentSize(nr_t iFNr) const
Definition: C4NetIO.cpp:2971
virtual bool CloseBroadcast()
Definition: C4NetIO.cpp:900
unsigned int iPacketCnt
Definition: C4NetIO.h:699
virtual void OnDisconn(const addr_t &AddrPeer, C4NetIO *pNetIO, const char *szReason)
Definition: C4NetIO.h:220
C4NetIOTCP *const pParent
Definition: C4NetIO.h:394
const EndpointAddress & operator->() const
Definition: C4NetIO.h:179
bool IsNotify() override
Definition: C4NetIO.h:258
bool fDoBroadcast
Definition: C4NetIO.h:749
CStdCSec StatCSec
Definition: C4NetIO.h:777
bool Send(const C4NetIOPacket &rPacket) override
Definition: C4NetIO.cpp:2118
CStdCSec OutCSec
Definition: C4NetIO.h:765
bool AddFragment(const C4NetIOPacket &Packet, const C4NetIO::addr_t &addr)
Definition: C4NetIO.cpp:2918
virtual bool Init(uint16_t iPort=addr_t::IPPORT_NONE)=0
C4TimeMilliseconds tNextCheck
Definition: C4NetIO.h:874
C4NetIO::addr_t addr
Definition: C4NetIO.h:396
void RemoveIO(C4NetIO *pNetIO)
Definition: C4NetIO.cpp:3928
bool SendDirect(const Packet &rPacket, unsigned int iNr=~0)
Definition: C4NetIO.cpp:3456
bool IsNull() const
Definition: C4NetIO.cpp:503
virtual bool GetStatistic(int *pBroadcastRate)=0
bool Connect(bool fFailCallback)
Definition: C4NetIO.cpp:3114
PacketList IMCPackets
Definition: C4NetIO.h:755
virtual void OnPacket(const class C4NetIOPacket &rPacket, C4NetIO *pNetIO)=0
size_t getPSize() const
Definition: C4NetIO.h:319
int GetLoss() const
Definition: C4NetIO.h:822
EndpointAddress AsIPv6() const
Definition: C4NetIO.cpp:336
EndpointAddress AsIPv4() const
Definition: C4NetIO.cpp:341
Peer(const C4NetIO::addr_t &naddr, C4NetIOUDP *pnParent)
Definition: C4NetIO.cpp:3094
bool Broadcast(const C4NetIOPacket &rPacket) override
Definition: C4NetIO.cpp:2678
const char * getPData() const
Definition: C4NetIO.h:318
nr_t FragmentCnt() const
Definition: C4NetIO.cpp:2880
C4NetIO()
Definition: C4NetIO.cpp:718
void ClearPackets(unsigned int iUntil)
Definition: C4NetIO.cpp:3071
PacketList OPackets
Definition: C4NetIO.h:754
Peer(const C4NetIO::addr_t &naddr, SOCKET nsock, C4NetIOTCP *pnParent)
Definition: C4NetIO.cpp:1673
unsigned int iRIMCPacketCounter
Definition: C4NetIO.h:760
CStdCSec PeerListAddCSec
Definition: C4NetIO.h:854
C4NetIOPacket Duplicate() const
Definition: C4NetIO.h:324
const C4NetIOPacket & GetData() const
Definition: C4NetIO.h:671
EndpointAddress(HostAddress::SpecialAddress addr, uint16_t port=IPPORT_NONE)
Definition: C4NetIO.h:141
void AddIO(C4NetIO *pNetIO, bool fSetCallback=true)
Definition: C4NetIO.cpp:3915
void Clear()
Definition: C4NetIO.cpp:784
bool SendDirect(C4NetIOPacket &&rPacket)
Definition: C4NetIO.cpp:3598
C4NetIO::addr_t addr2
Definition: C4NetIO.h:737
void SetDefaultPort(uint16_t port)
Definition: C4NetIO.cpp:536
static const unsigned int iStdTimeout
Definition: C4NetIO.h:636
CStdCSec ExecuteCSec
Definition: C4NetIO.h:906
SOCKET GetSocket() const
Definition: C4NetIO.h:413
uint16_t GetPort() const
Definition: C4NetIO.cpp:542
virtual ~CBClass()=default
Packet * Prev
Definition: C4NetIO.h:685
C4TimeMilliseconds tNextReCheck
Definition: C4NetIO.h:768
void SetAddr(const C4NetIO::addr_t &naddr)
Definition: C4NetIO.h:326
CStdCSec OCSec
Definition: C4NetIO.h:409
ConnectWait * GetConnectWait(const addr_t &addr)
Definition: C4NetIO.cpp:1605
bool SetBroadcast(const addr_t &addr, bool fSet=true) override
Definition: C4NetIO.h:523
virtual bool SetBroadcast(const addr_t &addr, bool fSet=true)=0
HostAddress(const sockaddr *addr)
Definition: C4NetIO.h:94
bool Connect(const addr_t &addr) override
Definition: C4NetIO.cpp:1149
int iSize
Definition: TstC4NetIO.cpp:32
static const unsigned int iConnectRetries
Definition: C4NetIO.h:728
StdBuf Duplicate() const
Definition: StdBuf.h:248
virtual bool Connect(const addr_t &addr)=0
CStdCSec StatCSec
Definition: C4NetIO.h:882
uint16_t iListenPort
Definition: C4NetIO.h:461
~C4NetIO() override
bool Init(uint16_t iPort=addr_t::IPPORT_NONE) override
Definition: C4NetIO.cpp:2405