trantor
Non-blocking I/O cross-platform TCP network library, using C++14
Loading...
Searching...
No Matches
trantor::MsgBuffer Class Reference

This class represents a memory buffer used for sending and receiving data. More...

#include <trantor/utils/MsgBuffer.h>

Public Member Functions

 MsgBuffer (size_t len=TRANTOR_BUFFER_DEFAULT_LENGTH)
 Construct a new message buffer instance.
const char * peek () const
 Get the beginning of the buffer.
const char * beginWrite () const
 Get the end of the buffer where new data can be written.
char * beginWrite ()
uint8_t peekInt8 () const
 Get a byte value from the buffer.
uint16_t peekInt16 () const
 Get a unsigned short value from the buffer.
uint32_t peekInt32 () const
 Get a unsigned int value from the buffer.
uint64_t peekInt64 () const
 Get a unsigned int64 value from the buffer.
std::string read (size_t len)
 Get and remove some bytes from the buffer.
uint8_t readInt8 ()
 Get the remove a byte value from the buffer.
uint16_t readInt16 ()
 Get and remove a unsigned short value from the buffer.
uint32_t readInt32 ()
 Get and remove a unsigned int value from the buffer.
uint64_t readInt64 ()
 Get and remove a unsigned int64 value from the buffer.
void swap (MsgBuffer &buf) noexcept
 swap the buffer with another.
size_t readableBytes () const
 Return the size of the data in the buffer.
size_t writableBytes () const
 Return the size of the empty part in the buffer.
void append (const MsgBuffer &buf)
 Append new data to the buffer.
template<int N>
void append (const char(&buf)[N])
void append (const char *buf, size_t len)
void append (const std::string &buf)
void appendInt8 (const uint8_t b)
 Append a byte value to the end of the buffer.
void appendInt16 (const uint16_t s)
 Append a unsigned short value to the end of the buffer.
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.
void addInFront (const char *buf, size_t len)
 Put new data to the beginning of the buffer.
void addInFrontInt8 (const uint8_t b)
 Put a byte value to the beginning of the buffer.
void addInFrontInt16 (const uint16_t s)
 Put a unsigned short value to the beginning of the buffer.
void addInFrontInt32 (const uint32_t i)
 Put a unsigned int value to the beginning of the buffer.
void addInFrontInt64 (const uint64_t l)
 Put a unsigned int64 value to the beginning of the buffer.
void retrieveAll ()
 Remove all data in the buffer.
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 retrieveUntil (const char *end)
 Remove the data before a certain position from the buffer.
const char * findCRLF () const
 Find the position of the buffer where the CRLF is found.
void ensureWritableBytes (size_t len)
 Make sure the buffer has enough spaces to write data.
void hasWritten (size_t len)
 Move the write pointer forward when the new data has been written to the buffer.
void unwrite (size_t offset)
 Move the write pointer backward to remove data in the end of the buffer.
const char & operator[] (size_t offset) const
 Access a byte in the buffer.
char & operator[] (size_t offset)

Detailed Description

This class represents a memory buffer used for sending and receiving data.

Constructor & Destructor Documentation

◆ MsgBuffer()

trantor::MsgBuffer::MsgBuffer ( size_t len = TRANTOR_BUFFER_DEFAULT_LENGTH)
explicit

Construct a new message buffer instance.

Parameters
lenThe initial size of the buffer.

Member Function Documentation

◆ addInFront()

void trantor::MsgBuffer::addInFront ( const char * buf,
size_t len )

Put new data to the beginning of the buffer.

Parameters
buf
len

◆ addInFrontInt16()

void trantor::MsgBuffer::addInFrontInt16 ( const uint16_t s)

Put a unsigned short value to the beginning of the buffer.

Parameters
s

◆ addInFrontInt32()

void trantor::MsgBuffer::addInFrontInt32 ( const uint32_t i)

Put a unsigned int value to the beginning of the buffer.

Parameters
i

◆ addInFrontInt64()

void trantor::MsgBuffer::addInFrontInt64 ( const uint64_t l)

Put a unsigned int64 value to the beginning of the buffer.

Parameters
l

◆ addInFrontInt8()

void trantor::MsgBuffer::addInFrontInt8 ( const uint8_t b)
inline

Put a byte value to the beginning of the buffer.

Parameters
b

◆ appendInt16()

void trantor::MsgBuffer::appendInt16 ( const uint16_t s)

Append a unsigned short value to the end of the buffer.

Parameters
s

◆ appendInt32()

void trantor::MsgBuffer::appendInt32 ( const uint32_t i)

Append a unsigned int value to the end of the buffer.

Parameters
i

◆ appendInt64()

void trantor::MsgBuffer::appendInt64 ( const uint64_t l)

Append a unsigned int64 value to the end of the buffer.

Parameters
l

◆ appendInt8()

void trantor::MsgBuffer::appendInt8 ( const uint8_t b)
inline

Append a byte value to the end of the buffer.

Parameters
b

◆ beginWrite()

const char * trantor::MsgBuffer::beginWrite ( ) const
inline

Get the end of the buffer where new data can be written.

Returns
const char*

◆ ensureWritableBytes()

void trantor::MsgBuffer::ensureWritableBytes ( size_t len)

Make sure the buffer has enough spaces to write data.

Parameters
len

◆ findCRLF()

const char * trantor::MsgBuffer::findCRLF ( ) const
inline

Find the position of the buffer where the CRLF is found.

Returns
const char*

◆ hasWritten()

void trantor::MsgBuffer::hasWritten ( size_t len)
inline

Move the write pointer forward when the new data has been written to the buffer.

Parameters
len

◆ operator[]()

const char & trantor::MsgBuffer::operator[] ( size_t offset) const
inline

Access a byte in the buffer.

Parameters
offset
Returns
const char&

◆ peek()

const char * trantor::MsgBuffer::peek ( ) const
inline

Get the beginning of the buffer.

Returns
const char*

◆ peekInt16()

uint16_t trantor::MsgBuffer::peekInt16 ( ) const

Get a unsigned short value from the buffer.

Returns
uint16_t

◆ peekInt32()

uint32_t trantor::MsgBuffer::peekInt32 ( ) const

Get a unsigned int value from the buffer.

Returns
uint32_t

◆ peekInt64()

uint64_t trantor::MsgBuffer::peekInt64 ( ) const

Get a unsigned int64 value from the buffer.

Returns
uint64_t

◆ peekInt8()

uint8_t trantor::MsgBuffer::peekInt8 ( ) const
inline

Get a byte value from the buffer.

Returns
uint8_t

◆ read()

std::string trantor::MsgBuffer::read ( size_t len)

Get and remove some bytes from the buffer.

Parameters
len
Returns
std::string

◆ readableBytes()

size_t trantor::MsgBuffer::readableBytes ( ) const
inline

Return the size of the data in the buffer.

Returns
size_t

◆ readFd()

ssize_t trantor::MsgBuffer::readFd ( int fd,
int * retErrno )

Read data from a file descriptor and put it into the buffer.˝

Parameters
fdThe file descriptor. It is usually a socket.
retErrnoThe error code when reading.
Returns
ssize_t The number of bytes read from the file descriptor. -1 is returned when an error occurs.

◆ readInt16()

uint16_t trantor::MsgBuffer::readInt16 ( )

Get and remove a unsigned short value from the buffer.

Returns
uint16_t

◆ readInt32()

uint32_t trantor::MsgBuffer::readInt32 ( )

Get and remove a unsigned int value from the buffer.

Returns
uint32_t

◆ readInt64()

uint64_t trantor::MsgBuffer::readInt64 ( )

Get and remove a unsigned int64 value from the buffer.

Returns
uint64_t

◆ readInt8()

uint8_t trantor::MsgBuffer::readInt8 ( )

Get the remove a byte value from the buffer.

Returns
uint8_t

◆ retrieve()

void trantor::MsgBuffer::retrieve ( size_t len)

Remove some bytes in the buffer.

Parameters
len

◆ retrieveUntil()

void trantor::MsgBuffer::retrieveUntil ( const char * end)
inline

Remove the data before a certain position from the buffer.

Parameters
endThe position.

◆ swap()

void trantor::MsgBuffer::swap ( MsgBuffer & buf)
noexcept

swap the buffer with another.

Parameters
buf

◆ unwrite()

void trantor::MsgBuffer::unwrite ( size_t offset)
inline

Move the write pointer backward to remove data in the end of the buffer.

Parameters
offset

◆ writableBytes()

size_t trantor::MsgBuffer::writableBytes ( ) const
inline

Return the size of the empty part in the buffer.

Returns
size_t

The documentation for this class was generated from the following file: