SEGS  0.6.1-develop [d2cac982]
Super Entity Game Server
Buffer.h
Go to the documentation of this file.
1 /*
2  * SEGS - Super Entity Game Server
3  * http://www.segs.io/
4  * Copyright (c) 2006 - 2019 SEGS Team (see AUTHORS.md)
5  * This software is licensed under the terms of the 3-clause BSD License. See LICENSE.md for details.
6  */
7 
8 #pragma once
9 
10 #include <stdint.h>
11 #include <cstring>
12 
13 // Warning those classes aren't thread-safe!
14 // NOTE: maybe this should derive from ACE_Data_Block/Message_Block ?
15 
16 // Template functions are part of include file to avoid the need of explicit instantiation of templates.
18 {
19 static constexpr uint32_t DEFAULT_MAX_SIZE = 0x10000; //64K should be enough for everyone :P
20 public:
21 
22  // Con/De-struction
24 
25 explicit GrowingBuffer(const GrowingBuffer &); // copy constructor
26 explicit GrowingBuffer(GrowingBuffer &&) noexcept; // copy constructor
27  GrowingBuffer(uint8_t *buf, uint32_t size,bool become_owner);
28  GrowingBuffer(uint32_t max_size,uint8_t safe_area, uint32_t current_size);
30 
31  // Public methods
33  /* 'u' before a method denotes that this method is 'Unchecked'*/
34  /* all 'Checked' methods set internal m_lasterr variable*/
35  /* Unchecked functions are faster, but assume that they can read/write without violating the buffer, or their arguments*/
36 
37  void PutString(const char *t);
38  void PutBytes(const uint8_t *t, uint32_t len);
39  void GetString(char *t);
40  bool GetBytes(uint8_t *t, uint32_t len);
41 
42 inline void uGetString(char *t);
43  void uGetBytes(uint8_t *t, uint32_t len);
44 inline void uPutString(const char *t);
45  void uPutBytes(const uint8_t *t, uint32_t len);
46  template <typename T>
47  void Put(const T &val)
48  {
49  if(sizeof(T) > GetAvailSize())
50  if(resize(m_write_off + sizeof(T)) == -1) // space exhausted
51  {
52  m_last_err = 1;
53  return;
54  }
55  uPut(val);
56  }
57 
58  template <typename T>
59  void Get(T &val)
60  {
61  if(GetReadableDataSize() < sizeof(T))
62  {
63  m_last_err = 1;
64  return;
65  }
66  uGet(val);
67  }
68 
69  template<typename T>
70 inline void uPut(const T &val)
71  {
72  *((T *)&m_buf[m_write_off]) = val; //let the compiler decide how to put type T into array
73  m_write_off+=sizeof(T);
74  }
75 
76  template<typename T>
77 inline void uGet(T &val)
78  {
79  val = *((T *)&m_buf[m_read_off]);
80  m_read_off += sizeof(T);
81  }
82  template<typename T>
83 inline T ruGet()
84  {
85  T val = *((T *)&m_buf[m_read_off]);
86  m_read_off += sizeof(T);
87  return val;
88  }
89 
90  void PopFront(uint32_t count);
91 
92  // Accessors
94  uint32_t GetMaxSize() const { return m_max_size;}
95  uint32_t GetAvailSize() const { return (m_size-m_write_off);}
96  uint32_t GetDataSize() const { return m_write_off; } //writing point gives actual size of readable data
97  uint32_t GetReadableDataSize() const { return m_write_off-m_read_off; } //this much data can be read still
98  uint8_t * GetBuffer() const { return m_buf; }
99  uint32_t getLastError() const { return m_last_err;}
100  void setLastError(uint32_t val) { m_last_err=val;}
101 
102  uint8_t * read_ptr() { return m_buf+m_read_off;}
103  const uint8_t * read_ptr() const { return m_buf+m_read_off;}
104  void read_ptr(int off) { m_read_off+=off;}
105  uint8_t * write_ptr() { return m_buf+m_write_off;}
106  void write_ptr(int off) { m_write_off+=off;}
107 
110  void ResetWriting() {m_write_off=0;} //will start at the beginning and overwrite existing contents
111  void ResetContents() {memset(m_buf,0,m_size);}
112 protected:
113  GrowingBuffer & operator=(const GrowingBuffer &) = delete;
114  uint8_t * m_buf;
115  uint32_t m_write_off;
116  uint32_t m_read_off;
117  uint32_t m_size;
118  uint32_t m_max_size;
119  uint32_t m_last_err;
120  uint8_t m_safe_area;
121  int resize(uint32_t accommodate_size);
122 };
const uint8_t * read_ptr() const
Definition: Buffer.h:103
void PutBytes(const uint8_t *t, uint32_t len)
Definition: Buffer.cpp:97
uint32_t GetDataSize() const
Definition: Buffer.h:96
uint32_t m_max_size
Definition: Buffer.h:118
void ResetReading()
Definition: Buffer.h:109
void Reset()
Definition: Buffer.h:108
GrowingBuffer & operator=(const GrowingBuffer &)=delete
void uGetBytes(uint8_t *t, uint32_t len)
Definition: Buffer.cpp:144
void uGet(T &val)
Definition: Buffer.h:77
uint32_t m_size
Definition: Buffer.h:117
void read_ptr(int off)
Definition: Buffer.h:104
void PopFront(uint32_t count)
Definition: Buffer.cpp:149
void GetString(char *t)
Definition: Buffer.cpp:116
uint32_t m_last_err
Definition: Buffer.h:119
T ruGet()
Definition: Buffer.h:83
uint32_t GetAvailSize() const
Definition: Buffer.h:95
void write_ptr(int off)
Definition: Buffer.h:106
Definition: Buffer.h:17
uint8_t m_safe_area
Definition: Buffer.h:120
uint32_t GetReadableDataSize() const
Definition: Buffer.h:97
void Get(T &val)
Definition: Buffer.h:59
void uPutString(const char *t)
Definition: Buffer.cpp:91
uint8_t * m_buf
Definition: Buffer.h:114
uint8_t * write_ptr()
Definition: Buffer.h:105
uint32_t m_write_off
Definition: Buffer.h:115
uint8_t * GetBuffer() const
Definition: Buffer.h:98
uint32_t getLastError() const
Definition: Buffer.h:99
void ResetContents()
Definition: Buffer.h:111
bool GetBytes(uint8_t *t, uint32_t len)
Definition: Buffer.cpp:137
~GrowingBuffer()
Definition: Buffer.cpp:80
GrowingBuffer(const GrowingBuffer &)
Definition: Buffer.cpp:56
void uPut(const T &val)
Definition: Buffer.h:70
void Put(const T &val)
Definition: Buffer.h:47
void setLastError(uint32_t val)
Definition: Buffer.h:100
void ResetWriting()
Definition: Buffer.h:110
void PutString(const char *t)
Definition: Buffer.cpp:86
uint8_t * read_ptr()
Definition: Buffer.h:102
void uPutBytes(const uint8_t *t, uint32_t len)
Definition: Buffer.cpp:108
int resize(uint32_t accommodate_size)
Definition: Buffer.cpp:178
uint32_t m_read_off
Definition: Buffer.h:116
void uGetString(char *t)
Definition: Buffer.cpp:131
uint32_t GetMaxSize() const
this method removes up-to &#39;count&#39; elements from the beginning of the buffer
Definition: Buffer.h:94