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