R-Type
R-Type project
NetworkWriter.hpp
1 #pragma once
2 
3 #include "Array.hpp"
4 #include "ArraySegment.hpp"
5 #include "Platform.hpp"
6 #include <string>
7 #include <stdexcept>
8 
9 #define BUFFER_SIZE 1024
10 
11 namespace KapMirror {
12  class NetworkWriter {
13  private:
14  byte* buffer = nullptr;
15  int bufferSize;
16  int position;
17 
18  public:
19  NetworkWriter() {
20  buffer = new byte[BUFFER_SIZE];
21  bufferSize = BUFFER_SIZE;
22  position = 0;
23  }
24 
25  explicit NetworkWriter(int size) {
26  buffer = new byte[size];
27  bufferSize = size;
28  position = 0;
29  }
30 
31  ~NetworkWriter() { delete[] buffer; }
32 
36  inline void reset() { position = 0; }
37 
43  template <typename T>
44  inline void write(T value) {
45  int size = sizeof(T);
46 
47  ensureCapacity(position + size);
48 
49  std::copy((char*)&value, (char*)&value + size, buffer + position);
50  position += size;
51  }
52 
57  inline byte* toArray() { return Array::copyArray(buffer, position); }
58 
59  explicit operator byte*() { return toArray(); }
60 
65  inline std::shared_ptr<ArraySegment<byte>> toArraySegment() { return ArraySegment<byte>::createArraySegment(buffer, position); }
66 
71  inline int size() const { return position; }
72 
73  private:
74  inline void ensureCapacity(int value) {
75  if (position > bufferSize) {
76  Array::resizeArray(buffer, bufferSize, bufferSize * 2);
77  bufferSize *= 2;
78  }
79  }
80 
81  public:
88  void writeBytes(byte* array, int offset, int count) {
89  if (offset < 0 || count < 0) {
90  throw std::invalid_argument("offset or count is less than 0");
91  }
92  if (count == 0) {
93  return;
94  }
95  ensureCapacity(position + count);
96  std::copy(array + offset, array + offset + count, buffer + position);
97  position += count;
98  }
99 
104  void writeString(const std::string& value) {
105  write((short)value.length());
106  writeBytes((byte*)value.c_str(), 0, (int)value.length());
107  }
108 
109  void writeArraySegment(const std::shared_ptr<ArraySegment<byte>>& value) { writeBytes(value->toArray(), 0, value->getSize()); }
110  };
111 } // namespace KapMirror
KapMirror::NetworkWriter::toArraySegment
std::shared_ptr< ArraySegment< byte > > toArraySegment()
Get the buffer.
Definition: NetworkWriter.hpp:65
KapMirror::NetworkWriter::writeBytes
void writeBytes(byte *array, int offset, int count)
Write a byte array to the buffer.
Definition: NetworkWriter.hpp:88
KapMirror::NetworkWriter::toArray
byte * toArray()
Get the buffer.
Definition: NetworkWriter.hpp:57
KapMirror::NetworkWriter::writeString
void writeString(const std::string &value)
Write a string to the buffer.
Definition: NetworkWriter.hpp:104
KapMirror::NetworkWriter::reset
void reset()
Reset the position to 0.
Definition: NetworkWriter.hpp:36
KapMirror::Array::resizeArray
static void resizeArray(T *&array, int size, int newSize)
Resize an array.
Definition: Array.hpp:15
KapMirror::NetworkWriter::write
void write(T value)
Write a value to the buffer.
Definition: NetworkWriter.hpp:44
KapMirror::NetworkWriter
Definition: NetworkWriter.hpp:12
KapMirror::NetworkWriter::size
int size() const
Get the size of the buffer.
Definition: NetworkWriter.hpp:71
KapMirror::Array::copyArray
static T * copyArray(T *array, int size)
Copy an array.
Definition: Array.hpp:33
KapMirror::ArraySegment
Definition: ArraySegment.hpp:9