LibNRG  0.0.1
Networking for Real-time Games library
 All Classes Files Functions Variables Friends
nrg_client_state.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_STATE_H
26 #define NRG_CLIENT_STATE_H
27 #include "nrg_core.h"
28 #include "nrg_state.h"
29 #include "nrg_replay.h"
30 #include "nrg_snapshot.h"
31 #include "nrg_message.h"
32 #include "nrg_message_manager.h"
33 #include <map>
34 #include <vector>
35 #include <functional>
36 
37 namespace nrg {
38 
39 /* Performs handshaking with the server by comparing game names and versions */
40 class ClientHandshakeState : public State {
41 public:
43  bool init(Client*, Server*, Player*);
44  bool onRecvPacket(Packet& p, PacketFlags f);
45  bool needsUpdate() const;
46  size_t getTimeoutSeconds() const { return 2; }
47  StateResult update(StateConnectionOut& out, StateFlags f);
49 private:
50  Client* client;
51  Packet buffer;
52  int phase, timeouts;
53 };
54 
55 class Entity;
56 class InputBase;
57 
58 /* Encapsulates the Client's game-specific components like Entity, Message, Replay */
59 class ClientGameState : public State, public EntityManager {
60 public:
62  bool init(Client*, Server*, Player*);
63  bool onRecvPacket(Packet& p, PacketFlags f);
64  bool needsUpdate() const;
65  size_t getTimeoutSeconds() const { return 1; }
66  StateResult update(StateConnectionOut& out, StateFlags f);
67  ~ClientGameState();
68 
69  void registerEntity(Entity* e);
70  void registerMessage(const MessageBase& m);
71  void registerMessage(MessageBase&& m);
72 
73  void sendMessage(const MessageBase& m);
74 
75  double getInterpTimer() const;
76  const ClientStats& getClientStats() const;
77 
78  void startRecordingReplay(const char* filename);
79  void stopRecordingReplay();
80 private:
81  std::function<Entity*(ClientSnapshot::Action, uint16_t, uint16_t)> snap_func;
82  Entity* SnapFuncImpl(ClientSnapshot::Action, uint16_t, uint16_t);
83  std::map<uint16_t, Entity*> entities;
84  std::map<uint16_t, Entity*> entity_types;
85  MessageManager msg_manager;
86  EventQueue* client_eventq;
87  std::unique_ptr<ClientStats> stats;
88  int timeouts;
89  double ss_timer;
90  uint8_t server_seq_prev;
91  uint16_t server_ms_prev;
92  uint32_t client_ms, client_ms_prev, interval;
93  ClientSnapshot snapshot;
94  Packet buffer;
95  InputBase* input;
96  ReplayRecorder replay;
97  bool got_packet;
98  Client* client;
99 };
100 
101 }
102 
103 #endif
size_t getTimeoutSeconds() const
Returns a time that the state can go without needsUpdate() returning true, after which update() will ...
Definition: nrg_client_state.h:65
Abstract base class for Message.
Definition: nrg_message.h:36
StateResult update(StateConnectionOut &out, StateFlags f)
Called to update the state (or on a timeout) allowing it to optionally send some Packets using out...
Definition: nrg_snapshot.h:102
Contains Message classes and functionality for two-way RPC between Server and Client.
Interface for wrapping ConnectionOut with rate-limiting functionality.
Definition: nrg_state.h:61
bool onRecvPacket(Packet &p, PacketFlags f)
Called when a Packet is recieved and this is the active State, f shows if the packet was out-of-order...
Class for storing data to be sent / received across the network.
Definition: nrg_packet.h:58
bool needsUpdate() const
Called to see if the State would like to be updated.
Represents a connected client on the server.
Definition: nrg_player.h:34
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
Holds a queue of Event objects.
Definition: nrg_event.h:92
bool needsUpdate() const
Called to see if the State would like to be updated.
Abstract base class for Input.
Definition: nrg_input.h:37
Abstract class that contains functionality required by Entity objects.
Definition: nrg_entity.h:84
Contains functionality for managing the storage and reliable transmission or Messages.
bool onRecvPacket(Packet &p, PacketFlags f)
Called when a Packet is recieved and this is the active State, f shows if the packet was out-of-order...
Class that stores Messages to be sent, and parses received messages from packets, running their callb...
Definition: nrg_message_manager.h:37
The main server-side class of the library.
Definition: nrg_server.h:44
size_t getTimeoutSeconds() const
Returns a time that the state can go without needsUpdate() returning true, after which update() will ...
Definition: nrg_client_state.h:46
Abstract class that represents a protocol followed by the Server and Client.
Definition: nrg_state.h:73
Functionality for recording replay files.
StateResult update(StateConnectionOut &out, StateFlags f)
Called to update the state (or on a timeout) allowing it to optionally send some Packets using out...
Class that writes a replay to a file.
Definition: nrg_replay.h:39
bool init(Client *, Server *, Player *)
Called to (re)initialise this State instance, for Client-side c won't be null, but s and p will...
Definition: nrg_client_state.h:59
The main client-side nrg class.
Definition: nrg_client.h:42
bool init(Client *, Server *, Player *)
Called to (re)initialise this State instance, for Client-side c won't be null, but s and p will...
Definition: nrg_client_state.h:40
Common defines and includes used by all the other nrg header files.
Functionality related to the State abstract class.
Classes to hold a specific snapshot in time of Entity Field values.