16 #include "../../stdafx.h" 17 #include "../../debug.h" 21 #include "../../safeguards.h" 29 packet_queue(NULL), packet_recv(NULL),
30 sock(s), writable(false)
34 NetworkTCPSocketHandler::~NetworkTCPSocketHandler()
38 if (this->
sock != INVALID_SOCKET) closesocket(this->
sock);
39 this->
sock = INVALID_SOCKET;
68 assert(packet != NULL);
112 int err = GET_LAST_ERROR();
113 if (err != EWOULDBLOCK) {
116 DEBUG(net, 0,
"send failed with error %d", err);
167 int err = GET_LAST_ERROR();
168 if (err != EWOULDBLOCK) {
170 if (err != 104)
DEBUG(net, 0,
"recv failed with error %d", err);
198 int err = GET_LAST_ERROR();
199 if (err != EWOULDBLOCK) {
201 if (err != 104)
DEBUG(net, 0,
"recv failed with error %d", err);
231 fd_set read_fd, write_fd;
237 FD_SET(this->
sock, &read_fd);
238 FD_SET(this->
sock, &write_fd);
240 tv.tv_sec = tv.tv_usec = 0;
241 #if !defined(__MORPHOS__) && !defined(__AMIGA__) 242 if (select(FD_SETSIZE, &read_fd, &write_fd, NULL, &tv) < 0)
return false;
244 if (WaitSelect(FD_SETSIZE, &read_fd, &write_fd, NULL, &tv, NULL) < 0)
return false;
248 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.
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.
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 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.