OpenTTD Source: src/network/network_query.cpp Source File

OpenTTD Source 13.1

network_query.cpp
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
10 #include "../stdafx.h"
11 #include "core/game_info.h"
12 #include "network_query.h"
13 #include "network_gamelist.h"
14 #include "../error.h"
15
16 #include "table/strings.h"
17
18 #include "../safeguards.h"
19
20 std::vector<std::unique_ptr<QueryNetworkGameSocketHandler>> QueryNetworkGameSocketHandler::queries = {};
21
22 NetworkRecvStatus QueryNetworkGameSocketHandler::CloseConnection(NetworkRecvStatus status)
23 {
24 assert(status != NETWORK_RECV_STATUS_OKAY);
25 assert(this->sock != INVALID_SOCKET);
26
27 return status;
28 }
29
33 bool QueryNetworkGameSocketHandler::CheckConnection()
34 {
35 std::chrono::steady_clock::duration lag = std::chrono::steady_clock::now() - this->last_packet;
36
37 /* If there was no response in 5 seconds, terminate the query. */
38 if (lag > std::chrono::seconds(5)) {
39 this->CloseConnection(NETWORK_RECV_STATUS_CONNECTION_LOST);
40 return false;
41 }
42
43 return true;
44 }
45
51 bool QueryNetworkGameSocketHandler::Receive()
52 {
53 if (this->CanSendReceive()) {
54 NetworkRecvStatus res = this->ReceivePackets();
55 if (res != NETWORK_RECV_STATUS_OKAY) {
56 this->CloseConnection(res);
57 return false;
58 }
59 }
60 return true;
61 }
62
64 void QueryNetworkGameSocketHandler::Send()
65 {
66 this->SendPackets();
67 }
68
72 NetworkRecvStatus QueryNetworkGameSocketHandler::SendGameInfo()
73 {
74 this->SendPacket(new Packet(PACKET_CLIENT_GAME_INFO));
75 return NETWORK_RECV_STATUS_OKAY;
76 }
77
78 NetworkRecvStatus QueryNetworkGameSocketHandler::Receive_SERVER_FULL(Packet *p)
79 {
80 NetworkGameList *item = NetworkGameListAddItem(this->connection_string);
81 item->status = NGLS_FULL;
82 item->refreshing = false;
83
84 UpdateNetworkGameWindow();
85
86 return NETWORK_RECV_STATUS_CLOSE_QUERY;
87 }
88
89 NetworkRecvStatus QueryNetworkGameSocketHandler::Receive_SERVER_BANNED(Packet *p)
90 {
91 NetworkGameList *item = NetworkGameListAddItem(this->connection_string);
92 item->status = NGLS_BANNED;
93 item->refreshing = false;
94
95 UpdateNetworkGameWindow();
96
97 return NETWORK_RECV_STATUS_CLOSE_QUERY;
98 }
99
100 NetworkRecvStatus QueryNetworkGameSocketHandler::Receive_SERVER_GAME_INFO(Packet *p)
101 {
102 NetworkGameList *item = NetworkGameListAddItem(this->connection_string);
103
104 /* Clear any existing GRFConfig chain. */
105 ClearGRFConfigList(&item->info.grfconfig);
106 /* Retrieve the NetworkGameInfo from the packet. */
107 DeserializeNetworkGameInfo(p, &item->info);
108 /* Check for compatability with the client. */
109 CheckGameCompatibility(item->info);
110 /* Ensure we consider the server online. */
111 item->status = NGLS_ONLINE;
112 item->refreshing = false;
113
114 UpdateNetworkGameWindow();
115
116 return NETWORK_RECV_STATUS_CLOSE_QUERY;
117 }
118
119 NetworkRecvStatus QueryNetworkGameSocketHandler::Receive_SERVER_ERROR(Packet *p)
120 {
121 NetworkErrorCode error = (NetworkErrorCode)p->Recv_uint8();
122
123 NetworkGameList *item = NetworkGameListAddItem(this->connection_string);
124
125 if (error == NETWORK_ERROR_NOT_EXPECTED) {
126 /* If we query a server that is 1.11.1 or older, we get an
127 * NETWORK_ERROR_NOT_EXPECTED on requesting the game info. Show to the
128 * user this server is too old to query.
129 */
130 item->status = NGLS_TOO_OLD;
131 } else {
132 item->status = NGLS_OFFLINE;
133 }
134 item->refreshing = false;
135
136 UpdateNetworkGameWindow();
137
138 return NETWORK_RECV_STATUS_CLOSE_QUERY;
139 }
140
144 /* static */ void QueryNetworkGameSocketHandler::SendReceive()
145 {
146 for (auto it = QueryNetworkGameSocketHandler::queries.begin(); it != QueryNetworkGameSocketHandler::queries.end(); /* nothing */) {
147 if (!(*it)->Receive()) {
148 it = QueryNetworkGameSocketHandler::queries.erase(it);
149 } else if (!(*it)->CheckConnection()) {
150 it = QueryNetworkGameSocketHandler::queries.erase(it);
151 } else {
152 it++;
153 }
154 }
155
156 for (auto &query : QueryNetworkGameSocketHandler::queries) {
157 query->Send();
158 }
159 }
NetworkGameSocketHandler::ReceivePackets
NetworkRecvStatus ReceivePackets()
Do the actual receiving of packets.
Definition: tcp_game.cpp:134
ClearGRFConfigList
void ClearGRFConfigList(GRFConfig **config)
Clear a GRF Config list, freeing all nodes.
Definition: newgrf_config.cpp:400
NetworkGameSocketHandler::last_packet
std::chrono::steady_clock::time_point last_packet
Time we received the last frame.
Definition: tcp_game.h:516
QueryNetworkGameSocketHandler::Receive_SERVER_FULL
NetworkRecvStatus Receive_SERVER_FULL(Packet *p) override
Notification that the server is full.
Definition: network_query.cpp:78
QueryNetworkGameSocketHandler::Send
void Send()
Send the packets of this socket handler.
Definition: network_query.cpp:64
NetworkTCPSocketHandler::sock
SOCKET sock
The socket currently connected to.
Definition: tcp.h:39
QueryNetworkGameSocketHandler::Receive
bool Receive()
Check whether we received/can send some data from/to the server and when that's the case handle it ap...
Definition: network_query.cpp:51
UpdateNetworkGameWindow
void UpdateNetworkGameWindow()
Update the network new window because a new server is found on the network.
Definition: network_gui.cpp:67
NetworkGameListAddItem
NetworkGameList * NetworkGameListAddItem(const std::string &connection_string)
Add a new item to the linked gamelist.
Definition: network_gamelist.cpp:32
QueryNetworkGameSocketHandler::queries
static std::vector< std::unique_ptr< QueryNetworkGameSocketHandler > > queries
Pending queries.
Definition: network_query.h:18
QueryNetworkGameSocketHandler::CheckConnection
bool CheckConnection()
Check the connection's state, i.e.
Definition: network_query.cpp:33
NGLS_TOO_OLD
@ NGLS_TOO_OLD
Server is too old to query.
Definition: network_gamelist.h:23
QueryNetworkGameSocketHandler::CloseConnection
NetworkRecvStatus CloseConnection(NetworkRecvStatus status) override
Close the network connection due to the given status.
Definition: network_query.cpp:22
PACKET_CLIENT_GAME_INFO
@ PACKET_CLIENT_GAME_INFO
Request information about the server.
Definition: tcp_game.h:47
NETWORK_RECV_STATUS_CLOSE_QUERY
@ NETWORK_RECV_STATUS_CLOSE_QUERY
Done querying the server.
Definition: core.h:32
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
Packet
Internal entity of a packet.
Definition: packet.h:44
DeserializeNetworkGameInfo
void DeserializeNetworkGameInfo(Packet *p, NetworkGameInfo *info, const GameInfoNewGRFLookupTable *newgrf_lookup_table)
Deserializes the NetworkGameInfo struct from the packet.
Definition: game_info.cpp:256
network_query.h
NetworkTCPSocketHandler::SendPackets
SendPacketsState SendPackets(bool closing_down=false)
Sends all the buffered packets out for this client.
Definition: tcp.cpp:99
NetworkTCPSocketHandler::CanSendReceive
bool CanSendReceive()
Check whether this socket can send or receive something.
Definition: tcp.cpp:218
QueryNetworkGameSocketHandler::Receive_SERVER_GAME_INFO
NetworkRecvStatus Receive_SERVER_GAME_INFO(Packet *p) override
Sends information about the game.
Definition: network_query.cpp:100
QueryNetworkGameSocketHandler::Receive_SERVER_ERROR
NetworkRecvStatus Receive_SERVER_ERROR(Packet *p) override
The client made an error: uint8 Error code caused (see NetworkErrorCode).
Definition: network_query.cpp:119
NetworkRecvStatus
NetworkRecvStatus
Status of a network client; reasons why a client has quit.
Definition: core.h:22
NGLS_BANNED
@ NGLS_BANNED
You are banned from this server.
Definition: network_gamelist.h:22
NGLS_ONLINE
@ NGLS_ONLINE
Server is online.
Definition: network_gamelist.h:20
game_info.h
Packet::Recv_uint8
uint8 Recv_uint8()
Read a 8 bits integer from the packet.
Definition: packet.cpp:317
error
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
Definition: openttd.cpp:134
CheckGameCompatibility
void CheckGameCompatibility(NetworkGameInfo &ngi)
Check if an game entry is compatible with our client.
Definition: game_info.cpp:107
NetworkGameList
Structure with information shown in the game list (GUI)
Definition: network_gamelist.h:27
NGLS_OFFLINE
@ NGLS_OFFLINE
Server is offline (or cannot be queried).
Definition: network_gamelist.h:19
NETWORK_RECV_STATUS_CONNECTION_LOST
@ NETWORK_RECV_STATUS_CONNECTION_LOST
The connection is lost unexpectedly.
Definition: core.h:33
QueryNetworkGameSocketHandler::SendReceive
static void SendReceive()
Check if any query needs to send or receive.
Definition: network_query.cpp:144
NETWORK_RECV_STATUS_OKAY
@ NETWORK_RECV_STATUS_OKAY
Everything is okay.
Definition: core.h:23
network_gamelist.h
QueryNetworkGameSocketHandler::SendGameInfo
NetworkRecvStatus SendGameInfo()
Query the server for server information.
Definition: network_query.cpp:72
NetworkGameList::refreshing
bool refreshing
Whether this server is being queried.
Definition: network_gamelist.h:34
NetworkGameList::info
NetworkGameInfo info
The game information of this server.
Definition: network_gamelist.h:30
QueryNetworkGameSocketHandler::connection_string
std::string connection_string
Address we are connected to.
Definition: network_query.h:19
NGLS_FULL
@ NGLS_FULL
Server is full and cannot be queried.
Definition: network_gamelist.h:21
NetworkServerGameInfo::grfconfig
GRFConfig * grfconfig
List of NewGRF files used.
Definition: game_info.h:94
NetworkErrorCode
NetworkErrorCode
The error codes we send around in the protocols.
Definition: network_type.h:119
QueryNetworkGameSocketHandler::Receive_SERVER_BANNED
NetworkRecvStatus Receive_SERVER_BANNED(Packet *p) override
Notification that the client trying to join is banned.
Definition: network_query.cpp:89
NetworkGameList::status
NetworkGameListStatus status
Stats of the server.
Definition: network_gamelist.h:32