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

#include <C4AulDebug.h>

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

Public Types

typedef EndpointAddress addr_t
 

Public Member Functions

 C4AulDebug ()
 
 ~C4AulDebug () override
 
bool Listen (uint16_t iPort, bool fWait)
 
bool isConnected () const
 
void SetPassword (const char *szPassword)
 
bool SetAllowed (const char *szHost)
 
void SetEngine (class C4AulExec *pnExec)
 
bool Init (uint16_t iPort) override
 
bool Close () override
 
bool Close (const addr_t &addr) override
 
void ControlScriptEvaluated (const char *script, const char *result)
 
void OnLog (const char *szLine)
 
void DebugStep (C4AulBCC *pCPos, C4Value *stackTop)
 
virtual bool InitBroadcast (addr_t *pBroadcastAddr)
 
virtual bool CloseBroadcast ()
 
bool Execute (int iMaxTime=TO_INF, pollfd *readyfds=nullptr) override
 
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)
 
virtual C4TimeMilliseconds GetNextTick (C4TimeMilliseconds tNow)
 
bool IsSignaled ()
 
virtual bool IsLowPriority ()
 
virtual uint32_t TimerInterval ()
 

Static Public Member Functions

static bool InitDebug (const char *szPassword, const char *szHost)
 
static C4AulDebugGetDebugger ()
 
static std::vector< HostAddressGetLocalAddresses ()
 

Static Public Attributes

static const int TO_INF = -1
 

Protected Member Functions

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

PeerpPeerList {nullptr}
 
struct C4NetIOTCP::ConnectWait nullptr
 
CStdCSecEx PeerListCSec
 
CStdCSec PeerListAddCSec
 
uint16_t iListenPort
 
SOCKET lsock
 
int Pipe [2]
 
StdCopyStrBuf Error
 

Detailed Description

Definition at line 24 of file C4AulDebug.h.

Member Typedef Documentation

typedef EndpointAddress C4NetIO::addr_t
inherited

Definition at line 211 of file C4NetIO.h.

Constructor & Destructor Documentation

C4AulDebug::C4AulDebug ( )

Definition at line 31 of file C4AulDebug.cpp.

Referenced by InitDebug().

32  : fInit(false), fConnected(false)
33 {
34 }

Here is the caller graph for this function:

C4AulDebug::~C4AulDebug ( )
override

Definition at line 36 of file C4AulDebug.cpp.

37 {
38  for (auto & it : StackTrace)
39  delete it;
40  if (pDebug == this) pDebug = nullptr;
41 }

Member Function Documentation

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

Definition at line 1365 of file C4NetIO.cpp.

References StdSchedulerProc::Changed(), C4NetIO::EndpointAddress::Clear(), CStdLock::Clear(), C4NetIOTCP::Peer::Close(), closesocket, 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().

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

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

Definition at line 1589 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().

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

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 1270 of file C4NetIO.cpp.

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

1271 {
1272  CStdShareLock PeerListLock(&PeerListCSec);
1273  // just send to all clients
1274  bool fSuccess = true;
1275  for (Peer *pPeer = pPeerList; pPeer; pPeer = pPeer->Next)
1276  if (pPeer->Open() && pPeer->doBroadcast())
1277  fSuccess &= Send(C4NetIOPacket(rPacket.getRef(), pPeer->GetAddr()));
1278  return fSuccess;
1279 }
bool Send(const C4NetIOPacket &rPacket) override
Definition: C4NetIO.cpp:1248
C4NetIOPacket getRef() const
Definition: C4NetIO.h:323
Peer * pPeerList
Definition: C4NetIO.h:442
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:454
friend class Peer
Definition: C4NetIO.h:440

Here is the call 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 C4NetIOTCP::ClearConnectWaits ( )
protectedinherited

Definition at line 1615 of file C4NetIO.cpp.

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

Referenced by C4NetIOTCP::Close().

1616 {
1617  CStdShareLock PeerListLock(&PeerListCSec);
1618  for (ConnectWait *pWait = pConnectWaits; pWait; pWait = pWait->Next)
1619  if (pWait->sock)
1620  {
1621  closesocket(pWait->sock);
1622  pWait->sock = 0;
1623  }
1624 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:454
#define closesocket
Definition: C4NetIO.cpp:46

Here is the caller graph for this function:

void C4NetIOTCP::ClearStatistic ( )
overridevirtualinherited

Implements C4NetIO.

Definition at line 1357 of file C4NetIO.cpp.

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

1358 {
1359  CStdShareLock PeerListLock(&PeerListCSec);
1360  // clear all peer statistics
1361  for (Peer *pPeer = pPeerList; pPeer; pPeer = pPeer->Next)
1362  pPeer->ClearStatistics();
1363 }
Peer * pPeerList
Definition: C4NetIO.h:442
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:454
friend class Peer
Definition: C4NetIO.h:440
bool C4AulDebug::Close ( )
overridevirtual

Reimplemented from C4NetIOTCP.

Definition at line 181 of file C4AulDebug.cpp.

References C4NetIOTCP::Close().

Referenced by DebugStep(), and Init().

182 {
183  if (!fInit) return true;
184  fInit = fConnected = false;
185  return C4NetIOTCP::Close();
186 }
bool Close() override
Definition: C4NetIO.cpp:852

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4AulDebug::Close ( const addr_t addr)
overridevirtual

Reimplemented from C4NetIOTCP.

Definition at line 188 of file C4AulDebug.cpp.

References C4NetIOTCP::Close().

189 {
190  if (!fInit) return true;
191  bool success = C4NetIOTCP::Close(addr);
192  if (success)
193  fInit = fConnected = false;
194  return success;
195 }
bool Close() override
Definition: C4NetIO.cpp:852

Here is the call graph for this function:

bool C4NetIOTCP::CloseBroadcast ( )
virtualinherited

Definition at line 900 of file C4NetIO.cpp.

901 {
902  return true;
903 }
bool C4NetIOTCP::Connect ( const addr_t addr)
overridevirtualinherited

Implements C4NetIO.

Definition at line 1149 of file C4NetIO.cpp.

References C4NetIOTCP::AddConnectWait(), closesocket, 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().

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

void C4AulDebug::ControlScriptEvaluated ( const char *  script,
const char *  result 
)

Definition at line 438 of file C4AulDebug.cpp.

References FormatString().

439 {
440  SendLine("EVR", FormatString("%s=%s", script, result).getData());
441 }
StdStrBuf FormatString(const char *szFmt,...)
Definition: StdBuf.cpp:270

Here is the call graph for this function:

void C4AulDebug::DebugStep ( C4AulBCC pCPos,
C4Value stackTop 
)

Definition at line 354 of file C4AulDebug.cpp.

References AB_DEBUG, Application, C4AulBCC::bccType, Close(), C4NetIOTCP::Execute(), FormatString(), Game, C4AulExec::GetContext(), C4AulExec::GetContextDepth(), StdStrBuf::getData(), C4Game::HaltCount, C4AulBCC::Par, and StdScheduler::ScheduleProcs().

355 {
356  // Already stopped? Ignore.
357  // This means we are doing some calculation with suspended script engine.
358  // We do /not/ want to have recursive suspensions...
359  if (eState == DS_Stop)
360  return;
361 
362  // Have break point?
363  if (pCPos->Par.i)
364  eState = DS_Step;
365 
366  int iCallDepth = pExec->GetContextDepth();
367  // Stop?
368  switch (eState)
369  {
370  // Continue normally
371  case DS_Go: return;
372 
373  // Always stop
374  case DS_Stop: break;
375  case DS_Step: break;
376 
377  // Only stop for same level or above
378  case DS_StepOver:
379  if (iCallDepth > iStepCallDepth)
380  return;
381  break;
382 
383  // Only stop above
384  case DS_StepOut:
385  if (iCallDepth >= iStepCallDepth)
386  return;
387  break;
388  }
389 
390  // Get current script context
391  C4AulScriptContext *pCtx = pExec->GetContext(iCallDepth-1);
392 
393  if (!fConnected)
394  {
395  // not connected anymore? nevermind
396  eState = DS_Go;
397  return;
398  }
399 
400  // Maybe got a command in the meantime?
401  Execute(0);
402 
403  // Let's stop here
404  eState = DS_Stop;
405  iStepCallDepth = iCallDepth;
406  Game.HaltCount++;
407 
408  // No valid stop position? Just continue
409  if (!pCtx)
410  {
411  Game.HaltCount--;
412  eState = DS_Step;
413  return;
414  }
415 
416  // Signal
417  if (pCPos && pCPos->bccType == AB_DEBUG && pCPos->Par.i)
418  SendLine("STP", FormatString("Stopped on breakpoint %d", pCPos->Par.i).getData());
419  else
420  SendLine("STP", "Stepped");
421 
422  // Position
423  ObtainStackTrace(pCtx, pCPos);
424  SendLine("POS", StackTrace.front()->getData());
425 
426  // Suspend until we get some command
427  while (fConnected && eState == DS_Stop)
428  if (!Application.ScheduleProcs())
429  {
430  Close();
431  return;
432  }
433 
434  // Do whatever we've been told.
435  Game.HaltCount--;
436 }
bool Execute(int iMaxTime=TO_INF, pollfd *readyfds=nullptr) override
Definition: C4NetIO.cpp:914
const char * getData() const
Definition: StdBuf.h:442
C4Game Game
Definition: C4Globals.cpp:52
union C4AulBCC::@81 Par
C4AulScriptContext * GetContext(int iLevel)
Definition: C4AulExec.h:88
bool Close() override
Definition: C4AulDebug.cpp:181
C4AulBCCType bccType
int32_t HaltCount
Definition: C4Game.h:112
bool ScheduleProcs(int iTimeout=1000/36)
int GetContextDepth() const
Definition: C4AulExec.h:87
C4Application Application
Definition: C4Globals.cpp:44
StdStrBuf FormatString(const char *szFmt,...)
Definition: StdBuf.cpp:270

Here is the call graph for this function:

bool C4NetIOTCP::Execute ( int  iMaxTime = TO_INF,
pollfd *  readyfds = nullptr 
)
overridevirtualinherited

Implements C4NetIO.

Reimplemented in C4Network2HTTPClient.

Definition at line 914 of file C4NetIO.cpp.

References C4NetIOTCP::Accept(), C4NetIOTCP::fInit, C4NetIOTCP::GetFDs(), GetSocketErrorMsg(), HaveWouldBlockError(), C4NetIOTCP::iListenPort, INFINITE, INVALID_SOCKET, ioctlsocket, C4NetIOTCP::Listen(), C4NetIOTCP::lsock, C4NetIOTCP::Peer::Next, C4NetIO::CBClass::OnDisconn(), C4NetIOTCP::PeerListCSec, C4NetIOTCP::Pipe, C4NetIOTCP::pPeerList, ResetSocketError(), C4NetIO::SetError(), SOCKET, SOCKET_ERROR, and C4NetIO::TO_INF.

Referenced by DebugStep(), and C4Network2HTTPClient::Execute().

915 {
916  // security
917  if (!fInit) return false;
918 
919 #ifdef STDSCHEDULER_USE_EVENTS
920  // wait for something to happen
921  if (WaitForSingleObject(Event, iMaxTime == C4NetIO::TO_INF ? INFINITE : iMaxTime) == WAIT_TIMEOUT)
922  // timeout -> nothing happened
923  return true;
924  WSAResetEvent(Event);
925 
926  WSANETWORKEVENTS wsaEvents;
927 #else
928 
929 #ifdef __APPLE__
930  iMaxTime = fix_poll_timeout(iMaxTime);
931 #endif
932 
933  std::vector<pollfd> fdvec;
934  std::map<SOCKET, const pollfd*> fdmap;
935  if (!fds)
936  {
937  // build socket sets
938  GetFDs(fdvec);
939  fds = &fdvec[0];
940  // wait for something to happen
941  int ret = poll(fds, fdvec.size(), iMaxTime);
942  // error
943  if (ret < 0)
944  {
945  SetError("poll failed");
946  return false;
947  }
948  // nothing happened
949  if (ret == 0)
950  return true;
951  }
952  else
953  {
954  // We need to know the size of fdvec, so construct the vector
955  GetFDs(fdvec);
956  // Now overwrite with the poll result
957  std::copy(fds, fds + fdvec.size(), fdvec.begin());
958  }
959 
960  // flush pipe
961  assert(fdvec[0].fd == Pipe[0]);
962  if (fdvec[0].events & fdvec[0].revents)
963  {
964  char c;
965  if (::read(Pipe[0], &c, 1) == -1)
966  SetError("read failed");
967  }
968 
969  for (std::vector<pollfd>::const_iterator i = fdvec.begin(); i != fdvec.end(); ++i)
970  fdmap[i->fd] = &*i;
971  std::map<SOCKET, const pollfd*>::const_iterator cur_fd;
972 #endif
973 
974  // check sockets for events
975 
976  // first: the listen socket
977  if (lsock != INVALID_SOCKET)
978  {
979 
980 #ifdef STDSCHEDULER_USE_EVENTS
981  // get event list
982  if (::WSAEnumNetworkEvents(lsock, nullptr, &wsaEvents) == SOCKET_ERROR)
983  return false;
984 
985  // a connection waiting for accept?
986  if (wsaEvents.lNetworkEvents & FD_ACCEPT)
987 #else
988  cur_fd = fdmap.find(lsock);
989  // a connection waiting for accept?
990  if (cur_fd != fdmap.end() && (cur_fd->second->events & cur_fd->second->revents))
991 #endif
992  if (!Accept())
993  return false;
994  // (note: what happens if there are more connections waiting?)
995 
996 #ifdef STDSCHEDULER_USE_EVENTS
997  // closed?
998  if (wsaEvents.lNetworkEvents & FD_CLOSE)
999  // try to recreate the listen socket
1001 #endif
1002  }
1003 
1004  // second: waited-for connection
1005  CStdShareLock PeerListLock(&PeerListCSec);
1006  for (ConnectWait *pWait = pConnectWaits, *pNext; pWait; pWait = pNext)
1007  {
1008  pNext = pWait->Next;
1009 
1010  // not closed?
1011  if (pWait->sock)
1012  {
1013 #ifdef STDSCHEDULER_USE_EVENTS
1014  // get event list
1015  if (::WSAEnumNetworkEvents(pWait->sock, nullptr, &wsaEvents) == SOCKET_ERROR)
1016  return false;
1017 
1018  if (wsaEvents.lNetworkEvents & FD_CONNECT)
1019 #else
1020  // got connection?
1021  cur_fd = fdmap.find(pWait->sock);
1022  if (cur_fd != fdmap.end() && (cur_fd->second->events & cur_fd->second->revents))
1023 #endif
1024  {
1025  // remove from list
1026  SOCKET sock = pWait->sock; pWait->sock = 0;
1027 
1028 #ifdef STDSCHEDULER_USE_EVENTS
1029  // error?
1030  if (wsaEvents.iErrorCode[FD_CONNECT_BIT])
1031  {
1032  // disconnect-callback
1033  if (pCB) pCB->OnDisconn(pWait->addr, this, GetSocketErrorMsg(wsaEvents.iErrorCode[FD_CONNECT_BIT]));
1034  }
1035  else
1036 #else
1037  // get error code
1038  int iErrCode; socklen_t iErrCodeLen = sizeof(iErrCode);
1039  if (getsockopt(sock, SOL_SOCKET, SO_ERROR, reinterpret_cast<char *>(&iErrCode), &iErrCodeLen) != 0)
1040  {
1041  close(sock);
1042  if (pCB) pCB->OnDisconn(pWait->addr, this, GetSocketErrorMsg());
1043  }
1044  // error?
1045  else if (iErrCode)
1046  {
1047  close(sock);
1048  if (pCB) pCB->OnDisconn(pWait->addr, this, GetSocketErrorMsg(iErrCode));
1049  }
1050  else
1051 #endif
1052  // accept connection, do callback
1053  if (!Accept(sock, pWait->addr))
1054  return false;
1055  }
1056  }
1057 
1058  }
1059 
1060  // last: all connected sockets
1061  for (Peer *pPeer = pPeerList; pPeer; pPeer = pPeer->Next)
1062  if (pPeer->Open())
1063  {
1064  SOCKET sock = pPeer->GetSocket();
1065 
1066 #ifdef STDSCHEDULER_USE_EVENTS
1067  // get event list
1068  if (::WSAEnumNetworkEvents(sock, nullptr, &wsaEvents) == SOCKET_ERROR)
1069  return false;
1070 
1071  // something to read from socket?
1072  if (wsaEvents.lNetworkEvents & FD_READ)
1073 #else
1074  // something to read from socket?
1075  cur_fd = fdmap.find(sock);
1076  if (cur_fd != fdmap.end() && (POLLIN & cur_fd->second->revents))
1077 #endif
1078  for (;;)
1079  {
1080  // how much?
1081 #ifdef _WIN32
1082  DWORD iBytesToRead;
1083 #else
1084  int iBytesToRead;
1085 #endif
1086  if (::ioctlsocket(pPeer->GetSocket(), FIONREAD, &iBytesToRead) == SOCKET_ERROR)
1087  {
1088  pPeer->Close();
1089  if (pCB) pCB->OnDisconn(pPeer->GetAddr(), this, GetSocketErrorMsg());
1090  break;
1091  }
1092  // The following two lines of code will make sure that if the variable
1093  // "iBytesToRead" is zero, it will be increased by one.
1094  // In this case, it will hold the value 1 after the operation.
1095  // Note it doesn't do anything for negative values.
1096  // (This comment has been sponsored by Sven2)
1097  if (!iBytesToRead)
1098  ++iBytesToRead;
1099  // get buffer
1100  void *pBuf = pPeer->GetRecvBuf(iBytesToRead);
1101  // read a buffer full of data from socket
1102  int iBytesRead;
1103  if ((iBytesRead = ::recv(sock, reinterpret_cast<char *>(pBuf), iBytesToRead, 0)) == SOCKET_ERROR)
1104  {
1105  // Would block? Ok, let's try this again later
1106  if (HaveWouldBlockError()) { ResetSocketError(); break; }
1107  // So he's serious after all...
1108  pPeer->Close ();
1109  if (pCB) pCB->OnDisconn(pPeer->GetAddr(), this, GetSocketErrorMsg());
1110  break;
1111  }
1112  // nothing? this means the conection was closed, if you trust in linux manpages.
1113  if (!iBytesRead)
1114  {
1115  pPeer->Close();
1116  if (pCB) pCB->OnDisconn(pPeer->GetAddr(), this, "connection closed");
1117  break;
1118  }
1119  // pass to Peer::OnRecv
1120  pPeer->OnRecv(iBytesRead);
1121  }
1122 
1123  // socket has become writeable?
1124 #ifdef STDSCHEDULER_USE_EVENTS
1125  if (wsaEvents.lNetworkEvents & FD_WRITE)
1126 #else
1127  if (cur_fd != fdmap.end() && (POLLOUT & cur_fd->second->revents))
1128 #endif
1129  // send remaining data
1130  pPeer->Send();
1131 
1132 #ifdef STDSCHEDULER_USE_EVENTS
1133  // socket was closed?
1134  if (wsaEvents.lNetworkEvents & FD_CLOSE)
1135  {
1136  const char *szReason = wsaEvents.iErrorCode[FD_CLOSE_BIT] ? GetSocketErrorMsg(wsaEvents.iErrorCode[FD_CLOSE_BIT]) : "closed by peer";
1137  // close socket
1138  pPeer->Close();
1139  // do callback
1140  if (pCB) pCB->OnDisconn(pPeer->GetAddr(), this, szReason);
1141  }
1142 #endif
1143  }
1144 
1145  // done
1146  return true;
1147 }
#define INVALID_SOCKET
Definition: C4NetIO.h:36
const char * GetSocketErrorMsg(int iError)
Definition: C4NetIO.cpp:190
void SetError(const char *strnError, bool fSockErr=false)
Definition: C4NetIO.cpp:745
#define ioctlsocket
Definition: C4NetIO.cpp:45
bool Listen(uint16_t inListenPort)
Definition: C4NetIO.cpp:1476
Peer * pPeerList
Definition: C4NetIO.h:442
Peer * Accept(SOCKET nsock=INVALID_SOCKET, const addr_t &ConnectAddr=addr_t())
Definition: C4NetIO.cpp:1365
int Pipe[2]
Definition: C4NetIO.h:469
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:454
void ResetSocketError()
Definition: C4NetIO.cpp:211
friend class Peer
Definition: C4NetIO.h:440
#define SOCKET
Definition: C4NetIO.h:35
void GetFDs(std::vector< struct pollfd > &FDs) override
Definition: C4NetIO.cpp:1302
bool HaveWouldBlockError()
Definition: C4NetIO.cpp:203
static const int TO_INF
Definition: C4NetIO.h:66
bool fInit
Definition: C4NetIO.h:458
SOCKET lsock
Definition: C4NetIO.h:462
virtual void OnDisconn(const addr_t &AddrPeer, C4NetIO *pNetIO, const char *szReason)
Definition: C4NetIO.h:220
#define INFINITE
Definition: StdSync.h:58
uint32_t DWORD
uint16_t iListenPort
Definition: C4NetIO.h:461
#define SOCKET_ERROR
Definition: C4NetIO.cpp:47

Here is the call graph for this function:

Here is the caller 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 1605 of file C4NetIO.cpp.

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

Referenced by C4NetIOTCP::Close().

1606 {
1607  CStdShareLock PeerListLock(&PeerListCSec);
1608  // search
1609  for (ConnectWait *pWait = pConnectWaits; pWait; pWait = pWait->Next)
1610  if (pWait->addr == addr)
1611  return pWait;
1612  return nullptr;
1613 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:454

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 1344 of file C4NetIO.cpp.

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

1345 {
1346  CStdShareLock PeerListLock(&PeerListCSec);
1347  // find peer
1348  Peer *pPeer = GetPeer(addr);
1349  if (!pPeer || !pPeer->Open()) return false;
1350  // return statistics
1351  if (pIRate) *pIRate = pPeer->GetIRate();
1352  if (pORate) *pORate = pPeer->GetORate();
1353  if (pLoss) *pLoss = 0;
1354  return true;
1355 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:454
Peer * GetPeer(const addr_t &addr)
Definition: C4NetIO.cpp:1531
friend class Peer
Definition: C4NetIO.h:440

Here is the call graph for this function:

static C4AulDebug* C4AulDebug::GetDebugger ( )
inlinestatic

Definition at line 31 of file C4AulDebug.h.

Referenced by C4Game::LinkScriptEngine(), and Log().

31 { return pDebug; }

Here is the caller graph for this function:

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

Definition at line 284 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().

284 { return Error.getData(); }
const char * getData() const
Definition: StdBuf.h:442
StdCopyStrBuf Error
Definition: C4NetIO.h:281

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 1302 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().

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

Here is the caller graph for this function:

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

Definition at line 626 of file C4NetIO.cpp.

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

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

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

Here is the call graph for this function:

Here is the caller graph for this function:

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 C4Network2HTTPClient::GetNextTick().

Here is the caller graph for this function:

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

Definition at line 1531 of file C4NetIO.cpp.

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

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

1532 {
1533  CStdShareLock PeerListLock(&PeerListCSec);
1534  for (Peer *pPeer = pPeerList; pPeer; pPeer = pPeer->Next)
1535  if (pPeer->Open())
1536  if (pPeer->GetAddr() == addr)
1537  return pPeer;
1538  return nullptr;
1539 }
Peer * pPeerList
Definition: C4NetIO.h:442
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:454
friend class Peer
Definition: C4NetIO.h:440

Here is the caller graph for this function:

bool C4NetIOTCP::GetStatistic ( int *  pBroadcastRate)
overridevirtualinherited

Implements C4NetIO.

Definition at line 1337 of file C4NetIO.cpp.

1338 {
1339  // no broadcast
1340  if (pBroadcastRate) *pBroadcastRate = 0;
1341  return true;
1342 }
bool C4AulDebug::Init ( uint16_t  iPort)
overridevirtual

Reimplemented from C4NetIOTCP.

Definition at line 163 of file C4AulDebug.cpp.

References Close(), C4NetIOTCP::Init(), C4NetIO::EndpointAddress::IPPORT_NONE, and C4NetIOTCP::SetCallback().

Referenced by Listen().

164 {
165  if (fInit) Close();
166  if (iPort == EndpointAddress::IPPORT_NONE) return false;
167 
168  // Register self as callback for network events
170 
171  // Start listening
172  if (!C4NetIOTCP::Init(iPort))
173  return false;
174 
175  // Okay
176  fInit = true;
177  eState = DS_Go;
178  return true;
179 }
void SetCallback(CBClass *pnCallback) override
Definition: C4NetIO.h:486
bool Init(uint16_t iPort=addr_t::IPPORT_NONE) override
Definition: C4NetIO.cpp:806
static const uint16_t IPPORT_NONE
Definition: C4NetIO.h:136
bool Close() override
Definition: C4AulDebug.cpp:181

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 846 of file C4NetIO.cpp.

Referenced by main().

847 {
848  // ignore
849  return true;
850 }

Here is the caller graph for this function:

bool C4AulDebug::InitDebug ( const char *  szPassword,
const char *  szHost 
)
static

Definition at line 43 of file C4AulDebug.cpp.

References AulExec, C4AulDebug(), SetAllowed(), SetEngine(), and SetPassword().

Referenced by C4Game::InitGame().

44 {
45  // Create debug object
46  if (!pDebug) pDebug = new C4AulDebug();
47  // Initialize
48  pDebug->SetPassword(szPassword);
49  pDebug->SetAllowed(szHost);
50  pDebug->SetEngine(&AulExec);
51  return true;
52 }
void SetEngine(class C4AulExec *pnExec)
Definition: C4AulDebug.h:73
C4AulExec AulExec
Definition: C4AulExec.cpp:29
bool SetAllowed(const char *szHost)
Definition: C4AulDebug.cpp:152
void SetPassword(const char *szPassword)
Definition: C4AulDebug.h:71

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4NetIO::InitIPv6Socket ( SOCKET  socket)
protectedinherited

Definition at line 725 of file C4NetIO.cpp.

References C4NetIO::SetError(), and SOCKET_ERROR.

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

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

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4AulDebug::isConnected ( ) const
inline

Definition at line 69 of file C4AulDebug.h.

Referenced by Listen().

69 { 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 258 of file C4NetIO.h.

258 { return true; }
bool StdSchedulerProc::IsSignaled ( )
inherited
bool C4AulDebug::Listen ( uint16_t  iPort,
bool  fWait 
)

Definition at line 54 of file C4AulDebug.cpp.

References StdScheduler::Add(), Application, Init(), isConnected(), Log(), LogF(), LogFatal(), and StdScheduler::ScheduleProcs().

55 {
56  if (!Init(iPort))
57  { LogFatal("C4Aul debugger failed to initialize!"); return false; }
58  // Log
59  LogF("C4Aul debugger initialized on port %d", iPort);
60  // Add to application
61  Application.Add(this);
62  // Wait for connection
63  if (fWait)
64  {
65  Log("C4Aul debugger waiting for connection...");
66  while (!isConnected())
68  return false;
69  }
70  // Done
71  return true;
72 }
void Add(StdSchedulerProc *pProc)
bool Init(uint16_t iPort) override
Definition: C4AulDebug.cpp:163
bool LogFatal(const char *szMessage)
Definition: C4Log.cpp:237
bool Log(const char *szMessage)
Definition: C4Log.cpp:202
bool ScheduleProcs(int iTimeout=1000/36)
bool LogF(const char *strMessage,...)
Definition: C4Log.cpp:260
bool isConnected() const
Definition: C4AulDebug.h:69
C4Application Application
Definition: C4Globals.cpp:44

Here is the call graph for this function:

bool C4NetIOTCP::Listen ( uint16_t  inListenPort)
protectedinherited

Definition at line 1476 of file C4NetIO.cpp.

References C4NetIO::HostAddress::Any, StdSchedulerProc::Changed(), closesocket, 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().

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

Here is the call graph for this function:

Here is the caller graph for this function:

void C4AulDebug::OnLog ( const char *  szLine)

Definition at line 197 of file C4AulDebug.cpp.

198 {
199  if (!fConnected) return;
200  SendLine("LOG", szLine);
201 }
void C4NetIOTCP::OnShareFree ( CStdCSecEx pCSec)
overrideprotectedvirtualinherited

Implements CStdCSecExCallback.

Definition at line 1541 of file C4NetIO.cpp.

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

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

Here is the call graph for this function:

void C4NetIO::ResetError ( )
inlineinherited

Definition at line 285 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().

285 { Error.Clear(); }
void Clear()
Definition: StdBuf.h:466
StdCopyStrBuf Error
Definition: C4NetIO.h:281

Here is the call graph for this function:

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 1248 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().

1249 {
1250  CStdShareLock PeerListLock(&PeerListCSec);
1251  // find peer
1252  Peer *pPeer = GetPeer(rPacket.getAddr());
1253  // not found?
1254  if (!pPeer) return false;
1255  // send
1256  return pPeer->Send(rPacket);
1257 }
const C4NetIO::addr_t & getAddr() const
Definition: C4NetIO.h:315
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:454
Peer * GetPeer(const addr_t &addr)
Definition: C4NetIO.cpp:1531
friend class Peer
Definition: C4NetIO.h:440
bool Send(const C4NetIOPacket &rPacket)
Definition: C4NetIO.cpp:1689

Here is the call graph for this function:

Here is the caller graph for this function:

bool C4AulDebug::SetAllowed ( const char *  szHost)

Definition at line 152 of file C4AulDebug.cpp.

References C4NetIO::EndpointAddress::Clear(), C4NetIO::EndpointAddress::IsNull(), and C4NetIO::EndpointAddress::SetAddress().

Referenced by InitDebug().

153 {
154  // Clear
155  AllowedAddr.Clear();
156  // No host?
157  if (!szHost || !*szHost) return true;
158  // Resolve the address
159  AllowedAddr.SetAddress(StdStrBuf(szHost));
160  return !AllowedAddr.IsNull();
161 }
void SetAddress(const sockaddr *addr)
Definition: C4NetIO.cpp:364
bool IsNull() const
Definition: C4NetIO.cpp:503

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 1259 of file C4NetIO.cpp.

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

1260 {
1261  CStdShareLock PeerListLock(&PeerListCSec);
1262  // find peer
1263  Peer *pPeer = GetPeer(addr);
1264  if (!pPeer) return false;
1265  // set flag
1266  pPeer->SetBroadcast(fSet);
1267  return true;
1268 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:454
Peer * GetPeer(const addr_t &addr)
Definition: C4NetIO.cpp:1531
friend class Peer
Definition: C4NetIO.h:440

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 486 of file C4NetIO.h.

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

486 { pCB = pnCallback; };

Here is the caller graph for this function:

void C4AulDebug::SetEngine ( class C4AulExec pnExec)
inline

Definition at line 73 of file C4AulDebug.h.

Referenced by InitDebug().

73 { pExec = pnExec; }

Here is the caller graph for this function:

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

Definition at line 745 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(), C4Network2UpdateClient::GetUpdateURL(), C4Network2UpdateClient::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().

746 {
747  fSockErr &= HaveSocketError();
748  if (fSockErr)
749  Error.Format("%s (%s)", strnError, GetSocketErrorMsg());
750  else
751  Error.Copy(strnError);
752 }
const char * GetSocketErrorMsg(int iError)
Definition: C4NetIO.cpp:190
StdCopyStrBuf Error
Definition: C4NetIO.h:281
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 C4AulDebug::SetPassword ( const char *  szPassword)
inline

Definition at line 71 of file C4AulDebug.h.

Referenced by InitDebug().

71 { Password = szPassword; }

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 1281 of file C4NetIO.cpp.

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

Referenced by C4NetIOTCP::AddConnectWait().

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

Here is the call graph for this function:

Here is the caller graph for this function:

Member Data Documentation

uint16_t C4NetIOTCP::iListenPort
protectedinherited

Definition at line 461 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 455 of file C4NetIO.h.

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

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

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