# network_server.h

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #ifndef NETWORK_SERVER_H
    00013 #define NETWORK_SERVER_H
    00014
    00015 #ifdef ENABLE_NETWORK
    00016
    00017 #include "network_internal.h"
    00018 #include "core/tcp_listen.h"
    00019 #include "../thread/thread.h"
    00020
    00021 class ServerNetworkGameSocketHandler;
    00022 typedef ServerNetworkGameSocketHandler NetworkClientSocket;
    00023 typedef Pool<NetworkClientSocket, ClientIndex, 8, MAX_CLIENT_SLOTS> NetworkClientSocketPool;
    00024 extern NetworkClientSocketPool _networkclientsocket_pool;
    00025
    00027 class ServerNetworkGameSocketHandler : public NetworkClientSocketPool::PoolItem<&_networkclientsocket_pool>, public NetworkGameSocketHandler, public TCPListenHandler<ServerNetworkGameSocketHandler, PACKET_SERVER_FULL, PACKET_SERVER_BANNED> {
    00028 protected:
    00029   DECLARE_GAME_RECEIVE_COMMAND(PACKET_CLIENT_JOIN);
    00030   DECLARE_GAME_RECEIVE_COMMAND(PACKET_CLIENT_COMPANY_INFO);
    00031   DECLARE_GAME_RECEIVE_COMMAND(PACKET_CLIENT_GAME_PASSWORD);
    00032   DECLARE_GAME_RECEIVE_COMMAND(PACKET_CLIENT_COMPANY_PASSWORD);
    00033   DECLARE_GAME_RECEIVE_COMMAND(PACKET_CLIENT_GETMAP);
    00034   DECLARE_GAME_RECEIVE_COMMAND(PACKET_CLIENT_MAP_OK);
    00035   DECLARE_GAME_RECEIVE_COMMAND(PACKET_CLIENT_ACK);
    00036   DECLARE_GAME_RECEIVE_COMMAND(PACKET_CLIENT_COMMAND);
    00037   DECLARE_GAME_RECEIVE_COMMAND(PACKET_CLIENT_CHAT);
    00038   DECLARE_GAME_RECEIVE_COMMAND(PACKET_CLIENT_SET_PASSWORD);
    00039   DECLARE_GAME_RECEIVE_COMMAND(PACKET_CLIENT_SET_NAME);
    00040   DECLARE_GAME_RECEIVE_COMMAND(PACKET_CLIENT_QUIT);
    00041   DECLARE_GAME_RECEIVE_COMMAND(PACKET_CLIENT_ERROR);
    00042   DECLARE_GAME_RECEIVE_COMMAND(PACKET_CLIENT_RCON);
    00043   DECLARE_GAME_RECEIVE_COMMAND(PACKET_CLIENT_NEWGRFS_CHECKED);
    00044   DECLARE_GAME_RECEIVE_COMMAND(PACKET_CLIENT_MOVE);
    00045
    00046   NetworkRecvStatus SendCompanyInfo();
    00047   NetworkRecvStatus SendNewGRFCheck();
    00048   NetworkRecvStatus SendWelcome();
    00049   NetworkRecvStatus SendWait();
    00050   NetworkRecvStatus SendNeedGamePassword();
    00051   NetworkRecvStatus SendNeedCompanyPassword();
    00052
    00053 public:
    00055   enum ClientStatus {
    00056     STATUS_INACTIVE,
    00057     STATUS_NEWGRFS_CHECK,
    00058     STATUS_AUTH_GAME,
    00059     STATUS_AUTH_COMPANY,
    00060     STATUS_AUTHORIZED,
    00061     STATUS_MAP_WAIT,
    00062     STATUS_MAP,
    00063     STATUS_DONE_MAP,
    00064     STATUS_PRE_ACTIVE,
    00065     STATUS_ACTIVE,
    00066     STATUS_END
    00067   };
    00068
    00069   byte lag_test;
    00070   byte last_token;
    00071   uint32 last_token_frame;
    00072   ClientStatus status;
    00073   CommandQueue outgoing_queue;
    00074   int receive_limit;
    00075
    00076   Packet *savegame_packets;
    00077   struct PacketWriter *savegame;
    00078   ThreadMutex *savegame_mutex;
    00079
    00080   ServerNetworkGameSocketHandler(SOCKET s);
    00081   ~ServerNetworkGameSocketHandler();
    00082
    00083   virtual Packet *ReceivePacket();
    00084   virtual void SendPacket(Packet *packet);
    00085   NetworkRecvStatus CloseConnection(NetworkRecvStatus status);
    00086   void GetClientName(char *client_name, size_t size) const;
    00087
    00088   NetworkRecvStatus SendMap();
    00089   NetworkRecvStatus SendErrorQuit(ClientID client_id, NetworkErrorCode errorno);
    00090   NetworkRecvStatus SendQuit(ClientID client_id);
    00091   NetworkRecvStatus SendShutdown();
    00092   NetworkRecvStatus SendNewGame();
    00093   NetworkRecvStatus SendRConResult(uint16 colour, const char *command);
    00094   NetworkRecvStatus SendMove(ClientID client_id, CompanyID company_id);
    00095
    00096   NetworkRecvStatus SendClientInfo(NetworkClientInfo *ci);
    00097   NetworkRecvStatus SendError(NetworkErrorCode error);
    00098   NetworkRecvStatus SendChat(NetworkAction action, ClientID client_id, bool self_send, const char *msg, int64 data);
    00099   NetworkRecvStatus SendJoin(ClientID client_id);
    00100   NetworkRecvStatus SendFrame();
    00101   NetworkRecvStatus SendSync();
    00102   NetworkRecvStatus SendCommand(const CommandPacket *cp);
    00103   NetworkRecvStatus SendCompanyUpdate();
    00104   NetworkRecvStatus SendConfigUpdate();
    00105
    00106   static void Send();
    00107   static void AcceptConnection(SOCKET s, const NetworkAddress &address);
    00108   static bool AllowConnection();
    00109
    00114   static const char *GetName()
    00115   {
    00116     return "server";
    00117   }
    00118 };
    00119
    00120 void NetworkServer_Tick(bool send_frame);
    00121 void NetworkServerSetCompanyPassword(CompanyID company_id, const char *password, bool already_hashed = true);
    00122
    00123 #define FOR_ALL_CLIENT_SOCKETS_FROM(var, start) FOR_ALL_ITEMS_FROM(NetworkClientSocket, clientsocket_index, var, start)
    00124 #define FOR_ALL_CLIENT_SOCKETS(var) FOR_ALL_CLIENT_SOCKETS_FROM(var, 0)
    00125
    00126 #else /* ENABLE_NETWORK */
    00127 /* Network function stubs when networking is disabled */
    00128
    00129 static inline void NetworkServerMonthlyLoop() {}
    00130 static inline void NetworkServerYearlyLoop() {}
    00131
    00132 #endif /* ENABLE_NETWORK */
    00133
    00134 #endif /* NETWORK_SERVER_H */