LibNRG  0.0.1
Networking for Real-time Games library
 All Classes Files Functions Variables Friends
nrg_packet.h
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 /* @file
23  * Contains functionality related to the Packet class, which stores data sent / received over the network
24  */
25 #ifndef NRG_PACKET_H
26 #define NRG_PACKET_H
27 #include "nrg_core.h"
28 #include "nrg_config.h"
29 #include "nrg_netaddress.h"
30 #include "nrg_endian.h"
31 #include <cstring> // memcpy
32 #include <cstdio> // SEEK_x
33 #include <algorithm>
34 
35 namespace nrg {
36 
39  virtual PacketReadable& read8(uint8_t& v) = 0;
40  virtual PacketReadable& read16(uint16_t& v) = 0;
41  virtual PacketReadable& read32(uint32_t& v) = 0;
42  virtual PacketReadable& read64(uint64_t& v) = 0;
43  virtual PacketReadable& readArray(uint8_t* v, size_t size) = 0;
44  virtual ~PacketReadable(){}
45 };
46 
49  virtual PacketWritable& write8(const uint8_t& v) = 0;
50  virtual PacketWritable& write16(const uint16_t& v) = 0;
51  virtual PacketWritable& write32(const uint32_t& v) = 0;
52  virtual PacketWritable& write64(const uint64_t& v) = 0;
53  virtual PacketWritable& writeArray(const void* v, size_t size) = 0;
54  virtual ~PacketWritable(){}
55 };
56 
58 class Packet : public PacketReadable, public PacketWritable {
59 public:
61  Packet();
62 
64  Packet(size_t initial_size);
65 
67  Packet(const Packet& copy);
68 
70  Packet& operator=(const Packet& other);
71 
73  virtual ~Packet();
74 
75  PacketWritable& write8(const uint8_t& v);
76  PacketWritable& write16(const uint16_t& v);
77  PacketWritable& write32(const uint32_t& v);
78  PacketWritable& write64(const uint64_t& v);
79  PacketWritable& writeArray(const void* v, size_t size);
80 
82  template<typename T>
83  void writeBE(const T& be_v){
84  while((size_t)(pointer - data) > (data_size - sizeof(be_v))){
85  resize();
86  }
87  memcpy(pointer, &be_v, sizeof(be_v));
88  pointer += sizeof(be_v);
89  used_size = std::max(used_size, (size_t)(pointer - data));
90  }
91 
93  template<typename T>
94  void write(const T& v){
95  writeBE(nrg::hton(v));
96  }
97 
98  PacketReadable& read8(uint8_t& v);
99  PacketReadable& read16(uint16_t& v);
100  PacketReadable& read32(uint32_t& v);
101  PacketReadable& read64(uint64_t& v);
102  PacketReadable& readArray(uint8_t* v, size_t size);
103 
105  template<typename T>
106  void readBE(T& be_v){
107  if((size_t)(pointer - data) <= (used_size - sizeof(be_v))){
108  memcpy(&be_v, pointer, sizeof(be_v));
109  pointer += sizeof(be_v);
110  }
111  }
112 
114  template<typename T>
115  void read(T& v){
116  T be = 0;
117  readBE(be);
118  v = nrg::ntoh(be);
119  }
120 
122  virtual Packet& reset();
123 
125  Packet& seek(off_t offset, int whence);
126 
128  off_t tell() const;
129 
131  size_t size() const { return used_size; }
132 
134  size_t remaining() const { return used_size - (pointer - data); }
135 
137  const uint8_t* getPointer() const { return pointer; }
138 
140  const uint8_t* getBasePointer() const { return data; }
141 private:
142  void resize();
143  uint8_t *data, *pointer;
144  size_t data_size, used_size;
145 };
146 
149  virtual bool apply(Packet& in, Packet& out) = 0;
150  virtual bool remove(Packet& in, Packet& out) = 0;
151 };
152 
153 #ifdef NRG_ENABLE_ZLIB_COMPRESSION
154 
156 struct PacketCompressor : PacketTransformation {
157  virtual bool apply(Packet& in, Packet& out);
158  virtual bool remove(Packet& in, Packet& out);
159 };
160 
161 #endif
162 
163 }
164 
165 #endif
Packet & seek(off_t offset, int whence)
Seeks the packet to some offset using SEEK_SET, SEEK_CUR or SEEK_END.
Contains several definitions that can be used to configure the library at compile time...
virtual PacketReadable & read32(uint32_t &v)=0
Read a uint32_t, automatically converting endianness.
virtual PacketReadable & read64(uint64_t &v)=0
Read a uint64_t, automatically converting endianness.
virtual ~PacketWritable()
Standard Destructor.
Definition: nrg_packet.h:54
Packet()
Default Constructor.
void writeBE(const T &be_v)
Generic write function without endian conversion, be careful with types like size_t that differ acros...
Definition: nrg_packet.h:83
virtual PacketWritable & write16(const uint16_t &v)=0
Write a uint16_t, automatically converting endianness.
virtual PacketWritable & write32(const uint32_t &v)=0
Write a uint32_t, automatically converting endianness.
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
virtual PacketReadable & readArray(uint8_t *v, size_t size)=0
Read an array of size size into v, make sure it's big enough!
virtual PacketWritable & write8(const uint8_t &v)=0
Write a uint8_t, automatically converting endianness.
void read(T &v)
Generic read function with endian conversion, be careful with types like size_t that differ across pl...
Definition: nrg_packet.h:115
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.
Contains the NetAddress class for wrapping POSIX sockaddr structures and resolving hostnames to IP ad...
const uint8_t * getPointer() const
Returns the internal pointer.
Definition: nrg_packet.h:137
PacketReadable & read64(uint64_t &v)
Read a uint64_t, automatically converting endianness.
PacketReadable & read16(uint16_t &v)
Read a uint16_t, automatically converting endianness.
virtual ~PacketReadable()
Standard Destructor.
Definition: nrg_packet.h:44
PacketReadable & readArray(uint8_t *v, size_t size)
Read an array of size size into v, make sure it's big enough!
PacketReadable & read32(uint32_t &v)
Read a uint32_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.
virtual Packet & reset()
Clears all data in the Packet.
off_t tell() const
Get the current offset of this Packet's internal pointer.
virtual PacketWritable & writeArray(const void *v, size_t size)=0
Write an array of size size, no endian conversion is performed.
Interface for Packet's reading functions.
Definition: nrg_packet.h:38
PacketWritable & write32(const uint32_t &v)
Write a uint32_t, automatically converting endianness.
size_t size() const
Get the total used-size of this packet.
Definition: nrg_packet.h:131
const uint8_t * getBasePointer() const
Returns the base address of the packet's data without affecting the internal pointer.
Definition: nrg_packet.h:140
virtual PacketWritable & write64(const uint64_t &v)=0
Write a uint64_t, automatically converting endianness.
Interface representing a transformation of a Packet, such as Compression.
Definition: nrg_packet.h:148
void readBE(T &be_v)
Generic read function without endian conversion, be careful with types like size_t that differ across...
Definition: nrg_packet.h:106
Defines functions for converting between big and little endian byte orders.
PacketWritable & write64(const uint64_t &v)
Write a uint64_t, automatically converting endianness.
virtual ~Packet()
Standard Destructor.
Packet & operator=(const Packet &other)
Assignment operator.
PacketWritable & writeArray(const void *v, size_t size)
Write an array of size size, no endian conversion is performed.
Common defines and includes used by all the other nrg header files.
void write(const T &v)
Generic write function with endian conversion, be careful with types like size_t that differ across p...
Definition: nrg_packet.h:94
PacketReadable & read8(uint8_t &v)
Read a uint8_t, automatically converting endianness.
Interface for Packet's writing functions.
Definition: nrg_packet.h:48