LibNRG  0.0.1
Networking for Real-time Games library
 All Classes Files Functions Variables Friends
nrg_packet_header.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_PACKET_HEADER_H
26 #define NRG_PACKET_HEADER_H
27 
28 namespace nrg {
29 
31 struct PacketHeader {
34  : seq_num(0)
35  , flags(0)
36  , version(0)
37  , frag_index(0){
38 
39  }
40 
42  PacketHeader(uint16_t seq, uint8_t flags, uint8_t frag_index = 0)
43  : seq_num(seq)
44  , flags(flags)
45  , version(0)
46  , frag_index(frag_index){
47 
48  }
49 
51  bool read(Packet& p){
52  if(p.remaining() >= size){
53  uint8_t ver_idx = 0;
54  p.read8(ver_idx).read8(flags).read16(seq_num);
55 
56  version = ver_idx >> 7;
57  frag_index = ver_idx & 0x1F;
58  return true;
59  } else {
60  return false;
61  }
62  }
63 
65  void write(Packet& p){
66  p.write8((version << 7) | (frag_index & 0x1F));
67  p.write8(flags);
68  p.write16(seq_num);
69  }
70 
71  uint16_t seq_num;
72  uint8_t flags;
73  uint32_t version, frag_index;
74 
75  static const size_t size = 4;
76 };
77 
78 }
79 
80 #endif
PacketHeader()
Default constructor.
Definition: nrg_packet_header.h:33
Class representing a header to be prepended to packets that pass through ConnectionIn and ConnectionO...
Definition: nrg_packet_header.h:31
PacketHeader(uint16_t seq, uint8_t flags, uint8_t frag_index=0)
Constructor specifying all attributes.
Definition: nrg_packet_header.h:42
size_t remaining() const
Get the amount of data that can be read from the internal pointer's current position.
Definition: nrg_packet.h:134
Class for storing data to be sent / received across the network.
Definition: nrg_packet.h:58
PacketWritable & write16(const uint16_t &v)
Write a uint16_t, automatically converting endianness.
PacketWritable & write8(const uint8_t &v)
Write a uint8_t, automatically converting endianness.
virtual PacketReadable & read8(uint8_t &v)=0
Read a uint8_t, automatically converting endianness.
virtual PacketReadable & read16(uint16_t &v)=0
Read a uint16_t, automatically converting endianness.
bool read(Packet &p)
Read a header from a packet p, returning true if successful.
Definition: nrg_packet_header.h:51
void write(Packet &p)
Write this PacketHeader instance to the given packet p.
Definition: nrg_packet_header.h:65
PacketReadable & read8(uint8_t &v)
Read a uint8_t, automatically converting endianness.