diff --git a/include/enetpp/client.h b/include/enetpp/client.h index 7aa662d..901cb1d 100644 --- a/include/enetpp/client.h +++ b/include/enetpp/client.h @@ -32,11 +32,11 @@ namespace enetpp { client_statistics _statistics; public: - client::client() + client() : _should_exit_thread(false) { } - client::~client() { + ~client() { //responsibility of owners to make sure disconnect is always called. not calling disconnect() in destructor due to //trace_handler side effects. assert(_thread == nullptr); @@ -50,11 +50,11 @@ namespace enetpp { _trace_handler = handler; } - bool client::is_connecting_or_connected() const { + bool is_connecting_or_connected() const { return _thread != nullptr; } - void client::connect(const client_connect_params& params) { + void connect(const client_connect_params& params) { assert(global_state::get().is_initialized()); assert(!is_connecting_or_connected()); assert(params._channel_count > 0); @@ -64,10 +64,10 @@ namespace enetpp { trace("connecting to '" + params._server_host_name + ":" + std::to_string(params._server_port) + "'"); _should_exit_thread = false; - _thread = std::make_unique(&client::run_in_thread, this, params); + _thread = std::make_unique(&run_in_thread, this, params); } - void client::disconnect() { + void disconnect() { if (_thread != nullptr) { _should_exit_thread = true; _thread->join(); @@ -78,7 +78,7 @@ namespace enetpp { destroy_all_queued_events(); } - void client::send_packet(enet_uint8 channel_id, const enet_uint8* data, size_t data_size, enet_uint32 flags) { + void send_packet(enet_uint8 channel_id, const enet_uint8* data, size_t data_size, enet_uint32 flags) { assert(is_connecting_or_connected()); if (_thread != nullptr) { std::lock_guard lock(_packet_queue_mutex); @@ -87,7 +87,7 @@ namespace enetpp { } } - void client::consume_events( + void consume_events( std::function on_connected, std::function on_disconnected, std::function on_data_received) { @@ -97,13 +97,13 @@ namespace enetpp { //!IMPORTANT! neet to copy the events for consumption to prevent deadlocks! //ex. //- event = JoinGameFailed packet received - //- causes event_handler to call client::disconnect - //- client::disconnect deadlocks as the thread needs a critical section on events to exit + //- causes event_handler to call disconnect + //- disconnect deadlocks as the thread needs a critical section on events to exit { std::lock_guard lock(_event_queue_mutex); assert(_event_queue_copy.empty()); _event_queue_copy = _event_queue; - _event_queue = {}; + _event_queue = std::queue(); } bool is_disconnected = false; @@ -148,7 +148,7 @@ namespace enetpp { } private: - void client::destroy_all_queued_packets() { + void destroy_all_queued_packets() { std::lock_guard lock(_packet_queue_mutex); while (!_packet_queue.empty()) { enet_packet_destroy(_packet_queue.front()._packet); @@ -156,7 +156,7 @@ namespace enetpp { } } - void client::destroy_all_queued_events() { + void destroy_all_queued_events() { std::lock_guard lock(_event_queue_mutex); while (!_event_queue.empty()) { destroy_unhandled_event_data(_event_queue.front()); @@ -164,13 +164,13 @@ namespace enetpp { } } - void client::destroy_unhandled_event_data(ENetEvent& e) { + void destroy_unhandled_event_data(ENetEvent& e) { if (e.type == ENET_EVENT_TYPE_RECEIVE) { enet_packet_destroy(e.packet); } } - void client::run_in_thread(const client_connect_params& params) { + void run_in_thread(const client_connect_params& params) { set_current_thread_name("enetpp::client"); ENetHost* host = enet_host_create(nullptr, 1, params._channel_count, params._incoming_bandwidth, params._outgoing_bandwidth); @@ -179,6 +179,10 @@ namespace enetpp { return; } + if(params._compress) { + enet_host_compress_with_range_coder(host); + } + auto address = params.make_server_address(); ENetPeer* peer = enet_host_connect(host, &address, params._channel_count, 0); if (peer == nullptr) { @@ -240,7 +244,7 @@ namespace enetpp { enet_host_destroy(host); } - void client::send_queued_packets_in_thread(ENetPeer* peer) { + void send_queued_packets_in_thread(ENetPeer* peer) { if (!_packet_queue.empty()) { std::lock_guard lock(_packet_queue_mutex); while (!_packet_queue.empty()) { @@ -258,7 +262,7 @@ namespace enetpp { } } - void client::trace(const std::string& s) { + void trace(const std::string& s) { if (_trace_handler != nullptr) { _trace_handler(s); } @@ -267,4 +271,4 @@ namespace enetpp { } -#endif \ No newline at end of file +#endif diff --git a/include/enetpp/client_connect_params.h b/include/enetpp/client_connect_params.h index 71e2f77..992a209 100644 --- a/include/enetpp/client_connect_params.h +++ b/include/enetpp/client_connect_params.h @@ -13,15 +13,19 @@ namespace enetpp { enet_uint32 _outgoing_bandwidth; std::string _server_host_name; enet_uint16 _server_port; + bool _compress; + ENetCompressor *_comp; std::chrono::milliseconds _timeout; public: - client_connect_params() + client_connect_params() : _channel_count(0) , _incoming_bandwidth(0) , _outgoing_bandwidth(0) , _server_host_name() , _server_port(0) + , _compress(false) + , _comp(nullptr) , _timeout(0) { } @@ -40,6 +44,18 @@ namespace enetpp { return *this; } + client_connect_params& set_compression +(bool compression) { + _compress=compression; + return *this; + } + + client_connect_params& set_compressor +(ENetCompressor *comp) { + _comp=comp; + return *this; + } + client_connect_params& set_server_host_name_and_port(const char* host_name, enet_uint16 port) { _server_host_name = host_name; _server_port = port; @@ -62,4 +78,4 @@ namespace enetpp { } -#endif \ No newline at end of file +#endif diff --git a/include/enetpp/server.h b/include/enetpp/server.h index 2e739b9..de67cf1 100644 --- a/include/enetpp/server.h +++ b/include/enetpp/server.h @@ -1,6 +1,7 @@ #ifndef ENETPP_SERVER_H_ #define ENETPP_SERVER_H_ +#include #include #include #include @@ -43,7 +44,7 @@ namespace enetpp { std::mutex _event_queue_mutex; public: - server() + server() : _should_exit_thread(false) { } @@ -101,7 +102,7 @@ namespace enetpp { } } - void send_packet_to_all_if(enet_uint8 channel_id, const enet_uint8* data, size_t data_size, enet_uint32 flags, std::function predicate) { + void send_packet_to_all_if(enet_uint8 channel_id, const enet_uint8* data, size_t data_size, enet_uint32 flags, std::function predicate) { assert(is_listening()); if (_thread != nullptr) { std::lock_guard lock(_packet_queue_mutex); @@ -116,7 +117,7 @@ namespace enetpp { void consume_events( std::function on_client_connected, - std::function on_client_disconnected, + std::function on_client_disconnected, std::function on_client_data_received) { if (!_event_queue.empty()) { @@ -125,7 +126,7 @@ namespace enetpp { std::lock_guard lock(_event_queue_mutex); assert(_event_queue_copy.empty()); _event_queue_copy = _event_queue; - _event_queue = {}; + _event_queue = std::queue(); } while (!_event_queue_copy.empty()) { @@ -141,9 +142,8 @@ namespace enetpp { auto iter = std::find(_connected_clients.begin(), _connected_clients.end(), e._client); assert(iter != _connected_clients.end()); _connected_clients.erase(iter); - unsigned int client_id = e._client->get_id(); + on_client_disconnected(*e._client); delete e._client; - on_client_disconnected(client_id); break; } @@ -183,7 +183,10 @@ namespace enetpp { } while (host != nullptr) { - + if(params._compress) { + enet_host_compress_with_range_coder(host); + } + } if (_should_exit_thread) { disconnect_all_peers_in_thread(); enet_host_destroy(host); @@ -271,7 +274,7 @@ namespace enetpp { enet_address_get_host_ip(&e.peer->address, peer_ip, 256); //!IMPORTANT! PeerData and it's UID must be created immediately in this worker thread. Otherwise - //there is a chance the first few packets are received on the worker thread when the peer is not + //there is a chance the first few packets are received on the worker thread when the peer is not //initialized with data causing them to be discarded. auto client = new ClientT(); @@ -352,4 +355,4 @@ namespace enetpp { } -#endif \ No newline at end of file +#endif diff --git a/include/enetpp/server_listen_params.h b/include/enetpp/server_listen_params.h index 6831ae9..d91f747 100644 --- a/include/enetpp/server_listen_params.h +++ b/include/enetpp/server_listen_params.h @@ -18,6 +18,7 @@ namespace enetpp { enet_uint32 _incoming_bandwidth; enet_uint32 _outgoing_bandwidth; enet_uint16 _listen_port; + bool _compress; std::chrono::milliseconds _peer_timeout; initialize_client_function _initialize_client_function; @@ -27,6 +28,7 @@ namespace enetpp { , _channel_count(0) , _incoming_bandwidth(0) , _outgoing_bandwidth(0) + , _compress(false) , _peer_timeout(0) { } @@ -55,6 +57,12 @@ namespace enetpp { return *this; } + server_listen_params& set_compression +(bool compression) { + _compress=compression; + return *this; + } + server_listen_params& set_peer_timeout(std::chrono::milliseconds timeout) { _peer_timeout = timeout; return *this; diff --git a/include/enetpp/set_current_thread_name.h b/include/enetpp/set_current_thread_name.h index 78211be..f5b1cde 100644 --- a/include/enetpp/set_current_thread_name.h +++ b/include/enetpp/set_current_thread_name.h @@ -2,7 +2,9 @@ #define ENETPP_SET_CURRENT_THREAD_NAME_H_ #include "enet/enet.h" - +#ifdef __GNUC__ +#include +#endif namespace enetpp { inline void set_current_thread_name(const char* name) { @@ -26,12 +28,22 @@ namespace enetpp { info.szName = name; info.dwThreadID = ::GetCurrentThreadId(); info.dwFlags = 0; - +#ifdef __GNUC__ +__seh_try{ +#else __try { +#endif RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(ULONG_PTR), (ULONG_PTR*)&info); } +#ifdef __GNUC__ + __seh_except (EXCEPTION_EXECUTE_HANDLER) { +#else __except (EXCEPTION_EXECUTE_HANDLER) { +#endif } +#ifdef __GNUC__ +__seh_end_except +#endif #else