OpenTTD Source 20241224-master-gf74b0cf984
ClientNetworkStunSocketHandler Class Reference

Class for handling the client side of the STUN connection. More...

#include <network_stun.h>

Inheritance diagram for ClientNetworkStunSocketHandler:
NetworkStunSocketHandler NetworkTCPSocketHandler NetworkSocketHandler

Public Member Functions

NetworkRecvStatus CloseConnection (bool error=true) override
 This will put this socket handler in a close state.
 
void SendReceive ()
 Check whether we received/can send some data from/to the STUN server and when that's the case handle it appropriately.
 
void Connect (const std::string &token, uint8_t family)
 Connect to the STUN server over either IPv4 or IPv6.
 
- Public Member Functions inherited from NetworkStunSocketHandler
 NetworkStunSocketHandler (SOCKET s=INVALID_SOCKET)
 Create a new cs socket handler for a given cs.
 
- Public Member Functions inherited from NetworkTCPSocketHandler
bool IsConnected () const
 Whether this socket is currently bound to a socket.
 
void CloseSocket ()
 Close the actual socket of the connection.
 
virtual void SendPacket (std::unique_ptr< Packet > &&packet)
 This function puts the packet in the send-queue and it is send as soon as possible.
 
SendPacketsState SendPackets (bool closing_down=false)
 Sends all the buffered packets out for this client.
 
virtual std::unique_ptr< PacketReceivePacket ()
 Receives a packet for the given client.
 
bool CanSendReceive ()
 Check whether this socket can send or receive something.
 
bool HasSendQueue ()
 Whether there is something pending in the send queue.
 
 NetworkTCPSocketHandler (SOCKET s=INVALID_SOCKET)
 Construct a socket handler for a TCP connection.
 
- Public Member Functions inherited from NetworkSocketHandler
 NetworkSocketHandler ()
 Create a new unbound socket.
 
virtual ~NetworkSocketHandler ()=default
 Close the socket when destructing the socket handler.
 
void MarkClosed ()
 Mark the connection as closed.
 
bool HasClientQuit () const
 Whether the current client connected to the socket has quit.
 
void Reopen ()
 Reopen the socket so we can send/receive stuff again.
 

Static Public Member Functions

static std::unique_ptr< ClientNetworkStunSocketHandlerStun (const std::string &token, uint8_t family)
 Send a STUN packet to the STUN server.
 

Data Fields

std::shared_ptr< TCPConnecterconnecter {}
 Connecter instance.
 
NetworkAddress local_addr
 Local addresses of the socket.
 
- Data Fields inherited from NetworkTCPSocketHandler
SOCKET sock
 The socket currently connected to.
 
bool writable
 Can we write to this socket?
 

Private Attributes

std::string token
 Token of this STUN handler.
 
uint8_t family = AF_UNSPEC
 Family of this STUN handler.
 
bool sent_result = false
 Did we sent the result of the STUN connection?
 

Additional Inherited Members

- Protected Member Functions inherited from NetworkStunSocketHandler
bool ReceiveInvalidPacket (PacketStunType type)
 Helper for logging receiving invalid packets.
 
virtual bool Receive_SERCLI_STUN (Packet &p)
 Send a STUN request to the STUN server letting the Game Coordinator know what our actually public IP:port is.
 
- Protected Attributes inherited from NetworkSocketHandler
std::unique_ptr< class NetworkEncryptionHandlerreceive_encryption_handler
 The handler for decrypting received packets.
 
std::unique_ptr< class NetworkEncryptionHandlersend_encryption_handler
 The handler for encrypting sent packets.
 

Detailed Description

Class for handling the client side of the STUN connection.

Definition at line 16 of file network_stun.h.

Constructor & Destructor Documentation

◆ ~ClientNetworkStunSocketHandler()

ClientNetworkStunSocketHandler::~ClientNetworkStunSocketHandler ( )
override

Definition at line 118 of file network_stun.cpp.

Member Function Documentation

◆ CloseConnection()

NetworkRecvStatus ClientNetworkStunSocketHandler::CloseConnection ( bool  error = true)
overridevirtual

This will put this socket handler in a close state.

It will not actually close the OS socket; use CloseSocket for this.

Parameters
errorWhether we quit under an error condition or not.
Returns
new status of the connection.

Reimplemented from NetworkTCPSocketHandler.

Definition at line 105 of file network_stun.cpp.

References NetworkTCPSocketHandler::CloseConnection(), connecter, and NETWORK_RECV_STATUS_OKAY.

◆ Connect()

void ClientNetworkStunSocketHandler::Connect ( const std::string &  token,
uint8_t  family 
)

Connect to the STUN server over either IPv4 or IPv6.

Parameters
tokenThe token as received from the Game Coordinator.
familyWhat IP family to use.

Definition at line 73 of file network_stun.cpp.

References connecter, Debug, family, NetworkStunConnectionString(), and token.

◆ SendReceive()

void ClientNetworkStunSocketHandler::SendReceive ( )

Check whether we received/can send some data from/to the STUN server and when that's the case handle it appropriately.

Definition at line 130 of file network_stun.cpp.

References _network_coordinator_client, NetworkTCPSocketHandler::CanSendReceive(), family, NetworkTCPSocketHandler::SendPackets(), sent_result, NetworkTCPSocketHandler::sock, SPS_ALL_SENT, ClientNetworkCoordinatorSocketHandler::StunResult(), and token.

◆ Stun()

std::unique_ptr< ClientNetworkStunSocketHandler > ClientNetworkStunSocketHandler::Stun ( const std::string &  token,
uint8_t  family 
)
static

Send a STUN packet to the STUN server.

Parameters
tokenThe token as received from the Game Coordinator.
familyWhat IP family this STUN request is for.
Returns
The handler for this STUN request.

Definition at line 89 of file network_stun.cpp.

References family, NETWORK_COORDINATOR_VERSION, PACKET_STUN_SERCLI_STUN, and token.

Referenced by ClientNetworkCoordinatorSocketHandler::Receive_GC_STUN_REQUEST().

Field Documentation

◆ connecter

std::shared_ptr<TCPConnecter> ClientNetworkStunSocketHandler::connecter {}

Connecter instance.

Definition at line 23 of file network_stun.h.

Referenced by CloseConnection(), Connect(), NetworkStunConnecter::OnConnect(), and NetworkStunConnecter::OnFailure().

◆ family

uint8_t ClientNetworkStunSocketHandler::family = AF_UNSPEC
private

Family of this STUN handler.

Definition at line 19 of file network_stun.h.

Referenced by Connect(), SendReceive(), and Stun().

◆ local_addr

NetworkAddress ClientNetworkStunSocketHandler::local_addr

Local addresses of the socket.

Definition at line 24 of file network_stun.h.

Referenced by NetworkStunConnecter::OnConnect().

◆ sent_result

bool ClientNetworkStunSocketHandler::sent_result = false
private

Did we sent the result of the STUN connection?

Definition at line 20 of file network_stun.h.

Referenced by SendReceive().

◆ token

std::string ClientNetworkStunSocketHandler::token
private

Token of this STUN handler.

Definition at line 18 of file network_stun.h.

Referenced by Connect(), SendReceive(), and Stun().


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