OpenTTD Source: NetworkTCPSocketHandler Class Reference

OpenTTD Source 13.1

Public Member Functions | Data Fields | Private Member Functions | Private Attributes
NetworkTCPSocketHandler Class Reference

Base socket handler for all TCP sockets. More...

`#include <tcp.h>`

Inheritance diagram for NetworkTCPSocketHandler:

## Public Member Functions
bool | IsConnected () const
| Whether this socket is currently bound to a socket. More...
virtual NetworkRecvStatus | CloseConnection (bool error=true)
| This will put this socket handler in a close state. More...
void | CloseSocket ()
| Close the actual socket of the connection. More...
virtual void | SendPacket (Packet *packet)
| This function puts the packet in the send-queue and it is send as soon as possible. More...
SendPacketsState | SendPackets (bool closing_down=false)
| Sends all the buffered packets out for this client. More...
virtual Packet * | ReceivePacket ()
| Receives a packet for the given client. More...
bool | CanSendReceive ()
| Check whether this socket can send or receive something. More...
bool | HasSendQueue ()
| Whether there is something pending in the send queue. More...
| NetworkTCPSocketHandler (SOCKET s=INVALID_SOCKET)
| Construct a socket handler for a TCP connection. More...
Public Member Functions inherited from NetworkSocketHandler
| NetworkSocketHandler ()
| Create a new unbound socket.
virtual | ~NetworkSocketHandler ()
| Close the socket when destructing the socket handler.
void | MarkClosed ()
| Mark the connection as closed. More...
bool | HasClientQuit () const
| Whether the current client connected to the socket has quit. More...
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 ()
| Free all pending and partially received packets.

## Private Attributes
Packet * | packet_queue
| Packets that are awaiting delivery.
Packet * | packet_recv
| Partially received packet.

## Detailed Description

Base socket handler for all TCP sockets.

Definition at line 32 of file tcp.h.

## Constructor & Destructor Documentation

## ◆ NetworkTCPSocketHandler()

NetworkTCPSocketHandler::NetworkTCPSocketHandler | ( | SOCKET | s = `INVALID_SOCKET` | ) |

Construct a socket handler for a TCP connection.

Parameters

  s | The just opened TCP connection.

Definition at line 23 of file tcp.cpp.

## Member Function Documentation

## ◆ CanSendReceive()

bool NetworkTCPSocketHandler::CanSendReceive | ( |  | ) |

Check whether this socket can send or receive something.

Returns
  `true` when there is something to receive.

Note
  Sets writable if more data can be sent.

Definition at line 218 of file tcp.cpp.

References sock, and writable.

Referenced by QueryNetworkGameSocketHandler::Receive(), ClientNetworkGameSocketHandler::Receive(), ClientNetworkStunSocketHandler::SendReceive(), ClientNetworkTurnSocketHandler::SendReceive(), and ClientNetworkContentSocketHandler::SendReceive().

## ◆ CloseConnection()

NetworkRecvStatus NetworkTCPSocketHandler::CloseConnection | ( | bool | error = `true` | ) |
| virtual

This will put this socket handler in a close state.

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

Parameters

  error | Whether we quit under an error condition or not.

Returns
  new status of the connection.

Reimplemented in NetworkGameSocketHandler, NetworkAdminSocketHandler, ClientNetworkContentSocketHandler, ClientNetworkCoordinatorSocketHandler, ClientNetworkTurnSocketHandler, and ClientNetworkStunSocketHandler.

Definition at line 65 of file tcp.cpp.

References EmptyPacketQueue(), NetworkSocketHandler::MarkClosed(), NETWORK_RECV_STATUS_OKAY, and writable.

Referenced by ClientNetworkStunSocketHandler::CloseConnection(), ClientNetworkTurnSocketHandler::CloseConnection(), ClientNetworkCoordinatorSocketHandler::CloseConnection(), ClientNetworkContentSocketHandler::CloseConnection(), ReceivePacket(), and SendPackets().

## ◆ CloseSocket()

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 53 of file tcp.cpp.

References sock.

Referenced by ClientNetworkCoordinatorSocketHandler::CloseConnection(), ClientNetworkContentSocketHandler::CloseConnection(), and NetworkContentSocketHandler::~NetworkContentSocketHandler().

## ◆ HasSendQueue()

bool NetworkTCPSocketHandler::HasSendQueue | ( |  | ) |
| inline

Whether there is something pending in the send queue.

Returns
  true when something is pending in the send queue.

Definition at line 62 of file tcp.h.

## ◆ IsConnected()

bool NetworkTCPSocketHandler::IsConnected | ( |  | ) | const
| inline

Whether this socket is currently bound to a socket.

Returns
  true when the socket is bound, false otherwise

Definition at line 46 of file tcp.h.

Referenced by ReceivePacket(), and SendPackets().

## ◆ ReceivePacket()

Packet * NetworkTCPSocketHandler::ReceivePacket | ( |  | ) |
| virtual

Receives a packet for the given client.

Returns
  The received packet (or nullptr when it didn't receive one)

Reimplemented in ServerNetworkGameSocketHandler.

Definition at line 144 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(), NetworkTurnSocketHandler::ReceivePackets(), NetworkContentSocketHandler::ReceivePackets(), NetworkCoordinatorSocketHandler::ReceivePackets(), NetworkAdminSocketHandler::ReceivePackets(), and NetworkGameSocketHandler::ReceivePackets().

## ◆ SendPacket()

void NetworkTCPSocketHandler::SendPacket | ( | Packet * | packet | ) |
| 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)

Parameters

  packet | the packet to send

Definition at line 81 of file tcp.cpp.

References Packet::AddToQueue(), packet_queue, and Packet::PrepareToSend().

Referenced by ClientNetworkCoordinatorSocketHandler::ConnectFailure(), ClientNetworkCoordinatorSocketHandler::ConnectSuccess(), ClientNetworkCoordinatorSocketHandler::ConnectToServer(), ClientNetworkCoordinatorSocketHandler::Register(), ClientNetworkContentSocketHandler::RequestContentList(), ClientNetworkGameSocketHandler::SendAck(), ServerNetworkAdminSocketHandler::SendChat(), ClientNetworkGameSocketHandler::SendChat(), ServerNetworkGameSocketHandler::SendChat(), ServerNetworkAdminSocketHandler::SendClientError(), ServerNetworkAdminSocketHandler::SendClientInfo(), ServerNetworkGameSocketHandler::SendClientInfo(), ServerNetworkAdminSocketHandler::SendClientJoin(), ServerNetworkAdminSocketHandler::SendClientQuit(), ServerNetworkAdminSocketHandler::SendClientUpdate(), ServerNetworkAdminSocketHandler::SendCmdLogging(), ServerNetworkAdminSocketHandler::SendCmdNames(), ClientNetworkGameSocketHandler::SendCommand(), ServerNetworkGameSocketHandler::SendCommand(), ServerNetworkAdminSocketHandler::SendCompanyNew(), ClientNetworkGameSocketHandler::SendCompanyPassword(), ServerNetworkAdminSocketHandler::SendCompanyRemove(), ServerNetworkGameSocketHandler::SendCompanyUpdate(), ServerNetworkGameSocketHandler::SendConfigUpdate(), ServerNetworkAdminSocketHandler::SendConsole(), ServerNetworkAdminSocketHandler::SendDate(), ServerNetworkAdminSocketHandler::SendError(), ClientNetworkGameSocketHandler::SendError(), ServerNetworkGameSocketHandler::SendError(), ServerNetworkGameSocketHandler::SendErrorQuit(), ServerNetworkGameSocketHandler::SendExternalChat(), QueryNetworkGameSocketHandler::SendGameInfo(), ServerNetworkGameSocketHandler::SendGameInfo(), ClientNetworkGameSocketHandler::SendGamePassword(), ServerNetworkAdminSocketHandler::SendGameScript(), ClientNetworkGameSocketHandler::SendGetMap(), ServerNetworkGameSocketHandler::SendJoin(), ClientNetworkGameSocketHandler::SendMapOk(), ServerNetworkGameSocketHandler::SendMove(), ClientNetworkGameSocketHandler::SendMove(), ServerNetworkGameSocketHandler::SendNeedCompanyPassword(), ServerNetworkGameSocketHandler::SendNeedGamePassword(), ServerNetworkAdminSocketHandler::SendNewGame(), ServerNetworkGameSocketHandler::SendNewGame(), ServerNetworkGameSocketHandler::SendNewGRFCheck(), ClientNetworkGameSocketHandler::SendNewGRFsOk(), ServerNetworkAdminSocketHandler::SendPong(), ServerNetworkAdminSocketHandler::SendProtocol(), ClientNetworkGameSocketHandler::SendQuit(), ServerNetworkGameSocketHandler::SendQuit(), ClientNetworkGameSocketHandler::SendRCon(), ServerNetworkAdminSocketHandler::SendRconEnd(), ServerNetworkGameSocketHandler::SendRConResult(), ClientNetworkCoordinatorSocketHandler::SendServerUpdate(), ClientNetworkGameSocketHandler::SendSetName(), ClientNetworkGameSocketHandler::SendSetPassword(), ServerNetworkAdminSocketHandler::SendShutdown(), ServerNetworkGameSocketHandler::SendShutdown(), ServerNetworkGameSocketHandler::SendSync(), ServerNetworkGameSocketHandler::SendWait(), ServerNetworkGameSocketHandler::SendWelcome(), ServerNetworkAdminSocketHandler::SendWelcome(), and PacketWriter::TransferToNetworkQueue().

## ◆ SendPackets()

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

Parameters

  closing_down | Whether we are closing down the connection.

Returns
  `true` if a (part of a) packet could be sent and the connection is not closed yet.

Definition at line 99 of file tcp.cpp.

References NetworkError::AsString(), CloseConnection(), Debug, NetworkError::GetLast(), IsConnected(), packet_queue, Packet::PopFromQueue(), Packet::RemainingBytesToTransfer(), sock, SPS_ALL_SENT, SPS_CLOSED, SPS_NONE_SENT, SPS_PARTLY_SENT, Packet::TransferOut(), NetworkError::WouldBlock(), and writable.

Referenced by ClientNetworkGameSocketHandler::CloseConnection(), QueryNetworkGameSocketHandler::Send(), ClientNetworkGameSocketHandler::Send(), ClientNetworkStunSocketHandler::SendReceive(), ClientNetworkTurnSocketHandler::SendReceive(), and ClientNetworkContentSocketHandler::SendReceive().

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

- src/network/core/tcp.h
- src/network/core/tcp.cpp