Exemple #1
0
void C4Network2ClientList::HandlePacket(char cStatus,
                                        const C4PacketBase *pBasePkt,
                                        C4Network2IOConnection *pConn) {
  // find associated client
  C4Network2Client *pClient = GetClient(pConn);
  if (!pClient) return;

#define GETPKT(type, name) \
  assert(pBasePkt);        \
  const type &name = /*dynamic_cast*/ static_cast<const type &>(*pBasePkt);

  switch (cStatus) {
    case PID_Addr:  // address propagation
    {
      GETPKT(C4PacketAddr, rPkt)
      // find client
      pClient = GetClientByID(rPkt.getClientID());
      if (pClient) {
        C4Network2Address addr = rPkt.getAddr();
        // IP zero? Set to IP from where the packet came
        if (addr.isIPNull()) addr.SetIP(pConn->getPeerAddr().sin_addr);
        // add (no announce)
        if (pClient->AddAddr(addr, true))
          // new address? Try to connect
          pClient->DoConnectAttempt(pIO);
      }
    } break;
  }

#undef GETPKT
}
bool C4Network2Client::DoTCPSimultaneousOpen(class C4Network2IO *pIO, const C4Network2Address &addr)
{
	if (!pIO->getNetIO(P_TCP)) return false;

	// Did we already bind a socket?
	if (TcpSimOpenSocket)
	{
		LogSilentF("Network: connecting client %s on %s with TCP simultaneous open...", getName(), addr.getAddr().ToString().getData());
		return pIO->ConnectWithSocket(addr.getAddr(), addr.getProtocol(), pClient->getCore(), std::move(TcpSimOpenSocket));
	}
	else
	{
		// No - bind one, inform peer, and schedule a connection attempt.
		auto NetIOTCP = dynamic_cast<C4NetIOTCP*>(pIO->getNetIO(P_TCP));
		auto bindAddr = pParent->GetLocal()->IPv6AddrFromPuncher;
		// We need to know an address that works.
		if (bindAddr.IsNull()) return false;
		bindAddr.SetPort(0);
		TcpSimOpenSocket = NetIOTCP->Bind(bindAddr);
		auto boundAddr = TcpSimOpenSocket->GetAddress();
		LogSilentF("Network: %s TCP simultaneous open request for client %s from %s...", addr.isIPNull() ? "initiating" : "responding to",
				getName(), boundAddr.ToString().getData());
		// Send address we bound to to the client.
		if (!SendMsg(MkC4NetIOPacket(PID_TCPSimOpen, C4PacketTCPSimOpen(pParent->GetLocal()->getID(), C4Network2Address(boundAddr, P_TCP)))))
			return false;
		if (!addr.isIPNull())
		{
			// We need to delay the connection attempt a bit. Unfortunately,
			// waiting for the next tick would usually take way too much time.
			// Instead, we block the main thread for a very short time and hope
			// that noone notices...
			int ping = getMsgConn()->getLag();
			std::this_thread::sleep_for(std::chrono::milliseconds(std::min(ping / 2, 10)));
			DoTCPSimultaneousOpen(pIO, addr);
		}
		return true;
	}
}