Fix issue in Rocket.lua script.
[cafu-Engine.git] / Ca3DE / Server / Server.hpp
blob0e2f52575f0d37516ed13fde4ecb68be649b4bc9
1 /*
2 Cafu Engine, http://www.cafu.de/
3 Copyright (c) Carsten Fuchs and other contributors.
4 This project is licensed under the terms of the MIT license.
5 */
7 #ifndef CAFU_SERVER_HPP_INCLUDED
8 #define CAFU_SERVER_HPP_INCLUDED
10 #include "Network/Network.hpp"
11 #include "Util/Util.hpp"
13 #include <stdexcept>
14 #include <string>
17 struct lua_State;
18 struct ClientInfoT;
19 class CaServerWorldT;
20 class GameInfoT;
21 class ModelManagerT;
22 namespace cf { namespace GuiSys { class GuiResourcesT; } }
25 /// The server, like the client, is a state machine.
26 /// It doesn't present its state explicitly however, but only as two implicit states: map loaded ("normal, running") and map unloaded ("idle").
27 /// As with the client, having a true "idle" state (rather than expressing it with a NULL ServerT instance pointer) has several advantages:
28 /// a) We can gracefully terminate pending network connections (e.g. resend reliable data in the clients zombie state), and
29 /// b) the server can receive and process conn-less network packets, and thus is available for administration via rcon commands.
30 class ServerT
32 public:
34 class InitErrorT;
36 /// A class that the server uses in order to let a GUI know in which state the server currently is.
37 /// The GUI uses it to decide which buttons it should enable/disable (i.e. which confuncs it makes sense to call).
38 /// (This is the C++ equivalent to a traditional C call-back function.)
39 class GuiCallbackI
41 public:
43 virtual void OnServerStateChanged(const char* NewState) const=0;
44 virtual ~GuiCallbackI() { }
48 /// The constructor.
49 /// @throws InitErrorT if the server could not be initialized (e.g. a socket for the desired port could not be aquired).
50 ServerT(const GameInfoT& GameInfo, const GuiCallbackI& GuiCallback_, ModelManagerT& ModelMan, cf::GuiSys::GuiResourcesT& GuiRes);
52 ~ServerT();
54 void MainLoop(); // Server main loop. To be called once per frame.
57 static int ConFunc_changeLevel_Callback(lua_State* LuaState);
59 /// A console function that stores the given command string until the server "thinks" next.
60 /// The RunMapCmdsFromConsole() method then runs the commands in the context of the current map/entity script.
61 static int ConFunc_runMapCmd_Callback(lua_State* LuaState);
64 private:
66 ServerT(const ServerT&); ///< Use of the Copy Constructor is not allowed.
67 void operator = (const ServerT&); ///< Use of the Assignment Operator is not allowed.
69 void DropClient(unsigned long ClientNr, const char* Reason);
70 void ProcessConnectionLessPacket(NetDataT& InData, const NetAddressT& SenderAddress);
71 void ProcessInGamePacket (NetDataT& InData);
72 static void ProcessInGamePacketHelper(NetDataT& InData, unsigned long LastIncomingSequenceNr);
75 TimerT Timer;
76 SOCKET ServerSocket;
77 ArrayT<ClientInfoT*> ClientInfos;
78 const GameInfoT& m_GameInfo;
79 std::string WorldName;
80 CaServerWorldT* World;
81 const GuiCallbackI& GuiCallback;
82 ModelManagerT& m_ModelMan;
83 cf::GuiSys::GuiResourcesT& m_GuiRes;
87 /// A class that is thrown on server initialization errors.
88 class ServerT::InitErrorT : public std::runtime_error
90 public:
92 InitErrorT(const std::string& Message) : std::runtime_error(Message) { }
95 #endif