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) {
108 int err = GET_LAST_ERROR();
109 if (err != EWOULDBLOCK) {
112 DEBUG(net, 0,
"send failed with error %d", err);
163 int err = GET_LAST_ERROR();
164 if (err != EWOULDBLOCK) {
166 if (err != 104)
DEBUG(net, 0,
"recv failed with error %d", err);
194 int err = GET_LAST_ERROR();
195 if (err != EWOULDBLOCK) {
197 if (err != 104)
DEBUG(net, 0,
"recv failed with error %d", err);
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;
SOCKET sock
The socket currently connected to.
Internal entity of a packet.
The connection got closed.
PacketSize pos
The current read/write position in the packet.
void PrepareToSend()
Writes the packet size from the raw packet from packet->size.
NetworkRecvStatus CloseConnection(bool error=true) override
Close the current connection; for TCP this will be mostly equivalent to Close(), but for UDP it just ...
virtual Packet * ReceivePacket()
Receives a packet for the given client.
SendPacketsState SendPackets(bool closing_down=false)
Sends all the buffered packets out for this client.
void PrepareToRead()
Prepares the packet so it can be read.
byte * buffer
The buffer of this packet, of basically variable length up to SEND_MTU.
bool writable
Can we write to this socket?
Packet * packet_recv
Partially received packet.
static T * ReallocT(T *t_ptr, size_t num_elements)
Simplified reallocation function that allocates the specified number of elements of the given type...
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
All packets in the queue are sent.
PacketSize size
The size of the whole packet for received packets.
The buffer is still full, so no (parts of) packets could be sent.
Packet * next
The next packet.
#define DEBUG(name, level,...)
Output a line of debugging information.
virtual NetworkRecvStatus CloseConnection(bool error=true)
Close the current connection; for TCP this will be mostly equivalent to Close(), but for UDP it just ...
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
The packets are partly sent; there are more packets to be sent in the queue.
static const uint16 SEND_MTU
Number of bytes we can pack in a single packet.
bool IsConnected() const
Whether this socket is currently bound to a socket.
void ReadRawPacketSize()
Reads the packet size from the raw packet and stores it in the packet->size.
virtual void SendPacket(Packet *packet)
This function puts the packet in the send-queue and it is send as soon as possible.
bool CanSendReceive()
Check whether this socket can send or receive something.
SendPacketsState
The states of sending the packets.
Basic functions to receive and send TCP packets.
NetworkTCPSocketHandler(SOCKET s=INVALID_SOCKET)
Construct a socket handler for a TCP connection.
Packet * packet_queue
Packets that are awaiting delivery.
uint16 PacketSize
Size of the whole packet.
SocketHandler for all network sockets in OpenTTD.