VOX
A little voxel engine
Loading...
Searching...
No Matches
Connection Class Reference

a bufferised and thread safe RAII wrapper for a socket representing a connection. More...

#include <Connection.hpp>

Collaboration diagram for Connection:
Collaboration graph

Public Member Functions

 Connection (std::shared_ptr< Socket > socket)
 
 ~Connection ()
 
 Connection (const Connection &other)=delete
 
Connectionoperator= (const Connection &other)=delete
 
 Connection (Connection &&other)
 
Connectionoperator= (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 SocketgetSocket () 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...
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ Connection() [1/3]

Connection::Connection ( std::shared_ptr< Socket socket)

◆ ~Connection()

Connection::~Connection ( )

◆ Connection() [2/3]

Connection::Connection ( const Connection other)
delete

◆ Connection() [3/3]

Connection::Connection ( Connection &&  other)

Member Function Documentation

◆ clearReadBuffer()

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.

◆ dataToSend()

bool Connection::dataToSend ( ) const

Check if there is data in the write buffer.

Returns
true
false

◆ getConnectionId()

const uint64_t & Connection::getConnectionId ( ) const

◆ getReadBufferPtr()

const uint8_t * Connection::getReadBufferPtr ( ) const

Get a pointer to the read buffer, offset by the internal read offset.

Returns
const uint8_t*

◆ getReadBufferSize()

size_t Connection::getReadBufferSize ( ) const

Get the number of bytes that can be read from the read buffer.

Note
this is not the true size of the internal read buffer, because some bytes that have been read can still be in the read buffer if it hasnt been cleared
Returns
size_t

◆ getSocket()

const Socket & Connection::getSocket ( ) const

◆ getWriteBufferRef()

const std::vector< uint8_t > & Connection::getWriteBufferRef ( ) const

Get a ref to the write buffer.

Returns
const std::vector<uint8_t>&

◆ LockableBase() [1/2]

Connection::LockableBase ( std::mutex  ) const &

get a ref to the read mutex

Returns
std::mutex

◆ LockableBase() [2/2]

Connection::LockableBase ( std::mutex  ) const &

get a ref to the write mutex

Returns
std::mutex

◆ operator=() [1/2]

Connection & Connection::operator= ( Connection &&  other)

◆ operator=() [2/2]

Connection & Connection::operator= ( const Connection other)
delete

◆ queueAndSendMessage()

void Connection::queueAndSendMessage ( const std::vector< uint8_t > &  msg)

add the message to the write buffer and try to send it.

Parameters
msg

◆ queueMessage()

void Connection::queueMessage ( const std::vector< uint8_t > &  msg)

add the message to the write buffer without sending it.

Parameters
msg

◆ recv()

ssize_t Connection::recv ( )

will call recv on the socket on a non blocking way. Filling the read buffer.

Returns
ssize_t

◆ reduceReadBuffer()

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.

Parameters
size

◆ sendQueue()

ssize_t Connection::sendQueue ( )

will send as much data as possible from the write buffer.

Returns
ssize_t

◆ setConnectionId()

void Connection::setConnectionId ( const uint64_t &  connection_id)

Member Data Documentation

◆ READ_BUFFER_SIZE_MAX

constexpr size_t Connection::READ_BUFFER_SIZE_MAX = 1024 * 1024 * 4
staticconstexpr

a constant representing the maximum size of the read buffer. when reading the connection will stop when this size is reached.


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