Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
J
jlib
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
captainwong
jlib
Commits
eb141b60
Commit
eb141b60
authored
Sep 18, 2020
by
captainwong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor simple libevent
parent
ffa2e61e
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
135 additions
and
78 deletions
+135
-78
simple_libevent_client.cpp
jlib/net/simple_libevent_client.cpp
+40
-38
simple_libevent_micros.h
jlib/net/simple_libevent_micros.h
+55
-0
simple_libevent_server.cpp
jlib/net/simple_libevent_server.cpp
+36
-40
simple_libevent_client.vcxproj
test/simple_libevent_client/simple_libevent_client.vcxproj
+1
-0
simple_libevent_client.vcxproj.filters
...le_libevent_client/simple_libevent_client.vcxproj.filters
+3
-0
No files found.
jlib/net/simple_libevent_client.cpp
View file @
eb141b60
...
...
@@ -19,43 +19,58 @@
#include <event2/thread.h>
#include <thread>
#include <mutex>
#ifdef SIMPLELIBEVENTCLIENTLIB
#include "../log2.h"
#else
#include <jlib/log2.h>
#endif
#if
ndef JLIB_LOG2_ENABLED
#
pragma error "jlib::log2 not found!"
#if
defined(DISABLE_JLIB_LOG2) && !defined(JLIB_DISABLE_LOG)
#
define JLIB_DISABLE_LOG
#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
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
{
Impl
(
void
*
user_data
)
:
user_data
(
user_data
)
{
static
OneTimeIniter
init
;
SIMPLE_LIBEVENT_ONE_TIME_INITTER
;
}
event_base
*
base
=
nullptr
;
...
...
@@ -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
)
{
simple_libevent_client
*
client
=
(
simple_libevent_client
*
)
user_data
;
...
...
jlib/net/simple_libevent_micros.h
0 → 100644
View file @
eb141b60
#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
;
}
}
}
jlib/net/simple_libevent_server.cpp
View file @
eb141b60
...
...
@@ -14,52 +14,48 @@
#include <algorithm>
#include <signal.h>
#include <inttypes.h>
#ifdef SIMPLELIBEVENTSERVERLIB
#include "../log2.h"
#else
#include <jlib/log2.h>
#endif
#if
ndef JLIB_LOG2_ENABLED
#
pragma error "jlib::log2 not found!"
#if
defined(DISABLE_JLIB_LOG2) && !defined(JLIB_DISABLE_LOG)
#
define JLIB_DISABLE_LOG
#endif
namespace
jlib
{
namespace
net
{
#ifndef JLIB_DISABLE_LOG
# 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
{
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"
);
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
# define dump_hex(...)
# define dump_asc(...)
# define JLOG_HEX(...)
# define JLOG_ASC(...)
# endif
#endif // JLIB_DISABLE_LOG
}
};
#include "simple_libevent_micros.h"
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
;
}
namespace
jlib
{
namespace
net
{
struct
BaseClientPrivateData
{
int
thread_id
=
0
;
...
...
@@ -306,7 +302,7 @@ struct simple_libevent_server::PrivateImpl
simple_libevent_server
::
simple_libevent_server
()
{
static
OneTimeIniter
initLibEvent
;
SIMPLE_LIBEVENT_ONE_TIME_INITTER
;
}
simple_libevent_server
::~
simple_libevent_server
()
...
...
test/simple_libevent_client/simple_libevent_client.vcxproj
View file @
eb141b60
...
...
@@ -23,6 +23,7 @@
</ItemGroup>
<ItemGroup>
<ClInclude
Include=
"..\..\jlib\net\simple_libevent_client.h"
/>
<ClInclude
Include=
"..\..\jlib\net\simple_libevent_micros.h"
/>
</ItemGroup>
<PropertyGroup
Label=
"Globals"
>
<VCProjectVersion>
16.0
</VCProjectVersion>
...
...
test/simple_libevent_client/simple_libevent_client.vcxproj.filters
View file @
eb141b60
...
...
@@ -18,6 +18,9 @@
<ClInclude
Include=
"..\..\jlib\net\simple_libevent_client.h"
>
<Filter>
Header Files
</Filter>
</ClInclude>
<ClInclude
Include=
"..\..\jlib\net\simple_libevent_micros.h"
>
<Filter>
Header Files
</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile
Include=
"..\..\jlib\net\simple_libevent_client.cpp"
>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment