3es  0.7
3escollatedpacket.h
1 //
2 // author: Kazys Stepanas
3 //
4 #ifndef _3ESCOLLATEDPACKET_H_
5 #define _3ESCOLLATEDPACKET_H_
6 
7 #include "3es-core.h"
8 
9 #include "3esconnection.h"
10 #include "3espacketheader.h"
11 
12 namespace tes
13 {
14  struct CollatedPacketMessage;
15  struct CollatedPacketZip;
16  class PacketWriter;
17 
51  class _3es_coreAPI CollatedPacket : public Connection
52  {
53  public:
56  static const size_t Overhead;
59  static const unsigned InitialCursorOffset;
61  static const uint16_t MaxPacketSize;
62 
63  public:
73  CollatedPacket(bool compress, uint16_t bufferSize = 0xff00u);
74 
82  CollatedPacket(unsigned bufferSize, unsigned maxPacketSize);
83 
85  ~CollatedPacket();
86 
89  bool compressionEnabled() const;
90 
99  unsigned maxPacketSize() const;
100 
102  void reset();
103 
113  int add(const PacketWriter &packet);
114 
120  int add(const uint8_t *buffer, uint16_t byteCount);
121 
125  bool finalise();
126 
131  const uint8_t *buffer(unsigned &byteCount) const;
132 
135  unsigned collatedBytes() const;
136 
137  //-------------------------------------------
138  // Connection methods.
139  //-------------------------------------------
140 
142  void close() override;
143 
146  void setActive(bool active) override;
147 
150  bool active() const override;
151 
154  const char *address() const override;
155 
158  uint16_t port() const override;
159 
162  bool isConnected() const override;
163 
167  int create(const Shape &shape) override;
168 
172  int destroy(const Shape &shape) override;
173 
177  int update(const Shape &shape) override;
178 
182  int updateTransfers(unsigned byteLimit) override;
183 
187  int updateFrame(float dt, bool flush = true) override;
188 
191  unsigned referenceResource(const Resource *resource) override;
192 
195  unsigned releaseResource(const Resource *resource) override;
196 
200  bool sendServerInfo(const ServerInfoMessage &info) override;
201 
206  int send(const uint8_t *data, int byteCount) override;
207 
208  private:
213  void init(bool compress, unsigned bufferSize, unsigned maxPacketSize);
214 
217  static void expand(unsigned expandBy, uint8_t *&buffer, unsigned &bufferSize, unsigned currentDataCount, unsigned maxPacketSize);
218 
219  CollatedPacketZip *_zip;
220  uint8_t *_buffer;
221  uint8_t *_finalBuffer;
223  unsigned _bufferSize;
224  unsigned _finalBufferSize;
225  unsigned _finalPacketCursor;
226  unsigned _cursor;
227  unsigned _maxPacketSize;
228  bool _finalised;
229  bool _active;
230  };
231 
232 
234  {
235  return _zip != nullptr;
236  }
237 
238 
239  inline unsigned CollatedPacket::maxPacketSize() const
240  {
241  return _maxPacketSize;
242  }
243 
244 
245  inline unsigned CollatedPacket::collatedBytes() const
246  {
247  return _cursor;
248  }
249 }
250 
251 #endif // _3ESCOLLATEDPACKET_H_
A base class for encapsulating a shape which is to be represented remotely.
Definition: 3esshape.h:39
Defines the interfaces for a client connection.
Definition: 3esconnection.h:19
static const unsigned InitialCursorOffset
Initial cursor position in the write buffer.
Definition: 3escollatedpacket.h:59
Information about the server.
Definition: 3esmessages.h:160
Definition: 3esbounds.h:13
unsigned maxPacketSize() const
Return the capacity of the collated packet.
Definition: 3escollatedpacket.h:239
The Resource base class defines an interface for any resource used by Shape objects such as MeshSet...
Definition: 3esresource.h:34
static const size_t Overhead
Byte count overhead added by using a CollatedPacket.
Definition: 3escollatedpacket.h:56
static const uint16_t MaxPacketSize
The default packet size limit for a CollatedPacketMessage.
Definition: 3escollatedpacket.h:61
A utility class which generates a MtCollatedPacket message by appending multiple other messages...
Definition: 3escollatedpacket.h:51
A utility class for writing payload data to a PacketHeader.
Definition: 3espacketwriter.h:34
bool compressionEnabled() const
Is compression enabled.
Definition: 3escollatedpacket.h:233
unsigned collatedBytes() const
Return the number of bytes that have been collated.
Definition: 3escollatedpacket.h:245