LibNRG  0.0.1
Networking for Real-time Games library
 All Classes Files Functions Variables Friends
nrg_client.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_CLIENT_H
26 #define NRG_CLIENT_H
27 #include "nrg_core.h"
28 #include "nrg_version.h"
29 #include "nrg_socket.h"
30 #include "nrg_netaddress.h"
31 #include "nrg_client_state.h"
32 #include "nrg_state_manager.h"
33 #include "nrg_event.h"
34 #include "nrg_input.h"
35 #include "nrg_message.h"
36 #include <vector>
37 #include <utility>
38 
39 namespace nrg {
40 
42 class Client {
43 public:
49  Client(const std::string& game_name, uint32_t game_version, InputBase& input);
50 
55  Client(const std::string& game_name, uint32_t game_version);
56 
58  bool connect(const NetAddress& server_addr);
59 
61  bool isConnected() const;
62 
64  const NetAddress* getConnectedAddress() const;
65 
67  void registerEntity(Entity* e);
68 
70  template<class M, class F>
71  void addMessageHandler(F&& f){
72  game_state.registerMessage(M(std::forward<F>(f)));
73  }
74 
76  void sendMessage(const MessageBase& m);
77 
79  bool update();
80 
82  bool pollEvent(Event& e);
83 
85  const ClientStats& getStats() const;
86 
89  uint32_t getPacketRateLimit(void);
91  void setPacketRateLimit(uint32_t packets_per_sec);
95  void startRecordingReplay(const char* filename);
97  void stopRecordingReplay();
98 
101  void* getUserPointer() const { return user_pointer; }
103  void setUserPointer(void* p) { user_pointer = p; }
108  InputBase* getInput() { return input; }
109  EventQueue& getEventQueue() { return eventq; }
110  UDPSocket& getSock() { return sock; }
111  const std::string& getGameName() { return game_name; }
112  const uint32_t getGameVersion() { return game_version; }
113  const int getPlayerID() { return player_id; }
117  void setServerParams(const Version& lib_v, uint32_t game_v, uint16_t pid);
121  ~Client();
122 private:
123  UDPSocket sock;
124  InputBase* input;
125  Packet buffer;
126  NetAddress serv_addr;
127  Connection con;
128  StateConnectionOutImpl state_con;
129  EventQueue eventq;
130  StateManager state_manager;
131  ClientHandshakeState handshake;
132  ClientGameState game_state;
133  uint32_t rate_limit_interval_ms;
134  uint32_t previous_ms;
135  std::string game_name;
136  uint32_t game_version;
137  int player_id;
138  void* user_pointer;
139  char dc_reason[NRG_MAX_ERRORMSG_LEN];
140 };
141 
142 }
143 
144 #endif
void addMessageHandler(F &&f)
Adds a function f, that will be called when the Client receives a Message of type M...
Definition: nrg_client.h:71
void setPacketRateLimit(uint32_t packets_per_sec)
Sets a limit on how many full Packets the Client will send per second.
Abstract base class for Message.
Definition: nrg_message.h:36
Functionality related to managing changing States.
Contains Message classes and functionality for two-way RPC between Server and Client.
const NetAddress * getConnectedAddress() const
Returns the NetAddress this Client is connected to, or nullptr if not connected.
Contains the default State classes used by the Client, ClientHandshakeState and ClientGameState.
const ClientStats & getStats() const
Returns a ClientStats reference, which contains statistics about this Client's connection.
Contains classes related to Input which is sent from Clients to a Server each Client frame...
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
Client statistics interface.
Definition: nrg_util.h:66
void sendMessage(const MessageBase &m)
Queues the the user-defined Message m to be sent to the connected Server.
Abstract base class for Input.
Definition: nrg_input.h:37
Library versioning stuff.
bool isConnected() const
Returns whether or not this Client is connected or not.
bool pollEvent(Event &e)
Places the next Event from the Client's EventQueue into e, returning true if this happened...
Classes to wrap POSIX sockets.
void stopRecordingReplay()
Stops recording received packets into a file called filename.
uint32_t getPacketRateLimit(void)
Gets a limit on how many full Packets the Client will send per second.
~Client()
Standard destructor.
void startRecordingReplay(const char *filename)
Starts recording received packets into a file called filename.
void setUserPointer(void *p)
Set a user-defined pointer to be associated with this Client instance.
Definition: nrg_client.h:103
bool update()
Sends and Recieves any queued Packets and updates the contained State classes, returns false on error...
The main client-side nrg class.
Definition: nrg_client.h:42
bool connect(const NetAddress &server_addr)
Sets the address of the Server to connect to, and calls Socket::connect on it.
Client(const std::string &game_name, uint32_t game_version, InputBase &input)
Standard Client constructor.
void registerEntity(Entity *e)
Registers a subclass of Entity with this Client, must be called once for each user-defined Entity bef...
Common defines and includes used by all the other nrg header files.
Class to wrap the various POSIX sockaddrs and resolve hostnames.
Definition: nrg_netaddress.h:32
void * getUserPointer() const
Get a user-defined pointer to be associated with this Client instance.
Definition: nrg_client.h:101