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)
Wrapper for network addresses.
Wrapper for (un)resolved network addresses; there's no reason to transform a numeric IP to a string a...
SocketHandler for all network sockets in OpenTTD.
Base socket handler for all TCP sockets.
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.
bool IsConnected() const
Whether this socket is currently bound to a socket.
NetworkTCPSocketHandler(SOCKET s=INVALID_SOCKET)
Construct a socket handler for a TCP connection.
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.
bool HasSendQueue()
Whether there is something pending in the send queue.
Address to a game server.
"Helper" class for creating TCP connections in a non-blocking manner
NetworkAddress bind_address
Address we're binding to, if any.
void Kill()
Kill this connecter.
std::chrono::steady_clock::time_point last_attempt
Time we last tried to connect.
virtual void OnConnect([[maybe_unused]] SOCKET s)
Callback when the connection succeeded.
std::atomic< Status > status
The current status of the connecter.
std::string connection_string
Current address we are connecting to (before resolving).
static std::vector< std::shared_ptr< TCPConnecter > > connecters
List of connections that are currently being created.
std::vector< SOCKET > sockets
Pending connect() attempts.
static void CheckCallbacks()
Check whether we need to call the callback, i.e.
Status
The current status of the connecter.
@ Connected
The connection is established.
@ Resolving
The hostname is being resolved (threaded).
@ Init
TCPConnecter is created but resolving hasn't started.
@ Failure
Resolving failed.
@ Connecting
We are currently connecting.
size_t current_address
Current index in addresses we are trying.
void Resolve()
Start resolving the hostname.
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...
virtual void OnFailure()
Callback for when the connection attempt failed.
void OnResolved(addrinfo *ai)
Callback when resolving is done.
virtual bool CheckActivity()
Check if there was activity for this connecter.
std::map< SOCKET, NetworkAddress > sock_to_address
Mapping of a socket to the real address it is connecting to. USed for DEBUG statements.
static void ResolveThunk(TCPConnecter *connecter)
Thunk to start Resolve() on the right instance.
std::vector< addrinfo * > addresses
Addresses we can connect to.
std::thread resolve_thread
Thread used during resolving.
static void KillAll()
Kill all connection attempts.
bool TryNextAddress()
Start the connect() for the next address in the list.
std::atomic< bool > killed
Whether this connecter is marked as killed.
void Connect(addrinfo *address)
Start a connection to the indicated address.
addrinfo * ai
getaddrinfo() allocated linked-list of resolved addresses.
int family
Family we are using to connect with.
void SetConnected(SOCKET sock)
The connection was successfully established.
bool CheckActivity() override
Check if there was activity for this connecter.
ServerAddress server_address
Address we are connecting to.
void SetFailure()
The connection couldn't be established.
SOCKET socket
The socket when a connection is established.
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
Basic functions to create, fill and read 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.