VOX
A little voxel engine
Loading...
Searching...
No Matches
Connection.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <vector>
4#include <mutex>
5#include <iostream>
6#include <memory>
8#include "Socket.hpp"
9#include "logger.hpp"
10#include "Tracy.hpp"
11
29{
30public:
35 constexpr static size_t READ_BUFFER_SIZE_MAX = 1024 * 1024 * 4; // 4 MB
36 Connection(std::shared_ptr<Socket> socket);
38
39 Connection(const Connection& other) = delete;
40 Connection& operator=(const Connection& other) = delete;
41
42 Connection(Connection&& other);
44
50 void queueAndSendMessage(const std::vector<uint8_t> & msg);
51
57 void queueMessage(const std::vector<uint8_t> & msg);
58
64 const uint8_t * getReadBufferPtr() const;
65
75 size_t getReadBufferSize() const;
76
83 void reduceReadBuffer(size_t size);
84
92 void clearReadBuffer();
93
99 const std::vector<uint8_t> & getWriteBufferRef() const;
100
106 LockableBase (std::mutex) & ReadLock() const;
107
113 LockableBase (std::mutex) & WriteLock() const;
114
121 bool dataToSend() const;
122
128 ssize_t recv();
129
135 ssize_t sendQueue();
136
137
138 const Socket & getSocket() const;
139 const uint64_t & getConnectionId() const;
140 void setConnectionId(const uint64_t & connection_id);
141private:
142
143 std::shared_ptr<Socket> m_socket;
144 uint64_t m_connection_id;
145 size_t m_read_offset = 0;
146 std::vector<uint8_t> m_read_buffer;
147 std::vector<uint8_t> m_write_buffer;
148
149 mutable TracyLockableN (std::mutex, m_mutex, "Connection Mutex");
150 mutable TracyLockableN (std::mutex, m_write_mutex, "Write Mutex");
151 mutable TracyLockableN (std::mutex, m_read_mutex, "Read Mutex");
152};
a bufferised and thread safe RAII wrapper for a socket representing a connection.
Definition: Connection.hpp:29
Connection & operator=(const Connection &other)=delete
const uint8_t * getReadBufferPtr() const
Get a pointer to the read buffer, offset by the internal read offset.
Definition: Connection.cpp:35
~Connection()
Definition: Connection.cpp:8
void queueMessage(const std::vector< uint8_t > &msg)
add the message to the write buffer without sending it.
Definition: Connection.cpp:128
bool dataToSend() const
Check if there is data in the write buffer.
Definition: Connection.cpp:150
LockableBase(std::mutex) &ReadLock() const
get a ref to the read mutex
Connection(const Connection &other)=delete
size_t getReadBufferSize() const
Get the number of bytes that can be read from the read buffer.
Definition: Connection.cpp:41
const uint64_t & getConnectionId() const
Definition: Connection.cpp:139
void reduceReadBuffer(size_t size)
Notify the connection that size bytes have been read from the read buffer.
Definition: Connection.cpp:51
ssize_t recv()
will call recv on the socket on a non blocking way. Filling the read buffer.
Definition: Connection.cpp:67
const Socket & getSocket() const
Definition: Connection.cpp:134
const std::vector< uint8_t > & getWriteBufferRef() const
Get a ref to the write buffer.
Definition: Connection.cpp:46
LockableBase(std::mutex) &WriteLock() const
get a ref to the write mutex
void clearReadBuffer()
Clear the read buffer and reset the read offset.
Definition: Connection.cpp:57
static constexpr size_t READ_BUFFER_SIZE_MAX
a constant representing the maximum size of the read buffer. when reading the connection will stop wh...
Definition: Connection.hpp:35
void queueAndSendMessage(const std::vector< uint8_t > &msg)
add the message to the write buffer and try to send it.
Definition: Connection.cpp:121
ssize_t sendQueue()
will send as much data as possible from the write buffer.
Definition: Connection.cpp:100
void setConnectionId(const uint64_t &connection_id)
Definition: Connection.cpp:145
a RAII wrapper for a socket.
Definition: Socket.hpp:17