OpenTTD: src/network/network_server.h Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

network_server.h
Go to the documentation of this file.
1 /* $Id: network_server.h 26576 2014-05-11 12:52:21Z rubidium $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
9
12 #ifndef NETWORK_SERVER_H
13 #define NETWORK_SERVER_H
14
15 #ifdef ENABLE_NETWORK
16
17 #include "network_internal.h"
18 #include "core/tcp_listen.h"
19 #include "../thread/thread.h"
20
21 class ServerNetworkGameSocketHandler;
23 typedef ServerNetworkGameSocketHandler NetworkClientSocket;
25 typedef Pool<NetworkClientSocket, ClientIndex, 8, MAX_CLIENT_SLOTS, PT_NCLIENT> NetworkClientSocketPool;
26 extern NetworkClientSocketPool _networkclientsocket_pool;
27
29 class ServerNetworkGameSocketHandler : public NetworkClientSocketPool::PoolItem<&_networkclientsocket_pool>, public NetworkGameSocketHandler, public TCPListenHandler<ServerNetworkGameSocketHandler, PACKET_SERVER_FULL, PACKET_SERVER_BANNED> {
30 protected:
31 virtual NetworkRecvStatus Receive_CLIENT_JOIN(Packet *p);
32 virtual NetworkRecvStatus Receive_CLIENT_COMPANY_INFO(Packet *p);
33 virtual NetworkRecvStatus Receive_CLIENT_GAME_PASSWORD(Packet *p);
34 virtual NetworkRecvStatus Receive_CLIENT_COMPANY_PASSWORD(Packet *p);
35 virtual NetworkRecvStatus Receive_CLIENT_GETMAP(Packet *p);
36 virtual NetworkRecvStatus Receive_CLIENT_MAP_OK(Packet *p);
37 virtual NetworkRecvStatus Receive_CLIENT_ACK(Packet *p);
38 virtual NetworkRecvStatus Receive_CLIENT_COMMAND(Packet *p);
39 virtual NetworkRecvStatus Receive_CLIENT_CHAT(Packet *p);
40 virtual NetworkRecvStatus Receive_CLIENT_SET_PASSWORD(Packet *p);
41 virtual NetworkRecvStatus Receive_CLIENT_SET_NAME(Packet *p);
42 virtual NetworkRecvStatus Receive_CLIENT_QUIT(Packet *p);
43 virtual NetworkRecvStatus Receive_CLIENT_ERROR(Packet *p);
44 virtual NetworkRecvStatus Receive_CLIENT_RCON(Packet *p);
45 virtual NetworkRecvStatus Receive_CLIENT_NEWGRFS_CHECKED(Packet *p);
46 virtual NetworkRecvStatus Receive_CLIENT_MOVE(Packet *p);
47
48 NetworkRecvStatus SendCompanyInfo();
49 NetworkRecvStatus SendNewGRFCheck();
50 NetworkRecvStatus SendWelcome();
51 NetworkRecvStatus SendWait();
52 NetworkRecvStatus SendNeedGamePassword();
53 NetworkRecvStatus SendNeedCompanyPassword();
54
55 public:
57 enum ClientStatus {
58 STATUS_INACTIVE,
59 STATUS_NEWGRFS_CHECK,
60 STATUS_AUTH_GAME,
61 STATUS_AUTH_COMPANY,
62 STATUS_AUTHORIZED,
63 STATUS_MAP_WAIT,
64 STATUS_MAP,
65 STATUS_DONE_MAP,
66 STATUS_PRE_ACTIVE,
67 STATUS_ACTIVE,
68 STATUS_END,
69 };
70
71 byte lag_test;
72 byte last_token;
73 uint32 last_token_frame;
74 ClientStatus status;
75 CommandQueue outgoing_queue;
76 int receive_limit;
77
78 struct PacketWriter *savegame;
79 NetworkAddress client_address;
80
81 ServerNetworkGameSocketHandler(SOCKET s);
82 ~ServerNetworkGameSocketHandler();
83
84 virtual Packet *ReceivePacket();
85 NetworkRecvStatus CloseConnection(NetworkRecvStatus status);
86 void GetClientName(char *client_name, const char *last) const;
87
88 NetworkRecvStatus SendMap();
89 NetworkRecvStatus SendErrorQuit(ClientID client_id, NetworkErrorCode errorno);
90 NetworkRecvStatus SendQuit(ClientID client_id);
91 NetworkRecvStatus SendShutdown();
92 NetworkRecvStatus SendNewGame();
93 NetworkRecvStatus SendRConResult(uint16 colour, const char *command);
94 NetworkRecvStatus SendMove(ClientID client_id, CompanyID company_id);
95
96 NetworkRecvStatus SendClientInfo(NetworkClientInfo *ci);
97 NetworkRecvStatus SendError(NetworkErrorCode error);
98 NetworkRecvStatus SendChat(NetworkAction action, ClientID client_id, bool self_send, const char *msg, int64 data);
99 NetworkRecvStatus SendJoin(ClientID client_id);
100 NetworkRecvStatus SendFrame();
101 NetworkRecvStatus SendSync();
102 NetworkRecvStatus SendCommand(const CommandPacket *cp);
103 NetworkRecvStatus SendCompanyUpdate();
104 NetworkRecvStatus SendConfigUpdate();
105
106 static void Send();
107 static void AcceptConnection(SOCKET s, const NetworkAddress &address);
108 static bool AllowConnection();
109
114 static const char *GetName()
115 {
116 return "server";
117 }
118
119 const char *GetClientIP();
120
121 static ServerNetworkGameSocketHandler *GetByClientID(ClientID client_id);
122 };
123
124 void NetworkServer_Tick(bool send_frame);
125 void NetworkServerSetCompanyPassword(CompanyID company_id, const char *password, bool already_hashed = true);
126 void NetworkServerUpdateCompanyPassworded(CompanyID company_id, bool passworded);
127
133 #define FOR_ALL_CLIENT_SOCKETS_FROM(var, start) FOR_ALL_ITEMS_FROM(NetworkClientSocket, clientsocket_index, var, start)
134
139 #define FOR_ALL_CLIENT_SOCKETS(var) FOR_ALL_CLIENT_SOCKETS_FROM(var, 0)
140
141 #else /* ENABLE_NETWORK */
142 /* Network function stubs when networking is disabled */
143
144 static inline void NetworkServerMonthlyLoop() {}
145 static inline void NetworkServerYearlyLoop() {}
146
147 #endif /* ENABLE_NETWORK */
148
149 #endif /* NETWORK_SERVER_H */