OpenTTD Source 20241224-master-gee860a5c8e
|
Base socket handler for all TCP sockets. More...
#include <tcp.h>
Public Member Functions | |
bool | IsConnected () const |
Whether this socket is currently bound to a socket. | |
virtual NetworkRecvStatus | CloseConnection (bool error=true) |
This will put this socket handler in a close state. | |
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< Packet > | ReceivePacket () |
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. | |
Data Fields | |
SOCKET | sock |
The socket currently connected to. | |
bool | writable |
Can we write to this socket? | |
Private Member Functions | |
void | EmptyPacketQueue () |
Private Attributes | |
std::deque< std::unique_ptr< Packet > > | packet_queue |
Packets that are awaiting delivery. Cannot be std::queue as that does not have a clear() function. | |
std::unique_ptr< Packet > | packet_recv |
Partially received packet. | |
Additional Inherited Members | |
Protected Attributes inherited from NetworkSocketHandler | |
std::unique_ptr< class NetworkEncryptionHandler > | receive_encryption_handler |
The handler for decrypting received packets. | |
std::unique_ptr< class NetworkEncryptionHandler > | send_encryption_handler |
The handler for encrypting sent packets. | |
NetworkTCPSocketHandler::NetworkTCPSocketHandler | ( | SOCKET | s = INVALID_SOCKET | ) |
bool NetworkTCPSocketHandler::CanSendReceive | ( | ) |
Check whether this socket can send or receive something.
true
when there is something to receive. Definition at line 204 of file tcp.cpp.
References sock, and writable.
Referenced by ClientNetworkGameSocketHandler::Receive(), QueryNetworkGameSocketHandler::Receive(), ClientNetworkContentSocketHandler::SendReceive(), ClientNetworkCoordinatorSocketHandler::SendReceive(), ClientNetworkStunSocketHandler::SendReceive(), and ClientNetworkTurnSocketHandler::SendReceive().
|
virtual |
This will put this socket handler in a close state.
It will not actually close the OS socket; use CloseSocket for this.
error | Whether we quit under an error condition or not. |
Reimplemented in NetworkAdminSocketHandler, NetworkGameSocketHandler, ClientNetworkContentSocketHandler, ClientNetworkCoordinatorSocketHandler, ClientNetworkStunSocketHandler, and ClientNetworkTurnSocketHandler.
Definition at line 51 of file tcp.cpp.
References NetworkSocketHandler::MarkClosed(), NETWORK_RECV_STATUS_OKAY, packet_queue, packet_recv, and writable.
Referenced by ClientNetworkContentSocketHandler::CloseConnection(), ClientNetworkCoordinatorSocketHandler::CloseConnection(), ClientNetworkStunSocketHandler::CloseConnection(), ClientNetworkTurnSocketHandler::CloseConnection(), ReceivePacket(), and SendPackets().
void NetworkTCPSocketHandler::CloseSocket | ( | ) |
Close the actual socket of the connection.
Please make sure CloseConnection is called before CloseSocket, as otherwise not all resources might be released.
Definition at line 39 of file tcp.cpp.
References sock.
Referenced by ClientNetworkContentSocketHandler::CloseConnection(), ClientNetworkCoordinatorSocketHandler::CloseConnection(), and NetworkContentSocketHandler::~NetworkContentSocketHandler().
|
inline |
|
inline |
Whether this socket is currently bound to a socket.
Definition at line 45 of file tcp.h.
Referenced by ReceivePacket(), and SendPackets().
|
virtual |
Receives a packet for the given client.
Reimplemented in ServerNetworkGameSocketHandler.
Definition at line 129 of file tcp.cpp.
References NetworkError::AsString(), CloseConnection(), Debug, NetworkError::GetLast(), Packet::HasPacketSizeData(), IsConnected(), NetworkError::IsConnectionReset(), packet_recv, Packet::ParsePacketSize(), Packet::PrepareToRead(), Packet::RemainingBytesToTransfer(), sock, TCP_MTU, Packet::TransferIn(), and NetworkError::WouldBlock().
Referenced by ServerNetworkGameSocketHandler::ReceivePacket(), NetworkAdminSocketHandler::ReceivePackets(), NetworkContentSocketHandler::ReceivePackets(), NetworkCoordinatorSocketHandler::ReceivePackets(), NetworkGameSocketHandler::ReceivePackets(), and NetworkTurnSocketHandler::ReceivePackets().
|
virtual |
This function puts the packet in the send-queue and it is send as soon as possible.
This is the next tick, or maybe one tick later if the OS-network-buffer is full)
packet | the packet to send |
Definition at line 68 of file tcp.cpp.
References packet_queue.
Referenced by ClientNetworkCoordinatorSocketHandler::ConnectFailure(), ClientNetworkCoordinatorSocketHandler::ConnectSuccess(), ClientNetworkCoordinatorSocketHandler::ConnectToServer(), ClientNetworkContentSocketHandler::DownloadSelectedContentFallback(), ClientNetworkCoordinatorSocketHandler::GetListing(), ClientNetworkCoordinatorSocketHandler::Register(), ClientNetworkContentSocketHandler::RequestContentList(), ClientNetworkContentSocketHandler::RequestContentList(), ClientNetworkContentSocketHandler::RequestContentList(), ClientNetworkGameSocketHandler::SendAck(), ServerNetworkGameSocketHandler::SendAuthRequest(), ClientNetworkGameSocketHandler::SendAuthResponse(), ServerNetworkGameSocketHandler::SendChat(), ServerNetworkAdminSocketHandler::SendChat(), ClientNetworkGameSocketHandler::SendChat(), ServerNetworkAdminSocketHandler::SendClientError(), ServerNetworkAdminSocketHandler::SendClientInfo(), ServerNetworkGameSocketHandler::SendClientInfo(), ServerNetworkAdminSocketHandler::SendClientJoin(), ServerNetworkAdminSocketHandler::SendClientQuit(), ServerNetworkAdminSocketHandler::SendClientUpdate(), ServerNetworkAdminSocketHandler::SendCmdLogging(), ServerNetworkAdminSocketHandler::SendCmdNames(), ClientNetworkGameSocketHandler::SendCommand(), ServerNetworkGameSocketHandler::SendCommand(), ServerNetworkAdminSocketHandler::SendCompanyEconomy(), ServerNetworkAdminSocketHandler::SendCompanyInfo(), ServerNetworkAdminSocketHandler::SendCompanyNew(), ServerNetworkAdminSocketHandler::SendCompanyRemove(), ServerNetworkAdminSocketHandler::SendCompanyStats(), ServerNetworkAdminSocketHandler::SendCompanyUpdate(), ServerNetworkGameSocketHandler::SendConfigUpdate(), ServerNetworkAdminSocketHandler::SendConsole(), ServerNetworkAdminSocketHandler::SendDate(), ServerNetworkGameSocketHandler::SendEnableEncryption(), ServerNetworkAdminSocketHandler::SendError(), ServerNetworkGameSocketHandler::SendError(), ClientNetworkGameSocketHandler::SendError(), ServerNetworkGameSocketHandler::SendErrorQuit(), ServerNetworkGameSocketHandler::SendExternalChat(), ServerNetworkGameSocketHandler::SendFrame(), QueryNetworkGameSocketHandler::SendGameInfo(), ServerNetworkGameSocketHandler::SendGameInfo(), ServerNetworkAdminSocketHandler::SendGameScript(), ClientNetworkGameSocketHandler::SendGetMap(), ClientNetworkGameSocketHandler::SendJoin(), ServerNetworkGameSocketHandler::SendJoin(), ServerNetworkGameSocketHandler::SendMap(), ClientNetworkGameSocketHandler::SendMapOk(), ServerNetworkGameSocketHandler::SendMove(), ClientNetworkGameSocketHandler::SendMove(), ServerNetworkAdminSocketHandler::SendNewGame(), ServerNetworkGameSocketHandler::SendNewGame(), ServerNetworkGameSocketHandler::SendNewGRFCheck(), ClientNetworkGameSocketHandler::SendNewGRFsOk(), ServerNetworkAdminSocketHandler::SendPong(), ServerNetworkAdminSocketHandler::SendProtocol(), ClientNetworkGameSocketHandler::SendQuit(), ServerNetworkGameSocketHandler::SendQuit(), ClientNetworkGameSocketHandler::SendRCon(), ServerNetworkAdminSocketHandler::SendRcon(), ServerNetworkAdminSocketHandler::SendRconEnd(), ServerNetworkGameSocketHandler::SendRConResult(), ClientNetworkCoordinatorSocketHandler::SendServerUpdate(), ClientNetworkGameSocketHandler::SendSetName(), ServerNetworkAdminSocketHandler::SendShutdown(), ServerNetworkGameSocketHandler::SendShutdown(), ServerNetworkGameSocketHandler::SendSync(), ServerNetworkGameSocketHandler::SendWait(), ServerNetworkAdminSocketHandler::SendWelcome(), ServerNetworkGameSocketHandler::SendWelcome(), ClientNetworkCoordinatorSocketHandler::StunResult(), and PacketWriter::TransferToNetworkQueue().
SendPacketsState NetworkTCPSocketHandler::SendPackets | ( | bool | closing_down = false | ) |
Sends all the buffered packets out for this client.
It stops when: 1) all packets are send (queue is empty) 2) the OS reports back that it can not send any more data right now (full network-buffer, it happens ;)) 3) sending took too long
closing_down | Whether we are closing down the connection. |
true
if a (part of a) packet could be sent and the connection is not closed yet. Definition at line 86 of file tcp.cpp.
References NetworkError::AsString(), CloseConnection(), Debug, NetworkError::GetLast(), IsConnected(), packet_queue, Packet::RemainingBytesToTransfer(), sock, SPS_ALL_SENT, SPS_CLOSED, SPS_NONE_SENT, SPS_PARTLY_SENT, Packet::TransferOut(), NetworkError::WouldBlock(), and writable.
Referenced by ClientNetworkGameSocketHandler::CloseConnection(), ServerNetworkGameSocketHandler::CloseConnection(), ClientNetworkGameSocketHandler::Send(), QueryNetworkGameSocketHandler::Send(), ClientNetworkContentSocketHandler::SendReceive(), ClientNetworkCoordinatorSocketHandler::SendReceive(), ClientNetworkStunSocketHandler::SendReceive(), and ClientNetworkTurnSocketHandler::SendReceive().
|
private |
Packets that are awaiting delivery. Cannot be std::queue as that does not have a clear() function.
Definition at line 33 of file tcp.h.
Referenced by CloseConnection(), SendPacket(), and SendPackets().
|
private |
Partially received packet.
Definition at line 34 of file tcp.h.
Referenced by CloseConnection(), and ReceivePacket().
SOCKET NetworkTCPSocketHandler::sock |
The socket currently connected to.
Definition at line 38 of file tcp.h.
Referenced by CanSendReceive(), ClientNetworkContentSocketHandler::CloseConnection(), ClientNetworkGameSocketHandler::CloseConnection(), QueryNetworkGameSocketHandler::CloseConnection(), ServerNetworkGameSocketHandler::CloseConnection(), CloseSocket(), ClientNetworkContentSocketHandler::Connect(), ClientNetworkCoordinatorSocketHandler::ConnectSuccess(), NetworkAdminSocketHandler::NetworkAdminSocketHandler(), NetworkGameSocketHandler::NetworkGameSocketHandler(), NetworkContentConnecter::OnConnect(), NetworkCoordinatorConnecter::OnConnect(), NetworkStunConnecter::OnConnect(), NetworkTurnConnecter::OnConnect(), ClientNetworkTurnSocketHandler::Receive_TURN_CONNECTED(), ReceivePacket(), SendPackets(), ClientNetworkContentSocketHandler::SendReceive(), ClientNetworkCoordinatorSocketHandler::SendReceive(), ClientNetworkStunSocketHandler::SendReceive(), and ClientNetworkTurnSocketHandler::SendReceive().
bool NetworkTCPSocketHandler::writable |
Can we write to this socket?
Definition at line 39 of file tcp.h.
Referenced by CanSendReceive(), CloseConnection(), and SendPackets().