Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #pragma once
00015
00019 #include <vector>
00020
00021 namespace kNet
00022 {
00023
00025 class RingBuffer
00026 {
00027 public:
00028 std::vector<char> data;
00029 int start;
00030 int end;
00031
00032 explicit RingBuffer(int capacity)
00033 {
00034 data.resize(capacity);
00035 start = 0;
00036 end = 0;
00037 }
00038
00040 int Size() const { return end - start; }
00041
00043 void Compact()
00044 {
00045
00046 if (start == 0)
00047 return;
00048
00049 const int numBytes = Size();
00050 for(int i = 0; i < numBytes; ++i)
00051 data[i] = data[start+i];
00052
00053 start = 0;
00054 end = numBytes;
00055 }
00056
00057 void Clear()
00058 {
00059 start = end = 0;
00060 }
00061
00063 char *Begin() { return &data[start]; }
00064
00066 char *End() { return &data[end]; }
00067
00068 int StartIndex() const { return start; }
00069
00071 void Inserted(int numBytes)
00072 {
00073 end += numBytes;
00074 assert(end <= (int)data.size());
00075 }
00076
00078 void Consumed(int numBytes)
00079 {
00080 start += numBytes;
00081 assert(start <= end);
00082 if (start == end)
00083 start = end = 0;
00084 }
00085
00087 int TotalFreeBytesLeft() const { return data.size() - Size(); }
00088
00090 int ContiguousFreeBytesLeft() const { return data.size() - end; }
00091 };
00092
00093 }