Cafu Engine
Server.hpp
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 */
6 
7 #ifndef CAFU_SERVER_HPP_INCLUDED
8 #define CAFU_SERVER_HPP_INCLUDED
9 
10 #include "Network/Network.hpp"
11 #include "Util/Util.hpp"
12 
13 #include <stdexcept>
14 #include <string>
15 
16 
17 struct lua_State;
18 struct ClientInfoT;
19 class CaServerWorldT;
20 class GameInfoT;
21 class ModelManagerT;
22 namespace cf { namespace GuiSys { class GuiResourcesT; } }
23 
24 
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
31 {
32  public:
33 
34  class InitErrorT;
35 
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.)
40  {
41  public:
42 
43  virtual void OnServerStateChanged(const char* NewState) const=0;
44  virtual ~GuiCallbackI() { }
45  };
46 
47 
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);
51 
52  ~ServerT();
53 
54  void MainLoop(); // Server main loop. To be called once per frame.
55 
56 
57  static int ConFunc_changeLevel_Callback(lua_State* LuaState);
58 
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);
62 
63 
64  private:
65 
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.
68 
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);
73 
74 
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;
84 };
85 
86 
87 /// A class that is thrown on server initialization errors.
88 class ServerT::InitErrorT : public std::runtime_error
89 {
90  public:
91 
92  InitErrorT(const std::string& Message) : std::runtime_error(Message) { }
93 };
94 
95 #endif
Network address consisting of an IP4 address and port number.
Definition: Network.hpp:98
A class that the server uses in order to let a GUI know in which state the server currently is...
Definition: Server.hpp:39
The server, like the client, is a state machine.
Definition: Server.hpp:30
static int ConFunc_runMapCmd_Callback(lua_State *LuaState)
A console function that stores the given command string until the server "thinks" next...
Definition: Server.cpp:141
Definition: ServerWorld.hpp:19
A class that is thrown on server initialization errors.
Definition: Server.hpp:88
Class that allows easy and portable handling, sending and receiving of data over a network...
Definition: Network.hpp:181
ServerT(const GameInfoT &GameInfo, const GuiCallbackI &GuiCallback_, ModelManagerT &ModelMan, cf::GuiSys::GuiResourcesT &GuiRes)
The constructor.
Definition: Server.cpp:155
A platform independent timer class that allows to measure the time passed since its construction or t...
Definition: Util.hpp:24
This class encapsulates information about a game.
Definition: GameInfo.hpp:14
Definition: ClientInfo.hpp:13
This class is used for managing model instances.
Definition: ModelManager.hpp:31
This class manages and provides resources (fonts and models) for GuiImplT instances.
Definition: GuiResources.hpp:26