Go to the documentation of this file.
12 #include "../../stdafx.h"
13 #include "../../debug.h"
17 #include "../../safeguards.h"
25 packet_queue(nullptr), packet_recv(nullptr),
26 sock(s), writable(false)
30 NetworkTCPSocketHandler::~NetworkTCPSocketHandler()
34 if (this->
sock != INVALID_SOCKET) closesocket(this->
sock);
35 this->
sock = INVALID_SOCKET;
64 assert(packet !=
nullptr);
80 while (p->
next !=
nullptr) p = p->
next;
105 while (p !=
nullptr) {
227 fd_set read_fd, write_fd;
233 FD_SET(this->
sock, &read_fd);
234 FD_SET(this->
sock, &write_fd);
236 tv.tv_sec = tv.tv_usec = 0;
237 if (select(FD_SETSIZE, &read_fd, &write_fd,
nullptr, &tv) < 0)
return false;
240 return FD_ISSET(this->
sock, &read_fd) != 0;
void PrepareToSend()
Writes the packet size from the raw packet from packet->size.
byte * buffer
The buffer of this packet, of basically variable length up to SEND_MTU.
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.
SOCKET sock
The socket currently connected to.
virtual NetworkRecvStatus CloseConnection(bool error=true)
Close the current connection; for TCP this will be mostly equivalent to Close(), but for UDP it just ...
@ SPS_ALL_SENT
All packets in the queue are sent.
virtual Packet * ReceivePacket()
Receives a packet for the given client.
Packet * packet_recv
Partially received packet.
#define DEBUG(name, level,...)
Output a line of debugging information.
SendPacketsState
The states of sending the packets.
void PrepareToRead()
Prepares the packet so it can be read.
bool WouldBlock() const
Check whether this error describes that the operation would block.
NetworkRecvStatus CloseConnection(bool error=true) override
Close the current connection; for TCP this will be mostly equivalent to Close(), but for UDP it just ...
PacketSize size
The size of the whole packet for received packets.
static NetworkError GetLast()
Get the last network error.
static const uint16 SEND_MTU
Number of bytes we can pack in a single packet.
virtual void SendPacket(Packet *packet)
This function puts the packet in the send-queue and it is send as soon as possible.
Packet * next
The next packet.
void ReadRawPacketSize()
Reads the packet size from the raw packet and stores it in the packet->size.
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.
uint16 PacketSize
Size of the whole packet.
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
bool IsConnectionReset() const
Check whether this error describes a connection reset.
Abstraction of a network error where all implementation details of the error codes are encapsulated i...
static T * ReallocT(T *t_ptr, size_t num_elements)
Simplified reallocation function that allocates the specified number of elements of the given type.
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
@ 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.
const char * AsString() const
Get the string representation of the error message.
Packet * packet_queue
Packets that are awaiting delivery.
PacketSize pos
The current read/write position in the packet.