OpenClonk
C4NetIO Class Referenceabstract

#include <C4NetIO.h>

Inheritance diagram for C4NetIO:
[legend]
Collaboration diagram for C4NetIO:
[legend]

Classes

class  CBClass
 
class  CBProxy
 
struct  EndpointAddress
 
struct  HostAddress
 

Public Types

typedef EndpointAddress addr_t
 

Public Member Functions

 C4NetIO ()
 
 ~C4NetIO () override
 
virtual bool Init (uint16_t iPort=addr_t::IPPORT_NONE)=0
 
virtual bool Close ()=0
 
bool Execute (int iTimeout=-1, pollfd *=nullptr) override=0
 
bool IsNotify () override
 
virtual bool Connect (const addr_t &addr)=0
 
virtual bool Close (const addr_t &addr)=0
 
virtual bool Send (const class C4NetIOPacket &rPacket)=0
 
virtual bool SetBroadcast (const addr_t &addr, bool fSet=true)=0
 
virtual bool Broadcast (const class C4NetIOPacket &rPacket)=0
 
virtual bool GetStatistic (int *pBroadcastRate)=0
 
virtual bool GetConnStatistic (const addr_t &addr, int *pIRate, int *pORate, int *pLoss)=0
 
virtual void ClearStatistic ()=0
 
virtual const char * GetError () const
 
void ResetError ()
 
virtual void SetCallback (CBClass *pnCallback)=0
 
bool ExecuteUntil (int iTimeout=-1)
 
virtual void GetFDs (std::vector< struct pollfd > &)
 
virtual C4TimeMilliseconds GetNextTick (C4TimeMilliseconds tNow)
 
bool IsSignaled ()
 
virtual bool IsLowPriority ()
 
virtual uint32_t TimerInterval ()
 

Static Public Member Functions

static std::vector< HostAddressGetLocalAddresses ()
 

Static Public Attributes

static const int TO_INF = -1
 

Protected Member Functions

bool InitIPv6Socket (SOCKET socket)
 
void SetError (const char *strnError, bool fSockErr=false)
 
void Changed ()
 

Protected Attributes

StdCopyStrBuf Error
 

Detailed Description

Definition at line 59 of file C4NetIO.h.

Member Typedef Documentation

◆ addr_t

Definition at line 212 of file C4NetIO.h.

Constructor & Destructor Documentation

◆ C4NetIO()

C4NetIO::C4NetIO ( )

Definition at line 723 of file C4NetIO.cpp.

References ResetError(), and ~C4NetIO().

724 {
725  ResetError();
726 }
void ResetError()
Definition: C4NetIO.h:286
Here is the call graph for this function:

◆ ~C4NetIO()

C4NetIO::~C4NetIO ( )
overridedefault

Referenced by C4NetIO().

Here is the caller graph for this function:

Member Function Documentation

◆ Broadcast()

virtual bool C4NetIO::Broadcast ( const class C4NetIOPacket rPacket)
pure virtual

Referenced by IsNotify().

Here is the caller graph for this function:

◆ Changed()

void StdSchedulerProc::Changed ( )
protectedinherited

Definition at line 108 of file StdScheduler.cpp.

References s.

Referenced by C4NetIOTCP::Accept(), C4NetIOTCP::AddConnectWait(), C4NetIOUDP::AddPeer(), StdScheduler::IsInManualLoop(), C4NetIOTCP::Listen(), and CStdTimerProc::SetDelay().

109 {
110  auto s = scheduler;
111  if (s)
112  s->Changed(this);
113 }
#define s
Here is the caller graph for this function:

◆ ClearStatistic()

virtual void C4NetIO::ClearStatistic ( )
pure virtual

Implemented in C4NetIOUDP, C4NetIOSimpleUDP, and C4NetIOTCP.

Referenced by C4Network2IO::GenerateStatistics(), and IsNotify().

Here is the caller graph for this function:

◆ Close() [1/2]

virtual bool C4NetIO::Close ( )
pure virtual

Implemented in C4NetIOUDP, C4NetIOSimpleUDP, C4NetIOTCP, C4Network2IRCClient, and C4AulDebug.

Referenced by C4Network2IOConnection::Close(), C4NetIOUDP::Peer::GetAltAddr(), C4NetIOTCP::Peer::GetORate(), C4Network2IO::HandlePuncherPacket(), and IsNotify().

Here is the caller graph for this function:

◆ Close() [2/2]

virtual bool C4NetIO::Close ( const addr_t addr)
pure virtual

◆ Connect()

virtual bool C4NetIO::Connect ( const addr_t addr)
pure virtual

Implemented in C4NetIOUDP, C4NetIOSimpleUDP, and C4NetIOTCP.

Referenced by C4Network2IOConnection::Connect(), C4NetIOUDP::Peer::GetAltAddr(), C4Network2IO::InitPuncher(), and IsNotify().

Here is the caller graph for this function:

◆ Execute()

bool C4NetIO::Execute ( int  iTimeout = -1,
pollfd *  = nullptr 
)
overridepure virtual

◆ ExecuteUntil()

bool StdSchedulerProc::ExecuteUntil ( int  iTimeout = -1)
inherited

Definition at line 33 of file StdScheduler.cpp.

References StdSchedulerProc::Execute(), and C4TimeMilliseconds::Now().

Referenced by main().

34 {
35  // Infinite?
36  if (iTimeout < 0)
37  for (;;)
38  if (!Execute())
39  return false;
40  // Calculate endpoint
41  C4TimeMilliseconds tStopTime = C4TimeMilliseconds::Now() + iTimeout;
42  for (;;)
43  {
44  // Call execute with given timeout
45  if (!Execute(std::max(iTimeout, 0)))
46  return false;
47  // Calculate timeout
49  if (tTime >= tStopTime)
50  break;
51  iTimeout = tStopTime - tTime;
52  }
53  // All ok.
54  return true;
55 }
virtual bool Execute(int iTimeout=-1, pollfd *readyfds=nullptr)=0
static C4TimeMilliseconds Now()
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetConnStatistic()

virtual bool C4NetIO::GetConnStatistic ( const addr_t addr,
int *  pIRate,
int *  pORate,
int *  pLoss 
)
pure virtual

Implemented in C4NetIOUDP, C4NetIOSimpleUDP, and C4NetIOTCP.

Referenced by C4Network2IOConnection::DoStatistics(), and IsNotify().

Here is the caller graph for this function:

◆ GetError()

virtual const char* C4NetIO::GetError ( ) const
inlinevirtual

Definition at line 285 of file C4NetIO.h.

References StdStrBuf::getData().

Referenced by C4Network2IO::ConnectWithSocket(), C4NetIOUDP::DoLoopbackTest(), C4StartupNetDlg::DoRefresh(), C4DownloadDlg::GetError(), C4Network2IO::Init(), C4NetIOUDP::InitBroadcast(), C4Network2::InitLeague(), C4Game::InitNetworkFromAddress(), C4Network2::LeagueEnd(), C4Network2::LeaguePlrAuthCheck(), C4Network2::LeagueStart(), C4Network2::LeagueUpdate(), C4Network2::LeagueUpdateProcessReply(), main(), MyCBClass::OnConn(), C4ChatControl::OnConnectBtn(), MyCBClass::OnPacket(), C4StartupNetListEntry::OnReference(), C4ChatControl::ProcessInput(), C4Network2IOConnection::Send(), and C4StartupNetListEntry::SetRefQuery().

285 { return Error.getData(); }
StdCopyStrBuf Error
Definition: C4NetIO.h:282
const char * getData() const
Definition: StdBuf.h:442
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetFDs()

virtual void StdSchedulerProc::GetFDs ( std::vector< struct pollfd > &  )
inlinevirtualinherited

Reimplemented in C4NetIOSimpleUDP, C4NetIOTCP, CStdNotifyProc, C4FileMonitor, and C4StdInProc.

Definition at line 72 of file StdScheduler.h.

References StdSchedulerProc::GetNextTick(), and StdSchedulerProc::IsSignaled().

Referenced by CStdNotifyProc::IsNotify(), and SCHNotify::registerAt:.

72 { }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetLocalAddresses()

std::vector< C4NetIO::HostAddress > C4NetIO::GetLocalAddresses ( )
static

Definition at line 631 of file C4NetIO.cpp.

References C4NetIO::HostAddress::IsLoopback(), and C4NetIO::HostAddress::SetHost().

Referenced by C4Network2Client::AddLocalAddrs(), and C4NetIOUDP::InitBroadcast().

632 {
633  std::vector<HostAddress> result;
634 
635 #ifdef HAVE_WINSOCK
636  HostAddress addr;
637  const size_t BUFFER_SIZE = 16000;
638  PIP_ADAPTER_ADDRESSES addresses = nullptr;
639  for (int i = 0; i < 3; ++i)
640  {
641  addresses = (PIP_ADAPTER_ADDRESSES) realloc(addresses, BUFFER_SIZE * (i+1));
642  if (!addresses)
643  // allocation failed
644  return result;
645  ULONG bufsz = BUFFER_SIZE * (i+1);
646  DWORD rv = GetAdaptersAddresses(AF_UNSPEC,
647  GAA_FLAG_SKIP_ANYCAST|GAA_FLAG_SKIP_MULTICAST|GAA_FLAG_SKIP_DNS_SERVER|GAA_FLAG_SKIP_FRIENDLY_NAME,
648  nullptr, addresses, &bufsz);
649  if (rv == ERROR_BUFFER_OVERFLOW)
650  // too little space, try again
651  continue;
652  if (rv != NO_ERROR)
653  {
654  // Something else happened
655  free(addresses);
656  return result;
657  }
658  // All okay, add addresses
659  for (PIP_ADAPTER_ADDRESSES address = addresses; address; address = address->Next)
660  {
661  for (PIP_ADAPTER_UNICAST_ADDRESS unicast = address->FirstUnicastAddress; unicast; unicast = unicast->Next)
662  {
663  addr.SetHost(unicast->Address.lpSockaddr);
664  if (addr.IsLoopback())
665  continue;
666  result.push_back(addr);
667  }
668  }
669  }
670  free(addresses);
671 #else
672  bool have_ipv6 = false;
673 
674 #ifdef __linux__
675  // Get IPv6 addresses on Linux from procfs which allows filtering deprecated privacy addresses.
676  FILE *f = fopen("/proc/net/if_inet6", "r");
677  if (f)
678  {
679  sockaddr_in6 sa6 = sockaddr_in6();
680  sa6.sin6_family = AF_INET6;
681  auto a6 = sa6.sin6_addr.s6_addr;
682  uint8_t if_idx, plen, scope, flags;
683  char devname[20];
684  while (fscanf(f, "%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx %02hhx %02hhx %02hhx %02hhx %20s\n",
685  &a6[0], &a6[1], &a6[2], &a6[3], &a6[4], &a6[5], &a6[6], &a6[7],
686  &a6[8], &a6[9], &a6[10], &a6[11], &a6[12], &a6[13], &a6[14], &a6[15],
687  &if_idx, &plen, &scope, &flags, devname) != EOF)
688  {
689  // Skip loopback and deprecated addresses.
690  if (scope == IPV6_ADDR_LOOPBACK || flags & IFA_F_DEPRECATED)
691  continue;
692  sa6.sin6_scope_id = scope == IPV6_ADDR_LINKLOCAL ? if_idx : 0;
693  result.emplace_back((sockaddr*) &sa6);
694  }
695  have_ipv6 = result.size() > 0;
696  fclose(f);
697  }
698 #endif
699 
700  struct ifaddrs* addrs;
701  if (getifaddrs(&addrs) < 0)
702  return result;
703  for (struct ifaddrs* ifaddr = addrs; ifaddr != nullptr; ifaddr = ifaddr->ifa_next)
704  {
705  struct sockaddr* ad = ifaddr->ifa_addr;
706  if (ad == nullptr) continue;
707 
708  if ((ad->sa_family == AF_INET || (!have_ipv6 && ad->sa_family == AF_INET6)) && (~ifaddr->ifa_flags & IFF_LOOPBACK)) // Choose only non-loopback IPv4/6 devices
709  {
710  result.emplace_back(ad);
711  }
712  }
713  freeifaddrs(addrs);
714 #endif
715 
716  return result;
717 }
uint32_t DWORD
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetNextTick()

C4TimeMilliseconds StdSchedulerProc::GetNextTick ( C4TimeMilliseconds  tNow)
virtualinherited

Reimplemented in C4NetIOUDP, C4Network2HTTPClient, C4Network2IO, and CStdTimerProc.

Definition at line 115 of file StdScheduler.cpp.

References C4TimeMilliseconds::PositiveInfinity.

Referenced by StdSchedulerProc::GetFDs(), C4Network2HTTPClient::GetNextTick(), StdScheduler::IsInManualLoop(), and StdScheduler::ScheduleProcs().

Here is the caller graph for this function:

◆ GetStatistic()

virtual bool C4NetIO::GetStatistic ( int *  pBroadcastRate)
pure virtual

Implemented in C4NetIOUDP, C4NetIOSimpleUDP, and C4NetIOTCP.

Referenced by C4Network2IO::GenerateStatistics(), and IsNotify().

Here is the caller graph for this function:

◆ Init()

virtual bool C4NetIO::Init ( uint16_t  iPort = addr_t::IPPORT_NONE)
pure virtual

Implemented in C4NetIOUDP, C4NetIOSimpleUDP, C4NetIOTCP, C4AulDebug, C4Network2IODiscoverClient, and C4Network2IODiscover.

Referenced by C4Network2IO::Init().

Here is the caller graph for this function:

◆ InitIPv6Socket()

bool C4NetIO::InitIPv6Socket ( SOCKET  socket)
protected

Definition at line 730 of file C4NetIO.cpp.

References SetError(), and SOCKET_ERROR.

Referenced by C4NetIOTCP::CreateSocket(), C4NetIOSimpleUDP::Init(), IsNotify(), and C4NetIOTCP::Listen().

731 {
732  int opt = 0;
733  if (setsockopt(socket, IPPROTO_IPV6, IPV6_V6ONLY, reinterpret_cast<char*>(&opt), sizeof(opt)) == SOCKET_ERROR)
734  {
735  SetError("could not enable dual-stack socket", true);
736  return false;
737  }
738 
739 #ifdef IPV6_ADDR_PREFERENCES
740  // Prefer stable addresses. This should prevent issues with address
741  // deprecation while a match is running. No error handling - if the call
742  // fails, we just take any address.
743  opt = IPV6_PREFER_SRC_PUBLIC;
744  setsockopt(socket, IPPROTO_IPV6, IPV6_ADDR_PREFERENCES, reinterpret_cast<char*>(&opt), sizeof(opt));
745 #endif
746 
747  return true;
748 }
void SetError(const char *strnError, bool fSockErr=false)
Definition: C4NetIO.cpp:750
#define SOCKET_ERROR
Definition: C4NetIO.cpp:47
Here is the call graph for this function:
Here is the caller graph for this function:

◆ IsLowPriority()

virtual bool StdSchedulerProc::IsLowPriority ( )
inlinevirtualinherited

Definition at line 82 of file StdScheduler.h.

82 { return false; }

◆ IsNotify()

bool C4NetIO::IsNotify ( )
inlineoverridevirtual

Reimplemented from StdSchedulerProc.

Definition at line 259 of file C4NetIO.h.

References Broadcast(), ClearStatistic(), Close(), Connect(), GetConnStatistic(), GetStatistic(), InitIPv6Socket(), Send(), SetBroadcast(), and SOCKET.

259 { return true; }
Here is the call graph for this function:

◆ IsSignaled()

bool StdSchedulerProc::IsSignaled ( )
inherited

Referenced by StdSchedulerProc::GetFDs().

Here is the caller graph for this function:

◆ ResetError()

void C4NetIO::ResetError ( )
inline

Definition at line 286 of file C4NetIO.h.

References StdStrBuf::Clear(), and SetCallback().

Referenced by C4NetIO(), C4NetIOTCP::Close(), C4NetIOSimpleUDP::Close(), C4NetIOUDP::CloseBroadcast(), C4Network2IO::ConnectWithSocket(), C4NetIOSimpleUDP::Execute(), C4NetIOUDP::Execute(), C4NetIOSimpleUDP::Init(), C4NetIOSimpleUDP::InitBroadcast(), C4NetIOUDP::InitBroadcast(), C4Network2IOConnection::Send(), and C4NetIOSimpleUDP::Send().

286 { Error.Clear(); }
void Clear()
Definition: StdBuf.h:466
StdCopyStrBuf Error
Definition: C4NetIO.h:282
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Send()

virtual bool C4NetIO::Send ( const class C4NetIOPacket rPacket)
pure virtual

Referenced by C4NetIOUDP::Peer::GetAltAddr(), C4NetIOTCP::Peer::GetORate(), IsNotify(), main(), MyCBClass::OnConn(), MyCBClass::OnPacket(), C4Network2IOConnection::Send(), and C4Network2IO::SendPuncherPacket().

Here is the caller graph for this function:

◆ SetBroadcast()

virtual bool C4NetIO::SetBroadcast ( const addr_t addr,
bool  fSet = true 
)
pure virtual

Implemented in C4NetIOUDP, C4NetIOSimpleUDP, and C4NetIOTCP.

Referenced by IsNotify(), and C4Network2IOConnection::SetBroadcastTarget().

Here is the caller graph for this function:

◆ SetCallback()

virtual void C4NetIO::SetCallback ( CBClass pnCallback)
pure virtual

Referenced by C4NetIOMan::AddIO(), C4Network2IO::Init(), and ResetError().

Here is the caller graph for this function:

◆ SetError()

void C4NetIO::SetError ( const char *  strnError,
bool  fSockErr = false 
)
protected

Definition at line 750 of file C4NetIO.cpp.

References C4NetIOPacket::C4NetIOPacket(), StdStrBuf::Copy(), Error, StdStrBuf::Format(), GetSocketErrorMsg(), and HaveSocketError().

Referenced by C4NetIOTCP::Accept(), C4NetIOTCP::Bind(), C4NetIOSimpleUDP::Broadcast(), C4NetIOSimpleUDP::CloseBroadcast(), C4NetIOTCP::Connect(), C4NetIOTCP::CreateSocket(), C4NetIOUDP::DoLoopbackTest(), C4NetIOTCP::Execute(), C4NetIOSimpleUDP::Execute(), C4NetIOUDP::Execute(), C4NetIOSimpleUDP::GetFDs(), C4NetIOTCP::Init(), C4NetIOSimpleUDP::Init(), C4NetIOSimpleUDP::InitBroadcast(), C4NetIOUDP::InitBroadcast(), InitIPv6Socket(), C4NetIOTCP::Listen(), C4NetIOTCP::Peer::Send(), C4NetIOSimpleUDP::Send(), C4NetIOTCP::UnBlock(), and C4NetIOSimpleUDP::UnBlock().

751 {
752  fSockErr &= HaveSocketError();
753  if (fSockErr)
754  Error.Format("%s (%s)", strnError, GetSocketErrorMsg());
755  else
756  Error.Copy(strnError);
757 }
const char * GetSocketErrorMsg(int iError)
Definition: C4NetIO.cpp:190
StdCopyStrBuf Error
Definition: C4NetIO.h:282
void Format(const char *szFmt,...) GNUC_FORMAT_ATTRIBUTE_O
Definition: StdBuf.cpp:174
bool HaveSocketError()
Definition: C4NetIO.cpp:199
void Copy()
Definition: StdBuf.h:467
Here is the call graph for this function:
Here is the caller graph for this function:

◆ TimerInterval()

virtual uint32_t StdSchedulerProc::TimerInterval ( )
inlinevirtualinherited

Reimplemented in CStdTimerProc.

Definition at line 84 of file StdScheduler.h.

Referenced by SCHAdditions::assignAdditionForProc:.

84 { return 0; }
Here is the caller graph for this function:

Member Data Documentation

◆ Error

StdCopyStrBuf C4NetIO::Error
protected

Definition at line 282 of file C4NetIO.h.

Referenced by SetError().

◆ TO_INF


The documentation for this class was generated from the following files: