VOX
A little voxel engine
|
a bufferised and thread safe RAII wrapper for a socket representing a connection. More...
#include <Connection.hpp>
Public Member Functions | |
Connection (std::shared_ptr< Socket > socket) | |
~Connection () | |
Connection (const Connection &other)=delete | |
Connection & | operator= (const Connection &other)=delete |
Connection (Connection &&other) | |
Connection & | operator= (Connection &&other) |
void | queueAndSendMessage (const std::vector< uint8_t > &msg) |
add the message to the write buffer and try to send it. More... | |
void | queueMessage (const std::vector< uint8_t > &msg) |
add the message to the write buffer without sending it. More... | |
const uint8_t * | getReadBufferPtr () const |
Get a pointer to the read buffer, offset by the internal read offset. More... | |
size_t | getReadBufferSize () const |
Get the number of bytes that can be read from the read buffer. More... | |
void | reduceReadBuffer (size_t size) |
Notify the connection that size bytes have been read from the read buffer. More... | |
void | clearReadBuffer () |
Clear the read buffer and reset the read offset. More... | |
const std::vector< uint8_t > & | getWriteBufferRef () const |
Get a ref to the write buffer. More... | |
LockableBase (std::mutex) &ReadLock() const | |
get a ref to the read mutex More... | |
LockableBase (std::mutex) &WriteLock() const | |
get a ref to the write mutex More... | |
bool | dataToSend () const |
Check if there is data in the write buffer. More... | |
ssize_t | recv () |
will call recv on the socket on a non blocking way. Filling the read buffer. More... | |
ssize_t | sendQueue () |
will send as much data as possible from the write buffer. More... | |
const Socket & | getSocket () const |
const uint64_t & | getConnectionId () const |
void | setConnectionId (const uint64_t &connection_id) |
Static Public Attributes | |
static constexpr size_t | READ_BUFFER_SIZE_MAX = 1024 * 1024 * 4 |
a constant representing the maximum size of the read buffer. when reading the connection will stop when this size is reached. More... | |
a bufferised and thread safe RAII wrapper for a socket representing a connection.
There are two internal buffers, one for reading and one for writing
Before doing any operation on the buffers, the appropriate mutex MUST be locked. The mutexes are exposed so multiple operations of the same type can be done in a row.
About the read buffer and the read offset: to minimize the number of memory allocations the read buffer is not cleared/erased after each read operation. Instead the user will call readuceReadBuffer to indicate the number of bytes read. This will increase the read offset. When the user calls getReadBufferPtr, it will return a pointer offset by the read offset. The user can call clearReadBuffer once he has finished all his read operations to clear the buffer and reset the read offset.
Connection::Connection | ( | std::shared_ptr< Socket > | socket | ) |
Connection::~Connection | ( | ) |
|
delete |
Connection::Connection | ( | Connection && | other | ) |
void Connection::clearReadBuffer | ( | ) |
Clear the read buffer and reset the read offset.
this can cause some reallocations but will most importantly be pretty slow. prefer calling reduceReadBuffer when doing all your operations and call this once you finished.
bool Connection::dataToSend | ( | ) | const |
Check if there is data in the write buffer.
const uint64_t & Connection::getConnectionId | ( | ) | const |
const uint8_t * Connection::getReadBufferPtr | ( | ) | const |
Get a pointer to the read buffer, offset by the internal read offset.
size_t Connection::getReadBufferSize | ( | ) | const |
Get the number of bytes that can be read from the read buffer.
const Socket & Connection::getSocket | ( | ) | const |
const std::vector< uint8_t > & Connection::getWriteBufferRef | ( | ) | const |
Get a ref to the write buffer.
Connection::LockableBase | ( | std::mutex | ) | const & |
get a ref to the read mutex
Connection::LockableBase | ( | std::mutex | ) | const & |
get a ref to the write mutex
Connection & Connection::operator= | ( | Connection && | other | ) |
|
delete |
void Connection::queueAndSendMessage | ( | const std::vector< uint8_t > & | msg | ) |
add the message to the write buffer and try to send it.
msg |
void Connection::queueMessage | ( | const std::vector< uint8_t > & | msg | ) |
add the message to the write buffer without sending it.
msg |
ssize_t Connection::recv | ( | ) |
will call recv on the socket on a non blocking way. Filling the read buffer.
void Connection::reduceReadBuffer | ( | size_t | size | ) |
Notify the connection that size bytes have been read from the read buffer.
this will not cause any reallocation or any data to be moved.
size |
ssize_t Connection::sendQueue | ( | ) |
will send as much data as possible from the write buffer.
void Connection::setConnectionId | ( | const uint64_t & | connection_id | ) |
|
staticconstexpr |
a constant representing the maximum size of the read buffer. when reading the connection will stop when this size is reached.