LibNRG  0.0.1
Networking for Real-time Games library
 All Classes Files Functions Variables Friends
nrg_connection.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_CONNECTION_H
26 #define NRG_CONNECTION_H
27 #include "nrg_core.h"
28 #include "nrg_netaddress.h"
29 #include "nrg_packet.h"
30 #include "nrg_packet_header.h"
31 #include "nrg_socket.h"
32 #include <bitset>
33 #include <array>
34 
35 namespace nrg {
36 
38 enum PacketFlags : uint8_t {
39  PKTFLAG_NONE = 0x00,
40 
41  /* Not sent over the wire, but used in getLastestPacket() return value */
42  PKTFLAG_OUT_OF_ORDER = 0x01,
43 
44  /* Only sent over the wire, not present in getLatestPacket() return value */
45  PKTFLAG_CONTINUED = 0x02,
46 
47  /* Both sent over the wire and present in getLatestPacket() return value */
48  PKTFLAG_FINISHED = 0x04,
49  PKTFLAG_RETRANSMISSION = 0x08,
50  PKTFLAG_STATE_CHANGE = 0x10,
51  PKTFLAG_STATE_CHANGE_ACK = 0x20,
52 };
53 
58 
60  void setTransform(PacketTransformation* transform);
61 
62  uint16_t seq_num;
63  PacketTransformation* transform;
64 };
65 
67 class ConnectionIn {
68 public:
70  ConnectionIn();
71 
73  bool addPacket(Packet& p);
74 
76  bool hasNewPacket() const;
77 
79  PacketFlags getLatestPacket(Packet& p);
80 
82  void setTransform(PacketTransformation* transform);
83 private:
85  bool new_packet, full_packet;
86  std::bitset<NRG_CONN_PACKET_HISTORY> packet_history;
87  PacketFlags latest_flags;
88  Packet latest, buffer;
89  struct ReassemblyInfo {
90  ReassemblyInfo() : data(), age(-1), continued(false){}
91  Packet data;
92  int age;
93  bool continued;
94  };
95  std::array<ReassemblyInfo, NRG_CONN_PACKET_HISTORY> reassembly_buf;
96 };
97 
100 public:
102  ConnectionOut(const NetAddress& remote_addr, const Socket& sock_out);
103 
105  Status sendPacket(Packet& p, PacketFlags f = PKTFLAG_NONE);
106 
108  Status sendDisconnect(Packet& extra_data);
109 
111  Status resendLastPacket(void);
112 
114  Status getLastStatus() const;
115 
117  void setTransform(PacketTransformation* transform);
118 private:
119  Status sendPacketWithHeader(Packet& p, PacketHeader h);
120  ConnectionCommon cc;
121  const NetAddress& remote_addr;
122  const Socket& sock;
123  Packet buffer, buffer2, last;
124  PacketHeader last_header;
125  Status last_status;
126 };
127 
129 struct Connection {
131  Connection(const NetAddress& remote_addr, const Socket& sock_out)
132  : in(), out(remote_addr, sock_out){}
133  ConnectionIn in;
134  ConnectionOut out;
135 };
136 
137 }
138 
139 #endif
Common connection functionality that is used by both ConnectionIn and ConnectionOut.
Definition: nrg_connection.h:55
bool hasNewPacket() const
Returns true if there is a packet ready to be taken.
ConnectionOut(const NetAddress &remote_addr, const Socket &sock_out)
Create a new ConnectionOut that will send packets to remote_addr using the Socket sock_out...
Class representing a header to be prepended to packets that pass through ConnectionIn and ConnectionO...
Definition: nrg_packet_header.h:31
Class for storing data to be sent / received across the network.
Definition: nrg_packet.h:58
ConnectionCommon()
Default Constructor.
Contains the NetAddress class for wrapping POSIX sockaddr structures and resolving hostnames to IP ad...
Status getLastStatus() const
Gets the Status that the last sending operation returned.
Contains the PacketHeader class.
Status resendLastPacket(void)
Resends the last packet that was sent via this ConnectionOut instance.
Outgoing connection class.
Definition: nrg_connection.h:99
Status sendPacket(Packet &p, PacketFlags f=PKTFLAG_NONE)
Send Packet p, prepending header information including the given flags f, and applying any transforma...
bool addPacket(Packet &p)
Add a received packet with connection header information to be processed.
Incoming connection class.
Definition: nrg_connection.h:67
Connection(const NetAddress &remote_addr, const Socket &sock_out)
Standard Constructor.
Definition: nrg_connection.h:131
Classes to wrap POSIX sockets.
void setTransform(PacketTransformation *transform)
Set a PacketTransformation to be removed from packets added to the connection.
Class to wrap system and internal errors.
Definition: nrg_status.h:34
Combines both ConnectionIn and ConnectionOut into a single class.
Definition: nrg_connection.h:129
Interface representing a transformation of a Packet, such as Compression.
Definition: nrg_packet.h:148
Base socket class.
Definition: nrg_socket.h:35
PacketFlags getLatestPacket(Packet &p)
Placed the latest packet into p, and returns its associated PacketFlags.
Status sendDisconnect(Packet &extra_data)
Send a Packet informing the remote host that the connection is over, extra_data can be a message expl...
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 setTransform(PacketTransformation *transform)
Set a PacketTransformation to be removed from packets added to the connection.
ConnectionIn()
Default Constructor.
void setTransform(PacketTransformation *transform)
Add a PacketTransformation that will be applied to packets, or nullptr to disable.