17#include <trantor/exports.h>
25#if defined(_WIN32) && !defined(_SSIZE_T_DEFINED)
26using ssize_t = std::intptr_t;
29#define TRANTOR_BUFFER_DEFAULT_LENGTH 2048
47 explicit MsgBuffer(
size_t len = TRANTOR_BUFFER_DEFAULT_LENGTH);
56 return begin() + head_;
66 return begin() + tail_;
70 return begin() + tail_;
81 return *(
static_cast<const uint8_t *
>((
void *)
peek()));
155 return tail_ - head_;
165 return buffer_.size() - tail_;
174 void append(
const char (&buf)[N])
176 assert(strnlen(buf, N) == N - 1);
179 void append(
const char *buf,
size_t len);
180 void append(
const std::string &buf)
182 append(buf.c_str(), buf.length());
192 append(
static_cast<const char *
>((
void *)&b), 1);
231 addInFront(
static_cast<const char *
>((
void *)&b), 1);
285 assert(
peek() <= end);
297 constexpr char CRLF[]{
"\r\n"};
298 const char *crlf = std::search(
peek(),
beginWrite(), CRLF, CRLF + 2);
342 return peek()[offset];
344 char &operator[](
size_t offset)
346 assert(readableBytes() >= offset);
347 return begin()[head_ + offset];
353 std::vector<char> buffer_;
355 const char *begin()
const
374inline void swap(trantor::MsgBuffer &one, trantor::MsgBuffer &two)
noexcept
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.
Definition EventLoop.h:34