LibNRG  0.0.1
Networking for Real-time Games library
 All Classes Files Functions Variables Friends
nrg_server.h
Go to the documentation of this file.
1 /*
2  LibNRG - Networking for Real-time Games
3 
4  Copyright (C) 2012-2014 Alex Baines <alex@abaines.me.uk>
5 
6  This software is provided 'as-is', without any express or implied
7  warranty. In no event will the authors be held liable for any damages
8  arising from the use of this software.
9 
10  Permission is granted to anyone to use this software for any purpose,
11  including commercial applications, and to alter it and redistribute it
12  freely, subject to the following restrictions:
13 
14  1. The origin of this software must not be misrepresented; you must not
15  claim that you wrote the original software. If you use this software
16  in a product, an acknowledgment in the product documentation would be
17  appreciated but is not required.
18  2. Altered source versions must be plainly marked as such, and must not be
19  misrepresented as being the original software.
20  3. This notice may not be removed or altered from any source distribution.
21 */
25 #ifndef NRG_SERVER_H
26 #define NRG_SERVER_H
27 #include "nrg_core.h"
28 #include "nrg_version.h"
29 #include "nrg_socket.h"
30 #include "nrg_packet.h"
31 #include "nrg_netaddress.h"
32 #include "nrg_snapshot.h"
33 #include "nrg_event.h"
34 #include "nrg_player.h"
35 #include "nrg_input.h"
36 #include "nrg_message.h"
37 #include <map>
38 #include <set>
39 #include <vector>
40 
41 namespace nrg {
42 
44 class Server : public EntityManager {
45 public:
47  Server(const std::string& game_name, uint32_t game_version, InputBase& input);
49  Server(const std::string& game_name, uint32_t game_version);
50 
52  bool bind(const NetAddress& addr);
53 
55  bool isBound() const;
56 
58  bool update();
59 
61  bool pollEvent(Event& e);
62 
64  void pushEvent(const Event& e);
65 
67  size_t playerCount() const;
68 
70  void setMaxPlayers(uint16_t val) { max_players = val; }
71 
73  bool isFull() const { return max_players == 0 ? false : playerCount() >= max_players; }
74 
76  void setTickRate(uint8_t rate);
77 
79  uint8_t getTickRate() const;
80 
82  void registerEntity(Entity& e);
83 
85  void unregisterEntity(Entity& e);
86 
88  void markEntityUpdated(Entity& e);
89 
91  template<class M, class F>
92  void addMessageHandler(F&& func){
93  using namespace std;
94  using namespace std::placeholders;
95 
96  global_msg_handlers.insert(
97  make_unique<M>(std::bind(forward<F>(func), _1, _2, ref(current_player)))
98  );
99  }
100 
102  void broadcastMessage(const MessageBase& m);
103 
105  Player* getPlayerByID(uint16_t) const;
106 
109  const UDPSocket& getSocket() const { return sock; }
110  const Snapshot& getSnapshot() const { return master_snapshot; }
111  const DeltaSnapshotBuffer& getDeltaSnapshots() const { return snaps; }
112  InputBase* getInput() const { return input; }
113  const std::string& getGameName() { return game_name; }
114  const uint32_t getGameVersion() { return game_version; }
118  template<class F>
119  void setVersionComparison(F&& func){
120  version_func = std::forward<F>(func);
121  }
122 
124  bool isGameVersionCompatible(uint32_t client_ver) const;
125 
127  void setUserPointer(void* p) { user_pointer = p; }
128 
130  void* getUserPointer() const { return user_pointer; }
131 
133  virtual ~Server();
134 private:
135  UDPSocket sock;
136  Packet buffer;
137  InputBase* input;
138  EventQueue eventq;
139  std::map<NetAddress, Player*> clients;
140  std::set<std::unique_ptr<MessageBase>> global_msg_handlers;
141  Snapshot master_snapshot;
142  DeltaSnapshotBuffer snaps;
143  std::vector<Entity*> entities;
144  std::vector<uint16_t> updated_entities;
145  IDAssigner<uint16_t> player_ids, entity_ids;
146  Player* current_player;
147  uint64_t timer;
148  std::string game_name;
149  uint32_t game_version;
150  uint16_t max_players;
151  std::function<bool(uint32_t, uint32_t)> version_func;
152  std::function<bool(Server& s, Packet& p)> connect_check;
153  void* user_pointer;
154  int interval;
155 };
156 
157 }
158 
159 #endif
void registerEntity(Entity &e)
Register an Entity with the server to be tracked and replicated to clients.
Abstract base class for Message.
Definition: nrg_message.h:36
Definition: nrg_snapshot.h:38
Contains Message classes and functionality for two-way RPC between Server and Client.
void pushEvent(const Event &e)
void addMessageHandler(F &&func)
Adds a callback function func that will be called on receipt of a message of type M...
Definition: nrg_server.h:92
Socket derivative specifically for the User-Datagram Protocol.
Definition: nrg_socket.h:112
Class for storing data to be sent / received across the network.
Definition: nrg_packet.h:58
Represents a connected client on the server.
Definition: nrg_player.h:34
Contains classes related to Input which is sent from Clients to a Server each Client frame...
void setVersionComparison(F &&func)
Sets a function that should return true if two uint32_t game version parameters are compatible...
Definition: nrg_server.h:119
Union to contain all the event types.
Definition: nrg_event.h:72
Contains the NetAddress class for wrapping POSIX sockaddr structures and resolving hostnames to IP ad...
Functionality to notify users of Client and Server occurances.
Abstract class to be inherited by users of the library which acts as a container of one or more Field...
Definition: nrg_entity.h:37
Holds a queue of Event objects.
Definition: nrg_event.h:92
Abstract base class for Input.
Definition: nrg_input.h:37
void setTickRate(uint8_t rate)
Sets the number of ticks / master snapshots generated by the server per second, 256 max...
Abstract class that contains functionality required by Entity objects.
Definition: nrg_entity.h:84
Library versioning stuff.
virtual ~Server()
Default destructor.
The main server-side class of the library.
Definition: nrg_server.h:44
uint8_t getTickRate() const
Gets the number of ticks per second the server is running at, default: 20.
Contains the Player Interface used server-side.
bool bind(const NetAddress &addr)
Bind the server to the specified local address.
void markEntityUpdated(Entity &e)
void setUserPointer(void *p)
Associate a user-given pointer with this server.
Definition: nrg_server.h:127
Server(const std::string &game_name, uint32_t game_version, InputBase &input)
Construct a server with the given game name, version and a user-created subclass of Input...
Classes to wrap POSIX sockets.
bool isBound() const
Returns true if the server is bound to a port.
void setMaxPlayers(uint16_t val)
Sets the maximum number of players, doesn't kick anyone if the current count is larger than this...
Definition: nrg_server.h:70
bool isGameVersionCompatible(uint32_t client_ver) const
Runs its internal version comparison function against the server version and client_ver, returns true if they're compatible.
void broadcastMessage(const MessageBase &m)
Sends message m to all connected clients.
bool pollEvent(Event &e)
Receives Event instances into e, returns false if there are no more events.
bool update()
Blocks to accept input from clients until the next snapshot time, then creates the new snapshot and s...
bool isFull() const
Returns true if the server has reached its maximum player count.
Definition: nrg_server.h:73
size_t playerCount() const
Returns the number of players connected to this server.
Player * getPlayerByID(uint16_t) const
Gets the player with the given id, or nullptr if they don't exist.
void unregisterEntity(Entity &e)
Common defines and includes used by all the other nrg header files.
Classes to hold a specific snapshot in time of Entity Field values.
void * getUserPointer() const
Returns the user-given pointer associated with this server, nullptr by default.
Definition: nrg_server.h:130
Class to wrap the various POSIX sockaddrs and resolve hostnames.
Definition: nrg_netaddress.h:32