30 if (this->
sock != INVALID_SOCKET) closesocket(this->
sock);
31 this->
sock = INVALID_SOCKET;
59 assert(packet !=
nullptr);
61 packet->PrepareToSend();
181 Debug(net, 0,
"Invalid packet received (too small / decryption error)");
195 assert(this->
sock != INVALID_SOCKET);
197 fd_set read_fd, write_fd;
203 FD_SET(this->
sock, &read_fd);
204 FD_SET(this->
sock, &write_fd);
206 tv.tv_sec = tv.tv_usec = 0;
207 if (select(FD_SETSIZE, &read_fd, &write_fd,
nullptr, &tv) < 0)
return false;
210 return FD_ISSET(this->
sock, &read_fd) != 0;
Abstraction of a network error where all implementation details of the error codes are encapsulated i...
std::string_view AsString() const
Get the string representation of the error message.
bool WouldBlock() const
Check whether this error describes that the operation would block.
bool IsConnectionReset() const
Check whether this error describes a connection reset.
static NetworkError GetLast()
Get the last network error.
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.
~NetworkTCPSocketHandler() override
Close the socket.
bool IsConnected() const
Whether this socket is currently bound to a socket.
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.
Functions related to debugging.
#define Debug(category, level, format_string,...)
Output a line of debugging information.
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.
bool PrepareToRead()
Prepares the packet so it can be read.
ssize_t TransferIn(F transfer_function)
Transfer data from the given function into the packet.
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.
size_t RemainingBytesToTransfer() const
Get the amount of bytes that are still available for the Transfer functions.
ssize_t TransferOut(F transfer_function)
Transfer data from the packet to the given function.
Helper for Packet::TransferIn that reads data from a socket.
IPv4 addresses should be 4 bytes.
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.