OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4Network2UpdateClient Class Referenceabstract

#include <C4Network2Reference.h>

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

Public Types

typedef EndpointAddress addr_t
 

Public Member Functions

 C4Network2UpdateClient ()
 
bool QueryUpdateURL ()
 
bool GetUpdateURL (StdStrBuf *pUpdateURL)
 
bool GetVersion (StdStrBuf *pVersion)
 
bool Query (const StdBuf &Data, bool fBinary)
 
bool Query (const char *szData, bool fBinary)
 
bool isBusy () const
 
bool isSuccess () const
 
bool isConnected () const
 
size_t getTotalSize () const
 
size_t getDownloadedSize () const
 
const StdBufgetResultBin () const
 
const char * getResultString () const
 
const char * getServerName () const
 
const char * getRequest () const
 
const C4NetIO::addr_tgetServerAddress () const
 
void Cancel (const char *szReason)
 
void Clear ()
 
bool SetServer (const char *szServerAddress)
 
void SetNotify (class C4InteractiveThread *pnNotify)
 
bool Execute (int iMaxTime, pollfd *readyfds) override
 
virtual bool Execute (int iMaxTime=TO_INF)
 
C4TimeMilliseconds GetNextTick (C4TimeMilliseconds tNow) override
 
bool Init (uint16_t iPort=addr_t::IPPORT_NONE) override
 
virtual bool InitBroadcast (addr_t *pBroadcastAddr)
 
bool Close () override
 
bool Close (const addr_t &addr) override
 
virtual bool CloseBroadcast ()
 
bool Connect (const addr_t &addr) override
 
bool Send (const C4NetIOPacket &rPacket) override
 
virtual bool Send (const class C4NetIOPacket &rPacket)=0
 
bool Broadcast (const C4NetIOPacket &rPacket) override
 
virtual bool Broadcast (const class C4NetIOPacket &rPacket)=0
 
bool SetBroadcast (const addr_t &addr, bool fSet=true) override
 
virtual void UnBlock ()
 
void GetFDs (std::vector< struct pollfd > &FDs) override
 
bool GetStatistic (int *pBroadcastRate) override
 
bool GetConnStatistic (const addr_t &addr, int *pIRate, int *pORate, int *pLoss) override
 
void ClearStatistic () override
 
void SetCallback (CBClass *pnCallback) override
 
virtual void SetCallback (CBClass *pnCallback)=0
 
bool IsNotify () override
 
virtual const char * GetError () const
 
void ResetError ()
 
bool ExecuteUntil (int iTimeout=-1)
 
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

int32_t GetDefaultPort () override
 
void PackPacket (const C4NetIOPacket &rPacket, StdBuf &rOutBuf) override
 
size_t UnpackPacket (const StdBuf &rInBuf, const C4NetIO::addr_t &addr) override
 
bool OnConn (const C4NetIO::addr_t &AddrPeer, const C4NetIO::addr_t &AddrConnect, const addr_t *pOwnAddr, C4NetIO *pNetIO) override
 
void OnDisconn (const C4NetIO::addr_t &AddrPeer, C4NetIO *pNetIO, const char *szReason) override
 
void OnPacket (const class C4NetIOPacket &rPacket, C4NetIO *pNetIO) override
 
void ResetRequestTimeout ()
 
bool Listen (uint16_t inListenPort)
 
PeerAccept (SOCKET nsock=INVALID_SOCKET, const addr_t &ConnectAddr=addr_t())
 
PeerGetPeer (const addr_t &addr)
 
void OnShareFree (CStdCSecEx *pCSec) override
 
void AddConnectWait (SOCKET sock, const addr_t &addr)
 
ConnectWaitGetConnectWait (const addr_t &addr)
 
void ClearConnectWaits ()
 
bool InitIPv6Socket (SOCKET socket)
 
void SetError (const char *strnError, bool fSockErr=false)
 
void Changed ()
 

Protected Attributes

StdCopyBuf ResultBin
 
StdCopyStrBuf ResultString
 
PeerpPeerList {nullptr}
 
struct C4NetIOTCP::ConnectWait nullptr
 
CStdCSecEx PeerListCSec
 
CStdCSec PeerListAddCSec
 
bool fInit {false}
 
uint16_t iListenPort
 
SOCKET lsock
 
int Pipe [2]
 
StdCopyStrBuf Error
 

Detailed Description

Definition at line 203 of file C4Network2Reference.h.

Member Typedef Documentation

typedef EndpointAddress C4NetIO::addr_t
inherited

Definition at line 212 of file C4NetIO.h.

Constructor & Destructor Documentation

C4Network2UpdateClient::C4Network2UpdateClient ( )
inline

Definition at line 208 of file C4Network2Reference.h.

Member Function Documentation

C4NetIOTCP::Peer * C4NetIOTCP::Accept ( SOCKET  nsock = INVALID_SOCKET,
const addr_t ConnectAddr = addr_t() 
)
protectedinherited

Definition at line 1370 of file C4NetIO.cpp.

References StdSchedulerProc::Changed(), C4NetIO::EndpointAddress::Clear(), CStdLock::Clear(), C4NetIOTCP::Peer::Close(), closesocket, C4NetIO::HostAddress::GetAddrLen(), C4NetIO::HostAddress::GetFamily(), INVALID_SOCKET, ioctlsocket, C4NetIOTCP::lsock, C4NetIOTCP::Peer::Next, C4NetIO::CBClass::OnConn(), C4NetIOTCP::Peer, C4NetIOTCP::PeerListAddCSec, C4NetIOTCP::PeerListCSec, C4NetIOTCP::pPeerList, C4NetIO::SetError(), SOCK_CLOEXEC, SOCKET_ERROR, and C4NetIO::HostAddress::UnknownFamily.

Referenced by C4NetIOTCP::Execute().

1371 {
1372 
1373  addr_t caddr = ConnectAddr;
1374 
1375  // accept incoming connection?
1376  C4NetIO::addr_t addr; socklen_t iAddrSize = addr.GetAddrLen();
1377  if (nsock == INVALID_SOCKET)
1378  {
1379  // accept from listener
1380 #ifdef __linux__
1381  if ((nsock = ::accept4(lsock, &addr, &iAddrSize, SOCK_CLOEXEC)) == INVALID_SOCKET)
1382 #else
1383  if ((nsock = ::accept(lsock, &addr, &iAddrSize)) == INVALID_SOCKET)
1384 #endif
1385  {
1386  // set error
1387  SetError("socket accept failed", true);
1388  return nullptr;
1389  }
1390  // connect address unknown, so zero it
1391  caddr.Clear();
1392  }
1393  else
1394  {
1395  // get peer address
1396  if (::getpeername(nsock, &addr, &iAddrSize) == SOCKET_ERROR)
1397  {
1398 #ifndef HAVE_WINSOCK
1399  // getpeername behaves strangely on exotic platforms. Just ignore it.
1400  if (errno != ENOTCONN)
1401  {
1402 #endif
1403  // set error
1404  SetError("could not get peer address for connected socket", true);
1405  return nullptr;
1406 #ifndef HAVE_WINSOCK
1407  }
1408 #endif
1409  }
1410  }
1411 
1412  // check address
1413  if (addr.GetFamily() == addr_t::UnknownFamily)
1414  {
1415  // set error
1416  SetError("socket accept failed: invalid address returned");
1417  closesocket(nsock);
1418  return nullptr;
1419  }
1420 
1421  // disable nagle (yep, we know what we are doing here - I think)
1422  int iNoDelay = 1;
1423  ::setsockopt(nsock, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast<const char *>(&iNoDelay), sizeof(iNoDelay));
1424 
1425 #ifdef STDSCHEDULER_USE_EVENTS
1426  // set event
1427  if (::WSAEventSelect(nsock, Event, FD_READ | FD_WRITE | FD_CLOSE) == SOCKET_ERROR)
1428  {
1429  // set error
1430  SetError("connection accept failed: could not set event", true);
1431  closesocket(nsock);
1432  return nullptr;
1433  }
1434 #elif defined(HAVE_WINSOCK)
1435  // disable blocking
1436  unsigned long iBlock = 1;
1437  if (::ioctlsocket(nsock, FIONBIO, &iBlock) == SOCKET_ERROR)
1438  {
1439  // set error
1440  SetError("connect failed: could not disable blocking", true);
1441  close(nsock);
1442  return false;
1443  }
1444 #else
1445  // disable blocking
1446  if (::fcntl(nsock, F_SETFL, fcntl(nsock, F_GETFL) | O_NONBLOCK) == SOCKET_ERROR)
1447  {
1448  // set error
1449  SetError("connection accept failed: could not disable blocking", true);
1450  close(nsock);
1451  return nullptr;
1452  }
1453 #endif
1454 
1455 
1456  // create new peer
1457  Peer *pnPeer = new Peer(addr, nsock, this);
1458 
1459  // get required locks to add item to list
1460  CStdShareLock PeerListLock(&PeerListCSec);
1461  CStdLock PeerListAddLock(&PeerListAddCSec);
1462 
1463  // add to list
1464  pnPeer->Next = pPeerList;
1465  pPeerList = pnPeer;
1466 
1467  // clear add-lock
1468  PeerListAddLock.Clear();
1469 
1470  Changed();
1471 
1472  // ask callback if connection should be permitted
1473  if (pCB && !pCB->OnConn(addr, caddr, nullptr, this))
1474  // close socket immediately (will be deleted later)
1475  pnPeer->Close();
1476 
1477  // ok
1478  return pnPeer;
1479 }
#define INVALID_SOCKET
Definition: C4NetIO.h:36
void SetError(const char *strnError, bool fSockErr=false)
Definition: C4NetIO.cpp:750
virtual bool OnConn(const addr_t &AddrPeer, const addr_t &AddrConnect, const addr_t *pOwnAddr, C4NetIO *pNetIO)
Definition: C4NetIO.h:220
#define ioctlsocket
Definition: C4NetIO.cpp:45
Peer * pPeerList
Definition: C4NetIO.h:443
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:455
friend class Peer
Definition: C4NetIO.h:441
size_t GetAddrLen() const
Definition: C4NetIO.cpp:526
AddressFamily GetFamily() const
Definition: C4NetIO.cpp:520
EndpointAddress addr_t
Definition: C4NetIO.h:212
CStdCSec PeerListAddCSec
Definition: C4NetIO.h:456
#define SOCK_CLOEXEC
Definition: C4NetIO.h:50
#define closesocket
Definition: C4NetIO.cpp:46
SOCKET lsock
Definition: C4NetIO.h:463
#define SOCKET_ERROR
Definition: C4NetIO.cpp:47

Here is the call graph for this function:

Here is the caller graph for this function:

void C4NetIOTCP::AddConnectWait ( SOCKET  sock,
const addr_t addr 
)
protectedinherited

Definition at line 1594 of file C4NetIO.cpp.

References C4NetIOTCP::ConnectWait::addr, StdSchedulerProc::Changed(), C4NetIOTCP::ConnectWait::Next, C4NetIOTCP::PeerListAddCSec, C4NetIOTCP::PeerListCSec, C4NetIOTCP::ConnectWait::sock, and C4NetIOTCP::UnBlock().

Referenced by C4NetIOTCP::Connect().

1595 {
1596  CStdShareLock PeerListLock(&PeerListCSec);
1597  CStdLock PeerListAddLock(&PeerListAddCSec);
1598  // create new entry, add to list
1599  ConnectWait *pnWait = new ConnectWait;
1600  pnWait->sock = sock; pnWait->addr = addr;
1601  pnWait->Next = pConnectWaits;
1602  pConnectWaits = pnWait;
1603 #ifndef STDSCHEDULER_USE_EVENTS
1604  // unblock, so new FD can be realized
1605  UnBlock();
1606 #endif
1607  Changed();
1608 }
virtual void UnBlock()
Definition: C4NetIO.cpp:1286
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:455
CStdCSec PeerListAddCSec
Definition: C4NetIO.h:456

Here is the call graph for this function:

Here is the caller graph for this function:

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

Definition at line 1275 of file C4NetIO.cpp.

References C4NetIOPacket::getRef(), C4NetIOTCP::Peer::Next, C4NetIOTCP::PeerListCSec, C4NetIOTCP::pPeerList, and C4NetIOTCP::Send().

1276 {
1277  CStdShareLock PeerListLock(&PeerListCSec);
1278  // just send to all clients
1279  bool fSuccess = true;
1280  for (Peer *pPeer = pPeerList; pPeer; pPeer = pPeer->Next)
1281  if (pPeer->Open() && pPeer->doBroadcast())
1282  fSuccess &= Send(C4NetIOPacket(rPacket.getRef(), pPeer->GetAddr()));
1283  return fSuccess;
1284 }
bool Send(const C4NetIOPacket &rPacket) override
Definition: C4NetIO.cpp:1253
C4NetIOPacket getRef() const
Definition: C4NetIO.h:324
Peer * pPeerList
Definition: C4NetIO.h:443
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:455
friend class Peer
Definition: C4NetIO.h:441

Here is the call graph for this function:

void C4Network2HTTPClient::Cancel ( const char *  szReason)
inherited

Definition at line 532 of file C4Network2Reference.cpp.

References C4NetIOTCP::Close(), and C4NetIO::Error.

Referenced by C4StartupNetListEntry::Execute(), C4Network2HTTPClient::Execute(), C4Network2HTTPClient::Query(), and C4DownloadDlg::UserClose().

533 {
534  // Close connection - and connection attempt
535  Close(ServerAddr); Close(ServerAddrFallback); Close(PeerAddr);
536  // Reset flags
537  fBusy = fSuccess = fConnected = fBinary = false;
538  iDownloadedSize = iTotalSize = iDataOffset = 0;
539  Error = szReason;
540 }
StdCopyStrBuf Error
Definition: C4NetIO.h:282
bool Close() override
Definition: C4NetIO.cpp:857

Here is the call graph for this function:

Here is the caller graph for this function:

void StdSchedulerProc::Changed ( )
protectedinherited

Definition at line 108 of file StdScheduler.cpp.

References s.

Referenced by C4NetIOTCP::Accept(), C4NetIOTCP::AddConnectWait(), C4NetIOUDP::AddPeer(), 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:

void C4Network2HTTPClient::Clear ( )
inherited

Definition at line 542 of file C4Network2Reference.cpp.

References StdBuf::Clear(), StdStrBuf::Clear(), C4NetIO::Error, C4Network2HTTPClient::ResultBin, and C4Network2HTTPClient::ResultString.

Referenced by C4Network2::StreamOut().

543 {
544  fBusy = fSuccess = fConnected = fBinary = false;
545  iDownloadedSize = iTotalSize = iDataOffset = 0;
546  ResultBin.Clear();
548  Error.Clear();
549 }
void Clear()
Definition: StdBuf.h:466
void Clear()
Definition: StdBuf.h:190
StdCopyStrBuf Error
Definition: C4NetIO.h:282

Here is the call graph for this function:

Here is the caller graph for this function:

void C4NetIOTCP::ClearConnectWaits ( )
protectedinherited

Definition at line 1620 of file C4NetIO.cpp.

References closesocket, C4NetIOTCP::ConnectWait::Next, and C4NetIOTCP::PeerListCSec.

Referenced by C4NetIOTCP::Close().

1621 {
1622  CStdShareLock PeerListLock(&PeerListCSec);
1623  for (ConnectWait *pWait = pConnectWaits; pWait; pWait = pWait->Next)
1624  if (pWait->sock)
1625  {
1626  closesocket(pWait->sock);
1627  pWait->sock = 0;
1628  }
1629 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:455
#define closesocket
Definition: C4NetIO.cpp:46

Here is the caller graph for this function:

void C4NetIOTCP::ClearStatistic ( )
overridevirtualinherited

Implements C4NetIO.

Definition at line 1362 of file C4NetIO.cpp.

References C4NetIOTCP::Peer::Next, C4NetIOTCP::PeerListCSec, and C4NetIOTCP::pPeerList.

1363 {
1364  CStdShareLock PeerListLock(&PeerListCSec);
1365  // clear all peer statistics
1366  for (Peer *pPeer = pPeerList; pPeer; pPeer = pPeer->Next)
1367  pPeer->ClearStatistics();
1368 }
Peer * pPeerList
Definition: C4NetIO.h:443
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:455
friend class Peer
Definition: C4NetIO.h:441
bool C4NetIOTCP::Close ( )
overridevirtualinherited

Implements C4NetIO.

Reimplemented in C4Network2IRCClient, and C4AulDebug.

Definition at line 857 of file C4NetIO.cpp.

References C4NetIOTCP::ClearConnectWaits(), closesocket, C4NetIOTCP::fInit, INVALID_SOCKET, C4NetIOTCP::lsock, C4NetIOTCP::Peer::Next, C4NetIO::CBClass::OnDisconn(), C4NetIOTCP::PeerListCSec, C4NetIOTCP::Pipe, C4NetIOTCP::pPeerList, and C4NetIO::ResetError().

Referenced by C4Network2HTTPClient::Cancel(), C4Network2RefServer::Clear(), C4AulDebug::Close(), C4Network2IRCClient::Close(), C4NetIOTCP::Init(), main(), C4Network2HTTPClient::UnpackPacket(), C4NetIOTCP::~C4NetIOTCP(), and C4NetIOTCP::Peer::~Peer().

858 {
859  ResetError();
860 
861  // not init?
862  if (!fInit) return false;
863 
864  // terminate connections
865  CStdShareLock PeerListLock(&PeerListCSec);
866  for (Peer *pPeer = pPeerList; pPeer; pPeer = pPeer->Next)
867  if (pPeer->Open())
868  {
869  pPeer->Close();
870  if (pCB) pCB->OnDisconn(pPeer->GetAddr(), this, "owner class closed");
871  }
872 
874 
875  // close listen socket
876  if (lsock != INVALID_SOCKET)
877  {
880  }
881 
882 #ifdef STDSCHEDULER_USE_EVENTS
883  // close event
884  if (Event != nullptr)
885  {
886  WSACloseEvent(Event);
887  Event = nullptr;
888  }
889 #else
890  // close pipe
891  close(Pipe[0]);
892  close(Pipe[1]);
893 #endif
894 
895 #ifdef HAVE_WINSOCK
896  // release winsock
897  ReleaseWinSock();
898 #endif
899 
900  // ok
901  fInit = false;
902  return true;
903 }
void ResetError()
Definition: C4NetIO.h:286
void ClearConnectWaits()
Definition: C4NetIO.cpp:1620
#define INVALID_SOCKET
Definition: C4NetIO.h:36
Peer * pPeerList
Definition: C4NetIO.h:443
int Pipe[2]
Definition: C4NetIO.h:470
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:455
friend class Peer
Definition: C4NetIO.h:441
#define closesocket
Definition: C4NetIO.cpp:46
bool fInit
Definition: C4NetIO.h:459
SOCKET lsock
Definition: C4NetIO.h:463
virtual void OnDisconn(const addr_t &AddrPeer, C4NetIO *pNetIO, const char *szReason)
Definition: C4NetIO.h:221

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4NetIOTCP::Close ( const addr_t addr)
overridevirtualinherited

Implements C4NetIO.

Reimplemented in C4AulDebug.

Definition at line 1222 of file C4NetIO.cpp.

References C4NetIOTCP::ConnectWait::addr, C4NetIOTCP::Peer::Close(), closesocket, C4NetIOTCP::Peer::GetAddr(), C4NetIOTCP::GetConnectWait(), C4NetIOTCP::GetPeer(), C4NetIO::CBClass::OnDisconn(), C4NetIOTCP::PeerListCSec, and C4NetIOTCP::ConnectWait::sock.

1223 {
1224  CStdShareLock PeerListLock(&PeerListCSec);
1225  // find connect wait
1226  ConnectWait *pWait = GetConnectWait(addr);
1227  if (pWait)
1228  {
1229  // close socket, do callback
1230  closesocket(pWait->sock); pWait->sock = 0;
1231  if (pCB) pCB->OnDisconn(pWait->addr, this, "closed");
1232  }
1233  else
1234  {
1235  // find peer
1236  Peer *pPeer = GetPeer(addr);
1237  if (pPeer)
1238  {
1239  C4NetIO::addr_t addr = pPeer->GetAddr();
1240  // close peer
1241  pPeer->Close();
1242  // do callback
1243  if (pCB) pCB->OnDisconn(addr, this, "closed");
1244  }
1245  // not found
1246  else
1247  return false;
1248  }
1249  // ok
1250  return true;
1251 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:455
Peer * GetPeer(const addr_t &addr)
Definition: C4NetIO.cpp:1536
friend class Peer
Definition: C4NetIO.h:441
#define closesocket
Definition: C4NetIO.cpp:46
virtual void OnDisconn(const addr_t &AddrPeer, C4NetIO *pNetIO, const char *szReason)
Definition: C4NetIO.h:221
ConnectWait * GetConnectWait(const addr_t &addr)
Definition: C4NetIO.cpp:1610

Here is the call graph for this function:

bool C4NetIOTCP::CloseBroadcast ( )
virtualinherited

Definition at line 905 of file C4NetIO.cpp.

906 {
907  return true;
908 }
bool C4NetIOTCP::Connect ( const addr_t addr)
overridevirtualinherited

Implements C4NetIO.

Definition at line 1154 of file C4NetIO.cpp.

References C4NetIOTCP::AddConnectWait(), closesocket, C4NetIO::HostAddress::GetAddrLen(), C4NetIO::HostAddress::GetFamily(), HaveWouldBlockError(), C4NetIO::InitIPv6Socket(), INVALID_SOCKET, ioctlsocket, C4NetIO::HostAddress::IPv6, C4NetIO::SetError(), SOCK_CLOEXEC, SOCKET, and SOCKET_ERROR.

Referenced by C4Network2IRCClient::Connect(), C4Network2HTTPClient::Execute(), main(), and C4Network2HTTPClient::Query().

1155 {
1156  // create new socket
1157  SOCKET nsock = ::socket(addr.GetFamily() == HostAddress::IPv6 ? AF_INET6 : AF_INET, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP);
1158  if (nsock == INVALID_SOCKET)
1159  {
1160  SetError("socket creation failed", true);
1161  return false;
1162  }
1163 
1164  if (addr.GetFamily() == HostAddress::IPv6)
1165  if (!InitIPv6Socket(nsock))
1166  return false;
1167 
1168 #ifdef STDSCHEDULER_USE_EVENTS
1169  // set event
1170  if (::WSAEventSelect(nsock, Event, FD_CONNECT) == SOCKET_ERROR)
1171  {
1172  // set error
1173  SetError("connect failed: could not set event", true);
1174  closesocket(nsock);
1175  return false;
1176  }
1177 
1178  // add to list
1179  AddConnectWait(nsock, addr);
1180 
1181 #elif defined(HAVE_WINSOCK)
1182  // disable blocking
1183  unsigned long iBlock = 1;
1184  if (::ioctlsocket(nsock, FIONBIO, &iBlock) == SOCKET_ERROR)
1185  {
1186  // set error
1187  SetError("connect failed: could not disable blocking", true);
1188  close(nsock);
1189  return false;
1190  }
1191 #else
1192  // disable blocking
1193  if (::fcntl(nsock, F_SETFL, fcntl(nsock, F_GETFL) | O_NONBLOCK) == SOCKET_ERROR)
1194  {
1195  // set error
1196  SetError("connect failed: could not disable blocking", true);
1197  close(nsock);
1198  return false;
1199  }
1200 #endif
1201 
1202  // connect (async)
1203  if (::connect(nsock, &addr, addr.GetAddrLen()) == SOCKET_ERROR)
1204  {
1205  if (!HaveWouldBlockError()) // expected
1206  {
1207  SetError("socket connection failed", true);
1208  closesocket(nsock);
1209  return false;
1210  }
1211  }
1212 
1213 #ifndef STDSCHEDULER_USE_EVENTS
1214  // add to list
1215  AddConnectWait(nsock, addr);
1216 #endif
1217 
1218  // ok
1219  return true;
1220 }
#define INVALID_SOCKET
Definition: C4NetIO.h:36
void SetError(const char *strnError, bool fSockErr=false)
Definition: C4NetIO.cpp:750
#define ioctlsocket
Definition: C4NetIO.cpp:45
bool InitIPv6Socket(SOCKET socket)
Definition: C4NetIO.cpp:730
void AddConnectWait(SOCKET sock, const addr_t &addr)
Definition: C4NetIO.cpp:1594
#define SOCKET
Definition: C4NetIO.h:35
bool HaveWouldBlockError()
Definition: C4NetIO.cpp:203
#define SOCK_CLOEXEC
Definition: C4NetIO.h:50
#define closesocket
Definition: C4NetIO.cpp:46
#define SOCKET_ERROR
Definition: C4NetIO.cpp:47

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4Network2HTTPClient::Execute ( int  iMaxTime,
pollfd *  readyfds 
)
inlineoverridevirtualinherited

Reimplemented from C4NetIOTCP.

Definition at line 192 of file C4Network2Reference.h.

References C4Network2HTTPClient::Execute().

Referenced by C4Network2HTTPClient::Execute(), C4Game::InitNetworkFromAddress(), C4Network2::LeagueEnd(), C4Network2::LeaguePlrAuth(), C4Network2::LeaguePlrAuthCheck(), C4Network2::LeagueStart(), C4Network2::LeagueWaitNotBusy(), and C4DownloadDlg::OnIdle().

192 { return Execute(iMaxTime); }
bool Execute(int iMaxTime, pollfd *readyfds) override

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4Network2HTTPClient::Execute ( int  iMaxTime = TO_INF)
virtualinherited

Definition at line 432 of file C4Network2Reference.cpp.

References Application, C4Network2HTTPClient::Cancel(), C4NetIOTCP::Connect(), C4NetIOTCP::Execute(), StdStrBuf::getData(), C4Application::InteractiveThread, C4TimeMilliseconds::Now(), C4TimeMilliseconds::PositiveInfinity, C4InteractiveThread::ThreadLogS(), and C4NetIO::EndpointAddress::ToString().

433 {
434  // Check timeout
435  if (fBusy)
436  {
437  if (C4TimeMilliseconds::Now() > HappyEyeballsTimeout)
438  {
439  HappyEyeballsTimeout = C4TimeMilliseconds::PositiveInfinity;
440  Application.InteractiveThread.ThreadLogS("HTTP: Starting fallback connection to %s (%s)", Server.getData(), ServerAddrFallback.ToString().getData());
441  Connect(ServerAddrFallback);
442  }
443  if (time(nullptr) > iRequestTimeout)
444  {
445  Cancel("Request timeout");
446  return true;
447  }
448  }
449  // Execute normally
450  return C4NetIOTCP::Execute(iMaxTime);
451 }
bool Execute(int iMaxTime=TO_INF, pollfd *readyfds=nullptr) override
Definition: C4NetIO.cpp:919
const char * getData() const
Definition: StdBuf.h:442
bool ThreadLogS(const char *szMessage,...) GNUC_FORMAT_ATTRIBUTE_O
StdStrBuf ToString(int flags=0) const
Definition: C4NetIO.cpp:604
void Cancel(const char *szReason)
C4Application Application
Definition: C4Globals.cpp:44
C4InteractiveThread InteractiveThread
Definition: C4Application.h:45
static C4TimeMilliseconds Now()
bool Connect(const addr_t &addr) override
Definition: C4NetIO.cpp:1154

Here is the call graph for this function:

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:

C4NetIOTCP::ConnectWait * C4NetIOTCP::GetConnectWait ( const addr_t addr)
protectedinherited

Definition at line 1610 of file C4NetIO.cpp.

References C4NetIOTCP::ConnectWait::Next, and C4NetIOTCP::PeerListCSec.

Referenced by C4NetIOTCP::Close().

1611 {
1612  CStdShareLock PeerListLock(&PeerListCSec);
1613  // search
1614  for (ConnectWait *pWait = pConnectWaits; pWait; pWait = pWait->Next)
1615  if (pWait->addr == addr)
1616  return pWait;
1617  return nullptr;
1618 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:455

Here is the caller graph for this function:

bool C4NetIOTCP::GetConnStatistic ( const addr_t addr,
int *  pIRate,
int *  pORate,
int *  pLoss 
)
overridevirtualinherited

Implements C4NetIO.

Definition at line 1349 of file C4NetIO.cpp.

References C4NetIOTCP::Peer::GetIRate(), C4NetIOTCP::Peer::GetORate(), C4NetIOTCP::GetPeer(), C4NetIOTCP::Peer::Open(), and C4NetIOTCP::PeerListCSec.

1350 {
1351  CStdShareLock PeerListLock(&PeerListCSec);
1352  // find peer
1353  Peer *pPeer = GetPeer(addr);
1354  if (!pPeer || !pPeer->Open()) return false;
1355  // return statistics
1356  if (pIRate) *pIRate = pPeer->GetIRate();
1357  if (pORate) *pORate = pPeer->GetORate();
1358  if (pLoss) *pLoss = 0;
1359  return true;
1360 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:455
Peer * GetPeer(const addr_t &addr)
Definition: C4NetIO.cpp:1536
friend class Peer
Definition: C4NetIO.h:441

Here is the call graph for this function:

int32_t C4Network2UpdateClient::GetDefaultPort ( )
inlineoverrideprotectedvirtual

Reimplemented from C4Network2HTTPClient.

Definition at line 206 of file C4Network2Reference.h.

References C4NetStdPortHTTP.

206 { return C4NetStdPortHTTP; }
const int16_t C4NetStdPortHTTP
Definition: C4Network2.h:34
size_t C4Network2HTTPClient::getDownloadedSize ( ) const
inlineinherited

Definition at line 177 of file C4Network2Reference.h.

Referenced by C4DownloadDlg::OnIdle().

177 { return iDownloadedSize; }

Here is the caller graph for this function:

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

Definition at line 285 of file C4NetIO.h.

References StdStrBuf::getData().

Referenced by C4Network2IO::Connect(), 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(); }
const char * getData() const
Definition: StdBuf.h:442
StdCopyStrBuf Error
Definition: C4NetIO.h:282

Here is the call graph for this function:

Here is the caller graph for this function:

void C4NetIOTCP::GetFDs ( std::vector< struct pollfd > &  FDs)
overridevirtualinherited

Reimplemented from StdSchedulerProc.

Definition at line 1307 of file C4NetIO.cpp.

References INVALID_SOCKET, C4NetIOTCP::lsock, C4NetIOTCP::Peer::Next, C4NetIOTCP::ConnectWait::Next, C4NetIOTCP::PeerListCSec, C4NetIOTCP::Pipe, and C4NetIOTCP::pPeerList.

Referenced by C4NetIOTCP::Execute().

1308 {
1309  pollfd pfd; pfd.revents = 0;
1310  // add pipe
1311  pfd.fd = Pipe[0]; pfd.events = POLLIN;
1312  fds.push_back(pfd);
1313  // add listener
1314  if (lsock != INVALID_SOCKET)
1315  {
1316  pfd.fd = lsock; pfd.events = POLLIN;
1317  fds.push_back(pfd);
1318  }
1319  // add connect waits (wait for them to become writeable)
1320  CStdShareLock PeerListLock(&PeerListCSec);
1321  for (ConnectWait *pWait = pConnectWaits; pWait; pWait = pWait->Next)
1322  {
1323  pfd.fd = pWait->sock; pfd.events = POLLOUT;
1324  fds.push_back(pfd);
1325  }
1326  // add sockets
1327  for (Peer *pPeer = pPeerList; pPeer; pPeer = pPeer->Next)
1328  if (pPeer->GetSocket())
1329  {
1330  // Wait for socket to become readable
1331  pfd.fd = pPeer->GetSocket(); pfd.events = POLLIN;
1332  // Wait for socket to become writeable, if there is data waiting
1333  if (pPeer->hasWaitingData())
1334  {
1335  pfd.events |= POLLOUT;
1336  }
1337  fds.push_back(pfd);
1338  }
1339 }
#define INVALID_SOCKET
Definition: C4NetIO.h:36
Peer * pPeerList
Definition: C4NetIO.h:443
int Pipe[2]
Definition: C4NetIO.h:470
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:455
friend class Peer
Definition: C4NetIO.h:441
SOCKET lsock
Definition: C4NetIO.h:463

Here is the caller graph for this function:

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

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:

C4TimeMilliseconds C4Network2HTTPClient::GetNextTick ( C4TimeMilliseconds  tNow)
overridevirtualinherited

Reimplemented from StdSchedulerProc.

Definition at line 453 of file C4Network2Reference.cpp.

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

454 {
455  C4TimeMilliseconds tNetIOTCPTick = C4NetIOTCP::GetNextTick(tNow);
456  if (!fBusy)
457  return tNetIOTCPTick;
458 
459  C4TimeMilliseconds tHTTPClientTick = tNow + 1000 * std::max<time_t>(iRequestTimeout - time(nullptr), 0);
460 
461  C4TimeMilliseconds HappyEyeballsTick = tNow + std::max(HappyEyeballsTimeout - C4TimeMilliseconds::Now(), 0);
462 
463  return std::min({tNetIOTCPTick, tHTTPClientTick, HappyEyeballsTick});
464 }
virtual C4TimeMilliseconds GetNextTick(C4TimeMilliseconds tNow)
static C4TimeMilliseconds Now()

Here is the call graph for this function:

C4NetIOTCP::Peer * C4NetIOTCP::GetPeer ( const addr_t addr)
protectedinherited

Definition at line 1536 of file C4NetIO.cpp.

References C4NetIOTCP::Peer::Next, C4NetIOTCP::PeerListCSec, and C4NetIOTCP::pPeerList.

Referenced by C4NetIOTCP::Close(), C4NetIOTCP::GetConnStatistic(), C4NetIOTCP::Send(), and C4NetIOTCP::SetBroadcast().

1537 {
1538  CStdShareLock PeerListLock(&PeerListCSec);
1539  for (Peer *pPeer = pPeerList; pPeer; pPeer = pPeer->Next)
1540  if (pPeer->Open())
1541  if (pPeer->GetAddr() == addr)
1542  return pPeer;
1543  return nullptr;
1544 }
Peer * pPeerList
Definition: C4NetIO.h:443
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:455
friend class Peer
Definition: C4NetIO.h:441

Here is the caller graph for this function:

const char* C4Network2HTTPClient::getRequest ( ) const
inlineinherited

Definition at line 181 of file C4Network2Reference.h.

References StdStrBuf::getData().

Referenced by C4DownloadDlg::OnIdle().

181 { return RequestPath.getData(); }
const char * getData() const
Definition: StdBuf.h:442

Here is the call graph for this function:

Here is the caller graph for this function:

const StdBuf& C4Network2HTTPClient::getResultBin ( ) const
inlineinherited

Definition at line 178 of file C4Network2Reference.h.

References C4Network2HTTPClient::ResultBin.

Referenced by C4DownloadDlg::ShowModal().

178 { assert(fBinary); return ResultBin; }

Here is the caller graph for this function:

const char* C4Network2HTTPClient::getResultString ( ) const
inlineinherited

Definition at line 179 of file C4Network2Reference.h.

References StdStrBuf::getData().

179 { assert(!fBinary); return ResultString.getData(); }
const char * getData() const
Definition: StdBuf.h:442

Here is the call graph for this function:

const C4NetIO::addr_t& C4Network2HTTPClient::getServerAddress ( ) const
inlineinherited

Definition at line 182 of file C4Network2Reference.h.

Referenced by C4Network2RefClient::GetReferences().

182 { return ServerAddr; }

Here is the caller graph for this function:

const char* C4Network2HTTPClient::getServerName ( ) const
inlineinherited

Definition at line 180 of file C4Network2Reference.h.

References StdStrBuf::getData().

Referenced by C4StartupNetListEntry::GetJoinAddress(), C4Network2::LeagueEnd(), C4Network2::LeaguePlrAuth(), C4Network2::LeagueStart(), and C4StartupNetListEntry::SetRefQuery().

180 { return Server.getData(); }
const char * getData() const
Definition: StdBuf.h:442

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4NetIOTCP::GetStatistic ( int *  pBroadcastRate)
overridevirtualinherited

Implements C4NetIO.

Definition at line 1342 of file C4NetIO.cpp.

1343 {
1344  // no broadcast
1345  if (pBroadcastRate) *pBroadcastRate = 0;
1346  return true;
1347 }
size_t C4Network2HTTPClient::getTotalSize ( ) const
inlineinherited

Definition at line 176 of file C4Network2Reference.h.

Referenced by C4DownloadDlg::OnIdle().

176 { return iTotalSize; }

Here is the caller graph for this function:

bool C4Network2UpdateClient::GetUpdateURL ( StdStrBuf pUpdateURL)

Definition at line 602 of file C4Network2Reference.cpp.

References StdStrBuf::getData(), C4Network2HTTPClient::isBusy(), C4Network2HTTPClient::isSuccess(), mkNamingAdapt(), mkParAdapt(), StdCompiler::Exception::Msg, StdCompiler::RCT_All, C4Network2HTTPClient::ResultString, and C4NetIO::SetError().

603 {
604  // Sanity check
605  if (isBusy() || !isSuccess()) return false;
606  // Parse response
607  try
608  {
609  CompileFromBuf<StdCompilerINIRead>(mkNamingAdapt(
610  mkNamingAdapt(mkParAdapt(*pUpdateURL, StdCompiler::RCT_All), "UpdateURL", ""),
611  C4ENGINENAME), ResultString);
612  }
613  catch (StdCompiler::Exception *pExc)
614  {
615  SetError(pExc->Msg.getData());
616  return false;
617  }
618  // done; version OK!
619  return true;
620 }
const char * getData() const
Definition: StdBuf.h:442
void SetError(const char *strnError, bool fSockErr=false)
Definition: C4NetIO.cpp:750
StdNamingAdapt< T > mkNamingAdapt(T &&rValue, const char *szName)
Definition: StdAdaptors.h:92
StdParameterAdapt< T, P > mkParAdapt(T &&rObj, P &&rPar)
Definition: StdAdaptors.h:476

Here is the call graph for this function:

bool C4Network2UpdateClient::GetVersion ( StdStrBuf pVersion)

Definition at line 622 of file C4Network2Reference.cpp.

References StdStrBuf::getData(), C4Network2HTTPClient::isBusy(), C4Network2HTTPClient::isSuccess(), mkNamingAdapt(), mkParAdapt(), StdCompiler::Exception::Msg, StdCompiler::RCT_All, C4Network2HTTPClient::ResultString, and C4NetIO::SetError().

623 {
624  // Sanity check
625  if (isBusy() || !isSuccess()) return false;
626  // Parse response
627  try
628  {
629  CompileFromBuf<StdCompilerINIRead>(mkNamingAdapt(
630  mkNamingAdapt(mkParAdapt(*pVersion, StdCompiler::RCT_All), "Version", ""),
631  C4ENGINENAME), ResultString);
632  }
633  catch (StdCompiler::Exception *pExc)
634  {
635  SetError(pExc->Msg.getData());
636  return false;
637  }
638  // done; version OK!
639  return true;
640 }
const char * getData() const
Definition: StdBuf.h:442
void SetError(const char *strnError, bool fSockErr=false)
Definition: C4NetIO.cpp:750
StdNamingAdapt< T > mkNamingAdapt(T &&rValue, const char *szName)
Definition: StdAdaptors.h:92
StdParameterAdapt< T, P > mkParAdapt(T &&rObj, P &&rPar)
Definition: StdAdaptors.h:476

Here is the call graph for this function:

bool C4NetIOTCP::Init ( uint16_t  iPort = addr_t::IPPORT_NONE)
overridevirtualinherited

Implements C4NetIO.

Reimplemented in C4AulDebug.

Definition at line 811 of file C4NetIO.cpp.

References C4NetIOTCP::Close(), C4NetIOTCP::fInit, C4NetIO::EndpointAddress::IPPORT_NONE, C4NetIOTCP::Listen(), C4NetIOTCP::Pipe, and C4NetIO::SetError().

Referenced by C4StartupNetDlg::CheckVersionUpdate(), C4Network2IRCClient::Connect(), C4AulDebug::Init(), C4Network2IO::Init(), C4Network2::InitLeague(), C4Game::InitNetworkFromAddress(), main(), C4StartupNetListEntry::SetRefQuery(), C4DownloadDlg::ShowModal(), and C4Network2::StartStreaming().

812 {
813  // already init? close first
814  if (fInit) Close();
815 
816 #ifdef HAVE_WINSOCK
817  // init winsock
818  if (!AcquireWinSock())
819  {
820  SetError("could not start winsock");
821  return false;
822  }
823 #endif
824 
825 #ifdef STDSCHEDULER_USE_EVENTS
826  // create event
827  if ((Event = WSACreateEvent()) == WSA_INVALID_EVENT)
828  {
829  SetError("could not create socket event", true); // to do: more error information
830  return false;
831  }
832 #else
833  // create pipe
834  if (pipe(Pipe) != 0)
835  {
836  SetError("could not create pipe", true);
837  return false;
838  }
839 #endif
840 
841  // create listen socket (if necessary)
842  if (iPort != addr_t::IPPORT_NONE)
843  if (!Listen(iPort))
844  return false;
845 
846  // ok
847  fInit = true;
848  return true;
849 }
void SetError(const char *strnError, bool fSockErr=false)
Definition: C4NetIO.cpp:750
bool Listen(uint16_t inListenPort)
Definition: C4NetIO.cpp:1481
int Pipe[2]
Definition: C4NetIO.h:470
bool Close() override
Definition: C4NetIO.cpp:857
bool fInit
Definition: C4NetIO.h:459
static const uint16_t IPPORT_NONE
Definition: C4NetIO.h:137

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4NetIOTCP::InitBroadcast ( addr_t pBroadcastAddr)
virtualinherited

Definition at line 851 of file C4NetIO.cpp.

Referenced by main().

852 {
853  // ignore
854  return true;
855 }

Here is the caller graph for this function:

bool C4NetIO::InitIPv6Socket ( SOCKET  socket)
protectedinherited

Definition at line 730 of file C4NetIO.cpp.

References C4NetIO::SetError(), and SOCKET_ERROR.

Referenced by C4NetIOTCP::Connect(), C4NetIOSimpleUDP::Init(), 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:

bool C4Network2HTTPClient::isBusy ( ) const
inlineinherited
bool C4Network2HTTPClient::isConnected ( ) const
inlineinherited

Definition at line 175 of file C4Network2Reference.h.

Referenced by C4DownloadDlg::OnIdle().

175 { return fConnected; }

Here is the caller graph for this function:

virtual bool StdSchedulerProc::IsLowPriority ( )
inlinevirtualinherited

Definition at line 82 of file StdScheduler.h.

82 { return false; }
bool C4NetIO::IsNotify ( )
inlineoverridevirtualinherited

Reimplemented from StdSchedulerProc.

Definition at line 259 of file C4NetIO.h.

259 { return true; }
bool StdSchedulerProc::IsSignaled ( )
inherited
bool C4Network2HTTPClient::isSuccess ( ) const
inlineinherited

Definition at line 174 of file C4Network2Reference.h.

Referenced by C4Network2RefClient::GetReferences(), C4LeagueClient::GetStartReply(), GetUpdateURL(), GetVersion(), C4Game::InitNetworkFromAddress(), C4Network2::LeagueEnd(), C4Network2::LeaguePlrAuthCheck(), C4Network2::LeagueStart(), C4Network2::LeagueUpdateProcessReply(), C4DownloadDlg::OnIdle(), C4StartupNetListEntry::OnReference(), and C4Network2::StreamOut().

174 { return fSuccess; }

Here is the caller graph for this function:

bool C4NetIOTCP::Listen ( uint16_t  inListenPort)
protectedinherited

Definition at line 1481 of file C4NetIO.cpp.

References C4NetIO::HostAddress::Any, StdSchedulerProc::Changed(), closesocket, C4NetIO::HostAddress::GetAddrLen(), C4NetIOTCP::iListenPort, C4NetIO::InitIPv6Socket(), INVALID_SOCKET, C4NetIO::EndpointAddress::IPPORT_NONE, C4NetIOTCP::lsock, C4NetIO::SetError(), C4NetIO::EndpointAddress::SetPort(), SOCK_CLOEXEC, and SOCKET_ERROR.

Referenced by C4NetIOTCP::Execute(), and C4NetIOTCP::Init().

1482 {
1483  // already listening?
1484  if (lsock != INVALID_SOCKET)
1485  // close existing socket
1486  closesocket(lsock);
1488 
1489  // create socket
1490  if ((lsock = ::socket(AF_INET6, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP)) == INVALID_SOCKET)
1491  {
1492  SetError("socket creation failed", true);
1493  return false;
1494  }
1495  if (!InitIPv6Socket(lsock))
1496  return false;
1497  // To be able to reuse the port after close
1498 #if !defined(_DEBUG) && !defined(_WIN32)
1499  int reuseaddr = 1;
1500  setsockopt(lsock, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<const char *>(&reuseaddr), sizeof(reuseaddr));
1501 #endif
1502  // bind listen socket
1503  addr_t addr = addr_t::Any;
1504  addr.SetPort(inListenPort);
1505  if (::bind(lsock, &addr, addr.GetAddrLen()) == SOCKET_ERROR)
1506  {
1507  SetError("socket bind failed", true);
1509  return false;
1510  }
1511 
1512 #ifdef STDSCHEDULER_USE_EVENTS
1513  // set event callback
1514  if (::WSAEventSelect(lsock, Event, FD_ACCEPT | FD_CLOSE) == SOCKET_ERROR)
1515  {
1516  SetError("could not set event for listen socket", true);
1518  return false;
1519  }
1520 #endif
1521 
1522  // start listening
1523  if (::listen(lsock, SOMAXCONN) == SOCKET_ERROR)
1524  {
1525  SetError("socket listen failed", true);
1527  return false;
1528  }
1529 
1530  // ok
1531  iListenPort = inListenPort;
1532  Changed();
1533  return true;
1534 }
#define INVALID_SOCKET
Definition: C4NetIO.h:36
void SetError(const char *strnError, bool fSockErr=false)
Definition: C4NetIO.cpp:750
bool InitIPv6Socket(SOCKET socket)
Definition: C4NetIO.cpp:730
EndpointAddress addr_t
Definition: C4NetIO.h:212
#define SOCK_CLOEXEC
Definition: C4NetIO.h:50
#define closesocket
Definition: C4NetIO.cpp:46
static const uint16_t IPPORT_NONE
Definition: C4NetIO.h:137
SOCKET lsock
Definition: C4NetIO.h:463
uint16_t iListenPort
Definition: C4NetIO.h:462
#define SOCKET_ERROR
Definition: C4NetIO.cpp:47

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4Network2HTTPClient::OnConn ( const C4NetIO::addr_t AddrPeer,
const C4NetIO::addr_t AddrConnect,
const addr_t pOwnAddr,
C4NetIO pNetIO 
)
overrideprotectedvirtualinherited

Reimplemented from C4NetIO::CBClass.

Definition at line 394 of file C4Network2Reference.cpp.

References StdBuf::Clear(), C4NetIO::Error, StdStrBuf::Format(), StdStrBuf::getData(), and C4NetIOTCP::Send().

395 {
396  // Make sure we're actually waiting for this connection
397  if (fConnected || (AddrConnect != ServerAddr && AddrConnect != ServerAddrFallback))
398  return false;
399  // Save pack peer address
400  PeerAddr = AddrPeer;
401  // Send the request
402  if (!Send(C4NetIOPacket(Request, AddrPeer)))
403  {
404  Error.Format("Unable to send HTTP request: %s", Error.getData());
405  }
406  Request.Clear();
407  fConnected = true;
408  return true;
409 }
const char * getData() const
Definition: StdBuf.h:442
bool Send(const C4NetIOPacket &rPacket) override
Definition: C4NetIO.cpp:1253
void Clear()
Definition: StdBuf.h:190
StdCopyStrBuf Error
Definition: C4NetIO.h:282
void Format(const char *szFmt,...) GNUC_FORMAT_ATTRIBUTE_O
Definition: StdBuf.cpp:174

Here is the call graph for this function:

void C4Network2HTTPClient::OnDisconn ( const C4NetIO::addr_t AddrPeer,
C4NetIO pNetIO,
const char *  szReason 
)
overrideprotectedvirtualinherited

Reimplemented from C4NetIO::CBClass.

Definition at line 411 of file C4Network2Reference.cpp.

References C4NetIO::Error, Ev_HTTP_Response, StdStrBuf::Format(), StdStrBuf::isNull(), and C4InteractiveThread::PushEvent().

412 {
413  // Got no complete packet? Failure...
414  if (!fSuccess && Error.isNull())
415  {
416  fBusy = false;
417  Error.Format("Unexpected disconnect: %s", szReason);
418  }
419  fConnected = false;
420  // Notify
421  if (pNotify)
422  pNotify->PushEvent(Ev_HTTP_Response, this);
423 }
StdCopyStrBuf Error
Definition: C4NetIO.h:282
void Format(const char *szFmt,...) GNUC_FORMAT_ATTRIBUTE_O
Definition: StdBuf.cpp:174
bool PushEvent(C4InteractiveEventType eEventType, void *pData=nullptr)
bool isNull() const
Definition: StdBuf.h:441

Here is the call graph for this function:

void C4Network2HTTPClient::OnPacket ( const class C4NetIOPacket rPacket,
C4NetIO pNetIO 
)
overrideprotectedvirtualinherited

Implements C4NetIO::CBClass.

Definition at line 425 of file C4Network2Reference.cpp.

References Ev_HTTP_Response, and C4InteractiveThread::PushEvent().

Referenced by C4Network2HTTPClient::UnpackPacket().

426 {
427  // Everything worthwhile was already done in UnpackPacket. Only do notify callback
428  if (pNotify)
429  pNotify->PushEvent(Ev_HTTP_Response, this);
430 }
bool PushEvent(C4InteractiveEventType eEventType, void *pData=nullptr)

Here is the call graph for this function:

Here is the caller graph for this function:

void C4NetIOTCP::OnShareFree ( CStdCSecEx pCSec)
overrideprotectedvirtualinherited

Implements CStdCSecExCallback.

Definition at line 1546 of file C4NetIO.cpp.

References C4NetIOTCP::Peer::Next, C4NetIOTCP::ConnectWait::Next, C4NetIOTCP::Peer::Open(), C4NetIOTCP::PeerListCSec, C4NetIOTCP::pPeerList, and C4NetIOTCP::ConnectWait::sock.

1547 {
1548  if (pCSec == &PeerListCSec)
1549  {
1550  // clear up
1551  Peer *pPeer = pPeerList, *pLast = nullptr;
1552  while (pPeer)
1553  {
1554  // delete?
1555  if (!pPeer->Open())
1556  {
1557  // unlink
1558  Peer *pDelete = pPeer;
1559  pPeer = pPeer->Next;
1560  (pLast ? pLast->Next : pPeerList) = pPeer;
1561  // delete
1562  delete pDelete;
1563  }
1564  else
1565  {
1566  // next peer
1567  pLast = pPeer;
1568  pPeer = pPeer->Next;
1569  }
1570  }
1571  ConnectWait *pWait = pConnectWaits, *pWLast = nullptr;
1572  while (pWait)
1573  {
1574  // delete?
1575  if (!pWait->sock)
1576  {
1577  // unlink
1578  ConnectWait *pDelete = pWait;
1579  pWait = pWait->Next;
1580  (pWLast ? pWLast->Next : pConnectWaits) = pWait;
1581  // delete
1582  delete pDelete;
1583  }
1584  else
1585  {
1586  // next peer
1587  pWLast = pWait;
1588  pWait = pWait->Next;
1589  }
1590  }
1591  }
1592 }
Peer * pPeerList
Definition: C4NetIO.h:443
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:455
friend class Peer
Definition: C4NetIO.h:441

Here is the call graph for this function:

void C4Network2HTTPClient::PackPacket ( const C4NetIOPacket rPacket,
StdBuf rOutBuf 
)
overrideprotectedvirtualinherited

Reimplemented from C4NetIOTCP.

Definition at line 247 of file C4Network2Reference.cpp.

References StdBuf::Append().

248 {
249  // Just append the packet
250  rOutBuf.Append(rPacket);
251 }
void Append(const void *pnData, size_t inSize)
Definition: StdBuf.h:254

Here is the call graph for this function:

bool C4Network2HTTPClient::Query ( const StdBuf Data,
bool  fBinary 
)
inherited

Definition at line 466 of file C4Network2Reference.cpp.

References StdBuf::Append(), C4Network2HTTPHappyEyeballsTimeout, C4Network2HTTPClient::Cancel(), StdStrBuf::Clear(), Config, C4NetIOTCP::Connect(), StdStrBuf::Format(), C4Config::General, StdStrBuf::getData(), StdStrBuf::getLength(), StdBuf::getSize(), StdStrBuf::GrabPointer(), C4NetIO::EndpointAddress::IsNull(), StdStrBuf::isNull(), C4ConfigGeneral::LanguageEx, C4TimeMilliseconds::Now(), C4TimeMilliseconds::PositiveInfinity, C4NetIO::ResetError(), C4Network2HTTPClient::ResetRequestTimeout(), C4Network2HTTPClient::ResultString, and StdBuf::Take().

Referenced by C4LeagueClient::Auth(), C4LeagueClient::AuthCheck(), C4LeagueClient::End(), C4Network2RefClient::QueryReferences(), QueryUpdateURL(), C4LeagueClient::ReportDisconnect(), C4DownloadDlg::ShowModal(), C4LeagueClient::Start(), C4Network2::StreamOut(), and C4LeagueClient::Update().

467 {
468  if (Server.isNull()) return false;
469  // Cancel previous request
470  if (fBusy)
471  Cancel("Cancelled");
472  // No result known yet
474  // store mode
475  this->fBinary = fBinary;
476  // Create request
477  StdStrBuf Header;
478  if (Data.getSize())
479  Header.Format(
480  "POST %s HTTP/1.0\r\n"
481  "Host: %s\r\n"
482  "Connection: Close\r\n"
483  "Content-Length: %lu\r\n"
484  "Content-Type: text/plain; charset=utf-8\r\n"
485  "Accept-Charset: utf-8\r\n"
486  "Accept-Encoding: gzip\r\n"
487  "Accept-Language: %s\r\n"
488  "User-Agent: " C4ENGINENAME "/" C4VERSION "\r\n"
489  "\r\n",
490  RequestPath.getData(),
491  Server.getData(),
492  static_cast<unsigned long>(Data.getSize()),
494  else
495  Header.Format(
496  "GET %s HTTP/1.0\r\n"
497  "Host: %s\r\n"
498  "Connection: Close\r\n"
499  "Accept-Charset: utf-8\r\n"
500  "Accept-Encoding: gzip\r\n"
501  "Accept-Language: %s\r\n"
502  "User-Agent: " C4ENGINENAME "/" C4VERSION "\r\n"
503  "\r\n",
504  RequestPath.getData(),
505  Server.getData(),
507  // Compose query
508  Request.Take(Header.GrabPointer(), Header.getLength());
509  Request.Append(Data);
510  // Start connecting
511  if (!Connect(ServerAddr))
512  return false;
513  // Also try the fallback address after some time (if there is one)
514  if (!ServerAddrFallback.IsNull())
516  else
517  HappyEyeballsTimeout = C4TimeMilliseconds::PositiveInfinity;
518  // Okay, request will be performed when connection is complete
519  fBusy = true;
520  iDataOffset = 0;
522  ResetError();
523  return true;
524 }
const char * getData() const
Definition: StdBuf.h:442
void ResetError()
Definition: C4NetIO.h:286
C4Config Config
Definition: C4Config.cpp:833
char * GrabPointer()
Definition: StdBuf.h:459
void Take(void *pnData, size_t inSize)
Definition: StdBuf.h:124
void Clear()
Definition: StdBuf.h:466
C4ConfigGeneral General
Definition: C4Config.h:251
void Format(const char *szFmt,...) GNUC_FORMAT_ATTRIBUTE_O
Definition: StdBuf.cpp:174
size_t getSize() const
Definition: StdBuf.h:101
void Cancel(const char *szReason)
char LanguageEx[CFG_MaxString+1]
Definition: C4Config.h:37
void Append(const void *pnData, size_t inSize)
Definition: StdBuf.h:254
const uint32_t C4Network2HTTPHappyEyeballsTimeout
bool isNull() const
Definition: StdBuf.h:441
bool IsNull() const
Definition: C4NetIO.cpp:503
size_t getLength() const
Definition: StdBuf.h:445
static C4TimeMilliseconds Now()
bool Connect(const addr_t &addr) override
Definition: C4NetIO.cpp:1154

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4Network2HTTPClient::Query ( const char *  szData,
bool  fBinary 
)
inlineinherited

Definition at line 171 of file C4Network2Reference.h.

References C4Network2HTTPClient::Query(), and SLen().

Referenced by C4Network2HTTPClient::Query().

171 { return Query(StdBuf(szData, SLen(szData)), fBinary); }
Definition: StdBuf.h:29
size_t SLen(const char *sptr)
Definition: Standard.h:74
bool Query(const StdBuf &Data, bool fBinary)

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4Network2UpdateClient::QueryUpdateURL ( )

Definition at line 596 of file C4Network2Reference.cpp.

References C4Network2HTTPClient::Query().

Referenced by C4StartupNetDlg::CheckVersionUpdate().

597 {
598  // Perform an Query query
599  return Query(nullptr, false);
600 }
bool Query(const StdBuf &Data, bool fBinary)

Here is the call graph for this function:

Here is the caller graph for this function:

void C4NetIO::ResetError ( )
inlineinherited

Definition at line 286 of file C4NetIO.h.

References StdStrBuf::Clear().

Referenced by C4NetIO::C4NetIO(), C4NetIOTCP::Close(), C4NetIOSimpleUDP::Close(), C4NetIOUDP::CloseBroadcast(), C4Network2IO::Connect(), C4NetIOSimpleUDP::Execute(), C4NetIOUDP::Execute(), C4Network2RefClient::GetReferences(), C4NetIOSimpleUDP::Init(), C4NetIOSimpleUDP::InitBroadcast(), C4NetIOUDP::InitBroadcast(), C4Network2HTTPClient::Query(), C4Network2IOConnection::Send(), C4NetIOSimpleUDP::Send(), and C4Network2HTTPClient::SetServer().

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:

void C4Network2HTTPClient::ResetRequestTimeout ( )
protectedinherited

Definition at line 526 of file C4Network2Reference.cpp.

References C4Network2HTTPQueryTimeout.

Referenced by C4Network2HTTPClient::Query(), and C4Network2HTTPClient::UnpackPacket().

527 {
528  // timeout C4Network2HTTPQueryTimeout seconds from this point
529  iRequestTimeout = time(nullptr) + C4Network2HTTPQueryTimeout;
530 }
const int C4Network2HTTPQueryTimeout

Here is the caller graph for this function:

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

Referenced by main(), MyCBClass::OnConn(), MyCBClass::OnPacket(), C4Network2IOConnection::Send(), and C4Network2IO::SendPuncherPacket().

Here is the caller graph for this function:

bool C4NetIOTCP::Send ( const C4NetIOPacket rPacket)
overrideinherited

Definition at line 1253 of file C4NetIO.cpp.

References C4NetIOPacket::getAddr(), C4NetIOTCP::GetPeer(), C4NetIOTCP::PeerListCSec, and C4NetIOTCP::Peer::Send().

Referenced by C4NetIOTCP::Broadcast(), C4Network2HTTPClient::OnConn(), C4Network2IRCClient::Send(), and C4NetIOTCP::Peer::Send().

1254 {
1255  CStdShareLock PeerListLock(&PeerListCSec);
1256  // find peer
1257  Peer *pPeer = GetPeer(rPacket.getAddr());
1258  // not found?
1259  if (!pPeer) return false;
1260  // send
1261  return pPeer->Send(rPacket);
1262 }
const C4NetIO::addr_t & getAddr() const
Definition: C4NetIO.h:316
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:455
Peer * GetPeer(const addr_t &addr)
Definition: C4NetIO.cpp:1536
friend class Peer
Definition: C4NetIO.h:441
bool Send(const C4NetIOPacket &rPacket)
Definition: C4NetIO.cpp:1694

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4NetIOTCP::SetBroadcast ( const addr_t addr,
bool  fSet = true 
)
overridevirtualinherited

Implements C4NetIO.

Definition at line 1264 of file C4NetIO.cpp.

References C4NetIOTCP::GetPeer(), C4NetIOTCP::PeerListCSec, and C4NetIOTCP::Peer::SetBroadcast().

1265 {
1266  CStdShareLock PeerListLock(&PeerListCSec);
1267  // find peer
1268  Peer *pPeer = GetPeer(addr);
1269  if (!pPeer) return false;
1270  // set flag
1271  pPeer->SetBroadcast(fSet);
1272  return true;
1273 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:455
Peer * GetPeer(const addr_t &addr)
Definition: C4NetIO.cpp:1536
friend class Peer
Definition: C4NetIO.h:441

Here is the call graph for this function:

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

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

Here is the caller graph for this function:

void C4NetIOTCP::SetCallback ( CBClass *  pnCallback)
inlineoverrideinherited

Definition at line 487 of file C4NetIO.h.

Referenced by C4Network2HTTPClient::C4Network2HTTPClient(), C4Network2IRCClient::Connect(), and C4AulDebug::Init().

487 { pCB = pnCallback; };

Here is the caller graph for this function:

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

Definition at line 750 of file C4NetIO.cpp.

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

Referenced by C4NetIOTCP::Accept(), C4NetIOSimpleUDP::CloseBroadcast(), C4Network2IRCClient::Connect(), C4NetIOTCP::Connect(), C4NetIOUDP::DoLoopbackTest(), C4NetIOTCP::Execute(), C4NetIOSimpleUDP::Execute(), C4NetIOUDP::Execute(), C4Network2RefClient::GetReferences(), GetUpdateURL(), GetVersion(), C4NetIOTCP::Init(), C4NetIOSimpleUDP::Init(), C4NetIOSimpleUDP::InitBroadcast(), C4NetIOUDP::InitBroadcast(), C4NetIO::InitIPv6Socket(), C4Network2IRCClient::Join(), C4NetIOTCP::Listen(), C4Network2IRCClient::Send(), C4NetIOSimpleUDP::Send(), C4Network2HTTPClient::SetServer(), 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:

void C4Network2HTTPClient::SetNotify ( class C4InteractiveThread pnNotify)
inlineinherited

Definition at line 189 of file C4Network2Reference.h.

Referenced by C4StartupNetDlg::CheckVersionUpdate(), and C4StartupNetListEntry::SetRefQuery().

189 { pNotify = pnNotify; }

Here is the caller graph for this function:

bool C4Network2HTTPClient::SetServer ( const char *  szServerAddress)
inherited

Definition at line 551 of file C4Network2Reference.cpp.

References C4NetIO::EndpointAddress::Clear(), StdStrBuf::CopyUntil(), FormatString(), StdStrBuf::getData(), C4Network2HTTPClient::GetDefaultPort(), C4NetIO::HostAddress::GetFamily(), C4NetIO::HostAddress::IPv4, C4NetIO::HostAddress::IPv6, C4NetIO::EndpointAddress::IsNull(), C4NetIO::ResetError(), C4NetIO::EndpointAddress::SetAddress(), C4NetIO::EndpointAddress::SetDefaultPort(), C4NetIO::SetError(), and StdStrBuf::SetLength().

Referenced by C4StartupNetDlg::CheckVersionUpdate(), C4Network2::InitLeague(), C4Game::InitNetworkFromAddress(), C4StartupNetListEntry::SetRefQuery(), C4DownloadDlg::ShowModal(), and C4Network2::StreamOut().

552 {
553  // Split address
554  const char *pRequestPath;
555  if ((pRequestPath = strchr(szServerAddress, '/')))
556  {
557  Server.CopyUntil(szServerAddress, '/');
558  RequestPath = pRequestPath;
559  }
560  else
561  {
562  Server = szServerAddress;
563  RequestPath = "/";
564  }
565  // Resolve address
566  ServerAddr.SetAddress(Server);
567  if (ServerAddr.IsNull())
568  {
569  SetError(FormatString("Could not resolve server address %s!", Server.getData()).getData());
570  return false;
571  }
572  ServerAddr.SetDefaultPort(GetDefaultPort());
573  if (ServerAddr.GetFamily() == C4NetIO::HostAddress::IPv6)
574  {
575  // Try to find a fallback IPv4 address for Happy Eyeballs.
576  ServerAddrFallback.SetAddress(Server, C4NetIO::HostAddress::IPv4);
577  ServerAddrFallback.SetDefaultPort(GetDefaultPort());
578  }
579  else
580  ServerAddrFallback.Clear();
581  // Remove port
582  const char *firstColon = strchr(Server.getData(), ':');
583  const char *lastColon = strrchr(Server.getData(), ':');
584  if (firstColon)
585  // hostname/IPv4 address or IPv6 address with port (e.g. [::1]:1234)
586  if (firstColon == lastColon || (Server[0] == '[' && *(lastColon - 1) == ']'))
587  Server.SetLength(lastColon - Server.getData());
588 
589  // Done
590  ResetError();
591  return true;
592 }
const char * getData() const
Definition: StdBuf.h:442
void ResetError()
Definition: C4NetIO.h:286
void SetError(const char *strnError, bool fSockErr=false)
Definition: C4NetIO.cpp:750
virtual int32_t GetDefaultPort()
void SetAddress(const sockaddr *addr)
Definition: C4NetIO.cpp:364
AddressFamily GetFamily() const
Definition: C4NetIO.cpp:520
void CopyUntil(const char *szString, char cUntil)
Definition: StdBuf.h:613
bool IsNull() const
Definition: C4NetIO.cpp:503
void SetLength(size_t iLength)
Definition: StdBuf.h:509
void SetDefaultPort(uint16_t port)
Definition: C4NetIO.cpp:541
StdStrBuf FormatString(const char *szFmt,...)
Definition: StdBuf.cpp:270

Here is the call graph for this function:

Here is the caller graph for this function:

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:

void C4NetIOTCP::UnBlock ( )
virtualinherited

Definition at line 1286 of file C4NetIO.cpp.

References C4NetIOTCP::Pipe, and C4NetIO::SetError().

Referenced by C4NetIOTCP::AddConnectWait().

1287 {
1288 #ifdef STDSCHEDULER_USE_EVENTS
1289  // unblock WaitForSingleObject in C4NetIOTCP::Execute manually
1290  // by setting the Event
1291  WSASetEvent(Event);
1292 #else
1293  // write one character to the pipe, this will unblock everything that
1294  // waits for the FD set returned by GetFDs.
1295  char c = 1;
1296  if (write(Pipe[1], &c, 1) == -1)
1297  SetError("write failed");
1298 #endif
1299 }
void SetError(const char *strnError, bool fSockErr=false)
Definition: C4NetIO.cpp:750
int Pipe[2]
Definition: C4NetIO.h:470

Here is the call graph for this function:

Here is the caller graph for this function:

size_t C4Network2HTTPClient::UnpackPacket ( const StdBuf rInBuf,
const C4NetIO::addr_t addr 
)
overrideprotectedvirtualinherited

Reimplemented from C4NetIOTCP.

Definition at line 253 of file C4Network2Reference.cpp.

References C4NetIOTCP::Close(), StdBuf::Copy(), StdStrBuf::Copy(), StdStrBuf::getData(), StdBuf::getPart(), StdBuf::getSize(), C4Network2HTTPClient::OnPacket(), C4Network2HTTPClient::ResetRequestTimeout(), C4Network2HTTPClient::ResultBin, C4Network2HTTPClient::ResultString, and SSearch().

254 {
255  // since new data arrived, increase timeout time
257  // Check for complete header
258  if (!iDataOffset)
259  {
260  // Copy data into string buffer (terminate)
261  StdStrBuf Data; Data.Copy(getBufPtr<char>(rInBuf), rInBuf.getSize());
262  const char *pData = Data.getData();
263  // Header complete?
264  const char *pContent = SSearch(pData, "\r\n\r\n");
265  if (!pContent)
266  return 0;
267  // Read the header
268  if (!ReadHeader(Data))
269  {
270  fBusy = fSuccess = false;
271  Close(addr);
272  return rInBuf.getSize();
273  }
274  }
275  iDownloadedSize = rInBuf.getSize() - iDataOffset;
276  // Check if the packet is complete
277  if (iTotalSize > iDownloadedSize)
278  {
279  return 0;
280  }
281  // Get data, uncompress it if needed
282  StdBuf Data = rInBuf.getPart(iDataOffset, iTotalSize);
283  if (fCompressed)
284  if (!Decompress(&Data))
285  {
286  fBusy = fSuccess = false;
287  Close(addr);
288  return rInBuf.getSize();
289  }
290  // Save the result
291  if (fBinary)
292  ResultBin.Copy(Data);
293  else
294  ResultString.Copy(getBufPtr<char>(Data), Data.getSize());
295  fBusy = false; fSuccess = true;
296  // Callback
297  OnPacket(C4NetIOPacket(Data, addr), this);
298  // Done
299  Close(addr);
300  return rInBuf.getSize();
301 }
const char * getData() const
Definition: StdBuf.h:442
Definition: StdBuf.h:29
const char * SSearch(const char *szString, const char *szIndex)
Definition: Standard.cpp:363
size_t getSize() const
Definition: StdBuf.h:101
StdBuf getPart(size_t iStart, size_t inSize) const
Definition: StdBuf.h:107
void Copy(size_t inSize)
Definition: StdBuf.h:225
bool Close() override
Definition: C4NetIO.cpp:857
void OnPacket(const class C4NetIOPacket &rPacket, C4NetIO *pNetIO) override
void Copy()
Definition: StdBuf.h:467

Here is the call graph for this function:

Member Data Documentation

bool C4NetIOTCP::fInit {false}
protectedinherited

Definition at line 459 of file C4NetIO.h.

Referenced by C4NetIOTCP::Close(), C4NetIOTCP::Execute(), and C4NetIOTCP::Init().

uint16_t C4NetIOTCP::iListenPort
protectedinherited

Definition at line 462 of file C4NetIO.h.

Referenced by C4NetIOTCP::Execute(), and C4NetIOTCP::Listen().

SOCKET C4NetIOTCP::lsock
protectedinherited
struct C4NetIOTCP::ConnectWait C4NetIOTCP::nullptr
protectedinherited
CStdCSec C4NetIOTCP::PeerListAddCSec
protectedinherited

Definition at line 456 of file C4NetIO.h.

Referenced by C4NetIOTCP::Accept(), and C4NetIOTCP::AddConnectWait().

int C4NetIOTCP::Pipe[2]
protectedinherited
StdCopyBuf C4Network2HTTPClient::ResultBin
protectedinherited
const int C4NetIO::TO_INF = -1
staticinherited

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