trantor
Non-blocking I/O cross-platform TCP network library, using C++14
Loading...
Searching...
No Matches
MsgBuffer.h
Go to the documentation of this file.
1
14
15#pragma once
17#include <trantor/exports.h>
18#include <vector>
19#include <string>
20#include <algorithm>
21#include <stdio.h>
22#include <assert.h>
23#include <string.h>
24#include <cstdint>
25#if defined(_WIN32) && !defined(_SSIZE_T_DEFINED)
26using ssize_t = std::intptr_t;
27#endif
28
29#define TRANTOR_BUFFER_DEFAULT_LENGTH 2048
30
31namespace trantor
32{
33
39class TRANTOR_EXPORT MsgBuffer
40{
41 public:
47 explicit MsgBuffer(size_t len = TRANTOR_BUFFER_DEFAULT_LENGTH);
48
54 const char *peek() const
55 {
56 return begin() + head_;
57 }
58
64 const char *beginWrite() const
65 {
66 return begin() + tail_;
67 }
68 char *beginWrite()
69 {
70 return begin() + tail_;
71 }
72
78 uint8_t peekInt8() const
79 {
80 assert(readableBytes() >= 1);
81 return *(static_cast<const uint8_t *>((void *)peek()));
82 }
83
89 uint16_t peekInt16() const;
90
96 uint32_t peekInt32() const;
97
103 uint64_t peekInt64() const;
104
111 std::string read(size_t len);
112
118 uint8_t readInt8();
119
125 uint16_t readInt16();
126
132 uint32_t readInt32();
133
139 uint64_t readInt64();
140
146 void swap(MsgBuffer &buf) noexcept;
147
153 size_t readableBytes() const
154 {
155 return tail_ - head_;
156 }
157
163 size_t writableBytes() const
164 {
165 return buffer_.size() - tail_;
166 }
167
172 void append(const MsgBuffer &buf);
173 template <int N>
174 void append(const char (&buf)[N])
175 {
176 assert(strnlen(buf, N) == N - 1);
177 append(buf, N - 1);
178 }
179 void append(const char *buf, size_t len);
180 void append(const std::string &buf)
181 {
182 append(buf.c_str(), buf.length());
183 }
184
190 void appendInt8(const uint8_t b)
191 {
192 append(static_cast<const char *>((void *)&b), 1);
193 }
194
200 void appendInt16(const uint16_t s);
201
207 void appendInt32(const uint32_t i);
208
214 void appendInt64(const uint64_t l);
215
222 void addInFront(const char *buf, size_t len);
223
229 void addInFrontInt8(const uint8_t b)
230 {
231 addInFront(static_cast<const char *>((void *)&b), 1);
232 }
233
239 void addInFrontInt16(const uint16_t s);
240
246 void addInFrontInt32(const uint32_t i);
247
253 void addInFrontInt64(const uint64_t l);
254
260
266 void retrieve(size_t len);
267
276 ssize_t readFd(int fd, int *retErrno);
277
283 void retrieveUntil(const char *end)
284 {
285 assert(peek() <= end);
286 assert(end <= beginWrite());
287 retrieve(end - peek());
288 }
289
295 const char *findCRLF() const
296 {
297 constexpr char CRLF[]{"\r\n"};
298 const char *crlf = std::search(peek(), beginWrite(), CRLF, CRLF + 2);
299 return crlf == beginWrite() ? NULL : crlf;
300 }
301
307 void ensureWritableBytes(size_t len);
308
315 void hasWritten(size_t len)
316 {
317 assert(len <= writableBytes());
318 tail_ += len;
319 }
320
327 void unwrite(size_t offset)
328 {
329 assert(readableBytes() >= offset);
330 tail_ -= offset;
331 }
332
339 const char &operator[](size_t offset) const
340 {
341 assert(readableBytes() >= offset);
342 return peek()[offset];
343 }
344 char &operator[](size_t offset)
345 {
346 assert(readableBytes() >= offset);
347 return begin()[head_ + offset];
348 }
349
350 private:
351 size_t head_;
352 size_t initCap_;
353 std::vector<char> buffer_;
354 size_t tail_;
355 const char *begin() const
356 {
357 return &buffer_[0];
358 }
359 char *begin()
360 {
361 return &buffer_[0];
362 }
363};
364
365inline void swap(MsgBuffer &one, MsgBuffer &two) noexcept
366{
367 one.swap(two);
368}
369} // namespace trantor
370
371namespace std
372{
373template <>
374inline void swap(trantor::MsgBuffer &one, trantor::MsgBuffer &two) noexcept
375{
376 one.swap(two);
377}
378} // namespace std
This class represents a memory buffer used for sending and receiving data.
Definition MsgBuffer.h:40
void appendInt16(const uint16_t s)
Append a unsigned short value to the end of the buffer.
uint64_t readInt64()
Get and remove a unsigned int64 value from the buffer.
size_t readableBytes() const
Return the size of the data in the buffer.
Definition MsgBuffer.h:153
uint64_t peekInt64() const
Get a unsigned int64 value from the buffer.
uint16_t readInt16()
Get and remove a unsigned short value from the buffer.
void retrieveUntil(const char *end)
Remove the data before a certain position from the buffer.
Definition MsgBuffer.h:283
void addInFrontInt32(const uint32_t i)
Put a unsigned int value to the beginning of the buffer.
size_t writableBytes() const
Return the size of the empty part in the buffer.
Definition MsgBuffer.h:163
uint8_t readInt8()
Get the remove a byte value from the buffer.
void retrieveAll()
Remove all data in the buffer.
void addInFrontInt8(const uint8_t b)
Put a byte value to the beginning of the buffer.
Definition MsgBuffer.h:229
void ensureWritableBytes(size_t len)
Make sure the buffer has enough spaces to write data.
void addInFrontInt16(const uint16_t s)
Put a unsigned short value to the beginning of the buffer.
const char * findCRLF() const
Find the position of the buffer where the CRLF is found.
Definition MsgBuffer.h:295
void appendInt32(const uint32_t i)
Append a unsigned int value to the end of the buffer.
void appendInt64(const uint64_t l)
Append a unsigned int64 value to the end of the buffer.
uint32_t peekInt32() const
Get a unsigned int value from the buffer.
void appendInt8(const uint8_t b)
Append a byte value to the end of the buffer.
Definition MsgBuffer.h:190
const char * beginWrite() const
Get the end of the buffer where new data can be written.
Definition MsgBuffer.h:64
uint16_t peekInt16() const
Get a unsigned short value from the buffer.
const char & operator[](size_t offset) const
Access a byte in the buffer.
Definition MsgBuffer.h:339
uint32_t readInt32()
Get and remove a unsigned int value from the buffer.
void swap(MsgBuffer &buf) noexcept
swap the buffer with another.
MsgBuffer(size_t len=TRANTOR_BUFFER_DEFAULT_LENGTH)
Construct a new message buffer instance.
const char * peek() const
Get the beginning of the buffer.
Definition MsgBuffer.h:54
std::string read(size_t len)
Get and remove some bytes from the buffer.
void hasWritten(size_t len)
Move the write pointer forward when the new data has been written to the buffer.
Definition MsgBuffer.h:315
void retrieve(size_t len)
Remove some bytes in the buffer.
ssize_t readFd(int fd, int *retErrno)
Read data from a file descriptor and put it into the buffer.˝
void append(const MsgBuffer &buf)
Append new data to the buffer.
void addInFrontInt64(const uint64_t l)
Put a unsigned int64 value to the beginning of the buffer.
uint8_t peekInt8() const
Get a byte value from the buffer.
Definition MsgBuffer.h:78
void unwrite(size_t offset)
Move the write pointer backward to remove data in the end of the buffer.
Definition MsgBuffer.h:327
void addInFront(const char *buf, size_t len)
Put new data to the beginning of the buffer.
STL namespace.
Definition EventLoop.h:34