OpenTTD Source
12.0-beta2
|
Go to the documentation of this file.
12 #ifndef NETWORK_CORE_TCP_H
13 #define NETWORK_CORE_TCP_H
46 bool IsConnected()
const {
return this->sock != INVALID_SOCKET; }
92 addrinfo *
ai =
nullptr;
107 void Connect(addrinfo *address);
bool CheckActivity() override
Check if there was activity for this connecter.
static void CheckCallbacks()
Check whether we need to call the callback, i.e.
virtual void OnConnect(SOCKET s)
Callback when the connection succeeded.
std::vector< SOCKET > sockets
Pending connect() attempts.
std::string connection_string
Current address we are connecting to (before resolving).
@ CONNECTED
The connection is established.
"Helper" class for creating TCP connections in a non-blocking manner
SocketHandler for all network sockets in OpenTTD.
@ SPS_CLOSED
The connection got closed.
std::chrono::steady_clock::time_point last_attempt
Time we last tried to connect.
bool IsConnected() const
Whether this socket is currently bound to a socket.
void Kill()
Kill this connecter.
SOCKET sock
The socket currently connected to.
int family
Family we are using to connect with.
addrinfo * ai
getaddrinfo() allocated linked-list of resolved addresses.
@ SPS_ALL_SENT
All packets in the queue are sent.
void SetFailure()
The connection couldn't be established.
virtual Packet * ReceivePacket()
Receives a packet for the given client.
void Resolve()
Start resolving the hostname.
Packet * packet_recv
Partially received packet.
SendPacketsState
The states of sending the packets.
void Connect(addrinfo *address)
Start a connection to the indicated address.
std::atomic< bool > killed
Whether this connecter is marked as killed.
@ RESOLVING
The hostname is being resolved (threaded).
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.
bool HasSendQueue()
Whether there is something pending in the send queue.
Internal entity of a packet.
static void KillAll()
Kill all connection attempts.
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?
std::vector< addrinfo * > addresses
Addresses we can connect to.
ServerAddress server_address
Address we are connecting to.
@ SPS_NONE_SENT
The buffer is still full, so no (parts of) packets could be sent.
virtual bool CheckActivity()
Check if there was activity for this connecter.
SendPacketsState SendPackets(bool closing_down=false)
Sends all the buffered packets out for this client.
Wrapper for (un)resolved network addresses; there's no reason to transform a numeric IP to a string a...
void SetConnected(SOCKET sock)
The connection was successfully established.
bool CanSendReceive()
Check whether this socket can send or receive something.
size_t current_address
Current index in addresses we are trying.
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
std::map< SOCKET, NetworkAddress > sock_to_address
Mapping of a socket to the real address it is connecting to. USed for DEBUG statements.
virtual void OnFailure()
Callback for when the connection attempt failed.
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.
void OnResolved(addrinfo *ai)
Callback when resolving is done.
SOCKET socket
The socket when a connection is established.
@ FAILURE
Resolving failed.
Status
The current status of the connecter.
NetworkAddress bind_address
Address we're binding to, if any.
@ CONNECTING
We are currently connecting.
Address to a game server.
void CloseSocket()
Close the actual socket of the connection.
std::atomic< Status > status
The current status of the connecter.
static void ResolveThunk(TCPConnecter *connecter)
Thunk to start Resolve() on the right instance.
@ INIT
TCPConnecter is created but resolving hasn't started.
Base socket handler for all TCP sockets.
std::thread resolve_thread
Thread used during resolving.
Packet * packet_queue
Packets that are awaiting delivery.
bool TryNextAddress()
Start the connect() for the next address in the list.