OpenClonk
C4Network2IOConnection Class Reference

#include <C4Network2IO.h>

Collaboration diagram for C4Network2IOConnection:
[legend]

Classes

struct  PacketLogEntry
 

Public Member Functions

 C4Network2IOConnection ()
 
 ~C4Network2IOConnection ()
 
C4NetIOgetNetClass () const
 
C4Network2IOProtocol getProtocol () const
 
const C4NetIO::addr_tgetPeerAddr () const
 
const C4NetIO::addr_tgetConnectAddr () const
 
uint32_t getID () const
 
uint32_t getRemoteID () const
 
time_t getTimestamp () const
 
const C4ClientCoregetCCore () const
 
CStdCSecgetCCoreCSec ()
 
int getClientID () const
 
bool isHost () const
 
int getPingTime () const
 
int getLag () const
 
int getIRate () const
 
int getORate () const
 
int getPacketLoss () const
 
const char * getPassword () const
 
bool isConnSent () const
 
uint32_t getInPacketCounter () const
 
uint32_t getOutPacketCounter () const
 
bool isConnecting () const
 
bool isOpen () const
 
bool isHalfAccepted () const
 
bool isAccepted () const
 
bool isClosed () const
 
bool isAutoAccepted () const
 
bool isBroadcastTarget () const
 
bool isFailed () const
 
void SetHalfAccepted ()
 
void SetAccepted ()
 
void SetCCore (const C4ClientCore &nCCore)
 
void ResetAutoAccepted ()
 
void SetConnSent ()
 
bool Connect ()
 
void Close ()
 
bool Send (const C4NetIOPacket &rPkt)
 
void SetBroadcastTarget (bool fSet)
 
void DoStatistics (int iInterval, int *pIRateSum, int *pORateSum)
 
void AddRef ()
 
void DelRef ()
 
bool CreatePostMortem (class C4PacketPostMortem *pPkt)
 

Protected Member Functions

void Set (C4NetIO *pnNetClass, C4Network2IOProtocol eProt, const C4NetIO::addr_t &nPeerAddr, const C4NetIO::addr_t &nConnectAddr, C4Network2IOConnStatus nStatus, const char *szPassword, uint32_t iID)
 
void SetSocket (std::unique_ptr< C4NetIOTCP::Socket > socket)
 
void SetRemoteID (uint32_t iRemoteID)
 
void SetPeerAddr (const C4NetIO::addr_t &nPeerAddr)
 
void OnPing ()
 
void SetPingTime (int iPingTime)
 
void SetStatus (C4Network2IOConnStatus nStatus)
 
void SetAutoAccepted ()
 
void OnPacketReceived (uint8_t iPacketType)
 
void ClearPacketLog (uint32_t iStartNumber=~0)
 

Protected Attributes

class C4NetIOpNetClass {nullptr}
 
C4Network2IOProtocol eProt
 
C4NetIO::addr_t PeerAddr
 
C4NetIO::addr_t ConnectAddr
 
std::unique_ptr< C4NetIOTCP::SocketTcpSimOpenSocket
 
C4Network2IOConnStatus Status
 
uint32_t iID
 
uint32_t iRemoteID
 
bool fAutoAccept {false}
 
bool fBroadcastTarget {false}
 
time_t iTimestamp {0}
 
int iPingTime {-1}
 
C4TimeMilliseconds tLastPing
 
C4TimeMilliseconds tLastPong
 
C4ClientCore CCore
 
CStdCSec CCoreCSec
 
int iIRate
 
int iORate
 
int iPacketLoss
 
StdCopyStrBuf Password
 
bool fConnSent {false}
 
bool fPostMortemSent {false}
 
uint32_t iOutPacketCounter {0}
 
uint32_t iInPacketCounter {0}
 
PacketLogEntrypPacketLog {nullptr}
 
CStdCSec PacketLogCSec
 
C4Network2IOConnectionpNext {nullptr}
 
std::atomic_long iRefCnt
 

Friends

class C4Network2IO
 

Detailed Description

Definition at line 214 of file C4Network2IO.h.


Class Documentation

◆ C4Network2IOConnection::PacketLogEntry

struct C4Network2IOConnection::PacketLogEntry

Definition at line 248 of file C4Network2IO.h.

Collaboration diagram for C4Network2IOConnection::PacketLogEntry:
[legend]
Class Members
PacketLogEntry * Next
uint32_t Number
C4NetIOPacket Pkt

Constructor & Destructor Documentation

◆ C4Network2IOConnection()

C4Network2IOConnection::C4Network2IOConnection ( )

Definition at line 1290 of file C4Network2IO.cpp.

◆ ~C4Network2IOConnection()

C4Network2IOConnection::~C4Network2IOConnection ( )

Definition at line 1300 of file C4Network2IO.cpp.

References ClearPacketLog(), Close(), iRefCnt, isClosed(), and pNetClass.

1301 {
1302  assert(!iRefCnt);
1303  // connection needs to be closed?
1304  if (pNetClass && !isClosed()) Close();
1305  // clear the packet log
1306  ClearPacketLog();
1307 }
class C4NetIO * pNetClass
Definition: C4Network2IO.h:224
std::atomic_long iRefCnt
Definition: C4Network2IO.h:261
void ClearPacketLog(uint32_t iStartNumber=~0)
Here is the call graph for this function:

Member Function Documentation

◆ AddRef()

void C4Network2IOConnection::AddRef ( )

Definition at line 1537 of file C4Network2IO.cpp.

References iRefCnt.

Referenced by C4Network2IO::AddConnection(), C4Network2IO::GetDataConnection(), C4Network2IO::GetMsgConnection(), C4Network2IO::HandlePacket(), C4Network2IO::OnDisconn(), C4Network2Client::SetDataConn(), and C4Network2Client::SetMsgConn().

1538 {
1539  ++iRefCnt;
1540 }
std::atomic_long iRefCnt
Definition: C4Network2IO.h:261
Here is the caller graph for this function:

◆ ClearPacketLog()

void C4Network2IOConnection::ClearPacketLog ( uint32_t  iStartNumber = ~0)
protected

Definition at line 1399 of file C4Network2IO.cpp.

References C4Network2IOConnection::PacketLogEntry::Next, and pPacketLog.

Referenced by C4Network2IO::HandlePacket(), and ~C4Network2IOConnection().

1400 {
1401  // Search position of first packet to delete
1402  PacketLogEntry *pPos, *pPrev = nullptr;
1403  for (pPos = pPacketLog; pPos; pPrev = pPos, pPos = pPos->Next)
1404  if (pPos->Number < iUntilID)
1405  break;
1406  if (pPos)
1407  {
1408  // Remove packets from list
1409  (pPrev ? pPrev->Next : pPacketLog) = nullptr;
1410  // Delete everything
1411  while (pPos)
1412  {
1413  PacketLogEntry *pDelete = pPos;
1414  pPos = pPos->Next;
1415  delete pDelete;
1416  }
1417  }
1418 }
PacketLogEntry * pPacketLog
Definition: C4Network2IO.h:254
Here is the caller graph for this function:

◆ Close()

void C4Network2IOConnection::Close ( )

Definition at line 1458 of file C4Network2IO.cpp.

References C4NetIO::Close(), CS_Closed, isClosed(), PeerAddr, pNetClass, and SetStatus().

Referenced by C4Network2Client::CloseConns(), C4Network2::HandleConn(), C4Network2::HandleConnRe(), C4Network2IO::HandlePacket(), C4Network2::HandlePacket(), Send(), C4Network2ClientDlg::UpdateText(), C4Network2Client::~C4Network2Client(), and ~C4Network2IOConnection().

1459 {
1460  if (!pNetClass || isClosed()) return;
1461  // set status
1463  // close
1465 }
C4NetIO::addr_t PeerAddr
Definition: C4Network2IO.h:226
class C4NetIO * pNetClass
Definition: C4Network2IO.h:224
virtual bool Close()=0
void SetStatus(C4Network2IOConnStatus nStatus)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Connect()

bool C4Network2IOConnection::Connect ( )

Definition at line 1446 of file C4Network2IO.cpp.

References C4NetIO::Connect(), C4NetIOTCP::Connect(), ConnectAddr, pNetClass, and TcpSimOpenSocket.

Referenced by C4Network2IO::ConnectWithSocket().

1447 {
1448  if (!pNetClass) return false;
1449  if (TcpSimOpenSocket)
1450  {
1451  auto pNetTCP = dynamic_cast<C4NetIOTCP*>(pNetClass);
1452  return pNetTCP->Connect(ConnectAddr, std::move(TcpSimOpenSocket));
1453  }
1454  // try connect
1455  return pNetClass->Connect(ConnectAddr);
1456 }
class C4NetIO * pNetClass
Definition: C4Network2IO.h:224
bool Connect(const addr_t &addr, std::unique_ptr< Socket > socket)
Definition: C4NetIO.cpp:1207
std::unique_ptr< C4NetIOTCP::Socket > TcpSimOpenSocket
Definition: C4Network2IO.h:227
C4NetIO::addr_t ConnectAddr
Definition: C4Network2IO.h:226
virtual bool Connect(const addr_t &addr)=0
Here is the call graph for this function:
Here is the caller graph for this function:

◆ CreatePostMortem()

bool C4Network2IOConnection::CreatePostMortem ( class C4PacketPostMortem pPkt)

Definition at line 1420 of file C4Network2IO.cpp.

References C4PacketPostMortem::Add(), fPostMortemSent, iOutPacketCounter, iRemoteID, C4Network2IOConnection::PacketLogEntry::Next, PacketLogCSec, pPacketLog, C4PacketPostMortem::SetConnID(), and C4PacketPostMortem::SetPacketCounter().

Referenced by C4Network2::OnDisconn().

1421 {
1422  // Security
1423  if (!pPkt) return false;
1424  CStdLock PacketLogLock(&PacketLogCSec);
1425  // Nothing to do?
1426  if (!pPacketLog) return false;
1427  // Already created?
1428  if (fPostMortemSent) return false;
1429  // Set connection ID and packet counter
1430  pPkt->SetConnID(iRemoteID);
1431  pPkt->SetPacketCounter(iOutPacketCounter);
1432  // Add packets
1433  for (PacketLogEntry *pEntry = pPacketLog; pEntry; pEntry = pEntry->Next)
1434  pPkt->Add(pEntry->Pkt);
1435  // Okay
1436  fPostMortemSent = true;
1437  return true;
1438 }
PacketLogEntry * pPacketLog
Definition: C4Network2IO.h:254
Here is the call graph for this function:
Here is the caller graph for this function:

◆ DelRef()

void C4Network2IOConnection::DelRef ( )

Definition at line 1542 of file C4Network2IO.cpp.

References iRefCnt.

Referenced by C4Network2IO::BroadcastMsg(), C4Network2IO::HandleFwdReq(), C4Network2IO::HandlePacket(), C4Network2IO::OnThreadEvent(), C4Network2Client::RemoveConn(), C4Network2IO::RemoveConnection(), C4Network2Res::SendChunk(), C4Network2IO::SendMsgToClient(), C4Network2Client::SetDataConn(), C4Network2Client::SetMsgConn(), C4Network2Res::StartLoad(), and C4Network2Client::~C4Network2Client().

1543 {
1544  if (--iRefCnt == 0)
1545  delete this;
1546 }
std::atomic_long iRefCnt
Definition: C4Network2IO.h:261
Here is the caller graph for this function:

◆ DoStatistics()

void C4Network2IOConnection::DoStatistics ( int  iInterval,
int *  pIRateSum,
int *  pORateSum 
)

Definition at line 1518 of file C4Network2IO.cpp.

References C4NetIO::GetConnStatistic(), iIRate, iORate, iPacketLoss, isOpen(), PeerAddr, and pNetClass.

1519 {
1520  // get C4NetIO statistics
1521  int inIRate, inORate, inLoss;
1522  if (!isOpen() || !pNetClass->GetConnStatistic(PeerAddr, &inIRate, &inORate, &inLoss))
1523  {
1524  iIRate = iORate = iPacketLoss = 0;
1525  return;
1526  }
1527  // normalize
1528  inIRate = inIRate * 1000 / iInterval;
1529  inORate = inORate * 1000 / iInterval;
1530  // set
1531  iIRate = inIRate; iORate = inORate; iPacketLoss = inLoss;
1532  // sum up
1533  if (pIRateSum) *pIRateSum += iIRate;
1534  if (pORateSum) *pORateSum += iORate;
1535 }
C4NetIO::addr_t PeerAddr
Definition: C4Network2IO.h:226
class C4NetIO * pNetClass
Definition: C4Network2IO.h:224
virtual bool GetConnStatistic(const addr_t &addr, int *pIRate, int *pORate, int *pLoss)=0
Here is the call graph for this function:

◆ getCCore()

const C4ClientCore& C4Network2IOConnection::getCCore ( ) const
inline

Definition at line 271 of file C4Network2IO.h.

References C4Network2IO::AutoAccept::CCore.

271 { return CCore; }

◆ getCCoreCSec()

CStdCSec& C4Network2IOConnection::getCCoreCSec ( )
inline

Definition at line 272 of file C4Network2IO.h.

272 { return CCoreCSec; }

◆ getClientID()

int C4Network2IOConnection::getClientID ( ) const
inline

Definition at line 273 of file C4Network2IO.h.

References C4ClientCore::getID().

Referenced by C4Network2IO::HandleFwdReq(), C4Network2::HandleLobbyPacket(), C4Network2Players::HandlePacket(), C4GameControlNetwork::HandlePacket(), C4Network2::HandlePacket(), C4Network2ResList::HandlePacket(), C4Network2::OnConnectFail(), C4Network2::OnDisconn(), and C4Network2Res::OnStatus().

273 { return CCore.getID(); }
int32_t getID() const
Definition: C4Client.h:57
Here is the call graph for this function:
Here is the caller graph for this function:

◆ getConnectAddr()

const C4NetIO::addr_t& C4Network2IOConnection::getConnectAddr ( ) const
inline

Definition at line 267 of file C4Network2IO.h.

Referenced by C4Network2::HandleConnRe().

267 { return ConnectAddr; }
C4NetIO::addr_t ConnectAddr
Definition: C4Network2IO.h:226
Here is the caller graph for this function:

◆ getID()

uint32_t C4Network2IOConnection::getID ( ) const
inline

Definition at line 268 of file C4Network2IO.h.

268 { return iID; }

◆ getInPacketCounter()

uint32_t C4Network2IOConnection::getInPacketCounter ( ) const
inline

Definition at line 283 of file C4Network2IO.h.

Referenced by C4Network2IO::HandlePacket().

283 { return iInPacketCounter; }
Here is the caller graph for this function:

◆ getIRate()

int C4Network2IOConnection::getIRate ( ) const
inline

Definition at line 277 of file C4Network2IO.h.

277 { return iIRate; }

◆ getLag()

int C4Network2IOConnection::getLag ( ) const

Definition at line 1309 of file C4Network2IO.cpp.

References iPingTime, C4TimeMilliseconds::Now(), tLastPing, and tLastPong.

Referenced by C4Network2Client::DoTCPSimultaneousOpen(), C4Network2Stats::ExecuteSecond(), and C4Network2ClientDlg::UpdateText().

1310 {
1311  if (iPingTime != -1)
1312  {
1313  // Last ping not answered yet?
1314  if(tLastPing > tLastPong)
1315  {
1316  int iPingLag = C4TimeMilliseconds::Now() - tLastPing;
1317  // Use it for lag measurement once it's larger then the last ping time
1318  // (the ping time won't be better than this anyway once the pong's here)
1319  return std::max(iPingLag, iPingTime);
1320  }
1321  }
1322  // Last ping result
1323  return iPingTime;
1324 }
C4TimeMilliseconds tLastPing
Definition: C4Network2IO.h:236
C4TimeMilliseconds tLastPong
Definition: C4Network2IO.h:237
static C4TimeMilliseconds Now()
Here is the call graph for this function:
Here is the caller graph for this function:

◆ getNetClass()

C4NetIO* C4Network2IOConnection::getNetClass ( ) const
inline

Definition at line 264 of file C4Network2IO.h.

Referenced by C4Network2::HandleConnRe(), C4Network2::OnConnect(), C4Network2::OnConnectFail(), C4Network2::OnDisconnect(), and C4Network2ClientDlg::UpdateText().

264 { return pNetClass; }
class C4NetIO * pNetClass
Definition: C4Network2IO.h:224
Here is the caller graph for this function:

◆ getORate()

int C4Network2IOConnection::getORate ( ) const
inline

Definition at line 278 of file C4Network2IO.h.

278 { return iORate; }

◆ getOutPacketCounter()

uint32_t C4Network2IOConnection::getOutPacketCounter ( ) const
inline

Definition at line 284 of file C4Network2IO.h.

Referenced by C4Network2::OnDisconn().

284 { return iOutPacketCounter; }
Here is the caller graph for this function:

◆ getPacketLoss()

int C4Network2IOConnection::getPacketLoss ( ) const
inline

Definition at line 279 of file C4Network2IO.h.

Referenced by C4Network2ClientDlg::UpdateText().

279 { return iPacketLoss; }
Here is the caller graph for this function:

◆ getPassword()

const char* C4Network2IOConnection::getPassword ( ) const
inline

Definition at line 280 of file C4Network2IO.h.

References StdStrBuf::getData().

280 { return Password.getData(); }
const char * getData() const
Definition: StdBuf.h:442
StdCopyStrBuf Password
Definition: C4Network2IO.h:242
Here is the call graph for this function:

◆ getPeerAddr()

const C4NetIO::addr_t& C4Network2IOConnection::getPeerAddr ( ) const
inline

Definition at line 266 of file C4Network2IO.h.

References C4NetIO::EndpointAddress::GetPort().

Referenced by C4Network2IO::doAutoAccept(), C4Network2::HandleConn(), C4Network2::HandleConnRe(), C4Network2IO::HandleFwdReq(), C4Network2ClientList::HandlePacket(), C4Network2IO::HandlePacket(), C4Network2::OnConnect(), C4Network2::OnConnectFail(), C4Network2::OnDisconnect(), C4Network2Client::SendAddresses(), and C4Network2ClientDlg::UpdateText().

266 { return PeerAddr.GetPort() ? PeerAddr : ConnectAddr; }
C4NetIO::addr_t PeerAddr
Definition: C4Network2IO.h:226
C4NetIO::addr_t ConnectAddr
Definition: C4Network2IO.h:226
uint16_t GetPort() const
Definition: C4NetIO.cpp:547
Here is the call graph for this function:
Here is the caller graph for this function:

◆ getPingTime()

int C4Network2IOConnection::getPingTime ( ) const
inline

Definition at line 275 of file C4Network2IO.h.

Referenced by C4GameControlNetwork::CalcPerformance(), C4Network2::HandleActivateReq(), and C4Network2ClientDlg::UpdateText().

275 { return iPingTime; }
Here is the caller graph for this function:

◆ getProtocol()

C4Network2IOProtocol C4Network2IOConnection::getProtocol ( ) const
inline

Definition at line 265 of file C4Network2IO.h.

Referenced by C4Network2Client::DoConnectAttempt(), and C4Network2::HandleConnRe().

265 { return eProt; }
C4Network2IOProtocol eProt
Definition: C4Network2IO.h:225
Here is the caller graph for this function:

◆ getRemoteID()

uint32_t C4Network2IOConnection::getRemoteID ( ) const
inline

Definition at line 269 of file C4Network2IO.h.

269 { return iRemoteID; }

◆ getTimestamp()

time_t C4Network2IOConnection::getTimestamp ( ) const
inline

Definition at line 270 of file C4Network2IO.h.

270 { return iTimestamp; }

◆ isAccepted()

bool C4Network2IOConnection::isAccepted ( ) const
inline

Definition at line 289 of file C4Network2IO.h.

References CS_Accepted.

Referenced by C4Network2::HandleConnRe(), C4Network2IO::HandleFwdReq(), C4GameControlNetwork::HandlePacket(), and C4Network2IO::HandlePacket().

289 { return Status == CS_Accepted; }
C4Network2IOConnStatus Status
Definition: C4Network2IO.h:230
Here is the caller graph for this function:

◆ isAutoAccepted()

bool C4Network2IOConnection::isAutoAccepted ( ) const
inline

Definition at line 291 of file C4Network2IO.h.

Referenced by C4Network2::HandleConnRe(), and C4Network2IO::HandlePacket().

291 { return fAutoAccept; }
Here is the caller graph for this function:

◆ isBroadcastTarget()

bool C4Network2IOConnection::isBroadcastTarget ( ) const
inline

Definition at line 292 of file C4Network2IO.h.

292 { return fBroadcastTarget; }

◆ isClosed()

bool C4Network2IOConnection::isClosed ( ) const
inline

Definition at line 290 of file C4Network2IO.h.

References CS_Closed, and CS_ConnectFail.

Referenced by Close(), C4Network2::HandleConn(), C4GameControlNetwork::HandlePacket(), C4Network2IO::HandlePacket(), C4Network2IO::OnConn(), C4Network2IO::OnDisconn(), C4Network2IO::OnThreadEvent(), SetStatus(), and ~C4Network2IOConnection().

290 { return Status == CS_Closed || Status == CS_ConnectFail; }
C4Network2IOConnStatus Status
Definition: C4Network2IO.h:230
Here is the caller graph for this function:

◆ isConnecting()

bool C4Network2IOConnection::isConnecting ( ) const
inline

Definition at line 286 of file C4Network2IO.h.

References CS_Connect.

Referenced by C4Network2IO::OnDisconn().

286 { return Status == CS_Connect; }
C4Network2IOConnStatus Status
Definition: C4Network2IO.h:230
Here is the caller graph for this function:

◆ isConnSent()

bool C4Network2IOConnection::isConnSent ( ) const
inline

Definition at line 281 of file C4Network2IO.h.

Referenced by C4Network2IO::HandlePacket().

281 { return fConnSent; }
Here is the caller graph for this function:

◆ isFailed()

bool C4Network2IOConnection::isFailed ( ) const
inline

Definition at line 293 of file C4Network2IO.h.

References CS_ConnectFail.

Referenced by C4Network2::OnDisconn().

293 { return Status == CS_ConnectFail; }
C4Network2IOConnStatus Status
Definition: C4Network2IO.h:230
Here is the caller graph for this function:

◆ isHalfAccepted()

bool C4Network2IOConnection::isHalfAccepted ( ) const
inline

Definition at line 288 of file C4Network2IO.h.

References CS_Accepted, and CS_HalfAccepted.

Referenced by C4Network2::HandleConn(), C4Network2IO::HandlePacket(), and C4Network2IO::OnDisconn().

Here is the caller graph for this function:

◆ isHost()

bool C4Network2IOConnection::isHost ( ) const
inline

Definition at line 274 of file C4Network2IO.h.

References C4ClientCore::isHost().

Referenced by C4GameControlNetwork::HandlePacket().

274 { return CCore.isHost(); }
bool isHost() const
Definition: C4Client.h:58
Here is the call graph for this function:
Here is the caller graph for this function:

◆ isOpen()

bool C4Network2IOConnection::isOpen ( ) const
inline

Definition at line 287 of file C4Network2IO.h.

References CS_Closed, CS_Connect, and CS_ConnectFail.

Referenced by C4Network2Client::CloseConns(), DoStatistics(), C4Network2::HandleConnRe(), C4Network2IO::HandlePacket(), C4Network2::HandlePacket(), C4Network2ResList::HandlePacket(), C4Network2IO::OnConn(), and Send().

Here is the caller graph for this function:

◆ OnPacketReceived()

void C4Network2IOConnection::OnPacketReceived ( uint8_t  iPacketType)
protected

Definition at line 1392 of file C4Network2IO.cpp.

References iInPacketCounter, and PID_PacketLogStart.

Referenced by C4Network2IO::OnPacket().

1393 {
1394  // Just count them
1395  if (iPacketType >= PID_PacketLogStart)
1396  iInPacketCounter++;
1397 }
Here is the caller graph for this function:

◆ OnPing()

void C4Network2IOConnection::OnPing ( )
protected

Definition at line 1355 of file C4Network2IO.cpp.

References C4TimeMilliseconds::Now(), tLastPing, and tLastPong.

1356 {
1357  // Still no pong for the last ping?
1358  if (tLastPong < tLastPing)
1359  return;
1360 
1361  // Save time
1363 }
C4TimeMilliseconds tLastPing
Definition: C4Network2IO.h:236
C4TimeMilliseconds tLastPong
Definition: C4Network2IO.h:237
static C4TimeMilliseconds Now()
Here is the call graph for this function:

◆ ResetAutoAccepted()

void C4Network2IOConnection::ResetAutoAccepted ( )
inline

Definition at line 313 of file C4Network2IO.h.

Referenced by C4Network2::HandleConnRe().

313 { fAutoAccept = false; }
Here is the caller graph for this function:

◆ Send()

bool C4Network2IOConnection::Send ( const C4NetIOPacket rPkt)

Definition at line 1467 of file C4Network2IO.cpp.

References Close(), fPostMortemSent, C4NetIO::GetError(), C4NetIOPacket::getStatus(), iOutPacketCounter, isOpen(), LogF(), C4Network2IOConnection::PacketLogEntry::Next, C4Network2IOConnection::PacketLogEntry::Number, PacketLogCSec, PeerAddr, PID_PacketLogStart, C4Network2IOConnection::PacketLogEntry::Pkt, pNetClass, pPacketLog, C4NetIO::ResetError(), C4NetIO::Send(), and C4NetIOPacket::SetAddr().

Referenced by C4Network2Client::CloseConns(), C4Network2::HandleConn(), C4GameControlNetwork::HandleControlReq(), C4Network2IO::HandleFwdReq(), C4Network2IO::HandlePacket(), C4Network2Client::SendAddresses(), C4Network2Res::SendChunk(), C4Network2Client::SendData(), C4Network2ResList::SendDiscover(), C4Network2Client::SendMsg(), C4Network2IO::SendMsgToClient(), C4Network2Res::SendStatus(), and C4Network2Res::StartLoad().

1468 {
1469  // some packets shouldn't go into the log
1470  if (rPkt.getStatus() < PID_PacketLogStart)
1471  {
1472  assert(isOpen());
1473  C4NetIOPacket Copy(rPkt);
1474  Copy.SetAddr(PeerAddr);
1475  return pNetClass->Send(Copy);
1476  }
1477  CStdLock PacketLogLock(&PacketLogCSec);
1478  // create log entry
1479  PacketLogEntry *pLogEntry = new PacketLogEntry();
1480  pLogEntry->Number = iOutPacketCounter++;
1481  pLogEntry->Pkt = rPkt;
1482  pLogEntry->Next = pPacketLog;
1483  pPacketLog = pLogEntry;
1484  // set address
1485  pLogEntry->Pkt.SetAddr(PeerAddr);
1486  // closed? No sweat, post mortem will reroute it later.
1487  if (!isOpen())
1488  {
1489  // post mortem already sent? This shouldn't happen
1490  if (fPostMortemSent) { assert(false); return false; }
1491  // okay then
1492  return true;
1493  }
1494  // send
1495  bool fSuccess = pNetClass->Send(pLogEntry->Pkt);
1496  if (fSuccess)
1497  assert(!fPostMortemSent);
1498  else {
1499  // Not being able to send a packet is actually a big deal,
1500  // as this means that we will have hole in the packet
1501  // order. Better close the connection - post mortem should
1502  // ideally sort everything out from here.
1503  LogF("Network: Fatal: Send failed (%s)", pNetClass->GetError());
1504  pNetClass->ResetError();
1505  Close();
1506  }
1507  return fSuccess;
1508 }
void ResetError()
Definition: C4NetIO.h:286
uint8_t getStatus() const
Definition: C4NetIO.h:318
C4NetIO::addr_t PeerAddr
Definition: C4Network2IO.h:226
class C4NetIO * pNetClass
Definition: C4Network2IO.h:224
virtual bool Send(const class C4NetIOPacket &rPacket)=0
PacketLogEntry * pPacketLog
Definition: C4Network2IO.h:254
virtual const char * GetError() const
Definition: C4NetIO.h:285
bool LogF(const char *strMessage,...)
Definition: C4Log.cpp:260
void SetAddr(const C4NetIO::addr_t &naddr)
Definition: C4NetIO.h:327
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Set()

void C4Network2IOConnection::Set ( C4NetIO pnNetClass,
C4Network2IOProtocol  eProt,
const C4NetIO::addr_t nPeerAddr,
const C4NetIO::addr_t nConnectAddr,
C4Network2IOConnStatus  nStatus,
const char *  szPassword,
uint32_t  iID 
)
protected

Definition at line 1326 of file C4Network2IO.cpp.

References ConnectAddr, eProt, fBroadcastTarget, iID, iPingTime, iTimestamp, Password, PeerAddr, pNetClass, and Status.

Referenced by C4Network2IO::ConnectWithSocket(), and C4Network2IO::OnConn().

1327 {
1328  // save data
1329  pNetClass = pnNetClass; eProt = enProt;
1330  PeerAddr = nPeerAddr; ConnectAddr = nConnectAddr;
1331  Status = nStatus;
1332  Password = szPassword;
1333  iID = inID;
1334  // initialize
1335  fBroadcastTarget = false;
1336  iTimestamp = time(nullptr); iPingTime = -1;
1337 }
C4Network2IOProtocol eProt
Definition: C4Network2IO.h:225
C4NetIO::addr_t PeerAddr
Definition: C4Network2IO.h:226
class C4NetIO * pNetClass
Definition: C4Network2IO.h:224
StdCopyStrBuf Password
Definition: C4Network2IO.h:242
C4Network2IOConnStatus Status
Definition: C4Network2IO.h:230
C4NetIO::addr_t ConnectAddr
Definition: C4Network2IO.h:226
Here is the caller graph for this function:

◆ SetAccepted()

void C4Network2IOConnection::SetAccepted ( )
inline

Definition at line 311 of file C4Network2IO.h.

References CS_Accepted.

Referenced by C4Network2::HandleConnRe(), and C4Network2IO::HandlePacket().

void SetStatus(C4Network2IOConnStatus nStatus)
Here is the caller graph for this function:

◆ SetAutoAccepted()

void C4Network2IOConnection::SetAutoAccepted ( )
protected

Definition at line 1387 of file C4Network2IO.cpp.

References fAutoAccept.

Referenced by C4Network2IO::HandlePacket().

1388 {
1389  fAutoAccept = true;
1390 }
Here is the caller graph for this function:

◆ SetBroadcastTarget()

void C4Network2IOConnection::SetBroadcastTarget ( bool  fSet)

Definition at line 1510 of file C4Network2IO.cpp.

References fBroadcastTarget, PeerAddr, pNetClass, and C4NetIO::SetBroadcast().

Referenced by C4Network2IO::BroadcastMsg(), and C4Network2IO::HandleFwdReq().

1511 {
1512  // Note that each thread will have to make sure that this flag won't be
1513  // changed until Broadcast() is called. See C4Network2IO::BroadcastCSec.
1515  fBroadcastTarget = fSet;
1516 }
C4NetIO::addr_t PeerAddr
Definition: C4Network2IO.h:226
class C4NetIO * pNetClass
Definition: C4Network2IO.h:224
virtual bool SetBroadcast(const addr_t &addr, bool fSet=true)=0
Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetCCore()

void C4Network2IOConnection::SetCCore ( const C4ClientCore nCCore)

Definition at line 1440 of file C4Network2IO.cpp.

References CCore, and CCoreCSec.

Referenced by C4Network2IO::ConnectWithSocket(), C4Network2::HandleConn(), C4Network2::HandleConnRe(), and C4Network2IO::HandlePacket().

1441 {
1442  CStdLock CCoreLock(&CCoreCSec);
1443  CCore = nCCore;
1444 }
Here is the caller graph for this function:

◆ SetConnSent()

void C4Network2IOConnection::SetConnSent ( )
inline

Definition at line 314 of file C4Network2IO.h.

References C4Network2IO::Connect().

314 { fConnSent = true; }
Here is the call graph for this function:

◆ SetHalfAccepted()

void C4Network2IOConnection::SetHalfAccepted ( )
inline

Definition at line 310 of file C4Network2IO.h.

References CS_HalfAccepted.

Referenced by C4Network2::HandleConn().

void SetStatus(C4Network2IOConnStatus nStatus)
Here is the caller graph for this function:

◆ SetPeerAddr()

void C4Network2IOConnection::SetPeerAddr ( const C4NetIO::addr_t nPeerAddr)
protected

Definition at line 1349 of file C4Network2IO.cpp.

References PeerAddr.

Referenced by C4Network2IO::OnConn().

1350 {
1351  // just do it
1352  PeerAddr = nPeerAddr;
1353 }
C4NetIO::addr_t PeerAddr
Definition: C4Network2IO.h:226
Here is the caller graph for this function:

◆ SetPingTime()

void C4Network2IOConnection::SetPingTime ( int  iPingTime)
protected

Definition at line 1365 of file C4Network2IO.cpp.

References iPingTime, C4TimeMilliseconds::Now(), and tLastPong.

Referenced by C4Network2IO::HandlePacket().

1366 {
1367  // save it
1368  iPingTime = inPingTime;
1369  // pong received - save timestamp
1371 }
C4TimeMilliseconds tLastPong
Definition: C4Network2IO.h:237
static C4TimeMilliseconds Now()
Here is the call graph for this function:
Here is the caller graph for this function:

◆ SetRemoteID()

void C4Network2IOConnection::SetRemoteID ( uint32_t  iRemoteID)
protected

Definition at line 1344 of file C4Network2IO.cpp.

References iRemoteID.

Referenced by C4Network2IO::HandlePacket().

1345 {
1346  iRemoteID = inRemoteID;
1347 }
Here is the caller graph for this function:

◆ SetSocket()

void C4Network2IOConnection::SetSocket ( std::unique_ptr< C4NetIOTCP::Socket socket)
protected

Definition at line 1339 of file C4Network2IO.cpp.

References TcpSimOpenSocket.

Referenced by C4Network2IO::ConnectWithSocket().

1340 {
1341  TcpSimOpenSocket = std::move(socket);
1342 }
std::unique_ptr< C4NetIOTCP::Socket > TcpSimOpenSocket
Definition: C4Network2IO.h:227
Here is the caller graph for this function:

◆ SetStatus()

void C4Network2IOConnection::SetStatus ( C4Network2IOConnStatus  nStatus)
protected

Definition at line 1373 of file C4Network2IO.cpp.

References CS_Accepted, CS_Closed, CS_Connect, CS_Connected, isClosed(), iTimestamp, and Status.

Referenced by Close(), C4Network2IO::HandlePacket(), C4Network2IO::OnConn(), and C4Network2IO::OnDisconn().

1374 {
1375  if (nStatus != Status)
1376  {
1377  // Connection can't return from these
1378  assert(!isClosed());
1379  // set status
1380  Status = nStatus;
1381  // reset timestamp for connect/accept/close
1383  iTimestamp = time(nullptr);
1384  }
1385 }
C4Network2IOConnStatus Status
Definition: C4Network2IO.h:230
Here is the call graph for this function:
Here is the caller graph for this function:

Friends And Related Function Documentation

◆ C4Network2IO

friend class C4Network2IO
friend

Definition at line 216 of file C4Network2IO.h.

Member Data Documentation

◆ CCore

C4ClientCore C4Network2IOConnection::CCore
protected

Definition at line 238 of file C4Network2IO.h.

Referenced by SetCCore().

◆ CCoreCSec

CStdCSec C4Network2IOConnection::CCoreCSec
protected

Definition at line 239 of file C4Network2IO.h.

Referenced by SetCCore().

◆ ConnectAddr

C4NetIO::addr_t C4Network2IOConnection::ConnectAddr
protected

Definition at line 226 of file C4Network2IO.h.

Referenced by Connect(), and Set().

◆ eProt

C4Network2IOProtocol C4Network2IOConnection::eProt
protected

Definition at line 225 of file C4Network2IO.h.

Referenced by Set().

◆ fAutoAccept

bool C4Network2IOConnection::fAutoAccept {false}
protected

Definition at line 232 of file C4Network2IO.h.

Referenced by SetAutoAccepted().

◆ fBroadcastTarget

bool C4Network2IOConnection::fBroadcastTarget {false}
protected

Definition at line 233 of file C4Network2IO.h.

Referenced by Set(), and SetBroadcastTarget().

◆ fConnSent

bool C4Network2IOConnection::fConnSent {false}
protected

Definition at line 243 of file C4Network2IO.h.

◆ fPostMortemSent

bool C4Network2IOConnection::fPostMortemSent {false}
protected

Definition at line 244 of file C4Network2IO.h.

Referenced by CreatePostMortem(), and Send().

◆ iID

uint32_t C4Network2IOConnection::iID
protected

Definition at line 231 of file C4Network2IO.h.

Referenced by Set().

◆ iInPacketCounter

uint32_t C4Network2IOConnection::iInPacketCounter {0}
protected

Definition at line 247 of file C4Network2IO.h.

Referenced by OnPacketReceived().

◆ iIRate

int C4Network2IOConnection::iIRate
protected

Definition at line 240 of file C4Network2IO.h.

Referenced by DoStatistics().

◆ iORate

int C4Network2IOConnection::iORate
protected

Definition at line 240 of file C4Network2IO.h.

Referenced by DoStatistics().

◆ iOutPacketCounter

uint32_t C4Network2IOConnection::iOutPacketCounter {0}
protected

Definition at line 247 of file C4Network2IO.h.

Referenced by CreatePostMortem(), and Send().

◆ iPacketLoss

int C4Network2IOConnection::iPacketLoss
protected

Definition at line 241 of file C4Network2IO.h.

Referenced by DoStatistics().

◆ iPingTime

int C4Network2IOConnection::iPingTime {-1}
protected

Definition at line 235 of file C4Network2IO.h.

Referenced by getLag(), Set(), and SetPingTime().

◆ iRefCnt

std::atomic_long C4Network2IOConnection::iRefCnt
protected

Definition at line 261 of file C4Network2IO.h.

Referenced by AddRef(), DelRef(), and ~C4Network2IOConnection().

◆ iRemoteID

uint32_t C4Network2IOConnection::iRemoteID
protected

Definition at line 231 of file C4Network2IO.h.

Referenced by CreatePostMortem(), and SetRemoteID().

◆ iTimestamp

time_t C4Network2IOConnection::iTimestamp {0}
protected

Definition at line 234 of file C4Network2IO.h.

Referenced by Set(), and SetStatus().

◆ PacketLogCSec

CStdCSec C4Network2IOConnection::PacketLogCSec
protected

Definition at line 255 of file C4Network2IO.h.

Referenced by CreatePostMortem(), and Send().

◆ Password

StdCopyStrBuf C4Network2IOConnection::Password
protected

Definition at line 242 of file C4Network2IO.h.

Referenced by Set().

◆ PeerAddr

C4NetIO::addr_t C4Network2IOConnection::PeerAddr
protected

Definition at line 226 of file C4Network2IO.h.

Referenced by Close(), DoStatistics(), Send(), Set(), SetBroadcastTarget(), and SetPeerAddr().

◆ pNetClass

class C4NetIO* C4Network2IOConnection::pNetClass {nullptr}
protected

◆ pNext

◆ pPacketLog

PacketLogEntry* C4Network2IOConnection::pPacketLog {nullptr}
protected

Definition at line 254 of file C4Network2IO.h.

Referenced by ClearPacketLog(), CreatePostMortem(), and Send().

◆ Status

C4Network2IOConnStatus C4Network2IOConnection::Status
protected

Definition at line 230 of file C4Network2IO.h.

Referenced by Set(), and SetStatus().

◆ TcpSimOpenSocket

std::unique_ptr<C4NetIOTCP::Socket> C4Network2IOConnection::TcpSimOpenSocket
protected

Definition at line 227 of file C4Network2IO.h.

Referenced by Connect(), and SetSocket().

◆ tLastPing

C4TimeMilliseconds C4Network2IOConnection::tLastPing
protected

Definition at line 236 of file C4Network2IO.h.

Referenced by getLag(), and OnPing().

◆ tLastPong

C4TimeMilliseconds C4Network2IOConnection::tLastPong
protected

Definition at line 237 of file C4Network2IO.h.

Referenced by getLag(), OnPing(), and SetPingTime().


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