OpenTTD Source
12.0-beta2
|
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()
55 if (this->
sock != INVALID_SOCKET) closesocket(this->
sock);
56 this->
sock = INVALID_SOCKET;
83 assert(packet !=
nullptr);
220 fd_set read_fd, write_fd;
226 FD_SET(this->
sock, &read_fd);
227 FD_SET(this->
sock, &write_fd);
229 tv.tv_sec = tv.tv_usec = 0;
230 if (select(FD_SETSIZE, &read_fd, &write_fd,
nullptr, &tv) < 0)
return false;
233 return FD_ISSET(this->
sock, &read_fd) != 0;
void PrepareToSend()
Writes the packet size from the raw packet from packet->size.
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.
static void AddToQueue(Packet **queue, Packet *packet)
Add the given Packet to the end of the queue of packets.
virtual Packet * ReceivePacket()
Receives a packet for the given client.
static Packet * PopFromQueue(Packet **queue)
Pop the packet from the begin of the queue and set the begin of the queue to the second element in th...
Packet * packet_recv
Partially received packet.
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.
static NetworkError GetLast()
Get the last network error.
void MarkClosed()
Mark the connection as closed.
virtual void SendPacket(Packet *packet)
This function puts the packet in the send-queue and it is send as soon as possible.
virtual NetworkRecvStatus CloseConnection(bool error=true)
This will put this socket handler in a close state.
ssize_t TransferIn(F transfer_function, S source, Args &&... args)
Transfer data from the given function into the packet.
static const uint16 TCP_MTU
Number of bytes we can pack in a single TCP packet.
Internal entity of a packet.
NetworkTCPSocketHandler(SOCKET s=INVALID_SOCKET)
Construct a socket handler for a TCP connection.
void EmptyPacketQueue()
Free all pending and partially received packets.
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...
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
#define Debug(name, level, format_string,...)
Ouptut a line of debugging information.
@ 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.
Packet * packet_queue
Packets that are awaiting delivery.