Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed errors in MinGW, added compression support in both client and s… #3

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 22 additions & 18 deletions include/enetpp/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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<std::thread>(&client::run_in_thread, this, params);
_thread = std::make_unique<std::thread>(&run_in_thread, this, params);
}

void client::disconnect() {
void disconnect() {
if (_thread != nullptr) {
_should_exit_thread = true;
_thread->join();
Expand All @@ -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<std::mutex> lock(_packet_queue_mutex);
Expand All @@ -87,7 +87,7 @@ namespace enetpp {
}
}

void client::consume_events(
void consume_events(
std::function<void()> on_connected,
std::function<void()> on_disconnected,
std::function<void(const enet_uint8* data, size_t data_size)> on_data_received) {
Expand All @@ -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<std::mutex> lock(_event_queue_mutex);
assert(_event_queue_copy.empty());
_event_queue_copy = _event_queue;
_event_queue = {};
_event_queue = std::queue<ENetEvent>();
}

bool is_disconnected = false;
Expand Down Expand Up @@ -148,29 +148,29 @@ namespace enetpp {
}

private:
void client::destroy_all_queued_packets() {
void destroy_all_queued_packets() {
std::lock_guard<std::mutex> lock(_packet_queue_mutex);
while (!_packet_queue.empty()) {
enet_packet_destroy(_packet_queue.front()._packet);
_packet_queue.pop();
}
}

void client::destroy_all_queued_events() {
void destroy_all_queued_events() {
std::lock_guard<std::mutex> lock(_event_queue_mutex);
while (!_event_queue.empty()) {
destroy_unhandled_event_data(_event_queue.front());
_event_queue.pop();
}
}

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);
Expand All @@ -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) {
Expand Down Expand Up @@ -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<std::mutex> lock(_packet_queue_mutex);
while (!_packet_queue.empty()) {
Expand All @@ -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);
}
Expand All @@ -267,4 +271,4 @@ namespace enetpp {

}

#endif
#endif
20 changes: 18 additions & 2 deletions include/enetpp/client_connect_params.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
}

Expand All @@ -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;
Expand All @@ -62,4 +78,4 @@ namespace enetpp {

}

#endif
#endif
21 changes: 12 additions & 9 deletions include/enetpp/server.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef ENETPP_SERVER_H_
#define ENETPP_SERVER_H_

#include <algorithm>
#include <functional>
#include <memory>
#include <thread>
Expand Down Expand Up @@ -43,7 +44,7 @@ namespace enetpp {
std::mutex _event_queue_mutex;

public:
server()
server()
: _should_exit_thread(false) {
}

Expand Down Expand Up @@ -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<bool(const ClientT& client)> predicate) {
void send_packet_to_all_if(enet_uint8 channel_id, const enet_uint8* data, size_t data_size, enet_uint32 flags, std::function<bool(ClientT& client)> predicate) {
assert(is_listening());
if (_thread != nullptr) {
std::lock_guard<std::mutex> lock(_packet_queue_mutex);
Expand All @@ -116,7 +117,7 @@ namespace enetpp {

void consume_events(
std::function<void(ClientT& client)> on_client_connected,
std::function<void(unsigned int client_id)> on_client_disconnected,
std::function<void(ClientT& client)> on_client_disconnected,
std::function<void(ClientT& client, const enet_uint8* data, size_t data_size)> on_client_data_received) {

if (!_event_queue.empty()) {
Expand All @@ -125,7 +126,7 @@ namespace enetpp {
std::lock_guard<std::mutex> lock(_event_queue_mutex);
assert(_event_queue_copy.empty());
_event_queue_copy = _event_queue;
_event_queue = {};
_event_queue = std::queue<event_type>();
}

while (!_event_queue_copy.empty()) {
Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -352,4 +355,4 @@ namespace enetpp {

}

#endif
#endif
8 changes: 8 additions & 0 deletions include/enetpp/server_listen_params.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -27,6 +28,7 @@ namespace enetpp {
, _channel_count(0)
, _incoming_bandwidth(0)
, _outgoing_bandwidth(0)
, _compress(false)
, _peer_timeout(0) {
}

Expand Down Expand Up @@ -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;
Expand Down
16 changes: 14 additions & 2 deletions include/enetpp/set_current_thread_name.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
#define ENETPP_SET_CURRENT_THREAD_NAME_H_

#include "enet/enet.h"

#ifdef __GNUC__
#include <seh.h>
#endif
namespace enetpp {

inline void set_current_thread_name(const char* name) {
Expand All @@ -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

Expand Down