LibNRG  0.0.1
Networking for Real-time Games library
 All Classes Files Functions Variables Friends
nrg_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 */
22 #ifndef NRG_STATE_H
23 #define NRG_STATE_H
24 #include "nrg_core.h"
25 #include "nrg_connection.h"
26 #include "nrg_packet.h"
30 namespace nrg {
31 
33 enum StateResult : uint32_t {
34  STATE_CONTINUE = 0x00,
35  STATE_EXIT_BIT = 0x10,
36  STATE_FAILURE = STATE_EXIT_BIT,
37  STATE_CHANGE = STATE_EXIT_BIT | 0x01
38 };
39 
41 enum StateFlags : uint32_t {
42  SFLAG_NONE = 0x00,
43  SFLAG_TIMED_OUT = 0x01
44 };
45 
47 enum HS_Reponses : int8_t {
48  HS_WRONG_LIB_VERSION = -1,
49  HS_WRONG_GAME = -2,
50  HS_WRONG_GAME_VERSION = -3,
51  HS_SERVER_FULL = -4,
52  HS_NONE = 0,
53  HS_ACCEPTED = 1
54 };
55 
56 class Client;
57 class Server;
58 class Player;
59 
63  virtual bool ready() = 0;
64 
66  virtual Status sendPacket(Packet& p, PacketFlags f = PKTFLAG_NONE) = 0;
67 
69  virtual Status resendLastPacket() = 0;
70 };
71 
73 struct State {
75  virtual bool init(Client* c, Server* s, Player* p) = 0;
76 
78  virtual bool onRecvPacket(Packet& p, PacketFlags f) = 0;
79 
81  virtual bool needsUpdate() const = 0;
82 
84  virtual size_t getTimeoutSeconds() const { return 10; }
85 
87  virtual StateResult update(StateConnectionOut& out, StateFlags f = SFLAG_NONE) = 0;
88 
90  virtual ~State(){}
91 };
92 
97 
99  void reset(bool ready_to_send);
100 
102  bool sentPackets();
103 
104  bool ready();
105  Status sendPacket(Packet& p, PacketFlags f = PKTFLAG_NONE);
107 private:
108  ConnectionOut& out;
109  bool isready, sent;
110 };
111 
112 
113 }
114 
115 #endif
bool sentPackets()
Returns true if one or more packets have been sent through this StateConnectionOutImpl since the last...
Implementation of StateConnectionOut.
Definition: nrg_state.h:94
virtual bool needsUpdate() const =0
Called to see if the State would like to be updated.
virtual Status sendPacket(Packet &p, PacketFlags f=PKTFLAG_NONE)=0
Try to send a packet, returns a error Status if it couldn't because of the rate-limit.
Status sendPacket(Packet &p, PacketFlags f=PKTFLAG_NONE)
Try to send a packet, returns a error Status if it couldn't because of the rate-limit.
Interface for wrapping ConnectionOut with rate-limiting functionality.
Definition: nrg_state.h:61
virtual StateResult update(StateConnectionOut &out, StateFlags f=SFLAG_NONE)=0
Called to update the state (or on a timeout) allowing it to optionally send some Packets using out...
Class for storing data to be sent / received across the network.
Definition: nrg_packet.h:58
virtual Status resendLastPacket()=0
Try to resend the last packet send, returns an error status if it couldn't because of the rate-limit...
Represents a connected client on the server.
Definition: nrg_player.h:34
virtual bool ready()=0
Returns true if more Packets can be sent right now with sendPacket.
virtual size_t getTimeoutSeconds() const
Returns a time that the state can go without needsUpdate() returning true, after which update() will ...
Definition: nrg_state.h:84
StateConnectionOutImpl(ConnectionOut &out)
Default constructor.
Outgoing connection class.
Definition: nrg_connection.h:99
virtual ~State()
Default destructor.
Definition: nrg_state.h:90
The main server-side class of the library.
Definition: nrg_server.h:44
virtual bool init(Client *c, Server *s, Player *p)=0
Called to (re)initialise this State instance, for Client-side c won't be null, but s and p will...
Abstract class that represents a protocol followed by the Server and Client.
Definition: nrg_state.h:73
Contains functionality that adds a connection abstraction over UDP.
Class to wrap system and internal errors.
Definition: nrg_status.h:34
Status resendLastPacket()
Try to resend the last packet send, returns an error status if it couldn't because of the rate-limit...
bool ready()
Returns true if more Packets can be sent right now with sendPacket.
The main client-side nrg class.
Definition: nrg_client.h:42
virtual bool onRecvPacket(Packet &p, PacketFlags f)=0
Called when a Packet is recieved and this is the active State, f shows if the packet was out-of-order...
void reset(bool ready_to_send)
Marks the StateConnectionOutImpl so that ready_to_send decides if more packets can be sent or not...
Common defines and includes used by all the other nrg header files.