SEGS  0.4.2
Super Entity Game Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 - 2018 SEGS Team (see Authors.txt)
5  * This software is licensed! (See License.txt 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 size_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  GrowingBuffer(uint8_t *buf, size_t size,bool become_owner);
27  GrowingBuffer(size_t max_size,uint8_t safe_area,size_t current_size);
29 
30  // Public methods
32  /* 'u' before a method denotes that this method is 'Unchecked'*/
33  /* all 'Checked' methods set internal m_lasterr variable*/
34  /* Unchecked functions are faster, but assume that they can read/write without violating the buffer, or their arguments*/
35 
36  void PutString(const char *t);
37  void PutBytes(const uint8_t *t, size_t len);
38  void GetString(char *t);
39  bool GetBytes(uint8_t *t, size_t len);
40 
41 inline void uGetString(char *t);
42  void uGetBytes(uint8_t *t,size_t len);
43 inline void uPutString(const char *t);
44  void uPutBytes(const uint8_t *t,size_t len);
45  template <typename T>
46  void Put(const T &val)
47  {
48  if (sizeof(T) > GetAvailSize())
49  if (resize(m_write_off + sizeof(T)) == -1) // space exhausted
50  {
51  m_last_err = 1;
52  return;
53  }
54  uPut(val);
55  }
56 
57  template <typename T>
58  void Get(T &val)
59  {
60  if (GetReadableDataSize() < sizeof(T))
61  {
62  m_last_err = 1;
63  return;
64  }
65  uGet(val);
66  }
67 
68  template<typename T>
69 inline void uPut(const T &val)
70  {
71  *((T *)&m_buf[m_write_off]) = val; //let the compiler decide how to put type T into array
72  m_write_off+=sizeof(T);
73  }
74 
75  template<typename T>
76 inline void uGet(T &val)
77  {
78  val = *((T *)&m_buf[m_read_off]);
79  m_read_off += sizeof(T);
80  }
81  template<typename T>
82 inline T ruGet()
83  {
84  T val = *((T *)&m_buf[m_read_off]);
85  m_read_off += sizeof(T);
86  return val;
87  }
88 
89  void PopFront(size_t count);
90 
91  // Accessors
93  size_t GetMaxSize() const { return m_max_size;}
94  size_t GetAvailSize() const { return (m_size-m_write_off);}
95  size_t GetDataSize() const { return m_write_off; } //writing point gives actual size of readable data
96  size_t GetReadableDataSize() const { return m_write_off-m_read_off; } //this much data can be read still
97  uint8_t * GetBuffer() const { return m_buf; }
98  uint32_t getLastError() const { return m_last_err;}
99  void setLastError(uint32_t val) { m_last_err=val;}
100 
101  uint8_t * read_ptr() { return m_buf+m_read_off;}
102  const uint8_t * read_ptr() const { return m_buf+m_read_off;}
103  void read_ptr(int off) { m_read_off+=off;}
104  uint8_t * write_ptr() { return m_buf+m_write_off;}
105  void write_ptr(int off) { m_write_off+=off;}
106 
109  void ResetWriting() {m_write_off=0;} //will start at the beginning and overwrite existing contents
110  void ResetContents() {memset(m_buf,0,m_size);}
111 protected:
112  GrowingBuffer & operator=(const GrowingBuffer &) = delete;
113  uint8_t * m_buf;
114  size_t m_write_off;
115  size_t m_read_off;
116  size_t m_size;
117  size_t m_max_size;
118  uint32_t m_last_err;
119  uint8_t m_safe_area;
120  int resize(size_t accommodate_size);
121 };
size_t m_read_off
Definition: Buffer.h:115
bool GetBytes(uint8_t *t, size_t len)
Definition: Buffer.cpp:126
size_t GetReadableDataSize() const
Definition: Buffer.h:96
uint8_t * GetBuffer() const
Definition: Buffer.h:97
void ResetReading()
Definition: Buffer.h:108
void Reset()
Definition: Buffer.h:107
GrowingBuffer & operator=(const GrowingBuffer &)=delete
void uGet(T &val)
Definition: Buffer.h:76
void read_ptr(int off)
Definition: Buffer.h:103
size_t m_max_size
Definition: Buffer.h:117
void PopFront(size_t count)
Definition: Buffer.cpp:138
void GetString(char *t)
Definition: Buffer.cpp:104
uint32_t m_last_err
Definition: Buffer.h:118
T ruGet()
Definition: Buffer.h:82
size_t GetAvailSize() const
Definition: Buffer.h:94
int resize(size_t accommodate_size)
Definition: Buffer.cpp:169
void write_ptr(int off)
Definition: Buffer.h:105
const uint8_t * read_ptr() const
Definition: Buffer.h:102
Definition: Buffer.h:17
uint8_t m_safe_area
Definition: Buffer.h:119
size_t GetDataSize() const
Definition: Buffer.h:95
void Get(T &val)
Definition: Buffer.h:58
void uPutBytes(const uint8_t *t, size_t len)
Definition: Buffer.cpp:96
void uPutString(const char *t)
Definition: Buffer.cpp:79
uint8_t * m_buf
Definition: Buffer.h:113
uint8_t * write_ptr()
Definition: Buffer.h:104
void ResetContents()
Definition: Buffer.h:110
~GrowingBuffer()
Definition: Buffer.cpp:68
GrowingBuffer(const GrowingBuffer &)
Definition: Buffer.cpp:55
void uPut(const T &val)
Definition: Buffer.h:69
void Put(const T &val)
Definition: Buffer.h:46
size_t GetMaxSize() const
this method removes up-to &#39;count&#39; elements from the beginning of the buffer
Definition: Buffer.h:93
void setLastError(uint32_t val)
Definition: Buffer.h:99
void PutBytes(const uint8_t *t, size_t len)
Definition: Buffer.cpp:85
size_t m_write_off
Definition: Buffer.h:114
void ResetWriting()
Definition: Buffer.h:109
void PutString(const char *t)
Definition: Buffer.cpp:74
uint32_t getLastError() const
Definition: Buffer.h:98
uint8_t * read_ptr()
Definition: Buffer.h:101
void uGetBytes(uint8_t *t, size_t len)
Definition: Buffer.cpp:133
size_t m_size
Definition: Buffer.h:116
void uGetString(char *t)
Definition: Buffer.cpp:120