Commit eb141b60 authored by captainwong's avatar captainwong

refactor simple libevent

parent ffa2e61e
...@@ -19,43 +19,58 @@ ...@@ -19,43 +19,58 @@
#include <event2/thread.h> #include <event2/thread.h>
#include <thread> #include <thread>
#include <mutex> #include <mutex>
#ifdef SIMPLELIBEVENTCLIENTLIB
#include "../log2.h"
#else
#include <jlib/log2.h>
#endif
#ifndef JLIB_LOG2_ENABLED #if defined(DISABLE_JLIB_LOG2) && !defined(JLIB_DISABLE_LOG)
#pragma error "jlib::log2 not found!" #define JLIB_DISABLE_LOG
#endif #endif
#ifndef JLIB_DISABLE_LOG
# ifdef SIMPLELIBEVENTCLIENTLIB
# include "../log2.h"
# else
# include <jlib/log2.h>
# endif
#else // JLIB_DISABLE_LOG
# ifdef SIMPLELIBEVENTCLIENTLIB
# include "../log2micros.h"
# else
# define init_logger(...)
# define JLOG_DBUG(...)
# define JLOG_INFO(...)
# define JLOG_WARN(...)
# define JLOG_ERRO(...)
# define JLOG_CRTC(...)
# define JLOG_ALL(...)
class range_log {
public:
range_log() {}
range_log(const char*) {}
};
# define AUTO_LOG_FUNCTION
# define dump_hex(...)
# define dump_asc(...)
# define JLOG_HEX(...)
# define JLOG_ASC(...)
# endif
#endif // JLIB_DISABLE_LOG
#include "simple_libevent_micros.h"
namespace jlib { namespace jlib {
namespace net { namespace net {
struct OneTimeIniter {
OneTimeIniter() {
#ifdef _WIN32
WSADATA wsa_data;
WSAStartup(0x0201, &wsa_data);
if (0 != evthread_use_windows_threads()) {
JLOG_CRTC("failed to init libevent with thread by calling evthread_use_windows_threads");
abort();
}
#else
if (0 != evthread_use_pthreads()) {
JLOG_CRTC("failed to init libevent with thread by calling evthread_use_pthreads");
abort();
}
#endif
}
};
struct simple_libevent_client::Impl struct simple_libevent_client::Impl
{ {
Impl(void* user_data) Impl(void* user_data)
: user_data(user_data) : user_data(user_data)
{ {
static OneTimeIniter init; SIMPLE_LIBEVENT_ONE_TIME_INITTER;
} }
event_base* base = nullptr; event_base* base = nullptr;
...@@ -95,19 +110,6 @@ struct simple_libevent_client::Impl ...@@ -95,19 +110,6 @@ struct simple_libevent_client::Impl
} }
} }
static std::string eventToString(short evs) {
std::string s;
#define check_event_append_to_s(e) if(evs & e){s += #e " ";}
check_event_append_to_s(BEV_EVENT_READING);
check_event_append_to_s(BEV_EVENT_WRITING);
check_event_append_to_s(BEV_EVENT_EOF);
check_event_append_to_s(BEV_EVENT_ERROR);
check_event_append_to_s(BEV_EVENT_TIMEOUT);
check_event_append_to_s(BEV_EVENT_CONNECTED);
#undef check_event_append_to_s
return s;
}
static void eventcb(struct bufferevent* bev, short events, void* user_data) static void eventcb(struct bufferevent* bev, short events, void* user_data)
{ {
simple_libevent_client* client = (simple_libevent_client*)user_data; simple_libevent_client* client = (simple_libevent_client*)user_data;
......
#pragma once
#ifdef _WIN32
namespace jlib { namespace net {
struct SimpleLibeventOneTimeInitHelper {
SimpleLibeventOneTimeInitHelper() {
WSADATA wsa_data;
WSAStartup(0x0201, &wsa_data);
if (0 != evthread_use_windows_threads()) {
JLOG_CRTC("failed to init libevent with thread by calling evthread_use_windows_threads");
abort();
}
}
};
}}
#else
namespace jlib { namespace net {
struct SimpleLibeventOneTimeInitHelper {
SimpleLibeventOneTimeInitHelper() {
if (0 != evthread_use_pthreads()) {
JLOG_CRTC("failed to init libevent with thread by calling evthread_use_pthreads");
abort();
}
}
};
}}
#endif
namespace jlib {
namespace net {
struct SimpleLibeventOneTimeInitter {
SimpleLibeventOneTimeInitter() { static SimpleLibeventOneTimeInitHelper __simple_libevent_one_time_init_helper; }
};
}
}
#define SIMPLE_LIBEVENT_ONE_TIME_INITTER static jlib::net::SimpleLibeventOneTimeInitter __simple_libevent_one_time_initter;
namespace jlib {
namespace net {
inline std::string eventToString(short evs) {
std::string s;
#define check_event_append_to_s(e) if(evs & e){s += #e " ";}
check_event_append_to_s(BEV_EVENT_READING);
check_event_append_to_s(BEV_EVENT_WRITING);
check_event_append_to_s(BEV_EVENT_EOF);
check_event_append_to_s(BEV_EVENT_ERROR);
check_event_append_to_s(BEV_EVENT_TIMEOUT);
check_event_append_to_s(BEV_EVENT_CONNECTED);
return s;
}
}
}
...@@ -14,52 +14,48 @@ ...@@ -14,52 +14,48 @@
#include <algorithm> #include <algorithm>
#include <signal.h> #include <signal.h>
#include <inttypes.h> #include <inttypes.h>
#ifdef SIMPLELIBEVENTSERVERLIB
#include "../log2.h"
#else
#include <jlib/log2.h>
#endif
#ifndef JLIB_LOG2_ENABLED #if defined(DISABLE_JLIB_LOG2) && !defined(JLIB_DISABLE_LOG)
#pragma error "jlib::log2 not found!" #define JLIB_DISABLE_LOG
#endif #endif
namespace jlib { #ifndef JLIB_DISABLE_LOG
namespace net { # ifdef SIMPLELIBEVENTSERVERLIB
# include "../log2.h"
# else
# include <jlib/log2.h>
# endif
#else // JLIB_DISABLE_LOG
# ifdef SIMPLELIBEVENTSERVERLIB
# include "../log2micros.h"
# else
# define init_logger(...)
# define JLOG_DBUG(...)
# define JLOG_INFO(...)
# define JLOG_WARN(...)
# define JLOG_ERRO(...)
# define JLOG_CRTC(...)
# define JLOG_ALL(...)
class range_log {
public:
range_log() {}
range_log(const char*) {}
};
# define AUTO_LOG_FUNCTION
struct OneTimeIniter { # define dump_hex(...)
OneTimeIniter() { # define dump_asc(...)
#ifdef _WIN32 # define JLOG_HEX(...)
WSADATA wsa_data; # define JLOG_ASC(...)
WSAStartup(0x0201, &wsa_data); # endif
if (0 != evthread_use_windows_threads()) { #endif // JLIB_DISABLE_LOG
JLOG_CRTC("failed to init libevent with thread by calling evthread_use_windows_threads");
exit(-1);
}
#else
if (0 != evthread_use_pthreads()) {
JLOG_CRTC("failed to init libevent with thread by calling evthread_use_pthreads");
exit(-1);
}
signal(SIGPIPE, SIG_IGN);
#endif
} #include "simple_libevent_micros.h"
};
static std::string eventToString(short evs) { namespace jlib {
std::string s; namespace net {
#define check_event_append_to_s(e) if(evs & e){s += #e " ";}
check_event_append_to_s(BEV_EVENT_READING);
check_event_append_to_s(BEV_EVENT_WRITING);
check_event_append_to_s(BEV_EVENT_EOF);
check_event_append_to_s(BEV_EVENT_ERROR);
check_event_append_to_s(BEV_EVENT_TIMEOUT);
check_event_append_to_s(BEV_EVENT_CONNECTED);
#undef check_event_append_to_s
return s;
}
struct BaseClientPrivateData { struct BaseClientPrivateData {
int thread_id = 0; int thread_id = 0;
...@@ -306,7 +302,7 @@ struct simple_libevent_server::PrivateImpl ...@@ -306,7 +302,7 @@ struct simple_libevent_server::PrivateImpl
simple_libevent_server::simple_libevent_server() simple_libevent_server::simple_libevent_server()
{ {
static OneTimeIniter initLibEvent; SIMPLE_LIBEVENT_ONE_TIME_INITTER;
} }
simple_libevent_server::~simple_libevent_server() simple_libevent_server::~simple_libevent_server()
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\..\jlib\net\simple_libevent_client.h" /> <ClInclude Include="..\..\jlib\net\simple_libevent_client.h" />
<ClInclude Include="..\..\jlib\net\simple_libevent_micros.h" />
</ItemGroup> </ItemGroup>
<PropertyGroup Label="Globals"> <PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion> <VCProjectVersion>16.0</VCProjectVersion>
......
...@@ -18,6 +18,9 @@ ...@@ -18,6 +18,9 @@
<ClInclude Include="..\..\jlib\net\simple_libevent_client.h"> <ClInclude Include="..\..\jlib\net\simple_libevent_client.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\jlib\net\simple_libevent_micros.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="..\..\jlib\net\simple_libevent_client.cpp"> <ClCompile Include="..\..\jlib\net\simple_libevent_client.cpp">
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment