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
8073c0da
Commit
8073c0da
authored
Sep 03, 2020
by
captainwong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update simple libevent client/server
parent
1a66d846
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
92 additions
and
87 deletions
+92
-87
resolve_fastest_ip.h
jlib/net/resolve_fastest_ip.h
+1
-1
simple_libevent_client.cpp
jlib/net/simple_libevent_client.cpp
+14
-13
simple_libevent_client.h
jlib/net/simple_libevent_client.h
+11
-11
simple_libevent_server.cpp
jlib/net/simple_libevent_server.cpp
+23
-18
simple_libevent_server.h
jlib/net/simple_libevent_server.h
+23
-24
simple_libevent_client.vcxproj
test/simple_libevent_client/simple_libevent_client.vcxproj
+4
-4
simple_libevent_client.vcxproj.filters
...le_libevent_client/simple_libevent_client.vcxproj.filters
+6
-6
simple_libevent_server.vcxproj
test/simple_libevent_server/simple_libevent_server.vcxproj
+4
-4
simple_libevent_server.vcxproj.filters
...le_libevent_server/simple_libevent_server.vcxproj.filters
+6
-6
No files found.
jlib/net/resolve_fastest_ip.h
View file @
8073c0da
...
...
@@ -80,7 +80,7 @@ static std::pair<bool, std::string> get_domain_ip_impl(const std::string& domain
}
// end of namespace detail
static
bool
get_domain_ip
(
const
std
::
string
&
domain
,
int
ping_times
,
std
::
string
&
result
,
waiting_cb
cb
)
{
static
bool
get_domain_ip
(
const
std
::
string
&
domain
,
int
ping_times
,
std
::
string
&
result
,
waiting_cb
cb
=
nullptr
)
{
auto
f
=
std
::
async
(
std
::
launch
::
async
,
detail
::
get_domain_ip_impl
,
domain
,
ping_times
);
while
(
true
)
{
auto
status
=
f
.
wait_for
(
std
::
chrono
::
milliseconds
(
1
));
...
...
jlib/net/
C
lient.cpp
→
jlib/net/
simple_libevent_c
lient.cpp
View file @
8073c0da
#include "
C
lient.h"
#include "
simple_libevent_c
lient.h"
#ifdef _WIN32
#include <WinSock2.h>
...
...
@@ -19,7 +19,11 @@
#include <event2/thread.h>
#include <thread>
#include <mutex>
#ifdef SIMPLELIBEVENTCLIENTLIB
#include "../log2.h"
#else
#include <jlib/log2.h>
#endif
#ifndef JLIB_LOG2_ENABLED
#pragma error "jlib::log2 not found!"
...
...
@@ -27,8 +31,6 @@
namespace
jlib
{
namespace
net
{
namespace
client
{
struct
OneTimeIniter
{
OneTimeIniter
()
{
...
...
@@ -48,7 +50,7 @@ struct OneTimeIniter {
}
};
struct
C
lient
::
Impl
struct
simple_libevent_c
lient
::
Impl
{
Impl
(
void
*
user_data
)
:
user_data
(
user_data
)
...
...
@@ -66,7 +68,7 @@ struct Client::Impl
static
void
writecb
(
struct
bufferevent
*
,
void
*
user_data
)
{
Client
*
client
=
(
C
lient
*
)
user_data
;
simple_libevent_client
*
client
=
(
simple_libevent_c
lient
*
)
user_data
;
client
->
lastTimeSendData
=
std
::
chrono
::
steady_clock
::
now
();
}
...
...
@@ -75,7 +77,7 @@ struct Client::Impl
char
buff
[
4096
];
auto
input
=
bufferevent_get_input
(
bev
);
JLOG_DBUG
(
"readcb, readable len {}"
,
evbuffer_get_length
(
input
));
Client
*
client
=
(
C
lient
*
)
user_data
;
simple_libevent_client
*
client
=
(
simple_libevent_c
lient
*
)
user_data
;
if
(
client
->
userData_
&&
client
->
onMsg_
)
{
while
(
1
)
{
int
len
=
evbuffer_copyout
(
input
,
buff
,
std
::
min
(
sizeof
(
buff
),
evbuffer_get_length
(
input
)));
...
...
@@ -108,7 +110,7 @@ struct Client::Impl
static
void
eventcb
(
struct
bufferevent
*
bev
,
short
events
,
void
*
user_data
)
{
Client
*
client
=
(
C
lient
*
)
user_data
;
simple_libevent_client
*
client
=
(
simple_libevent_c
lient
*
)
user_data
;
JLOG_DBUG
(
"eventcb events={}"
,
eventToString
(
events
));
std
::
string
msg
;
...
...
@@ -156,7 +158,7 @@ struct Client::Impl
static
void
timercb
(
evutil_socket_t
,
short
,
void
*
user_data
)
{
Client
*
client
=
(
C
lient
*
)
user_data
;
simple_libevent_client
*
client
=
(
simple_libevent_c
lient
*
)
user_data
;
if
(
client
->
strictTimer_
)
{
if
(
client
->
userData_
&&
client
->
onTimer_
)
{
client
->
onTimer_
(
client
->
userData_
);
...
...
@@ -179,7 +181,7 @@ struct Client::Impl
static
void
reconn_timercb
(
evutil_socket_t
,
short
,
void
*
user_data
)
{
Client
*
client
=
(
C
lient
*
)
user_data
;
simple_libevent_client
*
client
=
(
simple_libevent_c
lient
*
)
user_data
;
do
{
std
::
string
msg
=
"Reconnecting "
+
client
->
impl_
->
ip
+
":"
+
std
::
to_string
(
client
->
impl_
->
port
);
...
...
@@ -216,7 +218,7 @@ struct Client::Impl
}
};
bool
C
lient
::
start
(
const
std
::
string
&
ip
,
uint16_t
port
,
std
::
string
&
msg
)
bool
simple_libevent_c
lient
::
start
(
const
std
::
string
&
ip
,
uint16_t
port
,
std
::
string
&
msg
)
{
do
{
stop
();
...
...
@@ -267,7 +269,7 @@ bool Client::start(const std::string& ip, uint16_t port, std::string& msg)
return
false
;
}
void
C
lient
::
stop
()
void
simple_libevent_c
lient
::
stop
()
{
std
::
lock_guard
<
std
::
mutex
>
lg
(
mutex_
);
if
(
!
impl_
)
{
return
;
}
...
...
@@ -309,7 +311,7 @@ void Client::stop()
autoReconnect_
=
old
;
}
void
C
lient
::
send
(
const
char
*
data
,
size_t
len
)
void
simple_libevent_c
lient
::
send
(
const
char
*
data
,
size_t
len
)
{
std
::
lock_guard
<
std
::
mutex
>
lg
(
mutex_
);
if
(
!
impl_
||
!
impl_
->
base
||
!
impl_
->
bev
)
{
return
;
}
...
...
@@ -321,4 +323,3 @@ void Client::send(const char* data, size_t len)
}
}
}
jlib/net/
C
lient.h
→
jlib/net/
simple_libevent_c
lient.h
View file @
8073c0da
...
...
@@ -19,22 +19,23 @@
namespace
jlib
{
namespace
net
{
namespace
client
{
typedef
void
(
*
OnConnectinoCallback
)(
bool
up
,
const
std
::
string
&
msg
,
void
*
user_data
);
class
simple_libevent_client
{
public
:
typedef
void
(
*
OnConnectinoCallback
)(
bool
up
,
const
std
::
string
&
msg
,
void
*
user_data
);
// return > 0 for ate
// return 0 for stop
typedef
size_t
(
*
OnMessageCallback
)(
const
char
*
data
,
size_t
len
,
void
*
user_data
);
// return > 0 for ate
// return 0 for stop
typedef
size_t
(
*
OnMessageCallback
)(
const
char
*
data
,
size_t
len
,
void
*
user_data
);
typedef
void
(
*
OnTimerCallback
)(
void
*
user_data
);
typedef
void
(
*
OnTimerCallback
)(
void
*
user_data
);
class
Client
{
public
:
C
lient
()
{}
virtual
~
C
lient
()
{
stop
();
}
simple_libevent_c
lient
()
{}
virtual
~
simple_libevent_c
lient
()
{
stop
();
}
void
setUserData
(
void
*
d
)
{
userData_
=
d
;
}
void
setOnConnectionCallback
(
OnConnectinoCallback
cb
)
{
onConn_
=
cb
;
}
...
...
@@ -71,4 +72,3 @@ protected:
}
}
}
jlib/net/
S
erver.cpp
→
jlib/net/
simple_libevent_s
erver.cpp
View file @
8073c0da
#include "
S
erver.h"
#include "
simple_libevent_s
erver.h"
#include <assert.h>
#include <errno.h>
#include <stdlib.h>
...
...
@@ -14,12 +14,18 @@
#include <algorithm>
#include <signal.h>
#include <inttypes.h>
#ifdef SIMPLELIBEVENTSERVERLIB
#include "../log2.h"
#else
#include <jlib/log2.h>
#endif
#ifndef JLIB_LOG2_ENABLED
#pragma error "jlib::log2 not found!"
#endif
namespace
jlib
{
namespace
net
{
namespace
server
{
struct
OneTimeIniter
{
...
...
@@ -63,25 +69,25 @@ struct BaseClientPrivateData {
};
BaseClient
::
BaseClient
(
int
fd
,
void
*
bev
)
simple_libevent_server
::
BaseClient
::
BaseClient
(
int
fd
,
void
*
bev
)
:
fd
(
fd
)
,
privateData
(
new
BaseClientPrivateData
())
{
((
BaseClientPrivateData
*
)
privateData
)
->
bev
=
bev
;
}
BaseClient
::~
BaseClient
()
simple_libevent_server
::
BaseClient
::~
BaseClient
()
{
delete
(
BaseClientPrivateData
*
)
privateData
;
}
BaseClient
*
BaseClient
::
createDefaultClient
(
int
fd
,
void
*
bev
)
simple_libevent_server
::
BaseClient
*
simple_libevent_server
::
BaseClient
::
createDefaultClient
(
int
fd
,
void
*
bev
)
{
BaseClient
*
client
=
new
BaseClient
(
fd
,
bev
);
return
client
;
}
void
BaseClient
::
send
(
const
void
*
data
,
size_t
len
)
void
simple_libevent_server
::
BaseClient
::
send
(
const
void
*
data
,
size_t
len
)
{
if
(
!
((
BaseClientPrivateData
*
)
privateData
)
->
bev
)
{
JLOG_CRTC
(
"BaseClient::send bev is nullptr, #{}"
,
fd
);
...
...
@@ -99,7 +105,7 @@ void BaseClient::send(const void* data, size_t len)
evbuffer_unlock
(
output
);
}
void
BaseClient
::
shutdown
(
int
what
)
void
simple_libevent_server
::
BaseClient
::
shutdown
(
int
what
)
{
if
(
fd
!=
0
)
{
::
shutdown
(
fd
,
what
);
...
...
@@ -107,12 +113,12 @@ void BaseClient::shutdown(int what)
}
}
void
BaseClient
::
updateLastTimeComm
()
void
simple_libevent_server
::
BaseClient
::
updateLastTimeComm
()
{
((
BaseClientPrivateData
*
)
privateData
)
->
lastTimeComm
=
std
::
chrono
::
steady_clock
::
now
();
}
struct
S
erver
::
PrivateImpl
struct
simple_libevent_s
erver
::
PrivateImpl
{
struct
WorkerThreadContext
{
std
::
string
name
=
{};
...
...
@@ -143,7 +149,7 @@ struct Server::PrivateImpl
{
char
buff
[
4096
];
auto
input
=
bufferevent_get_input
(
bev
);
Server
*
server
=
(
S
erver
*
)
user_data
;
simple_libevent_server
*
server
=
(
simple_libevent_s
erver
*
)
user_data
;
if
(
server
->
userData_
&&
server
->
onMsg_
)
{
int
fd
=
(
int
)
bufferevent_getfd
(
bev
);
BaseClient
*
client
=
nullptr
;
...
...
@@ -176,7 +182,7 @@ struct Server::PrivateImpl
static
void
eventcb
(
struct
bufferevent
*
bev
,
short
events
,
void
*
user_data
)
{
Server
*
server
=
(
S
erver
*
)
user_data
;
simple_libevent_server
*
server
=
(
simple_libevent_s
erver
*
)
user_data
;
//printf("eventcb events=%d %s\n", events, eventToString(events).data());
std
::
string
msg
;
...
...
@@ -237,7 +243,7 @@ struct Server::PrivateImpl
static
void
timercb
(
evutil_socket_t
fd
,
short
,
void
*
user_data
)
{
auto
server
=
(
S
erver
*
)
user_data
;
auto
server
=
(
simple_libevent_s
erver
*
)
user_data
;
std
::
lock_guard
<
std
::
mutex
>
lg
(
server
->
mutex
);
auto
iter
=
server
->
clients
.
find
((
int
)
fd
);
if
(
iter
!=
server
->
clients
.
end
())
{
...
...
@@ -263,7 +269,7 @@ struct Server::PrivateImpl
auto
sin
=
(
sockaddr_in
*
)
addr
;
inet_ntop
(
AF_INET
,
&
sin
->
sin_addr
,
str
,
INET_ADDRSTRLEN
);
Server
*
server
=
(
S
erver
*
)
user_data
;
simple_libevent_server
*
server
=
(
simple_libevent_s
erver
*
)
user_data
;
auto
ctx
=
server
->
impl
->
workerThreadContexts
[
server
->
impl
->
curWorkerId
];
auto
bev
=
bufferevent_socket_new
(
ctx
->
base
,
fd
,
BEV_OPT_CLOSE_ON_FREE
);
...
...
@@ -298,17 +304,17 @@ struct Server::PrivateImpl
};
Server
::
S
erver
()
simple_libevent_server
::
simple_libevent_s
erver
()
{
static
OneTimeIniter
initLibEvent
;
}
Server
::~
S
erver
()
simple_libevent_server
::~
simple_libevent_s
erver
()
{
stop
();
}
bool
S
erver
::
start
(
uint16_t
port
,
std
::
string
&
msg
)
bool
simple_libevent_s
erver
::
start
(
uint16_t
port
,
std
::
string
&
msg
)
{
do
{
stop
();
...
...
@@ -365,7 +371,7 @@ bool Server::start(uint16_t port, std::string& msg)
return
false
;
}
void
S
erver
::
stop
()
void
simple_libevent_s
erver
::
stop
()
{
std
::
lock_guard
<
std
::
mutex
>
lg
(
mutex
);
if
(
!
impl
)
{
return
;
}
...
...
@@ -408,4 +414,3 @@ void Server::stop()
}
}
}
jlib/net/
S
erver.h
→
jlib/net/
simple_libevent_s
erver.h
View file @
8073c0da
...
...
@@ -28,39 +28,39 @@
namespace
jlib
{
namespace
net
{
namespace
server
{
class
simple_libevent_server
{
public
:
struct
BaseClient
{
explicit
BaseClient
(
int
fd
,
void
*
bev
);
virtual
~
BaseClient
();
struct
BaseClient
{
explicit
BaseClient
(
int
fd
,
void
*
bev
);
virtual
~
BaseClient
();
static
BaseClient
*
createDefaultClient
(
int
fd
,
void
*
bev
);
static
BaseClient
*
createDefaultClient
(
int
fd
,
void
*
bev
);
void
send
(
const
void
*
data
,
size_t
len
);
void
shutdown
(
int
what
=
1
);
void
updateLastTimeComm
();
void
send
(
const
void
*
data
,
size_t
len
);
void
shutdown
(
int
what
=
1
);
void
updateLastTimeComm
();
int
fd
=
0
;
std
::
string
ip
=
{};
uint16_t
port
=
0
;
void
*
privateData
=
nullptr
;
};
int
fd
=
0
;
std
::
string
ip
=
{};
uint16_t
port
=
0
;
void
*
privateData
=
nullptr
;
};
typedef
BaseClient
*
(
*
NewClientCallback
)(
int
fd
,
void
*
bev
);
typedef
BaseClient
*
(
*
NewClientCallback
)(
int
fd
,
void
*
bev
);
typedef
void
(
*
OnConnectinoCallback
)(
bool
up
,
const
std
::
string
&
msg
,
BaseClient
*
client
,
void
*
user_data
);
typedef
void
(
*
OnConnectinoCallback
)(
bool
up
,
const
std
::
string
&
msg
,
BaseClient
*
client
,
void
*
user_data
);
// return > 0 for ate
// return 0 for stop
typedef
size_t
(
*
OnMessageCallback
)(
const
char
*
data
,
size_t
len
,
BaseClient
*
client
,
void
*
user_data
);
// return > 0 for ate
// return 0 for stop
typedef
size_t
(
*
OnMessageCallback
)(
const
char
*
data
,
size_t
len
,
BaseClient
*
client
,
void
*
user_data
);
class
Server
{
public
:
explicit
Server
();
virtual
~
Server
();
explicit
simple_libevent_server
();
virtual
~
simple_libevent_server
();
// these functions wont take effect after start() is called
void
setName
(
const
std
::
string
&
name
)
{
name_
=
name
;
}
...
...
@@ -99,4 +99,3 @@ protected:
}
}
}
test/simple_libevent_client/simple_libevent_client.vcxproj
View file @
8073c0da
...
...
@@ -19,10 +19,10 @@
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ClCompile
Include=
"..\..\jlib\net\
C
lient.cpp"
/>
<ClCompile
Include=
"..\..\jlib\net\
simple_libevent_c
lient.cpp"
/>
</ItemGroup>
<ItemGroup>
<ClInclude
Include=
"..\..\jlib\net\
C
lient.h"
/>
<ClInclude
Include=
"..\..\jlib\net\
simple_libevent_c
lient.h"
/>
</ItemGroup>
<PropertyGroup
Label=
"Globals"
>
<VCProjectVersion>
16.0
</VCProjectVersion>
...
...
@@ -92,7 +92,7 @@
<ClCompile>
<WarningLevel>
Level3
</WarningLevel>
<SDLCheck>
true
</SDLCheck>
<PreprocessorDefinitions>
WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions)
</PreprocessorDefinitions>
<PreprocessorDefinitions>
WIN32;_DEBUG;_LIB;
SIMPLELIBEVENTCLIENTLIB;
%(PreprocessorDefinitions)
</PreprocessorDefinitions>
<ConformanceMode>
true
</ConformanceMode>
<PrecompiledHeader>
NotUsing
</PrecompiledHeader>
<PrecompiledHeaderFile>
pch.h
</PrecompiledHeaderFile>
...
...
@@ -116,7 +116,7 @@
<FunctionLevelLinking>
true
</FunctionLevelLinking>
<IntrinsicFunctions>
true
</IntrinsicFunctions>
<SDLCheck>
true
</SDLCheck>
<PreprocessorDefinitions>
WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions)
</PreprocessorDefinitions>
<PreprocessorDefinitions>
WIN32;NDEBUG;_LIB;
SIMPLELIBEVENTCLIENTLIB;
%(PreprocessorDefinitions)
</PreprocessorDefinitions>
<ConformanceMode>
true
</ConformanceMode>
<PrecompiledHeader>
NotUsing
</PrecompiledHeader>
<PrecompiledHeaderFile>
pch.h
</PrecompiledHeaderFile>
...
...
test/simple_libevent_client/simple_libevent_client.vcxproj.filters
View file @
8073c0da
...
...
@@ -15,13 +15,13 @@
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile
Include=
"..\..\jlib\net\Client.cpp"
>
<Filter>
Source Files
</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude
Include=
"..\..\jlib\net\Client.h"
>
<ClInclude
Include=
"..\..\jlib\net\simple_libevent_client.h"
>
<Filter>
Header Files
</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile
Include=
"..\..\jlib\net\simple_libevent_client.cpp"
>
<Filter>
Source Files
</Filter>
</ClCompile>
</ItemGroup>
</Project>
\ No newline at end of file
test/simple_libevent_server/simple_libevent_server.vcxproj
View file @
8073c0da
...
...
@@ -19,10 +19,10 @@
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ClCompile
Include=
"..\..\jlib\net\
S
erver.cpp"
/>
<ClCompile
Include=
"..\..\jlib\net\
simple_libevent_s
erver.cpp"
/>
</ItemGroup>
<ItemGroup>
<ClInclude
Include=
"..\..\jlib\net\
S
erver.h"
/>
<ClInclude
Include=
"..\..\jlib\net\
simple_libevent_s
erver.h"
/>
</ItemGroup>
<PropertyGroup
Label=
"Globals"
>
<VCProjectVersion>
16.0
</VCProjectVersion>
...
...
@@ -92,7 +92,7 @@
<ClCompile>
<WarningLevel>
Level3
</WarningLevel>
<SDLCheck>
true
</SDLCheck>
<PreprocessorDefinitions>
WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions)
</PreprocessorDefinitions>
<PreprocessorDefinitions>
WIN32;_DEBUG;_LIB;
SIMPLELIBEVENTSERVERLIB;
%(PreprocessorDefinitions)
</PreprocessorDefinitions>
<ConformanceMode>
true
</ConformanceMode>
<PrecompiledHeader>
NotUsing
</PrecompiledHeader>
<PrecompiledHeaderFile>
pch.h
</PrecompiledHeaderFile>
...
...
@@ -116,7 +116,7 @@
<FunctionLevelLinking>
true
</FunctionLevelLinking>
<IntrinsicFunctions>
true
</IntrinsicFunctions>
<SDLCheck>
true
</SDLCheck>
<PreprocessorDefinitions>
WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions)
</PreprocessorDefinitions>
<PreprocessorDefinitions>
WIN32;NDEBUG;_LIB;
SIMPLELIBEVENTSERVERLIB;
%(PreprocessorDefinitions)
</PreprocessorDefinitions>
<ConformanceMode>
true
</ConformanceMode>
<PrecompiledHeader>
NotUsing
</PrecompiledHeader>
<PrecompiledHeaderFile>
pch.h
</PrecompiledHeaderFile>
...
...
test/simple_libevent_server/simple_libevent_server.vcxproj.filters
View file @
8073c0da
...
...
@@ -15,13 +15,13 @@
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile
Include=
"..\..\jlib\net\Server.cpp"
>
<Filter>
Source Files
</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude
Include=
"..\..\jlib\net\Server.h"
>
<ClInclude
Include=
"..\..\jlib\net\simple_libevent_server.h"
>
<Filter>
Header Files
</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile
Include=
"..\..\jlib\net\simple_libevent_server.cpp"
>
<Filter>
Source Files
</Filter>
</ClCompile>
</ItemGroup>
</Project>
\ No newline at end of file
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