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