OpenTTD Source: src/network/core/tcp.h Source File

OpenTTD Source 13.2.1

tcp.h
Go to the documentation of this file.
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
12 #ifndef NETWORK_CORE_TCP_H
13 #define NETWORK_CORE_TCP_H
14
15 #include "address.h"
16 #include "packet.h"
17
18 #include <atomic>
19 #include <chrono>
20 #include <map>
21 #include <thread>
22
24 enum SendPacketsState {
25 SPS_CLOSED,
26 SPS_NONE_SENT,
27 SPS_PARTLY_SENT,
28 SPS_ALL_SENT,
29 };
30
32 class NetworkTCPSocketHandler : public NetworkSocketHandler {
33 private:
34 Packet *packet_queue;
35 Packet *packet_recv;
36
37 void EmptyPacketQueue();
38 public:
39 SOCKET sock;
40 bool writable;
41
46 bool IsConnected() const { return this->sock != INVALID_SOCKET; }
47
48 virtual NetworkRecvStatus CloseConnection(bool error = true);
49 void CloseSocket();
50
51 virtual void SendPacket(Packet *packet);
52 SendPacketsState SendPackets(bool closing_down = false);
53
54 virtual Packet *ReceivePacket();
55
56 bool CanSendReceive();
57
62 bool HasSendQueue() { return this->packet_queue != nullptr; }
63
64 NetworkTCPSocketHandler(SOCKET s = INVALID_SOCKET);
65 ~NetworkTCPSocketHandler();
66 };
67
71 class TCPConnecter {
72 private:
80 enum class Status {
81 Init,
82 Resolving,
83 Failure,
84 Connecting,
85 Connected,
86 };
87
88 std::thread resolve_thread;
89 std::atomic<Status> status = Status::Init;
90 std::atomic<bool> killed = false;
91
92 addrinfo *ai = nullptr;
93 std::vector<addrinfo *> addresses;
94 std::map<SOCKET, NetworkAddress> sock_to_address;
95 size_t current_address = 0;
96
97 std::vector<SOCKET> sockets;
98 std::chrono::steady_clock::time_point last_attempt;
99
100 std::string connection_string;
101 NetworkAddress bind_address;
102 int family = AF_UNSPEC;
103
104 void Resolve();
105 void OnResolved(addrinfo *ai);
106 bool TryNextAddress();
107 void Connect(addrinfo *address);
108 virtual bool CheckActivity();
109
110 /* We do not want any other derived classes from this class being able to
111 * access these private members, but it is okay for TCPServerConnecter. */
112 friend class TCPServerConnecter;
113
114 static void ResolveThunk(TCPConnecter *connecter);
115
116 public:
117 TCPConnecter() {};
118 TCPConnecter(const std::string &connection_string, uint16 default_port, NetworkAddress bind_address = {}, int family = AF_UNSPEC);
119 virtual ~TCPConnecter();
120
125 virtual void OnConnect(SOCKET s) {}
126
130 virtual void OnFailure() {}
131
132 void Kill();
133
134 static void CheckCallbacks();
135 static void KillAll();
136 };
137
138 class TCPServerConnecter : public TCPConnecter {
139 private:
140 SOCKET socket = INVALID_SOCKET;
141
142 bool CheckActivity() override;
143
144 public:
145 ServerAddress server_address;
146
147 TCPServerConnecter(const std::string &connection_string, uint16 default_port);
148
149 void SetConnected(SOCKET sock);
150 void SetFailure();
151 };
152
153 #endif /* NETWORK_CORE_TCP_H */
TCPServerConnecter::CheckActivity
bool CheckActivity() override
Check if there was activity for this connecter.
Definition: tcp_connect.cpp:414
TCPConnecter::CheckCallbacks
static void CheckCallbacks()
Check whether we need to call the callback, i.e.
Definition: tcp_connect.cpp:471
TCPConnecter::OnConnect
virtual void OnConnect(SOCKET s)
Callback when the connection succeeded.
Definition: tcp.h:125
TCPConnecter::sockets
std::vector< SOCKET > sockets
Pending connect() attempts.
Definition: tcp.h:97
TCPConnecter::connection_string
std::string connection_string
Current address we are connecting to (before resolving).
Definition: tcp.h:100
TCPConnecter
"Helper" class for creating TCP connections in a non-blocking manner
Definition: tcp.h:71
NetworkSocketHandler
SocketHandler for all network sockets in OpenTTD.
Definition: core.h:42
SPS_CLOSED
@ SPS_CLOSED
The connection got closed.
Definition: tcp.h:25
TCPConnecter::last_attempt
std::chrono::steady_clock::time_point last_attempt
Time we last tried to connect.
Definition: tcp.h:98
NetworkTCPSocketHandler::IsConnected
bool IsConnected() const
Whether this socket is currently bound to a socket.
Definition: tcp.h:46
TCPConnecter::Kill
void Kill()
Kill this connecter.
Definition: tcp_connect.cpp:85
NetworkTCPSocketHandler::sock
SOCKET sock
The socket currently connected to.
Definition: tcp.h:39
TCPConnecter::family
int family
Family we are using to connect with.
Definition: tcp.h:102
TCPConnecter::ai
addrinfo * ai
getaddrinfo() allocated linked-list of resolved addresses.
Definition: tcp.h:92
SPS_ALL_SENT
@ SPS_ALL_SENT
All packets in the queue are sent.
Definition: tcp.h:28
TCPServerConnecter::SetFailure
void SetFailure()
The connection couldn't be established.
Definition: tcp_connect.cpp:460
TCPConnecter::Status::Connecting
@ Connecting
We are currently connecting.
NetworkTCPSocketHandler::ReceivePacket
virtual Packet * ReceivePacket()
Receives a packet for the given client.
Definition: tcp.cpp:144
TCPConnecter::Resolve
void Resolve()
Start resolving the hostname.
Definition: tcp_connect.cpp:227
address.h
NetworkTCPSocketHandler::packet_recv
Packet * packet_recv
Partially received packet.
Definition: tcp.h:35
SendPacketsState
SendPacketsState
The states of sending the packets.
Definition: tcp.h:24
TCPConnecter::Connect
void Connect(addrinfo *address)
Start a connection to the indicated address.
Definition: tcp_connect.cpp:95
TCPConnecter::killed
std::atomic< bool > killed
Whether this connecter is marked as killed.
Definition: tcp.h:90
TCPConnecter::Status::Connected
@ Connected
The connection is established.
NetworkTCPSocketHandler::SendPacket
virtual void SendPacket(Packet *packet)
This function puts the packet in the send-queue and it is send as soon as possible.
Definition: tcp.cpp:81
NetworkTCPSocketHandler::CloseConnection
virtual NetworkRecvStatus CloseConnection(bool error=true)
This will put this socket handler in a close state.
Definition: tcp.cpp:65
NetworkTCPSocketHandler::HasSendQueue
bool HasSendQueue()
Whether there is something pending in the send queue.
Definition: tcp.h:62
Packet
Internal entity of a packet.
Definition: packet.h:44
TCPConnecter::KillAll
static void KillAll()
Kill all connection attempts.
Definition: tcp_connect.cpp:486
NetworkTCPSocketHandler::NetworkTCPSocketHandler
NetworkTCPSocketHandler(SOCKET s=INVALID_SOCKET)
Construct a socket handler for a TCP connection.
Definition: tcp.cpp:23
NetworkTCPSocketHandler::EmptyPacketQueue
void EmptyPacketQueue()
Free all pending and partially received packets.
Definition: tcp.cpp:39
NetworkTCPSocketHandler::writable
bool writable
Can we write to this socket?
Definition: tcp.h:40
TCPConnecter::addresses
std::vector< addrinfo * > addresses
Addresses we can connect to.
Definition: tcp.h:93
TCPConnecter::Status::Failure
@ Failure
Resolving failed.
TCPServerConnecter::server_address
ServerAddress server_address
Address we are connecting to.
Definition: tcp.h:145
SPS_NONE_SENT
@ SPS_NONE_SENT
The buffer is still full, so no (parts of) packets could be sent.
Definition: tcp.h:26
TCPConnecter::CheckActivity
virtual bool CheckActivity()
Check if there was activity for this connecter.
Definition: tcp_connect.cpp:279
NetworkTCPSocketHandler::SendPackets
SendPacketsState SendPackets(bool closing_down=false)
Sends all the buffered packets out for this client.
Definition: tcp.cpp:99
NetworkAddress
Wrapper for (un)resolved network addresses; there's no reason to transform a numeric IP to a string a...
Definition: address.h:30
TCPServerConnecter::SetConnected
void SetConnected(SOCKET sock)
The connection was successfully established.
Definition: tcp_connect.cpp:449
NetworkTCPSocketHandler::CanSendReceive
bool CanSendReceive()
Check whether this socket can send or receive something.
Definition: tcp.cpp:218
TCPConnecter::current_address
size_t current_address
Current index in addresses we are trying.
Definition: tcp.h:95
packet.h
NetworkRecvStatus
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
Definition: core.h:22
TCPConnecter::Status::Resolving
@ Resolving
The hostname is being resolved (threaded).
TCPConnecter::sock_to_address
std::map< SOCKET, NetworkAddress > sock_to_address
Mapping of a socket to the real address it is connecting to. USed for DEBUG statements.
Definition: tcp.h:94
TCPConnecter::OnFailure
virtual void OnFailure()
Callback for when the connection attempt failed.
Definition: tcp.h:130
TCPServerConnecter
Definition: tcp.h:138
error
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
Definition: openttd.cpp:134
SPS_PARTLY_SENT
@ SPS_PARTLY_SENT
The packets are partly sent; there are more packets to be sent in the queue.
Definition: tcp.h:27
TCPConnecter::OnResolved
void OnResolved(addrinfo *ai)
Callback when resolving is done.
Definition: tcp_connect.cpp:155
TCPServerConnecter::socket
SOCKET socket
The socket when a connection is established.
Definition: tcp.h:140
TCPConnecter::Status
Status
The current status of the connecter.
Definition: tcp.h:80
TCPConnecter::bind_address
NetworkAddress bind_address
Address we're binding to, if any.
Definition: tcp.h:101
ServerAddress
Address to a game server.
Definition: address.h:198
NetworkTCPSocketHandler::CloseSocket
void CloseSocket()
Close the actual socket of the connection.
Definition: tcp.cpp:53
TCPConnecter::Status::Init
@ Init
TCPConnecter is created but resolving hasn't started.
TCPConnecter::status
std::atomic< Status > status
The current status of the connecter.
Definition: tcp.h:89
TCPConnecter::ResolveThunk
static void ResolveThunk(TCPConnecter *connecter)
Thunk to start Resolve() on the right instance.
Definition: tcp_connect.cpp:270
NetworkTCPSocketHandler
Base socket handler for all TCP sockets.
Definition: tcp.h:32
TCPConnecter::resolve_thread
std::thread resolve_thread
Thread used during resolving.
Definition: tcp.h:88
NetworkTCPSocketHandler::packet_queue
Packet * packet_queue
Packets that are awaiting delivery.
Definition: tcp.h:34
TCPConnecter::TryNextAddress
bool TryNextAddress()
Start the connect() for the next address in the list.
Definition: tcp_connect.cpp:141