OpenTTD Source
20240919-master-gdf0233f4c2
|
Go to the documentation of this file.
12 #ifndef NETWORK_CORE_TCP_H
13 #define NETWORK_CORE_TCP_H
36 void EmptyPacketQueue();
45 bool IsConnected()
const {
return this->sock != INVALID_SOCKET; }
50 virtual void SendPacket(std::unique_ptr<Packet> &&packet);
91 addrinfo *
ai =
nullptr;
103 static std::vector<std::shared_ptr<TCPConnecter>>
connecters;
108 void Connect(addrinfo *address);
144 template <
class T,
typename... Args>
145 static std::shared_ptr<TCPConnecter>
Create(Args&& ... args)
bool CheckActivity() override
Check if there was activity for this connecter.
static void CheckCallbacks()
Check whether we need to call the callback, i.e.
std::vector< SOCKET > sockets
Pending connect() attempts.
std::string connection_string
Current address we are connecting to (before resolving).
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.
"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.
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.
@ Connecting
We are currently connecting.
static std::vector< std::shared_ptr< TCPConnecter > > connecters
List of connections that are currently being created.
void Resolve()
Start resolving the hostname.
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.
std::unique_ptr< Packet > packet_recv
Partially received packet.
@ Connected
The connection is established.
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.
static void KillAll()
Kill all connection attempts.
NetworkTCPSocketHandler(SOCKET s=INVALID_SOCKET)
Construct a socket handler for a TCP connection.
bool writable
Can we write to this socket?
std::vector< addrinfo * > addresses
Addresses we can connect to.
@ Failure
Resolving failed.
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.
static std::shared_ptr< TCPConnecter > Create(Args &&... args)
Create the connecter, and initiate connecting by putting it in the collection of TCP connections to m...
size_t current_address
Current index in addresses we are trying.
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
@ Resolving
The hostname is being resolved (threaded).
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.
virtual std::unique_ptr< Packet > ReceivePacket()
Receives a packet for the given client.
@ 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.
virtual void OnConnect([[maybe_unused]] SOCKET s)
Callback when the connection succeeded.
Status
The current status of the connecter.
NetworkAddress bind_address
Address we're binding to, if any.
Address to a game server.
void CloseSocket()
Close the actual socket of the connection.
@ Init
TCPConnecter is created but resolving hasn't started.
std::atomic< Status > status
The current status of the connecter.
static void ResolveThunk(TCPConnecter *connecter)
Thunk to start Resolve() on the right instance.
Base socket handler for all TCP sockets.
std::thread resolve_thread
Thread used during resolving.
bool TryNextAddress()
Start the connect() for the next address in the list.