Cuberite
A lightweight, fast and extensible game server for Minecraft
Server.h
Go to the documentation of this file.
1 
2 // cServer.h
3 
4 // Interfaces to the cServer object representing the network server
5 
6 
7 
8 
9 
10 #pragma once
11 
12 #include "RCONServer.h"
13 #include "OSSupport/IsThread.h"
14 #include "OSSupport/Network.h"
15 
16 #ifdef _MSC_VER
17  #pragma warning(push)
18  #pragma warning(disable:4127)
19  #pragma warning(disable:4244)
20  #pragma warning(disable:4231)
21  #pragma warning(disable:4189)
22  #pragma warning(disable:4702)
23 #endif
24 
26 
27 #ifdef _MSC_VER
28  #pragma warning(pop)
29 #endif
30 
31 
32 
33 
34 
35 // fwd:
36 class cClientHandle;
37 typedef std::shared_ptr<cClientHandle> cClientHandlePtr;
38 typedef std::list<cClientHandlePtr> cClientHandlePtrs;
39 typedef std::list<cClientHandle *> cClientHandles;
42 class cUUID;
43 
44 
45 namespace Json
46 {
47  class Value;
48 }
49 
50 
51 
52 
53 
54 // tolua_begin
55 class cServer
56 {
57 public:
58  // tolua_end
59 
60  virtual ~cServer() {}
61  bool InitServer(cSettingsRepositoryInterface & a_Settings, bool a_ShouldAuth);
62 
63  // tolua_begin
64 
65  const AString & GetDescription(void) const {return m_Description; }
66 
67  const AString & GetShutdownMessage(void) const { return m_ShutdownMessage; }
68 
69  // Player counts:
70  size_t GetMaxPlayers(void) const { return m_MaxPlayers; }
71  size_t GetNumPlayers(void) const { return m_PlayerCount; }
72  void SetMaxPlayers(size_t a_MaxPlayers) { m_MaxPlayers = a_MaxPlayers; }
73 
74  // tolua_end
75 
77  bool RegisterForgeMod(const AString & a_ModName, const AString & a_ModVersion, UInt32 a_ProtocolVersionNumber);
78 
79  // tolua_begin
80 
82  void UnregisterForgeMod(const AString & a_ModName, UInt32 a_ProtocolVersionNumber);
83 
86  bool IsPlayerInQueue(const AString & a_Username);
87 
90  bool DoesAllowMultiLogin(void) { return m_bAllowMultiLogin; }
91 
92  // Hardcore mode or not:
93  bool IsHardcore(void) const { return m_bIsHardcore; }
94 
95  // tolua_end
96 
99 
100  const AString & GetResourcePackUrl(void) { return m_ResourcePackUrl; }
101 
102  std::string_view GetCustomRedirectUrl(void) { return m_CustomRedirectUrl; }
103 
104  bool Start(void);
105 
106  bool Command(cClientHandle & a_Client, AString & a_Cmd);
107 
111  void QueueExecuteConsoleCommand(const AString & a_Cmd, cCommandOutputCallback & a_Output);
112 
114  void ScheduleTask(cTickTime a_DelayTicks, std::function<void(class cServer &)> a_Task);
115 
117  void PrintHelp(const AStringVector & a_Split, cCommandOutputCallback & a_Output);
118 
120  static void BindBuiltInConsoleCommands(void);
121 
122  void Shutdown(void);
123 
124  void KickUser(int a_ClientID, const AString & a_Reason);
125 
127  void AuthenticateUser(int a_ClientID, AString && a_Username, const cUUID & a_UUID, Json::Value && a_Properties);
128 
129  const AString & GetServerID(void) const { return m_ServerID; } // tolua_export
130 
132  void ClientMovedToWorld(const cClientHandle * a_Client);
133 
135  void PlayerCreated();
136 
138  void PlayerDestroyed();
139 
141  const AString & GetFaviconData(void) const { return m_FaviconData; }
142 
145 
147  bool ShouldAuthenticate(void) const { return m_ShouldAuthenticate; } // tolua_export
148 
151 
155  bool ShouldAllowBungeeCord(void) const { return m_ShouldAllowBungeeCord; }
156 
157  bool OnlyAllowBungeeCord(void) const { return m_OnlyAllowBungeeCord; }
158 
159  const AString & GetProxySharedSecret(void) const { return m_ProxySharedSecret; }
160 
164 
166  const AStringMap & GetRegisteredForgeMods(const UInt32 a_Protocol);
167 
168 private:
169 
170  friend class cRoot; // so cRoot can create and destroy cServer
171  friend class cServerListenCallbacks; // Accessing OnConnectionAccepted()
172 
173 
174 
176  class cTickThread:
177  public cIsThread
178  {
179  using Super = cIsThread;
180 
181  public:
182 
183  cTickThread(cServer & a_Server);
184 
185  protected:
187 
188  // cIsThread overrides:
189  virtual void Execute(void) override;
190  } ;
191 
192 
193 
196 
199 
202 
205 
207  std::atomic_size_t m_PlayerCount;
208 
210  std::vector<std::pair<AString, cCommandOutputCallback *>> m_PendingCommands;
211 
212  int m_ClientViewDistance; // The default view distance for clients; settable in Settings.ini
213 
214  bool m_bIsConnected; // true - connected false - not connected
215 
218 
221 
223 
227  size_t m_MaxPlayers;
232 
234  std::map<UInt32, AStringMap> m_ForgeModsByVersion;
235 
238 
240 
243 
247 
250 
253 
256 
259 
262 
266 
267 
271 
274 
277  std::vector<std::pair<std::chrono::milliseconds, std::function<void(class cServer &)>>> m_Tasks;
278 
279 
280  cServer(void);
281 
283  void ExecuteConsoleCommand(const AString & a_Cmd, cCommandOutputCallback & a_Output);
284 
286  AStringMap & RegisteredForgeMods(const UInt32 a_Protocol);
287 
289  void PrepareKeys(void);
290 
293  cTCPLink::cCallbacksPtr OnConnectionAccepted(const AString & a_RemoteIPAddress);
294 
295  void Tick(float a_Dt);
296 
298  void TickClients(float a_Dt);
299 
301  void TickCommands(void);
302 
303 
305  void TickQueuedTasks(void);
306 
307 
308 }; // tolua_export
309 
310 
311 
312 
std::chrono::duration< signed long long int, cTickTime::period > cTickTimeLong
Definition: Globals.h:367
unsigned int UInt32
Definition: Globals.h:157
std::basic_string_view< std::byte > ContiguousByteBufferView
Definition: Globals.h:376
std::chrono::duration< signed int, std::ratio_multiply< std::chrono::milliseconds::period, std::ratio< 50 > >> cTickTime
Definition: Globals.h:364
std::basic_string< std::byte > ContiguousByteBuffer
Definition: Globals.h:375
std::vector< cServerHandlePtr > cServerHandlePtrs
Definition: Network.h:30
std::list< cClientHandle * > cClientHandles
Definition: Server.h:39
std::shared_ptr< cClientHandle > cClientHandlePtr
Definition: Server.h:36
std::list< cClientHandlePtr > cClientHandlePtrs
Definition: Server.h:38
std::vector< AString > AStringVector
Definition: StringUtils.h:12
std::string AString
Definition: StringUtils.h:11
std::map< AString, AString > AStringMap
A string dictionary, used for key-value pairs.
Definition: StringUtils.h:16
Definition: Inventory.h:11
Interface for a callback that receives command output The Out() function is called for any output the...
Definition: CommandOutput.h:16
Encapsulates an RSA private key used in PKI cryptography.
Definition: RsaPrivateKey.h:21
cIsThread(AString &&a_ThreadName)
Definition: IsThread.cpp:16
std::shared_ptr< cCallbacks > cCallbacksPtr
Definition: Network.h:71
The root of the object hierarchy.
Definition: Root.h:50
Definition: Server.h:56
const AString & GetShutdownMessage(void) const
Definition: Server.h:67
AString m_CustomRedirectUrl
Definition: Server.h:231
cRCONServer m_RCONServer
Definition: Server.h:222
void SetMaxPlayers(size_t a_MaxPlayers)
Definition: Server.h:72
void TickClients(float a_Dt)
Ticks the clients in m_Clients, manages the list in respect to removing clients.
Definition: Server.cpp:354
void PlayerCreated()
Notifies the server that a player was created; the server uses this to adjust the number of players.
Definition: Server.cpp:135
ContiguousByteBufferView GetPublicKeyDER(void) const
Definition: Server.h:144
void ExecuteConsoleCommand(const AString &a_Cmd, cCommandOutputCallback &a_Output)
Executes the console command, sends output through the specified callback.
Definition: Server.cpp:470
bool m_ShouldLimitPlayerBlockChanges
True if limit for number of block changes per tick by a player should be enabled.
Definition: Server.h:249
int m_ClientViewDistance
Definition: Server.h:212
size_t GetMaxPlayers(void) const
Definition: Server.h:70
void PrepareKeys(void)
Loads, or generates, if missing, RSA keys for protocol encryption.
Definition: Server.cpp:308
const AString & GetProxySharedSecret(void) const
Definition: Server.h:159
cTCPLink::cCallbacksPtr OnConnectionAccepted(const AString &a_RemoteIPAddress)
Creates a new cClientHandle instance and adds it to the list of clients.
Definition: Server.cpp:319
void PrintHelp(const AStringVector &a_Split, cCommandOutputCallback &a_Output)
Lists all available console commands and their helpstrings.
Definition: Server.cpp:592
bool m_bAllowMultiLogin
True - allow same username to login more than once False - only once.
Definition: Server.h:237
const AString & GetDescription(void) const
Definition: Server.h:65
void TickCommands(void)
Executes commands queued in the command queue.
Definition: Server.cpp:740
void UnregisterForgeMod(const AString &a_ModName, UInt32 a_ProtocolVersionNumber)
Remove a Forge mod to the server ping list.
Definition: Server.cpp:249
cServer(void)
Definition: Server.cpp:106
void Tick(float a_Dt)
Definition: Server.cpp:332
ContiguousByteBuffer m_PublicKeyDER
Public key for m_PrivateKey, ASN1-DER-encoded.
Definition: Server.h:220
bool IsPlayerInQueue(const AString &a_Username)
Check if the player is queued to be transferred to a World.
Definition: Server.cpp:291
std::vector< std::pair< std::chrono::milliseconds, std::function< void(class cServer &)> > > m_Tasks
Tasks that have been queued onto the tick thread, possibly to be executed at target tick in the futur...
Definition: Server.h:277
bool m_ShouldAllowBungeeCord
True if BungeeCord handshake packets (with player UUID) should be accepted.
Definition: Server.h:252
cServerHandlePtrs m_ServerHandles
The network sockets listening for client connections.
Definition: Server.h:195
bool RegisterForgeMod(const AString &a_ModName, const AString &a_ModVersion, UInt32 a_ProtocolVersionNumber)
Add a Forge mod to the server ping list.
Definition: Server.cpp:238
std::string_view GetCustomRedirectUrl(void)
Definition: Server.h:102
const AStringMap & GetRegisteredForgeMods(const UInt32 a_Protocol)
Get the Forge mods (map of ModName -> ModVersionString) registered for a given protocol.
Definition: Server.cpp:282
bool Command(cClientHandle &a_Client, AString &a_Cmd)
Definition: Server.cpp:429
const AString & GetServerID(void) const
Definition: Server.h:129
bool m_bIsHardcore
Definition: Server.h:228
AString m_ServerID
The server ID used for client authentication.
Definition: Server.h:242
std::atomic_size_t m_PlayerCount
Number of players currently playing in the server.
Definition: Server.h:207
bool m_ShouldAuthenticate
If true, players will be online-authenticated agains Mojang servers.
Definition: Server.h:246
cClientHandles m_ClientsToRemove
Clients that have just been moved into a world and are to be removed from m_Clients in the next Tick(...
Definition: Server.h:204
bool ShouldAllowBungeeCord(void) const
Returns true if BungeeCord logins (that specify the player's UUID) are allowed.
Definition: Server.h:155
void TickQueuedTasks(void)
Executes all tasks queued onto the tick thread.
Definition: Server.cpp:759
cTickThread m_TickThread
Definition: Server.h:239
bool Start(void)
Definition: Server.cpp:400
AString m_ResourcePackUrl
Definition: Server.h:230
size_t m_MaxPlayers
Definition: Server.h:227
cCriticalSection m_CSTasks
Guards the m_Tasks.
Definition: Server.h:273
std::vector< std::pair< AString, cCommandOutputCallback * > > m_PendingCommands
Definition: Server.h:210
virtual ~cServer()
Definition: Server.h:60
void ClientMovedToWorld(const cClientHandle *a_Client)
Don't tick a_Client anymore, it will be ticked from its cPlayer instead.
Definition: Server.cpp:125
cCriticalSection m_CSClients
Protects m_Clients and m_ClientsToRemove against multithreaded access.
Definition: Server.h:198
bool InitServer(cSettingsRepositoryInterface &a_Settings, bool a_ShouldAuth)
Definition: Server.cpp:153
const AString & GetResourcePackUrl(void)
Definition: Server.h:100
static void BindBuiltInConsoleCommands(void)
Binds the built-in console commands with the plugin manager.
Definition: Server.cpp:636
bool OnlyAllowBungeeCord(void) const
Definition: Server.h:157
bool m_RequireResourcePack
Definition: Server.h:229
void QueueExecuteConsoleCommand(const AString &a_Cmd, cCommandOutputCallback &a_Output)
Queues a console command for execution through the cServer class.
Definition: Server.cpp:445
size_t GetNumPlayers(void) const
Definition: Server.h:71
std::map< UInt32, AStringMap > m_ForgeModsByVersion
Map of protocol version to Forge mods (map of ModName -> ModVersionString)
Definition: Server.h:234
bool m_ShouldAllowMultiWorldTabCompletion
True if usernames should be completed across worlds.
Definition: Server.h:261
AString m_Description
Definition: Server.h:224
cRsaPrivateKey m_PrivateKey
The private key used for the assymetric encryption start in the protocols.
Definition: Server.h:217
void Shutdown(void)
Definition: Server.cpp:671
AStringVector m_Ports
The list of ports on which the server should listen for connections.
Definition: Server.h:265
cRsaPrivateKey & GetPrivateKey(void)
Definition: Server.h:143
bool IsHardcore(void) const
Definition: Server.h:93
AString m_ProxySharedSecret
Security string that the proxy server should send, compatible with BungeeGuard.
Definition: Server.h:258
void KickUser(int a_ClientID, const AString &a_Reason)
Definition: Server.cpp:699
bool ShouldAllowMultiWorldTabCompletion(void) const
Returns true if usernames should be completed across worlds.
Definition: Server.h:163
bool m_bIsConnected
Definition: Server.h:214
void PlayerDestroyed()
Notifies the server that a player is being destroyed; the server uses this to adjust the number of pl...
Definition: Server.cpp:144
cClientHandlePtrs m_Clients
Clients that are connected to the server and not yet assigned to a cWorld.
Definition: Server.h:201
AStringMap & RegisteredForgeMods(const UInt32 a_Protocol)
Get the Forge mods registered for a given protocol, for modification.
Definition: Server.cpp:264
AString m_FaviconData
Definition: Server.h:226
void AuthenticateUser(int a_ClientID, AString &&a_Username, const cUUID &a_UUID, Json::Value &&a_Properties)
Authenticates the specified user, called by cAuthenticator supplying player details from Mojang.
Definition: Server.cpp:715
cCriticalSection m_CSPendingCommands
Definition: Server.h:209
bool ShouldRequireResourcePack(void)
Returns true if clients must accept resource pack.
Definition: Server.h:98
bool ShouldAuthenticate(void) const
Returns true if authentication has been turned on in server settings.
Definition: Server.h:147
AString m_ShutdownMessage
Definition: Server.h:225
const AString & GetFaviconData(void) const
Returns base64 encoded favicon data (obtained from favicon.png)
Definition: Server.h:141
void ScheduleTask(cTickTime a_DelayTicks, std::function< void(class cServer &)> a_Task)
Queues a lambda task onto the server tick thread, with the specified delay in ticks.
Definition: Server.cpp:456
bool m_OnlyAllowBungeeCord
True if BungeeCord handshake packets should be the only ones accepted.
Definition: Server.h:255
bool ShouldLimitPlayerBlockChanges(void) const
Returns true if limit for number of block changes per tick by a player has been turned on in server s...
Definition: Server.h:150
cTickTimeLong m_UpTime
Time, in ticks, since the server started Not persistent across server restarts.
Definition: Server.h:270
bool DoesAllowMultiLogin(void)
Can login more than once with same username.
Definition: Server.h:90
The server tick thread takes care of the players who aren't yet spawned in a world.
Definition: Server.h:178
cTickThread(cServer &a_Server)
Definition: Server.cpp:67
virtual void Execute(void) override
This function, overloaded by the descendants, is called in the new thread.
Definition: Server.cpp:77
cServer & m_Server
Definition: Server.h:186
Definition: UUID.h:11