LibNRG  0.0.1
Networking for Real-time Games library
 All Classes Files Functions Variables Friends
nrg_snapshot.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_SNAPSHOT_H
26 #define NRG_SNAPSHOT_H
27 #include "nrg_core.h"
28 #include "nrg_entity.h"
29 #include "nrg_event.h"
30 #include "nrg_ringbuffer.h"
31 #include <functional>
32 #include <vector>
33 #include <map>
34 
35 namespace nrg {
36 
38 class Snapshot {
39 public:
40  Snapshot();
41  Snapshot(uint16_t id);
42  uint16_t getID() const { return id; }
43  void setID(uint16_t id){ this->id = id; }
44  void addEntity(Entity* e);
45  void removeEntityById(uint16_t id);
46  void writeToPacket(Packet& p) const;
47  void reset();
48 private:
49  struct EntityData {
50  uint16_t eid, etype;
51  std::vector<size_t> field_sizes;
52  Packet field_data;
53  off_t getFieldOffset(int num) const;
54  size_t getTotalBytes() const;
55  };
56  uint16_t id;
57  std::map<uint16_t, EntityData> edata;
58  Packet buffer;
59 };
60 
62 struct DeltaSnapshot {
64  DeltaSnapshot(int i);
65  uint16_t getID() const { return id; }
66  void setID(uint16_t id){ this->id = id; }
67  void addEntity(Entity* e);
68  void removeEntityById(uint16_t id);
69  void mergeWithNext(const DeltaSnapshot& next);
70  void writeToPacket(Packet& p) const;
71  void reset();
72 private:
73  struct FieldInfo {
74  uint16_t entity;
75  uint16_t number;
76  size_t size;
77  size_t offset;
78  const uint8_t* get(const Packet& p) const {
79  return p.getBasePointer() + offset;
80  }
81  bool operator<(uint16_t v) const { return entity < v; }
82  bool operator==(uint16_t v) const { return entity == v; }
83  };
84  struct EntityInfo {
85  size_t num_fields;
86  uint16_t id;
87  uint16_t type;
88  bool full;
89  bool operator<(const EntityInfo& other) const { return id < other.id; }
90  bool operator==(const EntityInfo& other) const { return id == other.id; }
91  bool operator==(uint16_t v) const { return id == v; }
92  };
93 
94  uint16_t id;
95  std::vector<EntityInfo> entities;
96  std::vector<FieldInfo> fields, tmp_fields;
97  size_t full_count, del_count, upd_count;
98  Packet field_data, buffer;
99 };
100 
103  enum class Action { Get, Create, Destroy, Update };
104  typedef std::function<Entity*(Action, uint16_t eid, uint16_t etype)> CSnapFunc;
105 
106  bool readFromPacket(Packet& p, const CSnapFunc& f);
107 };
108 
110 
111 }
112 
113 #endif
Definition: nrg_snapshot.h:38
Definition: nrg_snapshot.h:102
Class for storing data to be sent / received across the network.
Definition: nrg_packet.h:58
Functionality to notify users of Client and Server occurances.
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
Contains classes related to nrg's Entity data-replication abstraction.
Definition: nrg_snapshot.h:62
const uint8_t * getBasePointer() const
Returns the base address of the packet's data without affecting the internal pointer.
Definition: nrg_packet.h:140
Simple implementation of a ring / circular buffer.
Common defines and includes used by all the other nrg header files.