OpenTTD Source
20240919-master-gdf0233f4c2
|
Go to the documentation of this file.
12 #include "../../stdafx.h"
13 #include "../../debug.h"
17 #include "../../safeguards.h"
25 sock(s), writable(false)
29 NetworkTCPSocketHandler::~NetworkTCPSocketHandler()
41 if (this->
sock != INVALID_SOCKET) closesocket(this->
sock);
42 this->
sock = INVALID_SOCKET;
70 assert(packet !=
nullptr);
72 packet->PrepareToSend();
192 Debug(net, 0,
"Invalid packet received (too small / decryption error)");
206 assert(this->
sock != INVALID_SOCKET);
208 fd_set read_fd, write_fd;
214 FD_SET(this->
sock, &read_fd);
215 FD_SET(this->
sock, &write_fd);
217 tv.tv_sec = tv.tv_usec = 0;
218 if (select(FD_SETSIZE, &read_fd, &write_fd,
nullptr, &tv) < 0)
return false;
221 return FD_ISSET(this->
sock, &read_fd) != 0;
bool PrepareToRead()
Prepares the packet so it can be read.
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.
SocketHandler for all network sockets in OpenTTD.
@ SPS_CLOSED
The connection got closed.
bool IsConnected() const
Whether this socket is currently bound to a socket.
bool HasPacketSizeData() const
Check whether the packet, given the position of the "write" pointer, has read enough of the packet to...
SOCKET sock
The socket currently connected to.
ssize_t TransferOut(F transfer_function, D destination, Args &&... args)
Transfer data from the packet to the given function.
bool ParsePacketSize()
Reads the packet size from the raw packet and stores it in the packet->size.
@ SPS_ALL_SENT
All packets in the queue are sent.
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
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.
SendPacketsState
The states of sending the packets.
bool WouldBlock() const
Check whether this error describes that the operation would block.
static NetworkError GetLast()
Get the last network error.
std::unique_ptr< Packet > packet_recv
Partially received packet.
void MarkClosed()
Mark the connection as closed.
virtual NetworkRecvStatus CloseConnection(bool error=true)
This will put this socket handler in a close state.
static const size_t TCP_MTU
Number of bytes we can pack in a single TCP packet.
ssize_t TransferIn(F transfer_function, S source, Args &&... args)
Transfer data from the given function into the packet.
Internal entity of a packet.
NetworkTCPSocketHandler(SOCKET s=INVALID_SOCKET)
Construct a socket handler for a TCP connection.
bool writable
Can we write to this socket?
@ SPS_NONE_SENT
The buffer is still full, so no (parts of) packets could be sent.
SendPacketsState SendPackets(bool closing_down=false)
Sends all the buffered packets out for this client.
bool CanSendReceive()
Check whether this socket can send or receive something.
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
bool IsConnectionReset() const
Check whether this error describes a connection reset.
const std::string & AsString() const
Get the string representation of the error message.
Abstraction of a network error where all implementation details of the error codes are encapsulated i...
virtual std::unique_ptr< Packet > ReceivePacket()
Receives a packet for the given client.
@ SPS_PARTLY_SENT
The packets are partly sent; there are more packets to be sent in the queue.
@ NETWORK_RECV_STATUS_OKAY
Everything is okay.
size_t RemainingBytesToTransfer() const
Get the amount of bytes that are still available for the Transfer functions.
void CloseSocket()
Close the actual socket of the connection.