# 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;
    00023 typedef ServerNetworkGameSocketHandler NetworkClientSocket;
    00025 typedef Pool<NetworkClientSocket, ClientIndex, 8, MAX_CLIENT_SLOTS, PT_NCLIENT> NetworkClientSocketPool;
    00026 extern NetworkClientSocketPool _networkclientsocket_pool;
    00027
    00029 class ServerNetworkGameSocketHandler : public NetworkClientSocketPool::PoolItem<&_networkclientsocket_pool>, public NetworkGameSocketHandler, public TCPListenHandler<ServerNetworkGameSocketHandler, PACKET_SERVER_FULL, PACKET_SERVER_BANNED> {
    00030 protected:
    00031   virtual NetworkRecvStatus Receive_CLIENT_JOIN(Packet *p);
    00032   virtual NetworkRecvStatus Receive_CLIENT_COMPANY_INFO(Packet *p);
    00033   virtual NetworkRecvStatus Receive_CLIENT_GAME_PASSWORD(Packet *p);
    00034   virtual NetworkRecvStatus Receive_CLIENT_COMPANY_PASSWORD(Packet *p);
    00035   virtual NetworkRecvStatus Receive_CLIENT_GETMAP(Packet *p);
    00036   virtual NetworkRecvStatus Receive_CLIENT_MAP_OK(Packet *p);
    00037   virtual NetworkRecvStatus Receive_CLIENT_ACK(Packet *p);
    00038   virtual NetworkRecvStatus Receive_CLIENT_COMMAND(Packet *p);
    00039   virtual NetworkRecvStatus Receive_CLIENT_CHAT(Packet *p);
    00040   virtual NetworkRecvStatus Receive_CLIENT_SET_PASSWORD(Packet *p);
    00041   virtual NetworkRecvStatus Receive_CLIENT_SET_NAME(Packet *p);
    00042   virtual NetworkRecvStatus Receive_CLIENT_QUIT(Packet *p);
    00043   virtual NetworkRecvStatus Receive_CLIENT_ERROR(Packet *p);
    00044   virtual NetworkRecvStatus Receive_CLIENT_RCON(Packet *p);
    00045   virtual NetworkRecvStatus Receive_CLIENT_NEWGRFS_CHECKED(Packet *p);
    00046   virtual NetworkRecvStatus Receive_CLIENT_MOVE(Packet *p);
    00047
    00048   NetworkRecvStatus SendCompanyInfo();
    00049   NetworkRecvStatus SendNewGRFCheck();
    00050   NetworkRecvStatus SendWelcome();
    00051   NetworkRecvStatus SendWait();
    00052   NetworkRecvStatus SendNeedGamePassword();
    00053   NetworkRecvStatus SendNeedCompanyPassword();
    00054
    00055 public:
    00057   enum ClientStatus {
    00058     STATUS_INACTIVE,
    00059     STATUS_NEWGRFS_CHECK,
    00060     STATUS_AUTH_GAME,
    00061     STATUS_AUTH_COMPANY,
    00062     STATUS_AUTHORIZED,
    00063     STATUS_MAP_WAIT,
    00064     STATUS_MAP,
    00065     STATUS_DONE_MAP,
    00066     STATUS_PRE_ACTIVE,
    00067     STATUS_ACTIVE,
    00068     STATUS_END,
    00069   };
    00070
    00071   byte lag_test;
    00072   byte last_token;
    00073   uint32 last_token_frame;
    00074   ClientStatus status;
    00075   CommandQueue outgoing_queue;
    00076   int receive_limit;
    00077
    00078   Packet *savegame_packets;
    00079   struct PacketWriter *savegame;
    00080   ThreadMutex *savegame_mutex;
    00081   NetworkAddress client_address;
    00082
    00083   ServerNetworkGameSocketHandler(SOCKET s);
    00084   ~ServerNetworkGameSocketHandler();
    00085
    00086   virtual Packet *ReceivePacket();
    00087   virtual void SendPacket(Packet *packet);
    00088   NetworkRecvStatus CloseConnection(NetworkRecvStatus status);
    00089   void GetClientName(char *client_name, size_t size) const;
    00090
    00091   NetworkRecvStatus SendMap();
    00092   NetworkRecvStatus SendErrorQuit(ClientID client_id, NetworkErrorCode errorno);
    00093   NetworkRecvStatus SendQuit(ClientID client_id);
    00094   NetworkRecvStatus SendShutdown();
    00095   NetworkRecvStatus SendNewGame();
    00096   NetworkRecvStatus SendRConResult(uint16 colour, const char *command);
    00097   NetworkRecvStatus SendMove(ClientID client_id, CompanyID company_id);
    00098
    00099   NetworkRecvStatus SendClientInfo(NetworkClientInfo *ci);
    00100   NetworkRecvStatus SendError(NetworkErrorCode error);
    00101   NetworkRecvStatus SendChat(NetworkAction action, ClientID client_id, bool self_send, const char *msg, int64 data);
    00102   NetworkRecvStatus SendJoin(ClientID client_id);
    00103   NetworkRecvStatus SendFrame();
    00104   NetworkRecvStatus SendSync();
    00105   NetworkRecvStatus SendCommand(const CommandPacket *cp);
    00106   NetworkRecvStatus SendCompanyUpdate();
    00107   NetworkRecvStatus SendConfigUpdate();
    00108
    00109   static void Send();
    00110   static void AcceptConnection(SOCKET s, const NetworkAddress &address);
    00111   static bool AllowConnection();
    00112
    00117   static const char *GetName()
    00118   {
    00119     return "server";
    00120   }
    00121
    00122   const char *GetClientIP();
    00123
    00124   static ServerNetworkGameSocketHandler *GetByClientID(ClientID client_id);
    00125 };
    00126
    00127 void NetworkServer_Tick(bool send_frame);
    00128 void NetworkServerSetCompanyPassword(CompanyID company_id, const char *password, bool already_hashed = true);
    00129
    00135 #define FOR_ALL_CLIENT_SOCKETS_FROM(var, start) FOR_ALL_ITEMS_FROM(NetworkClientSocket, clientsocket_index, var, start)
    00136
    00141 #define FOR_ALL_CLIENT_SOCKETS(var) FOR_ALL_CLIENT_SOCKETS_FROM(var, 0)
    00142
    00143 #else /* ENABLE_NETWORK */
    00144 /* Network function stubs when networking is disabled */
    00145
    00146 static inline void NetworkServerMonthlyLoop() {}
    00147 static inline void NetworkServerYearlyLoop() {}
    00148
    00149 #endif /* ENABLE_NETWORK */
    00150
    00151 #endif /* NETWORK_SERVER_H */