37 #include <tbb/concurrent_queue.h>
39 #ifdef E3D_TARGET_WINDOWS
41 # define _WINSOCK_DEPRECATED_NO_WARNINGS
46 # ifndef WINSOCK2_IMPORTED
47 # define WINSOCK2_IMPORTED
48 # include <winsock2.h>
50 #elif defined E3D_TARGET_UNIX
51 # define _XOPEN_SOURCE_EXTENDED 1
53 # include <arpa/inet.h>
61 #include "eng3d/utils.hpp"
74 virtual const char*
what() const noexcept {
75 return buffer.c_str();
80 bool is_server_stream =
false;
85 bool send(
const void* data,
size_t size, std::function<
bool()> pred);
86 bool recv(
void* data,
size_t size, std::function<
bool()> pred = 0);
114 return static_cast<void*
>(&
buffer[0]);
118 inline void data(
const T* buf =
nullptr,
size_t size =
sizeof(T)) {
152 tbb::concurrent_bounded_queue<Eng3D::Networking::Packet>
packets;
159 struct sockaddr_in
addr;
171 virtual const char*
what() const noexcept {
176 Server(
unsigned port,
unsigned max_conn);
179 void do_netloop(std::function<
void(
int i)> on_wake_thread,
int id);
192 struct sockaddr_in
addr;
203 virtual const char*
what() const noexcept {
208 Client(std::string host,
const unsigned port);
220 tbb::concurrent_bounded_queue<Eng3D::Networking::Packet>
packets;
virtual const char * what() const noexcept
Exception(const std::string &_msg)
tbb::concurrent_bounded_queue< Eng3D::Networking::Packet > packets
Client(std::string host, const unsigned port)
void do_netloop(std::function< bool()> cond, std::function< void(const Packet &packet, Eng3D::Deser::Archive &ar)> handler)
void send(const Eng3D::Networking::Packet &packet)
std::vector< uint8_t > buffer
std::function< bool()> pred
Packet(int _fd, const void *buf, size_t size)
void data(const T *buf=nullptr, size_t size=sizeof(T))
Exception(const std::string &_msg)
virtual const char * what() const noexcept
void flush_packets()
TODO: flush packets.
tbb::concurrent_bounded_queue< Eng3D::Networking::Packet > packets
std::unique_ptr< std::thread > thread
std::atomic< bool > is_connected
virtual void handler(const Packet &packet, Eng3D::Deser::Archive &ar, int id)=0
void do_netloop(std::function< void(int i)> on_wake_thread, int id)
Server(unsigned port, unsigned max_conn)
virtual void on_connect(int conn_fd, int id)=0
virtual void on_disconnect()=0
void broadcast(const Eng3D::Networking::Packet &packet)
This will broadcast the given packet to all clients currently on the server.
virtual const char * what() const noexcept
SocketException(const std::string &msg)
bool recv(void *data, size_t size, std::function< bool()> pred=0)
void set_timeout(int seconds)
bool send(const void *data, size_t size, std::function< bool()> pred)
void set_blocking(bool value)
Base class that serves as archiver, stores (in memory) the data required for serialization/deserializ...