OpenClonk
C4Network2IRCClient Class Referenceabstract

#include <C4Network2IRC.h>

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

Public Types

typedef EndpointAddress addr_t
 

Public Member Functions

 C4Network2IRCClient ()
 
 ~C4Network2IRCClient () override
 
CStdCSecgetCSec ()
 
C4Network2IRCChannelgetFirstChannel () const
 
C4Network2IRCChannelgetNextChannel (C4Network2IRCChannel *pPrevChan) const
 
C4Network2IRCChannelgetChannel (const char *szName) const
 
C4Network2IRCMessagegetMessageLog () const
 
C4Network2IRCMessagegetUnreadMessageLog () const
 
void ClearMessageLog ()
 
void MarkMessageLogRead ()
 
const char * getUserName () const
 
bool Connect (const char *szServer, const char *szNick, const char *szRealName, const char *szPassword=nullptr, const char *szChannel=nullptr)
 
bool Close () override
 
bool Send (const char *szCommand, const char *szParameters=nullptr)
 
void SetNotify (class C4InteractiveThread *pnNotify)
 
bool Quit (const char *szReason)
 
bool Join (const char *szChannel)
 
bool Part (const char *szChannel)
 
bool Message (const char *szTarget, const char *szText)
 
bool Notice (const char *szTarget, const char *szText)
 
bool Action (const char *szTarget, const char *szText)
 
bool ChangeNick (const char *szNewNick)
 
bool RegisterNick (const char *szPassword, const char *szMail)
 
bool IsActive () const
 
bool IsConnected () const
 
bool Init (uint16_t iPort=addr_t::IPPORT_NONE) override
 
virtual bool InitBroadcast (addr_t *pBroadcastAddr)
 
bool Close (const addr_t &addr) override
 
virtual bool CloseBroadcast ()
 
bool Execute (int iMaxTime=TO_INF, pollfd *readyfds=nullptr) override
 
std::unique_ptr< SocketBind (const addr_t &addr)
 
bool Connect (const addr_t &addr, std::unique_ptr< Socket > socket)
 
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 std::vector< HostAddressGetLocalAddresses ()
 

Static Public Attributes

static const int TO_INF = -1
 

Protected Member Functions

bool Connect (const addr_t &addr, SOCKET nsock)
 
bool Listen (uint16_t inListenPort)
 
SOCKET CreateSocket (addr_t::AddressFamily family)
 
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
 
bool fInit {false}
 
uint16_t iListenPort
 
SOCKET lsock
 
int Pipe [2]
 
StdCopyStrBuf Error
 

Detailed Description

Definition at line 122 of file C4Network2IRC.h.

Member Typedef Documentation

◆ addr_t

typedef EndpointAddress C4NetIO::addr_t
inherited

Definition at line 212 of file C4NetIO.h.

Constructor & Destructor Documentation

◆ C4Network2IRCClient()

C4Network2IRCClient::C4Network2IRCClient ( )
default

Referenced by C4Network2IRCChannel::getUser().

Here is the caller graph for this function:

◆ ~C4Network2IRCClient()

C4Network2IRCClient::~C4Network2IRCClient ( )
override

Definition at line 182 of file C4Network2IRC.cpp.

References StdStrBuf::Copy(), StdStrBuf::CopyUntil(), FormatString(), StdBuf::getData(), StdStrBuf::getData(), StdStrBuf::getLength(), StdBuf::getSize(), StdBuf::Grow(), iSize, LoadResStr(), MSG_Status, SSearch(), and StdBuf::Write().

183 {
184  Close();
185 }
bool Close() override
Here is the call graph for this function:

Member Function Documentation

◆ Accept()

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

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

1428 {
1429 
1430  addr_t caddr = ConnectAddr;
1431 
1432  // accept incoming connection?
1433  C4NetIO::addr_t addr; socklen_t iAddrSize = addr.GetAddrLen();
1434  if (nsock == INVALID_SOCKET)
1435  {
1436  // accept from listener
1437 #ifdef __linux__
1438  if ((nsock = ::accept4(lsock, &addr, &iAddrSize, SOCK_CLOEXEC)) == INVALID_SOCKET)
1439 #else
1440  if ((nsock = ::accept(lsock, &addr, &iAddrSize)) == INVALID_SOCKET)
1441 #endif
1442  {
1443  // set error
1444  SetError("socket accept failed", true);
1445  return nullptr;
1446  }
1447  // connect address unknown, so zero it
1448  caddr.Clear();
1449  }
1450  else
1451  {
1452  // get peer address
1453  if (::getpeername(nsock, &addr, &iAddrSize) == SOCKET_ERROR)
1454  {
1455 #ifndef HAVE_WINSOCK
1456  // getpeername behaves strangely on exotic platforms. Just ignore it.
1457  if (errno != ENOTCONN)
1458  {
1459 #endif
1460  // set error
1461  SetError("could not get peer address for connected socket", true);
1462  return nullptr;
1463 #ifndef HAVE_WINSOCK
1464  }
1465 #endif
1466  }
1467  }
1468 
1469  // check address
1470  if (addr.GetFamily() == addr_t::UnknownFamily)
1471  {
1472  // set error
1473  SetError("socket accept failed: invalid address returned");
1474  closesocket(nsock);
1475  return nullptr;
1476  }
1477 
1478  // disable nagle (yep, we know what we are doing here - I think)
1479  int iNoDelay = 1;
1480  ::setsockopt(nsock, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast<const char *>(&iNoDelay), sizeof(iNoDelay));
1481 
1482 #ifdef STDSCHEDULER_USE_EVENTS
1483  // set event
1484  if (::WSAEventSelect(nsock, Event, FD_READ | FD_WRITE | FD_CLOSE) == SOCKET_ERROR)
1485  {
1486  // set error
1487  SetError("connection accept failed: could not set event", true);
1488  closesocket(nsock);
1489  return nullptr;
1490  }
1491 #elif defined(HAVE_WINSOCK)
1492  // disable blocking
1493  unsigned long iBlock = 1;
1494  if (::ioctlsocket(nsock, FIONBIO, &iBlock) == SOCKET_ERROR)
1495  {
1496  // set error
1497  SetError("connect failed: could not disable blocking", true);
1498  close(nsock);
1499  return false;
1500  }
1501 #else
1502  // disable blocking
1503  if (::fcntl(nsock, F_SETFL, fcntl(nsock, F_GETFL) | O_NONBLOCK) == SOCKET_ERROR)
1504  {
1505  // set error
1506  SetError("connection accept failed: could not disable blocking", true);
1507  close(nsock);
1508  return nullptr;
1509  }
1510 #endif
1511 
1512 
1513  // create new peer
1514  Peer *pnPeer = new Peer(addr, nsock, this);
1515 
1516  // get required locks to add item to list
1517  CStdShareLock PeerListLock(&PeerListCSec);
1518  CStdLock PeerListAddLock(&PeerListAddCSec);
1519 
1520  // add to list
1521  pnPeer->Next = pPeerList;
1522  pPeerList = pnPeer;
1523 
1524  // clear add-lock
1525  PeerListAddLock.Clear();
1526 
1527  Changed();
1528 
1529  // ask callback if connection should be permitted
1530  if (pCB && !pCB->OnConn(addr, caddr, nullptr, this))
1531  // close socket immediately (will be deleted later)
1532  pnPeer->Close();
1533 
1534  // ok
1535  return pnPeer;
1536 }
#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:457
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:469
friend class Peer
Definition: C4NetIO.h:455
EndpointAddress addr_t
Definition: C4NetIO.h:212
CStdCSec PeerListAddCSec
Definition: C4NetIO.h:470
#define SOCK_CLOEXEC
Definition: C4NetIO.h:50
#define closesocket
Definition: C4NetIO.cpp:46
SOCKET lsock
Definition: C4NetIO.h:477
size_t GetAddrLen() const
Definition: C4NetIO.cpp:526
AddressFamily GetFamily() const
Definition: C4NetIO.cpp:520
#define SOCKET_ERROR
Definition: C4NetIO.cpp:47
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Action()

bool C4Network2IRCClient::Action ( const char *  szTarget,
const char *  szText 
)

Definition at line 422 of file C4Network2IRC.cpp.

References FormatString(), and MSG_Action.

Referenced by C4ChatControl::ProcessInput().

423 {
424  if (!Send("PRIVMSG", FormatString("%s :\1ACTION %s\1", szTarget, szText).getData()))
425  return false;
426  PushMessage(MSG_Action, Nick.getData(), szTarget, szText);
427  return true;
428 }
bool Send(const char *szCommand, const char *szParameters=nullptr)
const char * getData() const
Definition: StdBuf.h:442
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:

◆ AddConnectWait()

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

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

1652 {
1653  CStdShareLock PeerListLock(&PeerListCSec);
1654  CStdLock PeerListAddLock(&PeerListAddCSec);
1655  // create new entry, add to list
1656  ConnectWait *pnWait = new ConnectWait;
1657  pnWait->sock = sock; pnWait->addr = addr;
1658  pnWait->Next = pConnectWaits;
1659  pConnectWaits = pnWait;
1660 #ifndef STDSCHEDULER_USE_EVENTS
1661  // unblock, so new FD can be realized
1662  UnBlock();
1663 #endif
1664  Changed();
1665 }
virtual void UnBlock()
Definition: C4NetIO.cpp:1343
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:469
CStdCSec PeerListAddCSec
Definition: C4NetIO.h:470
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Bind()

std::unique_ptr< C4NetIOTCP::Socket > C4NetIOTCP::Bind ( const addr_t addr)
inherited

Definition at line 1192 of file C4NetIO.cpp.

References closesocket, C4NetIOTCP::CreateSocket(), C4NetIO::HostAddress::GetAddrLen(), C4NetIO::HostAddress::GetFamily(), INVALID_SOCKET, C4NetIO::SetError(), SOCKET, and SOCKET_ERROR.

1193 {
1194  SOCKET nsock = CreateSocket(addr.GetFamily());
1195  if (nsock == INVALID_SOCKET) return nullptr;
1196 
1197  // bind the socket to the given address
1198  if (::bind(nsock, &addr, addr.GetAddrLen()) == SOCKET_ERROR)
1199  {
1200  SetError("binding the socket failed", true);
1201  closesocket(nsock);
1202  return nullptr;
1203  }
1204  return std::unique_ptr<Socket>(new Socket(nsock));
1205 }
#define INVALID_SOCKET
Definition: C4NetIO.h:36
void SetError(const char *strnError, bool fSockErr=false)
Definition: C4NetIO.cpp:750
#define SOCKET
Definition: C4NetIO.h:35
SOCKET CreateSocket(addr_t::AddressFamily family)
Definition: C4NetIO.cpp:1172
#define closesocket
Definition: C4NetIO.cpp:46
#define SOCKET_ERROR
Definition: C4NetIO.cpp:47
Here is the call graph for this function:

◆ Broadcast() [1/2]

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

Referenced by C4NetIO::IsNotify().

Here is the caller graph for this function:

◆ Broadcast() [2/2]

bool C4NetIOTCP::Broadcast ( const C4NetIOPacket rPacket)
overrideinherited

Definition at line 1332 of file C4NetIO.cpp.

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

1333 {
1334  CStdShareLock PeerListLock(&PeerListCSec);
1335  // just send to all clients
1336  bool fSuccess = true;
1337  for (Peer *pPeer = pPeerList; pPeer; pPeer = pPeer->Next)
1338  if (pPeer->Open() && pPeer->doBroadcast())
1339  fSuccess &= Send(C4NetIOPacket(rPacket.getRef(), pPeer->GetAddr()));
1340  return fSuccess;
1341 }
bool Send(const C4NetIOPacket &rPacket) override
Definition: C4NetIO.cpp:1310
Peer * pPeerList
Definition: C4NetIO.h:457
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:469
friend class Peer
Definition: C4NetIO.h:455
C4NetIOPacket getRef() const
Definition: C4NetIO.h:324
Here is the call graph for this function:

◆ Changed()

void StdSchedulerProc::Changed ( )
protectedinherited

Definition at line 108 of file StdScheduler.cpp.

References s.

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

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

◆ ChangeNick()

bool C4Network2IRCClient::ChangeNick ( const char *  szNewNick)

Definition at line 430 of file C4Network2IRC.cpp.

Referenced by C4ChatControl::ProcessInput().

431 {
432  return Send("NICK", szNewNick);
433 }
bool Send(const char *szCommand, const char *szParameters=nullptr)
Here is the caller graph for this function:

◆ ClearConnectWaits()

void C4NetIOTCP::ClearConnectWaits ( )
protectedinherited

Definition at line 1677 of file C4NetIO.cpp.

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

Referenced by C4NetIOTCP::Close().

1678 {
1679  CStdShareLock PeerListLock(&PeerListCSec);
1680  for (ConnectWait *pWait = pConnectWaits; pWait; pWait = pWait->Next)
1681  if (pWait->sock)
1682  {
1683  closesocket(pWait->sock);
1684  pWait->sock = 0;
1685  }
1686 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:469
#define closesocket
Definition: C4NetIO.cpp:46
Here is the caller graph for this function:

◆ ClearMessageLog()

void C4Network2IRCClient::ClearMessageLog ( )

Definition at line 284 of file C4Network2IRC.cpp.

285 {
286  // Clear log
287  while (iLogLength)
288  PopMessage();
289 }

◆ ClearStatistic()

void C4NetIOTCP::ClearStatistic ( )
overridevirtualinherited

Implements C4NetIO.

Definition at line 1419 of file C4NetIO.cpp.

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

1420 {
1421  CStdShareLock PeerListLock(&PeerListCSec);
1422  // clear all peer statistics
1423  for (Peer *pPeer = pPeerList; pPeer; pPeer = pPeer->Next)
1424  pPeer->ClearStatistics();
1425 }
Peer * pPeerList
Definition: C4NetIO.h:457
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:469
friend class Peer
Definition: C4NetIO.h:455

◆ Close() [1/2]

bool C4Network2IRCClient::Close ( )
overridevirtual

Reimplemented from C4NetIOTCP.

Definition at line 331 of file C4Network2IRC.cpp.

References StdStrBuf::Append(), C4ConfigIRC::Channel, C4NetIOTCP::Close(), Config, StdStrBuf::getData(), C4Network2IRCChannel::getName(), and C4Config::IRC.

Referenced by C4Application::Clear(), C4ChatDlg::StopChat(), and C4ChatControl::UserQueryQuit().

332 {
333  // Close network
335  // Save & Clear channels
336  if(pChannels) // Don't override empty
337  {
338  // It's somewhat weird to loop backward through a singly linked list, but it's necessary to keep the order
339  StdStrBuf chanstr;
340  C4Network2IRCChannel * pChan = pChannels;
341  while(pChan->Next)
342  pChan = pChan->Next;
343  chanstr.Append(pChan->getName());
344  while (pChan != pChannels)
345  {
346  C4Network2IRCChannel * pChanPrev = pChannels;
347  while(pChanPrev->Next != pChan)
348  pChanPrev = pChanPrev->Next;
349  pChan = pChanPrev;
350  chanstr.Append(",");
351  chanstr.Append(pChan->getName());
352  }
353  strncpy(Config.IRC.Channel, chanstr.getData(), sizeof(Config.IRC.Channel)-1);
354  }
355  while (pChannels)
356  DeleteChannel(pChannels);
357  // Clear log
358  ClearMessageLog();
359  // Reset flags
360  fConnected = fConnecting = false;
361  return true;
362 }
C4Config Config
Definition: C4Config.cpp:833
const char * getName() const
void Append(const char *pnData, size_t iChars)
Definition: StdBuf.h:519
C4ConfigIRC IRC
Definition: C4Config.h:257
const char * getData() const
Definition: StdBuf.h:442
bool Close() override
Definition: C4NetIO.cpp:857
char Channel[CFG_MaxString+1]
Definition: C4Config.h:208
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Close() [2/2]

bool C4NetIOTCP::Close ( const addr_t addr)
overridevirtualinherited

Implements C4NetIO.

Reimplemented in C4AulDebug.

Definition at line 1279 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.

1280 {
1281  CStdShareLock PeerListLock(&PeerListCSec);
1282  // find connect wait
1283  ConnectWait *pWait = GetConnectWait(addr);
1284  if (pWait)
1285  {
1286  // close socket, do callback
1287  closesocket(pWait->sock); pWait->sock = 0;
1288  if (pCB) pCB->OnDisconn(pWait->addr, this, "closed");
1289  }
1290  else
1291  {
1292  // find peer
1293  Peer *pPeer = GetPeer(addr);
1294  if (pPeer)
1295  {
1296  C4NetIO::addr_t addr = pPeer->GetAddr();
1297  // close peer
1298  pPeer->Close();
1299  // do callback
1300  if (pCB) pCB->OnDisconn(addr, this, "closed");
1301  }
1302  // not found
1303  else
1304  return false;
1305  }
1306  // ok
1307  return true;
1308 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:469
Peer * GetPeer(const addr_t &addr)
Definition: C4NetIO.cpp:1593
friend class Peer
Definition: C4NetIO.h:455
#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:1667
Here is the call graph for this function:

◆ CloseBroadcast()

bool C4NetIOTCP::CloseBroadcast ( )
virtualinherited

Definition at line 905 of file C4NetIO.cpp.

906 {
907  return true;
908 }

◆ Connect() [1/4]

bool C4Network2IRCClient::Connect ( const char *  szServer,
const char *  szNick,
const char *  szRealName,
const char *  szPassword = nullptr,
const char *  szChannel = nullptr 
)

Definition at line 301 of file C4Network2IRC.cpp.

References C4NetIOTCP::Connect(), and C4NetIOTCP::SetCallback().

Referenced by C4ChatControl::OnConnectBtn().

302 {
303  // Already connected? Close connection
304  if (fConnecting || fConnected)
305  Close();
306  // Initialize
308  if (!Init())
309  return false;
310  // Resolve address
311  ServerAddr.SetAddress(StdStrBuf(szServer));
312  if (ServerAddr.IsNull())
313  { SetError("Could no resolve server address!"); return false; }
314  ServerAddr.SetDefaultPort(6666);
315  // Set connection data
316  Nick = szNick; RealName = szRealName;
317  Password = szPassword; AutoJoin = szAutoJoin;
318  // Truncate password
319  if (Password.getLength() > 31)
320  Password.SetLength(31);
321  // Start connecting
322  if (!C4NetIOTCP::Connect(ServerAddr))
323  return false;
324  // Reset status data
325  Prefixes = "(ov)@+";
326  // Okay, let's wait for the connection.
327  fConnecting = true;
328  return true;
329 }
void SetError(const char *strnError, bool fSockErr=false)
Definition: C4NetIO.cpp:750
void SetCallback(CBClass *pnCallback) override
Definition: C4NetIO.h:504
void SetAddress(const sockaddr *addr)
Definition: C4NetIO.cpp:364
bool Close() override
bool Connect(const addr_t &addr, std::unique_ptr< Socket > socket)
Definition: C4NetIO.cpp:1207
bool Init(uint16_t iPort=addr_t::IPPORT_NONE) override
Definition: C4NetIO.cpp:811
void SetLength(size_t iLength)
Definition: StdBuf.h:509
void SetDefaultPort(uint16_t port)
Definition: C4NetIO.cpp:541
size_t getLength() const
Definition: StdBuf.h:445
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Connect() [2/4]

bool C4NetIOTCP::Connect ( const addr_t addr,
std::unique_ptr< Socket socket 
)
inherited

Definition at line 1207 of file C4NetIO.cpp.

References INVALID_SOCKET, and SOCKET.

Referenced by Connect(), C4Network2IOConnection::Connect(), C4NetIOTCP::Connect(), getUserName(), and main().

1208 {
1209  SOCKET nsock = socket->sock;
1210  socket->sock = INVALID_SOCKET;
1211  return Connect(addr, nsock);
1212 }
#define INVALID_SOCKET
Definition: C4NetIO.h:36
bool Connect(const addr_t &addr, std::unique_ptr< Socket > socket)
Definition: C4NetIO.cpp:1207
#define SOCKET
Definition: C4NetIO.h:35
Here is the caller graph for this function:

◆ Connect() [3/4]

bool C4NetIOTCP::Connect ( const addr_t addr)
overridevirtualinherited

Implements C4NetIO.

Definition at line 1214 of file C4NetIO.cpp.

References C4NetIOTCP::Connect(), C4NetIOTCP::CreateSocket(), C4NetIO::HostAddress::GetFamily(), INVALID_SOCKET, and SOCKET.

1215 {
1216  // create new socket
1217  SOCKET nsock = CreateSocket(addr.GetFamily());
1218  if (nsock == INVALID_SOCKET) return false;
1219 
1220  return Connect(addr, nsock);
1221 }
#define INVALID_SOCKET
Definition: C4NetIO.h:36
bool Connect(const addr_t &addr, std::unique_ptr< Socket > socket)
Definition: C4NetIO.cpp:1207
#define SOCKET
Definition: C4NetIO.h:35
SOCKET CreateSocket(addr_t::AddressFamily family)
Definition: C4NetIO.cpp:1172
Here is the call graph for this function:

◆ Connect() [4/4]

bool C4NetIOTCP::Connect ( const addr_t addr,
SOCKET  nsock 
)
protectedinherited

Definition at line 1223 of file C4NetIO.cpp.

References C4NetIOTCP::AddConnectWait(), closesocket, C4NetIO::HostAddress::GetAddrLen(), HaveWouldBlockError(), ioctlsocket, C4NetIO::SetError(), and SOCKET_ERROR.

1224 {
1225 #ifdef STDSCHEDULER_USE_EVENTS
1226  // set event
1227  if (::WSAEventSelect(nsock, Event, FD_CONNECT) == SOCKET_ERROR)
1228  {
1229  // set error
1230  SetError("connect failed: could not set event", true);
1231  closesocket(nsock);
1232  return false;
1233  }
1234 
1235  // add to list
1236  AddConnectWait(nsock, addr);
1237 
1238 #elif defined(HAVE_WINSOCK)
1239  // disable blocking
1240  unsigned long iBlock = 1;
1241  if (::ioctlsocket(nsock, FIONBIO, &iBlock) == SOCKET_ERROR)
1242  {
1243  // set error
1244  SetError("connect failed: could not disable blocking", true);
1245  close(nsock);
1246  return false;
1247  }
1248 #else
1249  // disable blocking
1250  if (::fcntl(nsock, F_SETFL, fcntl(nsock, F_GETFL) | O_NONBLOCK) == SOCKET_ERROR)
1251  {
1252  // set error
1253  SetError("connect failed: could not disable blocking", true);
1254  close(nsock);
1255  return false;
1256  }
1257 #endif
1258 
1259  // connect (async)
1260  if (::connect(nsock, &addr, addr.GetAddrLen()) == SOCKET_ERROR)
1261  {
1262  if (!HaveWouldBlockError()) // expected
1263  {
1264  SetError("socket connection failed", true);
1265  closesocket(nsock);
1266  return false;
1267  }
1268  }
1269 
1270 #ifndef STDSCHEDULER_USE_EVENTS
1271  // add to list
1272  AddConnectWait(nsock, addr);
1273 #endif
1274 
1275  // ok
1276  return true;
1277 }
void SetError(const char *strnError, bool fSockErr=false)
Definition: C4NetIO.cpp:750
#define ioctlsocket
Definition: C4NetIO.cpp:45
void AddConnectWait(SOCKET sock, const addr_t &addr)
Definition: C4NetIO.cpp:1651
bool HaveWouldBlockError()
Definition: C4NetIO.cpp:203
#define closesocket
Definition: C4NetIO.cpp:46
#define SOCKET_ERROR
Definition: C4NetIO.cpp:47
Here is the call graph for this function:

◆ CreateSocket()

SOCKET C4NetIOTCP::CreateSocket ( addr_t::AddressFamily  family)
protectedinherited

Definition at line 1172 of file C4NetIO.cpp.

References closesocket, C4NetIO::InitIPv6Socket(), INVALID_SOCKET, C4NetIO::HostAddress::IPv6, C4NetIO::SetError(), SOCK_CLOEXEC, and SOCKET.

Referenced by C4NetIOTCP::Bind(), and C4NetIOTCP::Connect().

1173 {
1174  // create new socket
1175  SOCKET nsock = ::socket(family == HostAddress::IPv6 ? AF_INET6 : AF_INET, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP);
1176  if (nsock == INVALID_SOCKET)
1177  {
1178  SetError("socket creation failed", true);
1179  return INVALID_SOCKET;
1180  }
1181 
1182  if (family == HostAddress::IPv6)
1183  if (!InitIPv6Socket(nsock))
1184  {
1185  closesocket(nsock);
1186  return INVALID_SOCKET;
1187  }
1188 
1189  return nsock;
1190 }
#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
#define SOCKET
Definition: C4NetIO.h:35
#define SOCK_CLOEXEC
Definition: C4NetIO.h:50
#define closesocket
Definition: C4NetIO.cpp:46
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Execute()

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

Implements C4NetIO.

Reimplemented in C4Network2HTTPClient.

Definition at line 919 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 C4AulDebug::DebugStep(), and C4Network2HTTPClient::Execute().

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

◆ ExecuteUntil()

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

Definition at line 33 of file StdScheduler.cpp.

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

Referenced by main().

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

◆ getChannel()

C4Network2IRCChannel * C4Network2IRCClient::getChannel ( const char *  szName) const

Definition at line 276 of file C4Network2IRC.cpp.

References SEqualNoCase().

Referenced by C4ChatControl::ChatSheet::NickItem::SortFunc(), and C4ChatControl::Update().

277 {
278  for (C4Network2IRCChannel *pChan = pChannels; pChan; pChan = pChan->Next)
279  if (SEqualNoCase(pChan->getName(), szName))
280  return pChan;
281  return nullptr;
282 }
bool SEqualNoCase(const char *szStr1, const char *szStr2, int iLen)
Definition: Standard.cpp:207
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetConnectWait()

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

Definition at line 1667 of file C4NetIO.cpp.

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

Referenced by C4NetIOTCP::Close().

1668 {
1669  CStdShareLock PeerListLock(&PeerListCSec);
1670  // search
1671  for (ConnectWait *pWait = pConnectWaits; pWait; pWait = pWait->Next)
1672  if (pWait->addr == addr)
1673  return pWait;
1674  return nullptr;
1675 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:469
Here is the caller graph for this function:

◆ GetConnStatistic()

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

Implements C4NetIO.

Definition at line 1406 of file C4NetIO.cpp.

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

1407 {
1408  CStdShareLock PeerListLock(&PeerListCSec);
1409  // find peer
1410  Peer *pPeer = GetPeer(addr);
1411  if (!pPeer || !pPeer->Open()) return false;
1412  // return statistics
1413  if (pIRate) *pIRate = pPeer->GetIRate();
1414  if (pORate) *pORate = pPeer->GetORate();
1415  if (pLoss) *pLoss = 0;
1416  return true;
1417 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:469
Peer * GetPeer(const addr_t &addr)
Definition: C4NetIO.cpp:1593
friend class Peer
Definition: C4NetIO.h:455
Here is the call graph for this function:

◆ getCSec()

CStdCSec* C4Network2IRCClient::getCSec ( )
inline

Definition at line 165 of file C4Network2IRC.h.

Referenced by C4ChatControl::ProcessInput(), C4ChatControl::ChatSheet::NickItem::SortFunc(), and C4ChatControl::Update().

165 { return &CSec; }
Here is the caller graph for this function:

◆ GetError()

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

Definition at line 285 of file C4NetIO.h.

References StdStrBuf::getData().

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

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

◆ GetFDs()

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

Reimplemented from StdSchedulerProc.

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

1365 {
1366  pollfd pfd; pfd.revents = 0;
1367  // add pipe
1368  pfd.fd = Pipe[0]; pfd.events = POLLIN;
1369  fds.push_back(pfd);
1370  // add listener
1371  if (lsock != INVALID_SOCKET)
1372  {
1373  pfd.fd = lsock; pfd.events = POLLIN;
1374  fds.push_back(pfd);
1375  }
1376  // add connect waits (wait for them to become writeable)
1377  CStdShareLock PeerListLock(&PeerListCSec);
1378  for (ConnectWait *pWait = pConnectWaits; pWait; pWait = pWait->Next)
1379  {
1380  pfd.fd = pWait->sock; pfd.events = POLLOUT;
1381  fds.push_back(pfd);
1382  }
1383  // add sockets
1384  for (Peer *pPeer = pPeerList; pPeer; pPeer = pPeer->Next)
1385  if (pPeer->GetSocket())
1386  {
1387  // Wait for socket to become readable
1388  pfd.fd = pPeer->GetSocket(); pfd.events = POLLIN;
1389  // Wait for socket to become writeable, if there is data waiting
1390  if (pPeer->hasWaitingData())
1391  {
1392  pfd.events |= POLLOUT;
1393  }
1394  fds.push_back(pfd);
1395  }
1396 }
#define INVALID_SOCKET
Definition: C4NetIO.h:36
Peer * pPeerList
Definition: C4NetIO.h:457
int Pipe[2]
Definition: C4NetIO.h:484
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:469
friend class Peer
Definition: C4NetIO.h:455
SOCKET lsock
Definition: C4NetIO.h:477
Here is the caller graph for this function:

◆ getFirstChannel()

C4Network2IRCChannel * C4Network2IRCClient::getFirstChannel ( ) const

Definition at line 266 of file C4Network2IRC.cpp.

Referenced by C4ChatControl::Update().

267 {
268  return pChannels;
269 }
Here is the caller graph for this function:

◆ GetLocalAddresses()

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:

◆ getMessageLog()

C4Network2IRCMessage* C4Network2IRCClient::getMessageLog ( ) const
inline

Definition at line 171 of file C4Network2IRC.h.

Referenced by C4ChatControl::Update().

171 { return pLog; }
Here is the caller graph for this function:

◆ getNextChannel()

C4Network2IRCChannel * C4Network2IRCClient::getNextChannel ( C4Network2IRCChannel pPrevChan) const

Definition at line 271 of file C4Network2IRC.cpp.

Referenced by C4ChatControl::Update().

272 {
273  return pPrevChan ? pPrevChan->Next : pChannels;
274 }
Here is the caller graph for this function:

◆ GetNextTick()

C4TimeMilliseconds StdSchedulerProc::GetNextTick ( C4TimeMilliseconds  tNow)
virtualinherited

Reimplemented in C4NetIOUDP, C4Network2HTTPClient, C4Network2IO, and CStdTimerProc.

Definition at line 115 of file StdScheduler.cpp.

References C4TimeMilliseconds::PositiveInfinity.

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

Here is the caller graph for this function:

◆ GetPeer()

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

Definition at line 1593 of file C4NetIO.cpp.

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

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

1594 {
1595  CStdShareLock PeerListLock(&PeerListCSec);
1596  for (Peer *pPeer = pPeerList; pPeer; pPeer = pPeer->Next)
1597  if (pPeer->Open())
1598  if (pPeer->GetAddr() == addr)
1599  return pPeer;
1600  return nullptr;
1601 }
Peer * pPeerList
Definition: C4NetIO.h:457
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:469
friend class Peer
Definition: C4NetIO.h:455
Here is the caller graph for this function:

◆ GetStatistic()

bool C4NetIOTCP::GetStatistic ( int *  pBroadcastRate)
overridevirtualinherited

Implements C4NetIO.

Definition at line 1399 of file C4NetIO.cpp.

1400 {
1401  // no broadcast
1402  if (pBroadcastRate) *pBroadcastRate = 0;
1403  return true;
1404 }

◆ getUnreadMessageLog()

C4Network2IRCMessage* C4Network2IRCClient::getUnreadMessageLog ( ) const
inline

Definition at line 172 of file C4Network2IRC.h.

References C4Network2IRCMessage::getNext().

Referenced by C4ChatControl::Update().

172 { return pLogLastRead ? pLogLastRead->getNext() : pLog; }
C4Network2IRCMessage * getNext() const
Definition: C4Network2IRC.h:57
Here is the call graph for this function:
Here is the caller graph for this function:

◆ getUserName()

const char* C4Network2IRCClient::getUserName ( ) const
inline

Definition at line 175 of file C4Network2IRC.h.

References C4NetIOTCP::Close(), C4NetIOTCP::Connect(), StdStrBuf::getData(), and C4NetIOTCP::Send().

Referenced by C4ChatControl::Update().

175 { return Nick.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:

◆ Init()

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(), C4AulDebug::Init(), C4Network2IO::Init(), C4Network2::InitLeague(), C4Game::InitNetworkFromAddress(), main(), C4StartupNetListEntry::SetRefQuery(), and C4DownloadDlg::ShowModal().

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:1538
int Pipe[2]
Definition: C4NetIO.h:484
bool Close() override
Definition: C4NetIO.cpp:857
bool fInit
Definition: C4NetIO.h:473
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:

◆ InitBroadcast()

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:

◆ InitIPv6Socket()

bool C4NetIO::InitIPv6Socket ( SOCKET  socket)
protectedinherited

Definition at line 730 of file C4NetIO.cpp.

References C4NetIO::SetError(), and SOCKET_ERROR.

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

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

◆ IsActive()

bool C4Network2IRCClient::IsActive ( ) const
inline

Definition at line 199 of file C4Network2IRC.h.

Referenced by C4ChatDlg::IsChatActive(), C4ChatControl::UpdateShownPage(), and C4ChatControl::UserQueryQuit().

199 { return fConnecting || fConnected; }
Here is the caller graph for this function:

◆ IsConnected()

bool C4Network2IRCClient::IsConnected ( ) const
inline

Definition at line 200 of file C4Network2IRC.h.

Referenced by C4ChatControl::ProcessInput().

200 { return fConnected; }
Here is the caller graph for this function:

◆ IsLowPriority()

virtual bool StdSchedulerProc::IsLowPriority ( )
inlinevirtualinherited

Definition at line 82 of file StdScheduler.h.

82 { return false; }

◆ IsNotify()

bool C4NetIO::IsNotify ( )
inlineoverridevirtualinherited

Reimplemented from StdSchedulerProc.

Definition at line 259 of file C4NetIO.h.

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

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

◆ IsSignaled()

bool StdSchedulerProc::IsSignaled ( )
inherited

Referenced by StdSchedulerProc::GetFDs().

Here is the caller graph for this function:

◆ Join()

bool C4Network2IRCClient::Join ( const char *  szChannel)

Definition at line 386 of file C4Network2IRC.cpp.

References C4ConfigIRC::AllowAllChannels, Application, Config, C4GUI::Ico_Error, C4Application::InteractiveThread, C4Config::IRC, LoadResStr(), MSG_Status, pGUI, C4GUI::Screen::ShowMessage(), SWildcardMatchEx(), and C4InteractiveThread::ThreadPostAsync().

Referenced by C4ChatControl::ProcessInput().

387 {
388  // Newbie limitation: can only join channels beginning with #clonk
390  if (!SWildcardMatchEx(szChannel, "#*clonk*"))
391  {
392  const char* message = LoadResStr("IDS_ERR_CHANNELNOTALLOWED");
393  PushMessage(MSG_Status, "", "", message);
394  SetError("Joining this channel not allowed");
395  Application.InteractiveThread.ThreadPostAsync<bool>(std::bind(&C4GUI::Screen::ShowMessage, ::pGUI, message, LoadResStr("IDS_DLG_CHAT"), C4GUI::Ico_Error, static_cast<int32_t* const &>(nullptr)));
396  return false;
397  }
398  return Send("JOIN", szChannel);
399 }
C4Config Config
Definition: C4Config.cpp:833
bool Send(const char *szCommand, const char *szParameters=nullptr)
void SetError(const char *strnError, bool fSockErr=false)
Definition: C4NetIO.cpp:750
bool ThreadPostAsync(Functor function)
const char * LoadResStr(const char *id)
Definition: C4Language.h:83
C4GUIScreen * pGUI
Definition: C4Gui.cpp:1191
bool SWildcardMatchEx(const char *szString, const char *szWildcard)
Definition: Standard.cpp:629
C4ConfigIRC IRC
Definition: C4Config.h:257
bool ShowMessage(const char *szMessage, const char *szCaption, Icons icoIcon, int32_t *piConfigDontShowAgainSetting=nullptr)
int32_t AllowAllChannels
Definition: C4Config.h:210
C4Application Application
Definition: C4Globals.cpp:44
C4InteractiveThread InteractiveThread
Definition: C4Application.h:45
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Listen()

bool C4NetIOTCP::Listen ( uint16_t  inListenPort)
protectedinherited

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

1539 {
1540  // already listening?
1541  if (lsock != INVALID_SOCKET)
1542  // close existing socket
1543  closesocket(lsock);
1545 
1546  // create socket
1547  if ((lsock = ::socket(AF_INET6, SOCK_STREAM | SOCK_CLOEXEC, IPPROTO_TCP)) == INVALID_SOCKET)
1548  {
1549  SetError("socket creation failed", true);
1550  return false;
1551  }
1552  if (!InitIPv6Socket(lsock))
1553  return false;
1554  // To be able to reuse the port after close
1555 #if !defined(_DEBUG) && !defined(_WIN32)
1556  int reuseaddr = 1;
1557  setsockopt(lsock, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<const char *>(&reuseaddr), sizeof(reuseaddr));
1558 #endif
1559  // bind listen socket
1560  addr_t addr = addr_t::Any;
1561  addr.SetPort(inListenPort);
1562  if (::bind(lsock, &addr, addr.GetAddrLen()) == SOCKET_ERROR)
1563  {
1564  SetError("socket bind failed", true);
1566  return false;
1567  }
1568 
1569 #ifdef STDSCHEDULER_USE_EVENTS
1570  // set event callback
1571  if (::WSAEventSelect(lsock, Event, FD_ACCEPT | FD_CLOSE) == SOCKET_ERROR)
1572  {
1573  SetError("could not set event for listen socket", true);
1575  return false;
1576  }
1577 #endif
1578 
1579  // start listening
1580  if (::listen(lsock, SOMAXCONN) == SOCKET_ERROR)
1581  {
1582  SetError("socket listen failed", true);
1584  return false;
1585  }
1586 
1587  // ok
1588  iListenPort = inListenPort;
1589  Changed();
1590  return true;
1591 }
#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:477
uint16_t iListenPort
Definition: C4NetIO.h:476
#define SOCKET_ERROR
Definition: C4NetIO.cpp:47
Here is the call graph for this function:
Here is the caller graph for this function:

◆ MarkMessageLogRead()

void C4Network2IRCClient::MarkMessageLogRead ( )

Definition at line 291 of file C4Network2IRC.cpp.

References C4NetIRCMaxReadLogLength.

Referenced by C4ChatControl::Update().

292 {
293  // set read marker to last message
294  pLogLastRead = pLogEnd;
295  iUnreadLogLength = 0;
296  // message buffer is smaller for messages already read: Remove old ones
297  while (iLogLength > C4NetIRCMaxReadLogLength)
298  PopMessage();
299 }
const int C4NetIRCMaxReadLogLength
Definition: C4Network2IRC.h:31
Here is the caller graph for this function:

◆ Message()

bool C4Network2IRCClient::Message ( const char *  szTarget,
const char *  szText 
)

Definition at line 406 of file C4Network2IRC.cpp.

References FormatString(), and MSG_Message.

Referenced by C4ChatControl::ProcessInput().

407 {
408  if (!Send("PRIVMSG", FormatString("%s :%s", szTarget, szText).getData()))
409  return false;
410  PushMessage(MSG_Message, Nick.getData(), szTarget, szText);
411  return true;
412 }
bool Send(const char *szCommand, const char *szParameters=nullptr)
const char * getData() const
Definition: StdBuf.h:442
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:

◆ Notice()

bool C4Network2IRCClient::Notice ( const char *  szTarget,
const char *  szText 
)

Definition at line 414 of file C4Network2IRC.cpp.

References FormatString(), and MSG_Notice.

Referenced by C4ChatControl::ProcessInput().

415 {
416  if (!Send("NOTICE", FormatString("%s :%s", szTarget, szText).getData()))
417  return false;
418  PushMessage(MSG_Notice, Nick.getData(), szTarget, szText);
419  return true;
420 }
bool Send(const char *szCommand, const char *szParameters=nullptr)
const char * getData() const
Definition: StdBuf.h:442
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:

◆ OnShareFree()

void C4NetIOTCP::OnShareFree ( CStdCSecEx pCSec)
overrideprotectedvirtualinherited

Implements CStdCSecExCallback.

Definition at line 1603 of file C4NetIO.cpp.

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

1604 {
1605  if (pCSec == &PeerListCSec)
1606  {
1607  // clear up
1608  Peer *pPeer = pPeerList, *pLast = nullptr;
1609  while (pPeer)
1610  {
1611  // delete?
1612  if (!pPeer->Open())
1613  {
1614  // unlink
1615  Peer *pDelete = pPeer;
1616  pPeer = pPeer->Next;
1617  (pLast ? pLast->Next : pPeerList) = pPeer;
1618  // delete
1619  delete pDelete;
1620  }
1621  else
1622  {
1623  // next peer
1624  pLast = pPeer;
1625  pPeer = pPeer->Next;
1626  }
1627  }
1628  ConnectWait *pWait = pConnectWaits, *pWLast = nullptr;
1629  while (pWait)
1630  {
1631  // delete?
1632  if (!pWait->sock)
1633  {
1634  // unlink
1635  ConnectWait *pDelete = pWait;
1636  pWait = pWait->Next;
1637  (pWLast ? pWLast->Next : pConnectWaits) = pWait;
1638  // delete
1639  delete pDelete;
1640  }
1641  else
1642  {
1643  // next peer
1644  pWLast = pWait;
1645  pWait = pWait->Next;
1646  }
1647  }
1648  }
1649 }
Peer * pPeerList
Definition: C4NetIO.h:457
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:469
friend class Peer
Definition: C4NetIO.h:455
Here is the call graph for this function:

◆ Part()

bool C4Network2IRCClient::Part ( const char *  szChannel)

Definition at line 401 of file C4Network2IRC.cpp.

Referenced by C4ChatControl::ProcessInput(), and C4ChatControl::ChatSheet::NickItem::SortFunc().

402 {
403  return Send("PART", szChannel);
404 }
bool Send(const char *szCommand, const char *szParameters=nullptr)
Here is the caller graph for this function:

◆ Quit()

bool C4Network2IRCClient::Quit ( const char *  szReason)

Definition at line 378 of file C4Network2IRC.cpp.

References FormatString().

Referenced by C4ChatControl::ProcessInput().

379 {
380  if (!Send("QUIT", FormatString(":%s", szReason).getData()))
381  return false;
382  // Must be last message
383  return Close();
384 }
bool Send(const char *szCommand, const char *szParameters=nullptr)
bool Close() override
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:

◆ RegisterNick()

bool C4Network2IRCClient::RegisterNick ( const char *  szPassword,
const char *  szMail 
)

Definition at line 435 of file C4Network2IRC.cpp.

References StdStrBuf::AppendChar(), C4_OS, C4NetIRCMaxLogLength, C4Network2IRCChannel::C4Network2IRCChannel(), StdStrBuf::Copy(), StdStrBuf::CopyUntil(), Ev_IRC_Message, FormatString(), StdStrBuf::getData(), StdStrBuf::getLength(), C4Network2IRCChannel::getName(), ircExtractPar(), LoadResStr(), MSG_Action, MSG_Message, MSG_Notice, MSG_Server, MSG_Status, SEqualNoCase(), SLen(), and SSearch().

436 {
437  return Send("PRIVMSG", FormatString("NickServ :REGISTER %s %s", szPassword, szMail).getData());
438 }
bool Send(const char *szCommand, const char *szParameters=nullptr)
StdStrBuf FormatString(const char *szFmt,...)
Definition: StdBuf.cpp:270
Here is the call graph for this function:

◆ ResetError()

void C4NetIO::ResetError ( )
inlineinherited

Definition at line 286 of file C4NetIO.h.

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

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

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

◆ Send() [1/3]

bool C4Network2IRCClient::Send ( const char *  szCommand,
const char *  szParameters = nullptr 
)

Definition at line 364 of file C4Network2IRC.cpp.

References StdStrBuf::Format(), StdStrBuf::getData(), StdStrBuf::getLength(), StdStrBuf::Ref(), and C4NetIOTCP::Send().

Referenced by C4ChatControl::ProcessInput().

365 {
366  if (!fConnected)
367  { SetError("not connected"); return false; }
368  // Create message
369  StdStrBuf Msg;
370  if (szParameters)
371  Msg.Format("%s %s", szCommand, szParameters);
372  else
373  Msg.Ref(szCommand);
374  // Send
375  return C4NetIOTCP::Send(C4NetIOPacket(Msg.getData(), Msg.getLength(), false, PeerAddr));
376 }
bool Send(const C4NetIOPacket &rPacket) override
Definition: C4NetIO.cpp:1310
void SetError(const char *strnError, bool fSockErr=false)
Definition: C4NetIO.cpp:750
void Format(const char *szFmt,...) GNUC_FORMAT_ATTRIBUTE_O
Definition: StdBuf.cpp:174
const char * getData() const
Definition: StdBuf.h:442
void Ref(const char *pnData)
Definition: StdBuf.h:455
size_t getLength() const
Definition: StdBuf.h:445
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Send() [2/3]

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

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

Here is the caller graph for this function:

◆ Send() [3/3]

bool C4NetIOTCP::Send ( const C4NetIOPacket rPacket)
overrideinherited

Definition at line 1310 of file C4NetIO.cpp.

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

Referenced by C4NetIOTCP::Broadcast(), getUserName(), C4AulDebug::OnLog(), and Send().

1311 {
1312  CStdShareLock PeerListLock(&PeerListCSec);
1313  // find peer
1314  Peer *pPeer = GetPeer(rPacket.getAddr());
1315  // not found?
1316  if (!pPeer) return false;
1317  // send
1318  return pPeer->Send(rPacket);
1319 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:469
Peer * GetPeer(const addr_t &addr)
Definition: C4NetIO.cpp:1593
friend class Peer
Definition: C4NetIO.h:455
bool Send(const C4NetIOPacket &rPacket)
Definition: C4NetIO.cpp:1751
const C4NetIO::addr_t & getAddr() const
Definition: C4NetIO.h:316
Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetBroadcast()

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

Implements C4NetIO.

Definition at line 1321 of file C4NetIO.cpp.

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

1322 {
1323  CStdShareLock PeerListLock(&PeerListCSec);
1324  // find peer
1325  Peer *pPeer = GetPeer(addr);
1326  if (!pPeer) return false;
1327  // set flag
1328  pPeer->SetBroadcast(fSet);
1329  return true;
1330 }
CStdCSecEx PeerListCSec
Definition: C4NetIO.h:469
Peer * GetPeer(const addr_t &addr)
Definition: C4NetIO.cpp:1593
friend class Peer
Definition: C4NetIO.h:455
Here is the call graph for this function:

◆ SetCallback() [1/2]

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

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

Here is the caller graph for this function:

◆ SetCallback() [2/2]

void C4NetIOTCP::SetCallback ( CBClass *  pnCallback)
inlineoverrideinherited

Definition at line 504 of file C4NetIO.h.

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

504 { pCB = pnCallback; };
Here is the caller graph for this function:

◆ SetError()

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

Definition at line 750 of file C4NetIO.cpp.

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

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

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

◆ SetNotify()

void C4Network2IRCClient::SetNotify ( class C4InteractiveThread pnNotify)
inline

Definition at line 186 of file C4Network2IRC.h.

Referenced by C4ChatControl::OnConnectBtn().

186 { pNotify = pnNotify; }
Here is the caller graph for this function:

◆ TimerInterval()

virtual uint32_t StdSchedulerProc::TimerInterval ( )
inlinevirtualinherited

Reimplemented in CStdTimerProc.

Definition at line 84 of file StdScheduler.h.

Referenced by SCHAdditions::assignAdditionForProc:.

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

◆ UnBlock()

void C4NetIOTCP::UnBlock ( )
virtualinherited

Definition at line 1343 of file C4NetIO.cpp.

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

Referenced by C4NetIOTCP::AddConnectWait(), and C4NetIOTCP::Peer::Send().

1344 {
1345 #ifdef STDSCHEDULER_USE_EVENTS
1346  // unblock WaitForSingleObject in C4NetIOTCP::Execute manually
1347  // by setting the Event
1348  WSASetEvent(Event);
1349 #else
1350  // write one character to the pipe, this will unblock everything that
1351  // waits for the FD set returned by GetFDs.
1352  char c = 1;
1353  if (write(Pipe[1], &c, 1) == -1)
1354  SetError("write failed");
1355 #endif
1356 }
void SetError(const char *strnError, bool fSockErr=false)
Definition: C4NetIO.cpp:750
int Pipe[2]
Definition: C4NetIO.h:484
Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ Error

StdCopyStrBuf C4NetIO::Error
protectedinherited

Definition at line 282 of file C4NetIO.h.

Referenced by C4NetIO::SetError().

◆ fInit

bool C4NetIOTCP::fInit {false}
protectedinherited

Definition at line 473 of file C4NetIO.h.

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

◆ iListenPort

uint16_t C4NetIOTCP::iListenPort
protectedinherited

Definition at line 476 of file C4NetIO.h.

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

◆ lsock

SOCKET C4NetIOTCP::lsock
protectedinherited

◆ nullptr

struct C4NetIOTCP::ConnectWait C4NetIOTCP::nullptr
protectedinherited

◆ PeerListAddCSec

CStdCSec C4NetIOTCP::PeerListAddCSec
protectedinherited

Definition at line 470 of file C4NetIO.h.

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

◆ PeerListCSec

◆ Pipe

int C4NetIOTCP::Pipe[2]
protectedinherited

◆ pPeerList

◆ TO_INF


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