Emergency 20 Dokumentation  4.2.0
Host.h
Go to the documentation of this file.
1 // Copyright (C) 2012-2018 Promotion Software GmbH
2 
3 
4 //[-------------------------------------------------------]
5 //[ Header guard ]
6 //[-------------------------------------------------------]
7 #pragma once
8 
9 
10 //[-------------------------------------------------------]
11 //[ Includes ]
12 //[-------------------------------------------------------]
18 #include "em5/map/MapIdentifier.h"
19 
20 #include <qsf/job/JobProxy.h>
22 #include <qsf/math/Transform.h>
23 
24 #include <vector>
25 #include <unordered_map>
26 
27 
28 //[-------------------------------------------------------]
29 //[ Forward declarations ]
30 //[-------------------------------------------------------]
31 namespace qsf
32 {
33  class Layer;
34  class Map;
35  class Clock;
36  namespace game
37  {
38  class BitStream;
39  class ServerConnection;
40  }
41 }
42 namespace em5
43 {
44  class MapIdentifier;
45  class MatchmakingClient;
46  namespace multiplayer
47  {
48  namespace server
49  {
50  class ClientBase;
51  }
52  namespace proxy
53  {
54  class MultiplayerProxyClient;
55  }
56  }
57  namespace packet
58  {
59  class ProxyServerPropertiesResponse;
60  }
61 }
62 
63 
64 //[-------------------------------------------------------]
65 //[ Namespace ]
66 //[-------------------------------------------------------]
67 namespace em5
68 {
69  namespace multiplayer
70  {
71  namespace server
72  {
73 
74 
75  //[-------------------------------------------------------]
76  //[ Classes ]
77  //[-------------------------------------------------------]
83  {
84 
85 
86  //[-------------------------------------------------------]
87  //[ Public static methods ]
88  //[-------------------------------------------------------]
89  public:
101  static void setPlayerColorsToUse(const std::vector<qsf::Color3>& playerColorsToUse);
102 
103 
104  //[-------------------------------------------------------]
105  //[ Public boost signals ]
106  //[-------------------------------------------------------]
107  public:
115  boost::signals2::signal<void()> readyForGameStart;
116 
124  boost::signals2::signal<void()> lostConnectionToProxy;
125 
126 
127  //[-------------------------------------------------------]
128  //[ Public methods ]
129  //[-------------------------------------------------------]
130  public:
135  Host();
136 
141  virtual ~Host();
142 
143  bool createSession(const std::string& localUserName, uint16 port, const std::string& sessionPassword, const HostEntry& hostEntry);
144 
154  void createLocalSession(const std::string& dataPath, const MapIdentifier& mapIdentifier);
155 
160  void closeSession();
161 
166  bool loadMap();
167 
172  void onGameStart();
173 
178  bool areAllPlayersReady();
179 
184  void kickPlayer(uint32 playerIndex);
185 
190  void changeUserDataForPlayer(uint32 playerIndex, uint32 newUserData);
191 
196  void changeGameModeUpdate(bool finished, uint16 timeleftInSeconds);
197 
202  void addNewHint(const HintMessage::MessageType messageType, const std::string& message, uint32 playerIndex);
203 
204  void onNewAudio(AudioUpdateData& data);
205  void onAudioStopped(const uint32 audioId);
206  void onAudioUpdated(AudioUpdateData& data);
207 
208  void onLayerChanged(const qsf::Layer& layer);
209 
214  void sendOperationMessageToClient(uint32 playerIndex, const OperationMessage operationMessageId, const qsf::game::BitStream& operationMessageData);
215 
220  void sendOperationMessageToAllClients(const OperationMessage operationMessageId, const qsf::game::BitStream& operationMessageData, uint32 excludedPlayerIndex = qsf::getUninitialized<uint32>(0));
221 
226  void onSendHighscoreSubmitInitiated();
227 
232  void onSendHighscoreSubmitDeclined();
233 
234  void onNewFireHose(CreateFireHoseData& data);
235 
236  void onSpawnParticle(const uint32 prefabId, const float timeout, const glm::vec3& position, const glm::vec3& scale);
237 
238 
239  //[-------------------------------------------------------]
240  //[ Public virtual em5::multiplayer::SessionBase methods ]
241  //[-------------------------------------------------------]
242  public:
247  virtual bool startup(qsf::Map& map) override;
248 
253  virtual void loadingFinished() override;
254 
259  void acceptHighsocrePublising();
260 
261 
262  //[-------------------------------------------------------]
263  //[ Protected virtual em5::multiplayer::SessionBase methods ]
264  //[-------------------------------------------------------]
265  protected:
266  virtual void onSendChatMessage(const ChatMessage& chatMessage) override;
267  virtual void onChatMessageReceived(const ChatMessage& chatMessage);
268 
269 
270  //[-------------------------------------------------------]
271  //[ Private methods ]
272  //[-------------------------------------------------------]
273  private:
274  // qsf::game::server part
275  void onGameServerClientConnected(qsf::game::ServerConnection& serverConnection);
276  void onGameServerClientDisconnected(qsf::game::ServerConnection& serverConnection);
277  void onGameServerShutdown();
278 
279  // Matchmaking part
280  bool connectToMatchmaking(const std::string& localUserName);
281  void sendMatchmakingHostUpdate();
282  void onMatchmakingClientConnected();
283  void onMatchmakingClientDisconnected();
284  void onMatchmakingClientConnectionLost();
285  void onMatchmakingClientConnectFailed();
286  void onMatchmakingClientRegisterHostResponse(multiplayer::RegisterHostStatus status);
287  void onMatchmakingClientProxyServerPropertiesResponse(const packet::ProxyServerPropertiesResponse& response);
288 
289  void closeMatchmakingConnection();
290 
291  // Proxy part
292  void connectToProxy();
293  void onProxyClientConnected();
294  void onProxyClientDisconnected();
295  void onProxyClientConnectFailed();
296  void onProxyClientSessionCreated(proxy::CreateSessionResultType result, uint64 sessionId);
297  void onProxyClientClientConnectedToSession(uint8 proxySessionClientId);
298  void onProxyClientClientDisconnectedFromSession(uint8 proxySessionClientId);
299  void closeProxyConnection();
300 
301  // Local message handling
302  void onEntityCreatedFromPrefab(const qsf::MessageParameters& parameters);
303  void onEntityDestroyed(uint64 destroyedEntityId);
304  void onUnitSpawned(const qsf::MessageParameters& parameters);
305  void onUnitSpawnedPlayerUpdateSelection(const qsf::MessageParameters& parameters);
306  void onFreeplayEventSucceeded(const qsf::MessageParameters& parameters);
307  void onFreeplayEventFailed(const qsf::MessageParameters& parameters);
308  void onFreeplayEventUpdated(const qsf::MessageParameters& parameters);
309  void onFreeplayEventTriggered(const qsf::MessageParameters& parameters);
310  void onPlayerScoreChanged(const qsf::MessageParameters& parameters);
311 
312  void onMinimapRelatedMessage(const qsf::MessageParameters& parameters);
313  void onMinimapAddIconMessage(const qsf::MessageParameters& parameters);
314  void onMinimapAddCirclePolygonMessage(const qsf::MessageParameters& parameters);
315  void onMinimapRemoveCirclePolygonMessage(const qsf::MessageParameters& parameters);
316  void onMinimapRemoveIconMessage(const qsf::MessageParameters& parameters);
317 
318  // Data Update helper
319  void gatherDataForSend(const qsf::JobArguments& jobArguments);
320  void sendDataToAllClients(const qsf::Clock& clock, const qsf::game::BitStream& bitStream);
321  void sendDataToAllClients(const qsf::game::BitStream& bitStream);
322  void sendDataToAllClients(const qsf::game::BitStream& bitStream, uint32 excludedPlayerIndex);
323  void sendDataToProxyForAllClients(const qsf::game::BitStream& bitStream);
324  void getDataForUpdateDataOperationMessage(qsf::game::BitStream& bitStream, const qsf::Clock& clock);
325  void getDataForOtherOperationMessages(qsf::game::BitStream& bitStream, const qsf::Clock& clock);
326 
327  // highscorePublishing
328  void publishHighscore();
329 
334  void onClientConnected(std::unique_ptr<ClientBase> client);
335 
340  void onClientDisconnected(uint64 clientId);
341 
342  void onReceivedOperationMessage(ClientBase& client, const OperationMessage operationMessageId, uint64 dataSize, const qsf::game::BitStream& bitStream);
343 
348  void onCommandExecutionReceived(ClientBase& client, uint64 dataSize, const qsf::game::BitStream& bitStream);
349 
354  void handleClientJoinRequest(ClientBase& client, uint64 dataSize, const qsf::game::BitStream& bitStream);
355 
360  void handleClientChangeReady(ClientBase& client, uint64 dataSize, const qsf::game::BitStream& bitStream);
361 
366  void handleClientLoadedMap(uint32 playerIndex);
367 
372  void handleClientOrderedUnit(ClientBase& client, uint64 dataSize, const qsf::game::BitStream& bitStream);
373 
378  void handleHighscoreSubmitInitiated();
379 
384  void handleHighscoreSubmitAccept(uint32 playerIndex);
385 
390  void handleHighscoreSubmitDeclined();
391 
396  void handleClientPlayerChangedUserData(ClientBase& client, uint64 dataSize, const qsf::game::BitStream& bitStream);
397 
398  // Helper methods
399  void addUser(uint32 playerIndex, const std::string& userName, uint8 edition, uint8 mapQuality, bool isReady = false);
400  void removeUser(uint32 playerIndex);
401  void removePlayerUnitsFromMap(uint32 playerIndex);
402  void setupHostEntry(HostEntry& hostEntry);
403  std::string getUniquePlayerName(const std::string& playerName);
404 
405  PlayerInfo* getPlayerInfo(uint32 playerIndex);
406  ClientBase* getClient(uint32 playerIndex);
407  void removeClientFormActiveList(uint32 playerIndex);
408  void addClientToActiveList(ClientBase* client);
409 
410  void updatePing();
411  void handleClientPingAcknowledge(ClientBase& client, uint64 dataSize, const qsf::game::BitStream& bitStream);
412 
413 
414  //[-------------------------------------------------------]
415  //[ Private data ]
416  //[-------------------------------------------------------]
417  private:
418  std::string mDataPath;
419  qsf::Map* mCurrentMap;
420  qsf::GlobalAssetId mMapGlobalAssetId;
421  std::unordered_map<uint64, std::unique_ptr<ClientBase>> mClients;
422  std::vector<std::pair<uint32, ClientBase*>> mActiveClients;
423  qsf::JobProxy mUpdateDataJobProxy;
424  MapIdentifier mMapIdentifier;
425 
426  std::shared_ptr<MatchmakingClient> mMatchmakingClient;
427  std::vector<boost::signals2::connection> mMatchmakingSignalConnections;
428  uint16 mLocalPort;
429  uint16 mExternalPort;
430  bool mIsPortForwarded;
431  ContentAuthenticationChecksums mContentAuthenticationChecksums;
432 
433  proxy::MultiplayerProxyClient* mMultiplayerProxyClient;
434  std::vector<boost::signals2::connection> mMultiplayerProxyClientSignalConnections;
435  bool mReceivedProxyServerProperties;
436 
437  MapCache mMapCache;
438 
439  std::vector<EntityCreatedFromPrefabData> mEntityCreatedFromPrefabList;
440  std::vector<uint64> mEntityDestroyedList;
441  std::vector<UnitSpawnedData> mUnitSpawnedList;
442  std::vector<UnitSpawnedPlayerUpdateSelectionData> mUnitSpawnedPlayerUpdateSelectionList;
443  std::unordered_map<uint32, FreeplayEventData> mChangedEventList;
444  boost::container::flat_map<uint32, uint64> mFocusEntityOfActiveEventsList;
445  boost::container::flat_map<uint32, uint32> mChangedPlayerScoreList;
446  GameModeUpdateData mGameModeUpdateData;
447  std::vector<NewHintData> mNewHintsList;
448  std::vector<MinimapMessageData> mMinimapReleatedMessageList;
449  std::vector<MinimapIconMessageData> mMinimapIconMessageList;
450  std::vector<AudioUpdateData> mAudioUpdatedList;
451  std::vector<LayerChangedData> mLayerChangedList;
452  std::vector<CreateFireHoseData> mNewFireHosesList;
453  std::vector<SpawnParticleData> mSpawnParticlesList;
454 
455  qsf::MessageProxy mEntityCreatedFromPrefabMessageProxy;
456  qsf::MessageProxy mEntityDestroyedMessageProxy;
457  qsf::MessageProxy mUnitSpawnedMessageProxy;
458  qsf::MessageProxy mUnitSpawnedPlayerUpdateSelectionMessageProxy;
459  qsf::MessageProxy mEventSuccessMessageProxy;
460  qsf::MessageProxy mEventFailedMessageProxy;
461  qsf::MessageProxy mEventChangedMessageProxy;
462  qsf::MessageProxy mEventTriggeredMessageProxy;
463  qsf::MessageProxy mPlayerScoreChangedMessageProxy;
464 
465  // Minimap related messages, which not already implicit handled on client side via other data
466  // TODO(sw) We could avoid sending these information, when we could push certain actions on client side
467  qsf::MessageProxy mEntityEnteredVehicleMessageProxy;
468  qsf::MessageProxy mEntityLeftVehicleMessageProxy;
469  qsf::MessageProxy mPersonPlacedInPoliceMessageProxy;
470  qsf::MessageProxy mPersonPlacedInAmbulanceMessageProxy;
471  qsf::MessageProxy mGangsterAppearedMessageProxy;
472  qsf::MessageProxy mGangsterEscapedMessageProxy;
473  qsf::MessageProxy mPersonGetsHealedMessageProxy;
474  qsf::MessageProxy mCarPickedUpMessageProxy;
475  qsf::MessageProxy mPickupPersonByParamedicMessageProxy;
476  qsf::MessageProxy mRescueDogFoundPersonMessageProxy;
477  qsf::MessageProxy mUnloadVehicleFromTransporterMessageProxy;
478  qsf::MessageProxy mLoadVehicleToTransporterMessageProxy;
479 
480  // Minimap icon messages that directly add or remove icons
481  qsf::MessageProxy mMinimapAddIconMessageProxy;
482  qsf::MessageProxy mMinimapAddCirclePolygonMessageProxy;
483  qsf::MessageProxy mMinimapRemoveCirclePolygonMessageProxy;
484  qsf::MessageProxy mMinimapRemoveIconMessageProxy;
485 
486  qsf::Time mLastSendPingTime;
487 
488  // Statistics
489  uint64 mDataPerSecond;
490  uint64 mPeekDataPerSecond;
491  uint64 mPeekDataPerUpdate;
492  uint64 mAverageDataPerSecond;
493  uint64 mAverageDataPerUpdate;
494  uint64 mLowestDataPerSecond;
495  uint64 mLowestDataPerUpdate;
496  MultiValueHistory mDataValueHistory;
497 
498 
499  };
500 
501 
502 //[-------------------------------------------------------]
503 //[ Namespace ]
504 //[-------------------------------------------------------]
505  } // server
506  } // multiplayer
507 } // em5
Definition: ActionPriority.h:13
Job proxy class.
Definition: JobProxy.h:51
Definition: OperationMessages.h:580
Message parameters class.
Definition: MessageParameters.h:34
EMERGENCY 5 multiplayer per component value history.
Definition: MapCacheStatistics.h:174
Map class.
Definition: Map.h:93
CreateSessionResultType
Definition: MultiplayerProxyTypes.h:58
Layer class.
Definition: Layer.h:56
unsigned int uint32
Definition: PlatformTypes.h:181
RegisterHostStatus
< Connected to CAMP
Definition: MatchmakingTypes.h:34
Clock class.
Definition: Clock.h:33
#define EM5_API_EXPORT
Definition: Export.h:28
Definition: SessionBase.h:139
EMERGENCY 5 multiplayer client base class (represents a client on the server side) ...
Definition: ClientBase.h:52
boost::container::flat_map< qsf::NamedIdentifier, uint64 > ContentAuthenticationChecksums
Definition: OperationMessages.h:41
Message listener proxy class.
Definition: MessageProxy.h:47
boost::signals2::signal< void()> readyForGameStart
Called when all clients and the host have loaded the map.
Definition: Host.h:115
Definition: SessionBase.h:105
OperationMessage
EMERGENCY 5 multiplayer operation message types.
Definition: OperationMessages.h:51
uint64_t uint64
Definition: PlatformTypes.h:184
Definition: OperationMessages.h:346
Job arguments class: holds data to be passed to the called job method by the job manager.
Definition: JobArguments.h:40
Game bit stream class (just a wrapper for linnet bit stream)
Definition: BitStream.h:40
Definition: OperationMessages.h:507
Definition: PickupPersonByParamedicAction.h:21
TODO(co) Comment me.
Definition: ProxyServerPropertiesResponse.h:35
EMERGENCY 5 multiplayer host class.
Definition: Host.h:82
Game server connection class.
Definition: ServerConnection.h:48
EMERGENCY 5 multiplayer session base class.
Definition: SessionBase.h:55
EMERGENCY 5 map identifier class.
Definition: MapIdentifier.h:34
unsigned short uint16
Definition: PlatformTypes.h:178
EMERGENCY 5 multiplayer proxy client class.
Definition: MultiplayerProxyClient.h:37
EMERGENCY 5 multiplayer map cache.
Definition: MapCache.h:63
MessageType
Definition: HintMessage.h:40
boost::signals2::signal< void()> lostConnectionToProxy
Called when the host lost its connection to the proxy.
Definition: Host.h:124
uint64 GlobalAssetId
Definition: AssetSystemTypes.h:28
Definition: MatchmakingTypes.h:47
unsigned char uint8
Definition: PlatformTypes.h:175
Data type for encapsulating time information. It might represents points in time and time spans...
Definition: Time.h:32