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/>.
9 * @file tcp_game.cpp Basic functions to receive and send TCP packets for game purposes.
12 #include "../../stdafx.h"
14 #include "../network.h"
15 #include "../network_internal.h"
16 #include "../../debug.h"
17 #include "../../error.h"
19 #include "table/strings.h"
21 #include "../../safeguards.h"
23 static std::vector
<std::unique_ptr
<NetworkGameSocketHandler
>> _deferred_deletions
;
26 * Create a new socket for the game connection.
27 * @param s The socket to connect with.
29 NetworkGameSocketHandler::NetworkGameSocketHandler(SOCKET s
) : info(nullptr), client_id(INVALID_CLIENT_ID
),
30 last_frame(_frame_counter
), last_frame_server(_frame_counter
)
33 this->last_packet
= std::chrono::steady_clock::now();
37 * Functions to help ReceivePacket/SendPacket a bit
38 * A socket can make errors. When that happens this handles what to do.
39 * For clients: close connection and drop back to main-menu
40 * For servers: close connection and that is it
41 * @return the new status
43 NetworkRecvStatus
NetworkGameSocketHandler::CloseConnection(bool)
45 /* Clients drop back to the main menu */
46 if (!_network_server
&& _networking
) {
47 ClientNetworkEmergencySave();
48 _switch_mode
= SM_MENU
;
50 ShowErrorMessage(STR_NETWORK_ERROR_LOSTCONNECTION
, INVALID_STRING_ID
, WL_CRITICAL
);
52 return this->CloseConnection(NETWORK_RECV_STATUS_CLIENT_QUIT
);
55 return this->CloseConnection(NETWORK_RECV_STATUS_CONNECTION_LOST
);
60 * Handle the given packet, i.e. pass it to the right parser receive command.
61 * @param p the packet to handle
62 * @return #NetworkRecvStatus of handling.
64 NetworkRecvStatus
NetworkGameSocketHandler::HandlePacket(Packet
&p
)
66 PacketGameType type
= (PacketGameType
)p
.Recv_uint8();
68 if (this->HasClientQuit()) {
69 Debug(net
, 0, "[tcp/game] Received invalid packet from client {}", this->client_id
);
70 this->CloseConnection();
71 return NETWORK_RECV_STATUS_MALFORMED_PACKET
;
74 this->last_packet
= std::chrono::steady_clock::now();
77 case PACKET_SERVER_FULL
: return this->Receive_SERVER_FULL(p
);
78 case PACKET_SERVER_BANNED
: return this->Receive_SERVER_BANNED(p
);
79 case PACKET_CLIENT_JOIN
: return this->Receive_CLIENT_JOIN(p
);
80 case PACKET_SERVER_ERROR
: return this->Receive_SERVER_ERROR(p
);
81 case PACKET_CLIENT_GAME_INFO
: return this->Receive_CLIENT_GAME_INFO(p
);
82 case PACKET_SERVER_GAME_INFO
: return this->Receive_SERVER_GAME_INFO(p
);
83 case PACKET_SERVER_CLIENT_INFO
: return this->Receive_SERVER_CLIENT_INFO(p
);
84 case PACKET_CLIENT_IDENTIFY
: return this->Receive_CLIENT_IDENTIFY(p
);
85 case PACKET_SERVER_AUTH_REQUEST
: return this->Receive_SERVER_AUTH_REQUEST(p
);
86 case PACKET_CLIENT_AUTH_RESPONSE
: return this->Receive_CLIENT_AUTH_RESPONSE(p
);
87 case PACKET_SERVER_ENABLE_ENCRYPTION
: return this->Receive_SERVER_ENABLE_ENCRYPTION(p
);
88 case PACKET_SERVER_WELCOME
: return this->Receive_SERVER_WELCOME(p
);
89 case PACKET_CLIENT_GETMAP
: return this->Receive_CLIENT_GETMAP(p
);
90 case PACKET_SERVER_WAIT
: return this->Receive_SERVER_WAIT(p
);
91 case PACKET_SERVER_MAP_BEGIN
: return this->Receive_SERVER_MAP_BEGIN(p
);
92 case PACKET_SERVER_MAP_SIZE
: return this->Receive_SERVER_MAP_SIZE(p
);
93 case PACKET_SERVER_MAP_DATA
: return this->Receive_SERVER_MAP_DATA(p
);
94 case PACKET_SERVER_MAP_DONE
: return this->Receive_SERVER_MAP_DONE(p
);
95 case PACKET_CLIENT_MAP_OK
: return this->Receive_CLIENT_MAP_OK(p
);
96 case PACKET_SERVER_JOIN
: return this->Receive_SERVER_JOIN(p
);
97 case PACKET_SERVER_FRAME
: return this->Receive_SERVER_FRAME(p
);
98 case PACKET_SERVER_SYNC
: return this->Receive_SERVER_SYNC(p
);
99 case PACKET_CLIENT_ACK
: return this->Receive_CLIENT_ACK(p
);
100 case PACKET_CLIENT_COMMAND
: return this->Receive_CLIENT_COMMAND(p
);
101 case PACKET_SERVER_COMMAND
: return this->Receive_SERVER_COMMAND(p
);
102 case PACKET_CLIENT_CHAT
: return this->Receive_CLIENT_CHAT(p
);
103 case PACKET_SERVER_CHAT
: return this->Receive_SERVER_CHAT(p
);
104 case PACKET_SERVER_EXTERNAL_CHAT
: return this->Receive_SERVER_EXTERNAL_CHAT(p
);
105 case PACKET_CLIENT_SET_NAME
: return this->Receive_CLIENT_SET_NAME(p
);
106 case PACKET_CLIENT_QUIT
: return this->Receive_CLIENT_QUIT(p
);
107 case PACKET_CLIENT_ERROR
: return this->Receive_CLIENT_ERROR(p
);
108 case PACKET_SERVER_QUIT
: return this->Receive_SERVER_QUIT(p
);
109 case PACKET_SERVER_ERROR_QUIT
: return this->Receive_SERVER_ERROR_QUIT(p
);
110 case PACKET_SERVER_SHUTDOWN
: return this->Receive_SERVER_SHUTDOWN(p
);
111 case PACKET_SERVER_NEWGAME
: return this->Receive_SERVER_NEWGAME(p
);
112 case PACKET_SERVER_RCON
: return this->Receive_SERVER_RCON(p
);
113 case PACKET_CLIENT_RCON
: return this->Receive_CLIENT_RCON(p
);
114 case PACKET_SERVER_CHECK_NEWGRFS
: return this->Receive_SERVER_CHECK_NEWGRFS(p
);
115 case PACKET_CLIENT_NEWGRFS_CHECKED
: return this->Receive_CLIENT_NEWGRFS_CHECKED(p
);
116 case PACKET_SERVER_MOVE
: return this->Receive_SERVER_MOVE(p
);
117 case PACKET_CLIENT_MOVE
: return this->Receive_CLIENT_MOVE(p
);
118 case PACKET_SERVER_CONFIG_UPDATE
: return this->Receive_SERVER_CONFIG_UPDATE(p
);
121 Debug(net
, 0, "[tcp/game] Received invalid packet type {} from client {}", type
, this->client_id
);
122 this->CloseConnection();
123 return NETWORK_RECV_STATUS_MALFORMED_PACKET
;
128 * Do the actual receiving of packets.
129 * As long as HandlePacket returns OKAY packets are handled. Upon
130 * failure, or no more packets to process the last result of
131 * HandlePacket is returned.
132 * @return #NetworkRecvStatus of the last handled packet.
134 NetworkRecvStatus
NetworkGameSocketHandler::ReceivePackets()
136 std::unique_ptr
<Packet
> p
;
137 while ((p
= this->ReceivePacket()) != nullptr) {
138 NetworkRecvStatus res
= HandlePacket(*p
);
139 if (res
!= NETWORK_RECV_STATUS_OKAY
) return res
;
142 return NETWORK_RECV_STATUS_OKAY
;
146 * Helper for logging receiving invalid packets.
147 * @param type The received packet type.
148 * @return The status the network should have, in this case: "malformed packet error".
150 NetworkRecvStatus
NetworkGameSocketHandler::ReceiveInvalidPacket(PacketGameType type
)
152 Debug(net
, 0, "[tcp/game] Received illegal packet type {} from client {}", type
, this->client_id
);
153 return NETWORK_RECV_STATUS_MALFORMED_PACKET
;
156 NetworkRecvStatus
NetworkGameSocketHandler::Receive_SERVER_FULL(Packet
&) { return this->ReceiveInvalidPacket(PACKET_SERVER_FULL
); }
157 NetworkRecvStatus
NetworkGameSocketHandler::Receive_SERVER_BANNED(Packet
&) { return this->ReceiveInvalidPacket(PACKET_SERVER_BANNED
); }
158 NetworkRecvStatus
NetworkGameSocketHandler::Receive_CLIENT_JOIN(Packet
&) { return this->ReceiveInvalidPacket(PACKET_CLIENT_JOIN
); }
159 NetworkRecvStatus
NetworkGameSocketHandler::Receive_SERVER_ERROR(Packet
&) { return this->ReceiveInvalidPacket(PACKET_SERVER_ERROR
); }
160 NetworkRecvStatus
NetworkGameSocketHandler::Receive_CLIENT_GAME_INFO(Packet
&) { return this->ReceiveInvalidPacket(PACKET_CLIENT_GAME_INFO
); }
161 NetworkRecvStatus
NetworkGameSocketHandler::Receive_SERVER_GAME_INFO(Packet
&) { return this->ReceiveInvalidPacket(PACKET_SERVER_GAME_INFO
); }
162 NetworkRecvStatus
NetworkGameSocketHandler::Receive_SERVER_CLIENT_INFO(Packet
&) { return this->ReceiveInvalidPacket(PACKET_SERVER_CLIENT_INFO
); }
163 NetworkRecvStatus
NetworkGameSocketHandler::Receive_CLIENT_IDENTIFY(Packet
&) { return this->ReceiveInvalidPacket(PACKET_CLIENT_IDENTIFY
); }
164 NetworkRecvStatus
NetworkGameSocketHandler::Receive_SERVER_AUTH_REQUEST(Packet
&) { return this->ReceiveInvalidPacket(PACKET_SERVER_AUTH_REQUEST
); }
165 NetworkRecvStatus
NetworkGameSocketHandler::Receive_CLIENT_AUTH_RESPONSE(Packet
&) { return this->ReceiveInvalidPacket(PACKET_CLIENT_AUTH_RESPONSE
); }
166 NetworkRecvStatus
NetworkGameSocketHandler::Receive_SERVER_ENABLE_ENCRYPTION(Packet
&) { return this->ReceiveInvalidPacket(PACKET_SERVER_ENABLE_ENCRYPTION
); }
167 NetworkRecvStatus
NetworkGameSocketHandler::Receive_SERVER_WELCOME(Packet
&) { return this->ReceiveInvalidPacket(PACKET_SERVER_WELCOME
); }
168 NetworkRecvStatus
NetworkGameSocketHandler::Receive_CLIENT_GETMAP(Packet
&) { return this->ReceiveInvalidPacket(PACKET_CLIENT_GETMAP
); }
169 NetworkRecvStatus
NetworkGameSocketHandler::Receive_SERVER_WAIT(Packet
&) { return this->ReceiveInvalidPacket(PACKET_SERVER_WAIT
); }
170 NetworkRecvStatus
NetworkGameSocketHandler::Receive_SERVER_MAP_BEGIN(Packet
&) { return this->ReceiveInvalidPacket(PACKET_SERVER_MAP_BEGIN
); }
171 NetworkRecvStatus
NetworkGameSocketHandler::Receive_SERVER_MAP_SIZE(Packet
&) { return this->ReceiveInvalidPacket(PACKET_SERVER_MAP_SIZE
); }
172 NetworkRecvStatus
NetworkGameSocketHandler::Receive_SERVER_MAP_DATA(Packet
&) { return this->ReceiveInvalidPacket(PACKET_SERVER_MAP_DATA
); }
173 NetworkRecvStatus
NetworkGameSocketHandler::Receive_SERVER_MAP_DONE(Packet
&) { return this->ReceiveInvalidPacket(PACKET_SERVER_MAP_DONE
); }
174 NetworkRecvStatus
NetworkGameSocketHandler::Receive_CLIENT_MAP_OK(Packet
&) { return this->ReceiveInvalidPacket(PACKET_CLIENT_MAP_OK
); }
175 NetworkRecvStatus
NetworkGameSocketHandler::Receive_SERVER_JOIN(Packet
&) { return this->ReceiveInvalidPacket(PACKET_SERVER_JOIN
); }
176 NetworkRecvStatus
NetworkGameSocketHandler::Receive_SERVER_FRAME(Packet
&) { return this->ReceiveInvalidPacket(PACKET_SERVER_FRAME
); }
177 NetworkRecvStatus
NetworkGameSocketHandler::Receive_SERVER_SYNC(Packet
&) { return this->ReceiveInvalidPacket(PACKET_SERVER_SYNC
); }
178 NetworkRecvStatus
NetworkGameSocketHandler::Receive_CLIENT_ACK(Packet
&) { return this->ReceiveInvalidPacket(PACKET_CLIENT_ACK
); }
179 NetworkRecvStatus
NetworkGameSocketHandler::Receive_CLIENT_COMMAND(Packet
&) { return this->ReceiveInvalidPacket(PACKET_CLIENT_COMMAND
); }
180 NetworkRecvStatus
NetworkGameSocketHandler::Receive_SERVER_COMMAND(Packet
&) { return this->ReceiveInvalidPacket(PACKET_SERVER_COMMAND
); }
181 NetworkRecvStatus
NetworkGameSocketHandler::Receive_CLIENT_CHAT(Packet
&) { return this->ReceiveInvalidPacket(PACKET_CLIENT_CHAT
); }
182 NetworkRecvStatus
NetworkGameSocketHandler::Receive_SERVER_CHAT(Packet
&) { return this->ReceiveInvalidPacket(PACKET_SERVER_CHAT
); }
183 NetworkRecvStatus
NetworkGameSocketHandler::Receive_SERVER_EXTERNAL_CHAT(Packet
&) { return this->ReceiveInvalidPacket(PACKET_SERVER_EXTERNAL_CHAT
); }
184 NetworkRecvStatus
NetworkGameSocketHandler::Receive_CLIENT_SET_NAME(Packet
&) { return this->ReceiveInvalidPacket(PACKET_CLIENT_SET_NAME
); }
185 NetworkRecvStatus
NetworkGameSocketHandler::Receive_CLIENT_QUIT(Packet
&) { return this->ReceiveInvalidPacket(PACKET_CLIENT_QUIT
); }
186 NetworkRecvStatus
NetworkGameSocketHandler::Receive_CLIENT_ERROR(Packet
&) { return this->ReceiveInvalidPacket(PACKET_CLIENT_ERROR
); }
187 NetworkRecvStatus
NetworkGameSocketHandler::Receive_SERVER_QUIT(Packet
&) { return this->ReceiveInvalidPacket(PACKET_SERVER_QUIT
); }
188 NetworkRecvStatus
NetworkGameSocketHandler::Receive_SERVER_ERROR_QUIT(Packet
&) { return this->ReceiveInvalidPacket(PACKET_SERVER_ERROR_QUIT
); }
189 NetworkRecvStatus
NetworkGameSocketHandler::Receive_SERVER_SHUTDOWN(Packet
&) { return this->ReceiveInvalidPacket(PACKET_SERVER_SHUTDOWN
); }
190 NetworkRecvStatus
NetworkGameSocketHandler::Receive_SERVER_NEWGAME(Packet
&) { return this->ReceiveInvalidPacket(PACKET_SERVER_NEWGAME
); }
191 NetworkRecvStatus
NetworkGameSocketHandler::Receive_SERVER_RCON(Packet
&) { return this->ReceiveInvalidPacket(PACKET_SERVER_RCON
); }
192 NetworkRecvStatus
NetworkGameSocketHandler::Receive_CLIENT_RCON(Packet
&) { return this->ReceiveInvalidPacket(PACKET_CLIENT_RCON
); }
193 NetworkRecvStatus
NetworkGameSocketHandler::Receive_SERVER_CHECK_NEWGRFS(Packet
&) { return this->ReceiveInvalidPacket(PACKET_SERVER_CHECK_NEWGRFS
); }
194 NetworkRecvStatus
NetworkGameSocketHandler::Receive_CLIENT_NEWGRFS_CHECKED(Packet
&) { return this->ReceiveInvalidPacket(PACKET_CLIENT_NEWGRFS_CHECKED
); }
195 NetworkRecvStatus
NetworkGameSocketHandler::Receive_SERVER_MOVE(Packet
&) { return this->ReceiveInvalidPacket(PACKET_SERVER_MOVE
); }
196 NetworkRecvStatus
NetworkGameSocketHandler::Receive_CLIENT_MOVE(Packet
&) { return this->ReceiveInvalidPacket(PACKET_CLIENT_MOVE
); }
197 NetworkRecvStatus
NetworkGameSocketHandler::Receive_SERVER_CONFIG_UPDATE(Packet
&) { return this->ReceiveInvalidPacket(PACKET_SERVER_CONFIG_UPDATE
); }
199 void NetworkGameSocketHandler::DeferDeletion()
201 _deferred_deletions
.emplace_back(this);
202 this->is_pending_deletion
= true;
205 /* static */ void NetworkGameSocketHandler::ProcessDeferredDeletions()
207 /* Calls deleter on all items */
208 _deferred_deletions
.clear();