VOX
A little voxel engine
Loading...
Searching...
No Matches
Client.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "define.hpp"
4#include <memory>
5
6#include "DebugGui.hpp"
7#include "Poller.hpp"
8#include "ClientSocket.hpp"
9#include "Connection.hpp"
10#include "PacketFactory.hpp"
12
36class Client
37{
38public:
39 Client(const std::string& ip, int port);
40 ~Client();
41
42 Client(const Client& other) = delete;
43 Client& operator=(const Client& other) = delete;
44 Client(Client&& other) = delete;
45 Client& operator=(Client&& other) = delete;
46
47 void runOnce(const int & timeout_ms = -1);
48
49 enum flags : uint8_t
50 {
51 ASYNC = 1
52 };
53
54 struct sendInfo
55 {
56 std::shared_ptr<IPacket> packet;
57 uint8_t flags;
58 };
59 void send(const sendInfo & info);
60
61 class ServerDisconnected : public std::exception
62 {
63 public:
65 virtual const char* what() const throw()
66 {
67 return "Server disconnected";
68 }
69 };
70
71 std::shared_ptr<IPacket> popPacket();
72 size_t getQueueSize() const;
73
74 std::unordered_map<uint64_t, std::chrono::time_point<std::chrono::high_resolution_clock>> m_pings;
75private:
76
77 Poller m_poller;
78 std::shared_ptr<Socket> m_client_socket;
79 Connection m_connection;
80 ThreadSafePacketQueue m_incoming_packets;
81 PacketFactory & m_packet_factory = PacketFactory::GetInstance();
82 ThreadSafePacketQueue m_outgoing_packets;
83
84 int readData();
85 int sendData();
86 void emptyOutgoingPackets();
87
88 void sendPacket(std::shared_ptr<IPacket> packet);
89};
Definition: Client.hpp:62
ServerDisconnected()
Definition: Client.hpp:64
virtual const char * what() const
Definition: Client.hpp:65
The Client side interface with the network module.
Definition: Client.hpp:37
void send(const sendInfo &info)
Definition: Client.cpp:91
Client(Client &&other)=delete
flags
Definition: Client.hpp:50
@ ASYNC
Definition: Client.hpp:51
size_t getQueueSize() const
Definition: Client.cpp:116
std::unordered_map< uint64_t, std::chrono::time_point< std::chrono::high_resolution_clock > > m_pings
Definition: Client.hpp:74
Client & operator=(Client &&other)=delete
void runOnce(const int &timeout_ms=-1)
Definition: Client.cpp:15
Client(const Client &other)=delete
~Client()
Definition: Client.cpp:11
Client & operator=(const Client &other)=delete
std::shared_ptr< IPacket > popPacket()
Definition: Client.cpp:111
a bufferised and thread safe RAII wrapper for a socket representing a connection.
Definition: Connection.hpp:29
Definition: PacketFactory.hpp:8
static PacketFactory & GetInstance()
Get a ref to the singleton instance of the PacketFactory.
Definition: PacketFactory.cpp:110
Definition: Poller.hpp:13
Definition: Client.hpp:55
std::shared_ptr< IPacket > packet
Definition: Client.hpp:56
uint8_t flags
Definition: Client.hpp:57