From a5ada1edd960cb79d1c6238f5f4d2e2363500123 Mon Sep 17 00:00:00 2001 From: brightening-eyes Date: Mon, 25 Apr 2016 08:44:38 -0700 Subject: [PATCH 1/6] fixed errors in MinGW, added compression support in both client and server, MinGW is now dependent libseh --- include/enetpp/client.h | 40 +++++++++++++----------- include/enetpp/client_connect_params.h | 12 +++++-- include/enetpp/server.h | 17 +++++----- include/enetpp/server_listen_params.h | 8 +++++ include/enetpp/set_current_thread_name.h | 16 ++++++++-- 5 files changed, 64 insertions(+), 29 deletions(-) diff --git a/include/enetpp/client.h b/include/enetpp/client.h index 7aa662d..649f817 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_with_range_coder) { + 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..9fbc15c 100644 --- a/include/enetpp/client_connect_params.h +++ b/include/enetpp/client_connect_params.h @@ -13,15 +13,17 @@ namespace enetpp { enet_uint32 _outgoing_bandwidth; std::string _server_host_name; enet_uint16 _server_port; +bool _compress_with_range_coder; 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_with_range_coder(false) , _timeout(0) { } @@ -40,6 +42,12 @@ namespace enetpp { return *this; } + client_connect_params& set_compression +(bool compression) { + _compress_with_range_coder=compression; + 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 +70,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..b051843 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) { } @@ -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; } @@ -182,6 +182,9 @@ namespace enetpp { trace("enet_host_create failed"); } + if(params._compress_with_range_coder) { + enet_host_compress_with_range_coder(host); + } while (host != nullptr) { if (_should_exit_thread) { @@ -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..5f9ccc5 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_with_range_coder; 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_with_range_coder(false) , _peer_timeout(0) { } @@ -55,6 +57,12 @@ namespace enetpp { return *this; } + server_listen_params& set_compression +(bool compression) { + _compress_with_range_coder=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 From 778becce8ff657e9dfe0401780df2ead713697af Mon Sep 17 00:00:00 2001 From: brightening-eyes Date: Mon, 25 Apr 2016 13:47:47 -0700 Subject: [PATCH 2/6] fixed compression support --- include/enetpp/client.h | 2 +- include/enetpp/server.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/enetpp/client.h b/include/enetpp/client.h index 649f817..d758cdf 100644 --- a/include/enetpp/client.h +++ b/include/enetpp/client.h @@ -180,7 +180,7 @@ namespace enetpp { } if(params._compress_with_range_coder) { - enet_host_compress_with_range_coder(host); + enet_host_compress_with_range_coder(&host); } auto address = params.make_server_address(); diff --git a/include/enetpp/server.h b/include/enetpp/server.h index b051843..13f7123 100644 --- a/include/enetpp/server.h +++ b/include/enetpp/server.h @@ -183,7 +183,7 @@ namespace enetpp { } if(params._compress_with_range_coder) { - enet_host_compress_with_range_coder(host); + enet_host_compress_with_range_coder(&host); } while (host != nullptr) { From 5929896bb99e61eaee0972c1ce05dad7c42b03f3 Mon Sep 17 00:00:00 2001 From: brightening-eyes Date: Mon, 25 Apr 2016 13:52:46 -0700 Subject: [PATCH 3/6] fixed an error in compressor --- include/enetpp/client.h | 2 +- include/enetpp/server.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/enetpp/client.h b/include/enetpp/client.h index d758cdf..649f817 100644 --- a/include/enetpp/client.h +++ b/include/enetpp/client.h @@ -180,7 +180,7 @@ namespace enetpp { } if(params._compress_with_range_coder) { - enet_host_compress_with_range_coder(&host); + enet_host_compress_with_range_coder(host); } auto address = params.make_server_address(); diff --git a/include/enetpp/server.h b/include/enetpp/server.h index 13f7123..b051843 100644 --- a/include/enetpp/server.h +++ b/include/enetpp/server.h @@ -183,7 +183,7 @@ namespace enetpp { } if(params._compress_with_range_coder) { - enet_host_compress_with_range_coder(&host); + enet_host_compress_with_range_coder(host); } while (host != nullptr) { From a0c19c4d14426544e7b5aa651970c089a3839401 Mon Sep 17 00:00:00 2001 From: brightening-eyes Date: Tue, 26 Apr 2016 01:41:07 -0700 Subject: [PATCH 4/6] completely fixed compression with range coder, now it works --- include/enetpp/server.h | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/include/enetpp/server.h b/include/enetpp/server.h index b051843..9f35b4c 100644 --- a/include/enetpp/server.h +++ b/include/enetpp/server.h @@ -182,11 +182,10 @@ namespace enetpp { trace("enet_host_create failed"); } - if(params._compress_with_range_coder) { - enet_host_compress_with_range_coder(host); - } while (host != nullptr) { - + if(params._compress_with_range_coder) { + enet_host_compress_with_range_coder(host); + } if (_should_exit_thread) { disconnect_all_peers_in_thread(); enet_host_destroy(host); From ce4478d75abb3cb3f6d6de83c8f9e7b5ee0baa50 Mon Sep 17 00:00:00 2001 From: brightening-eyes Date: Sat, 30 Apr 2016 13:04:32 -0700 Subject: [PATCH 5/6] added a feature to use a custom ENetCompressor --- include/enetpp/client.h | 7 ++++++- include/enetpp/client_connect_params.h | 14 +++++++++++--- include/enetpp/server.h | 9 +++++++-- include/enetpp/server_listen_params.h | 14 +++++++++++--- 4 files changed, 35 insertions(+), 9 deletions(-) diff --git a/include/enetpp/client.h b/include/enetpp/client.h index 649f817..c46e62a 100644 --- a/include/enetpp/client.h +++ b/include/enetpp/client.h @@ -179,8 +179,13 @@ namespace enetpp { return; } - if(params._compress_with_range_coder) { + if(params._compress) { + if(params._comp!=nullptr) { + enet_host_compress(host, params._comp); + } + else { enet_host_compress_with_range_coder(host); + } } auto address = params.make_server_address(); diff --git a/include/enetpp/client_connect_params.h b/include/enetpp/client_connect_params.h index 9fbc15c..992a209 100644 --- a/include/enetpp/client_connect_params.h +++ b/include/enetpp/client_connect_params.h @@ -13,7 +13,8 @@ namespace enetpp { enet_uint32 _outgoing_bandwidth; std::string _server_host_name; enet_uint16 _server_port; -bool _compress_with_range_coder; + bool _compress; + ENetCompressor *_comp; std::chrono::milliseconds _timeout; public: @@ -23,7 +24,8 @@ bool _compress_with_range_coder; , _outgoing_bandwidth(0) , _server_host_name() , _server_port(0) - , _compress_with_range_coder(false) + , _compress(false) + , _comp(nullptr) , _timeout(0) { } @@ -44,7 +46,13 @@ bool _compress_with_range_coder; client_connect_params& set_compression (bool compression) { - _compress_with_range_coder=compression; + _compress=compression; + return *this; + } + + client_connect_params& set_compressor +(ENetCompressor *comp) { + _comp=comp; return *this; } diff --git a/include/enetpp/server.h b/include/enetpp/server.h index 9f35b4c..9e8b134 100644 --- a/include/enetpp/server.h +++ b/include/enetpp/server.h @@ -102,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); @@ -183,8 +183,13 @@ namespace enetpp { } while (host != nullptr) { - if(params._compress_with_range_coder) { + if(params._compress) { + if(params._comp==nullptr) { enet_host_compress_with_range_coder(host); + } + else { + enet_host_compress(host, params._comp); + } } if (_should_exit_thread) { disconnect_all_peers_in_thread(); diff --git a/include/enetpp/server_listen_params.h b/include/enetpp/server_listen_params.h index 5f9ccc5..8c7734b 100644 --- a/include/enetpp/server_listen_params.h +++ b/include/enetpp/server_listen_params.h @@ -18,7 +18,8 @@ namespace enetpp { enet_uint32 _incoming_bandwidth; enet_uint32 _outgoing_bandwidth; enet_uint16 _listen_port; - bool _compress_with_range_coder; + bool _compress; + ENetCompressor *_comp; std::chrono::milliseconds _peer_timeout; initialize_client_function _initialize_client_function; @@ -28,7 +29,8 @@ namespace enetpp { , _channel_count(0) , _incoming_bandwidth(0) , _outgoing_bandwidth(0) - , _compress_with_range_coder(false) + , _compress(false) + , _comp(nullptr) , _peer_timeout(0) { } @@ -59,7 +61,13 @@ namespace enetpp { server_listen_params& set_compression (bool compression) { - _compress_with_range_coder=compression; + _compress=compression; + return *this; + } + + server_params& set_compressor +(ENetCompressor *comp) { + _comp=comp; return *this; } From 312bc222e1dc1ac7771d0a2baa69c099434f8de4 Mon Sep 17 00:00:00 2001 From: brightening-eyes Date: Sun, 1 May 2016 07:53:32 -0700 Subject: [PATCH 6/6] reverting changes back for fixing the errors --- include/enetpp/client.h | 5 ----- include/enetpp/server.h | 4 ---- include/enetpp/server_listen_params.h | 8 -------- 3 files changed, 17 deletions(-) diff --git a/include/enetpp/client.h b/include/enetpp/client.h index c46e62a..901cb1d 100644 --- a/include/enetpp/client.h +++ b/include/enetpp/client.h @@ -180,12 +180,7 @@ namespace enetpp { } if(params._compress) { - if(params._comp!=nullptr) { - enet_host_compress(host, params._comp); - } - else { enet_host_compress_with_range_coder(host); - } } auto address = params.make_server_address(); diff --git a/include/enetpp/server.h b/include/enetpp/server.h index 9e8b134..de67cf1 100644 --- a/include/enetpp/server.h +++ b/include/enetpp/server.h @@ -184,12 +184,8 @@ namespace enetpp { while (host != nullptr) { if(params._compress) { - if(params._comp==nullptr) { enet_host_compress_with_range_coder(host); } - else { - enet_host_compress(host, params._comp); - } } if (_should_exit_thread) { disconnect_all_peers_in_thread(); diff --git a/include/enetpp/server_listen_params.h b/include/enetpp/server_listen_params.h index 8c7734b..d91f747 100644 --- a/include/enetpp/server_listen_params.h +++ b/include/enetpp/server_listen_params.h @@ -19,7 +19,6 @@ namespace enetpp { enet_uint32 _outgoing_bandwidth; enet_uint16 _listen_port; bool _compress; - ENetCompressor *_comp; std::chrono::milliseconds _peer_timeout; initialize_client_function _initialize_client_function; @@ -30,7 +29,6 @@ namespace enetpp { , _incoming_bandwidth(0) , _outgoing_bandwidth(0) , _compress(false) - , _comp(nullptr) , _peer_timeout(0) { } @@ -65,12 +63,6 @@ namespace enetpp { return *this; } - server_params& set_compressor -(ENetCompressor *comp) { - _comp=comp; - return *this; - } - server_listen_params& set_peer_timeout(std::chrono::milliseconds timeout) { _peer_timeout = timeout; return *this;