Terminus
networkmanager.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <memory>
4 
5 #include <QObject>
6 #include <QString>
7 
8 #include <util/timer.h>
9 
11 
12 namespace terminus
13 {
14 
15 class AbstractCommand;
16 class Game;
17 class NetworkClient;
18 class NetworkServer;
19 class Train;
20 
28 class NetworkManager : public QObject
29 {
30  Q_OBJECT
31 public:
32  enum class Mode
33  {
37  };
38 
39 public:
40  NetworkManager(Game & game);
41  virtual ~NetworkManager();
42 
49  void startServer(unsigned short port);
57  void startClient(QString host, unsigned short port);
58 
64  void update();
65 
66 
67  void sendPauseCommand(bool pause);
68  void sendPrepareNewGameCommand(bool isPlayerOne, unsigned int terrainSeed);
69  void sendProjectileFiredCommand(QVector3D startPosition, QVector3D velocity);
70  void sendProjectileHitCommand(int wagonIndex, float damage);
71  void sendSyncCommand(const Train &playerTrain);
72  void sendGameEndedCommand(bool youWin);
74 
81  void prepareAndSyncNewGame();
82 
88  Mode mode();
89 
90 signals:
100  void sendCommand(AbstractCommand * command);
101 
102 protected:
109  void setEndpoint(NetworkEndpoint * endpoint);
110 
111 protected slots:
118  void onReceivedCommand(AbstractCommand * command);
119 
127 
137  void onSendCommand(AbstractCommand* command);
138 
139 protected:
144 
149 
155  std::unique_ptr<NetworkEndpoint> m_networkEndpoint;
156 
161 };
162 
163 } // namespace terminus
The NetworkEndpoint class provides a common interface that NetworkServer and NetworkClient implement...
Definition: networkendpoint.h:19
void sendProjectileHitCommand(int wagonIndex, float damage)
Definition: networkmanager.cpp:104
Mode mode()
Definition: networkmanager.cpp:138
Timer::TimerID m_syncTimer
timer that determines when sync commands should be sent
Definition: networkmanager.h:160
void startServer(unsigned short port)
starts this NetworkManager in Mode::MultiplayerHost and creates the respective NetworkEndpoint ...
Definition: networkmanager.cpp:41
void onEndpointStateChange(NetworkEndpoint::State state)
called if the underlying NetworkEndpoints changes its state
Definition: networkmanager.cpp:154
void sendProjectileFiredCommand(QVector3D startPosition, QVector3D velocity)
Definition: networkmanager.cpp:98
The Game class is the main entry point. It subclasses QQuickItem and handles all communication betwee...
Definition: game.h:36
void sendCommand(AbstractCommand *command)
sends a command
Mode m_mode
operation mode of this NetworkManager
Definition: networkmanager.h:148
void sendPauseCommand(bool pause)
Definition: networkmanager.cpp:86
void sendClientReadyCommand()
Definition: networkmanager.cpp:122
void update()
checks the sync timer and sends a sync command as necessary
Definition: networkmanager.cpp:63
virtual ~NetworkManager()
Definition: networkmanager.cpp:36
State
Definition: networkendpoint.h:23
Mode
Definition: networkmanager.h:32
NetworkManager(Game &game)
Definition: networkmanager.cpp:25
The NetworkManager implements the basic functionality for the multiplayer mode of this game...
Definition: networkmanager.h:28
unsigned int TimerID
Definition: timer.h:21
void onSendCommand(AbstractCommand *command)
Slot-wrapper for sendMessage()
Definition: networkmanager.cpp:162
void setEndpoint(NetworkEndpoint *endpoint)
sets a new NetworkEndpoint and connects its signals
Definition: networkmanager.cpp:74
void prepareAndSyncNewGame()
Initiates the command sequence of a multiplayer game.
Definition: networkmanager.cpp:128
void onReceivedCommand(AbstractCommand *command)
called if the underlying NetworkEndpoint receives an AbstractCommand
Definition: networkmanager.cpp:143
void sendGameEndedCommand(bool youWin)
Definition: networkmanager.cpp:116
Definition: eventhandler.cpp:18
void startClient(QString host, unsigned short port)
starts this NetworkManager in Mode::MultiplayerClient and creates the respective NetworkEndpoint ...
Definition: networkmanager.cpp:52
void sendPrepareNewGameCommand(bool isPlayerOne, unsigned int terrainSeed)
Definition: networkmanager.cpp:92
Game & m_game
Game instance that this NetworkManager is associated to.
Definition: networkmanager.h:143
The AbstractCommand class defines the interface that every command has to implement.
Definition: abstractcommand.h:23
void sendSyncCommand(const Train &playerTrain)
Definition: networkmanager.cpp:110
The Train class works as a logical container for all wagons one train consists of.
Definition: train.h:25
std::unique_ptr< NetworkEndpoint > m_networkEndpoint
NetworkEndpoint that is used for low-level network communication.
Definition: networkmanager.h:155