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
1a66d846
Commit
1a66d846
authored
Aug 31, 2020
by
captainwong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update client&server
parent
a50b0a0b
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
47 additions
and
20 deletions
+47
-20
Client.cpp
jlib/net/Client.cpp
+37
-17
Client.h
jlib/net/Client.h
+8
-2
Server.cpp
jlib/net/Server.cpp
+2
-1
No files found.
jlib/net/Client.cpp
View file @
1a66d846
...
...
@@ -36,12 +36,12 @@ struct OneTimeIniter {
WSADATA
wsa_data
;
WSAStartup
(
0x0201
,
&
wsa_data
);
if
(
0
!=
evthread_use_windows_threads
())
{
fprintf
(
stderr
,
"failed to init libevent with thread by calling evthread_use_windows_threads
\n
"
);
JLOG_CRTC
(
"failed to init libevent with thread by calling evthread_use_windows_threads
"
);
abort
();
}
#else
if
(
0
!=
evthread_use_pthreads
())
{
fprintf
(
stderr
,
"failed to init libevent with thread by calling evthread_use_pthreads
\n
"
);
JLOG_CRTC
(
"failed to init libevent with thread by calling evthread_use_pthreads
"
);
abort
();
}
#endif
...
...
@@ -74,7 +74,7 @@ struct Client::Impl
{
char
buff
[
4096
];
auto
input
=
bufferevent_get_input
(
bev
);
JLOG_
INFO
(
"readcb, readable len {}"
,
evbuffer_get_length
(
input
));
JLOG_
DBUG
(
"readcb, readable len {}"
,
evbuffer_get_length
(
input
));
Client
*
client
=
(
Client
*
)
user_data
;
if
(
client
->
userData_
&&
client
->
onMsg_
)
{
while
(
1
)
{
...
...
@@ -109,19 +109,24 @@ struct Client::Impl
static
void
eventcb
(
struct
bufferevent
*
bev
,
short
events
,
void
*
user_data
)
{
Client
*
client
=
(
Client
*
)
user_data
;
JLOG_
INFO
(
"eventcb events={}"
,
eventToString
(
events
));
JLOG_
DBUG
(
"eventcb events={}"
,
eventToString
(
events
));
std
::
string
msg
;
if
(
events
&
BEV_EVENT_CONNECTED
)
{
client
->
connected_
=
true
;
if
(
client
->
userData_
&&
client
->
onConn_
)
{
client
->
onConn_
(
true
,
"connected"
,
client
->
userData_
);
}
client
->
lastTimeSendData
=
std
::
chrono
::
steady_clock
::
now
();
struct
timeval
tv
=
{
1
,
0
};
event_add
(
event_new
(
client
->
impl_
->
base
,
bufferevent_getfd
(
bev
),
0
,
Impl
::
timercb
,
client
),
&
tv
);
if
(
client
->
strictTimer_
)
{
struct
timeval
tv
=
{
client
->
timeout_
,
0
};
event_add
(
event_new
(
client
->
impl_
->
base
,
-
1
,
0
,
Impl
::
timercb
,
client
),
&
tv
);
}
else
{
struct
timeval
tv
=
{
1
,
0
};
event_add
(
event_new
(
client
->
impl_
->
base
,
-
1
,
0
,
Impl
::
timercb
,
client
),
&
tv
);
}
return
;
}
else
if
(
events
&
(
BEV_EVENT_EOF
))
{
msg
=
(
"Connection closed"
);
...
...
@@ -136,6 +141,8 @@ struct Client::Impl
}
client
->
impl_
->
bev
=
nullptr
;
client
->
connected_
=
false
;
if
(
client
->
userData_
&&
client
->
onConn_
)
{
client
->
onConn_
(
false
,
msg
,
client
->
userData_
);
}
...
...
@@ -147,19 +154,27 @@ struct Client::Impl
}
}
static
void
timercb
(
evutil_socket_t
fd
,
short
,
void
*
user_data
)
static
void
timercb
(
evutil_socket_t
,
short
,
void
*
user_data
)
{
Client
*
client
=
(
Client
*
)
user_data
;
auto
now
=
std
::
chrono
::
steady_clock
::
now
();
auto
diff
=
std
::
chrono
::
duration_cast
<
std
::
chrono
::
seconds
>
(
now
-
client
->
lastTimeSendData
);
if
(
diff
.
count
()
>=
client
->
timeout_
)
{
if
(
client
->
strictTimer_
)
{
if
(
client
->
userData_
&&
client
->
onTimer_
)
{
client
->
onTimer_
(
client
->
userData_
);
}
}
struct
timeval
tv
=
{
1
,
0
};
client
->
impl_
->
timer
=
event_new
(
client
->
impl_
->
base
,
fd
,
0
,
Impl
::
timercb
,
client
);
event_add
(
client
->
impl_
->
timer
,
&
tv
);
struct
timeval
tv
=
{
client
->
timeout_
,
0
};
event_add
(
event_new
(
client
->
impl_
->
base
,
-
1
,
0
,
Impl
::
timercb
,
client
),
&
tv
);
}
else
{
auto
now
=
std
::
chrono
::
steady_clock
::
now
();
auto
diff
=
std
::
chrono
::
duration_cast
<
std
::
chrono
::
seconds
>
(
now
-
client
->
lastTimeSendData
);
if
(
diff
.
count
()
>=
client
->
timeout_
)
{
if
(
client
->
userData_
&&
client
->
onTimer_
)
{
client
->
onTimer_
(
client
->
userData_
);
}
}
struct
timeval
tv
=
{
1
,
0
};
client
->
impl_
->
timer
=
event_new
(
client
->
impl_
->
base
,
-
1
,
0
,
Impl
::
timercb
,
client
);
event_add
(
client
->
impl_
->
timer
,
&
tv
);
}
}
static
void
reconn_timercb
(
evutil_socket_t
,
short
,
void
*
user_data
)
...
...
@@ -168,9 +183,9 @@ struct Client::Impl
do
{
std
::
string
msg
=
"Reconnecting "
+
client
->
impl_
->
ip
+
":"
+
std
::
to_string
(
client
->
impl_
->
port
);
if
(
client
->
userData_
&&
client
->
onConn_
)
{
/*
if (client->userData_ && client->onConn_) {
client->onConn_(false, msg, client->userData_);
}
}
*/
sockaddr_in
sin
=
{
0
};
sin
.
sin_family
=
AF_INET
;
...
...
@@ -260,6 +275,11 @@ void Client::stop()
auto
old
=
autoReconnect_
;
autoReconnect_
=
false
;
if
(
impl_
->
timer
)
{
event_del
(
impl_
->
timer
);
impl_
->
timer
=
nullptr
;
}
if
(
impl_
->
bev
)
{
shutdown
(
bufferevent_getfd
(
impl_
->
bev
),
1
);
//impl_->bev = nullptr;
...
...
jlib/net/Client.h
View file @
1a66d846
#pragma once
#
pragma
once
# ifndef _CRT_SECURE_NO_WARNINGS
# define _CRT_SECURE_NO_WARNINGS
...
...
@@ -39,21 +39,27 @@ public:
void
setUserData
(
void
*
d
)
{
userData_
=
d
;
}
void
setOnConnectionCallback
(
OnConnectinoCallback
cb
)
{
onConn_
=
cb
;
}
void
setOnMsgCallback
(
OnMessageCallback
cb
)
{
onMsg_
=
cb
;
}
void
setOnTimerCallback
(
OnTimerCallback
cb
,
int
seconds
)
{
onTimer_
=
cb
;
if
(
seconds
>
0
)
{
timeout_
=
seconds
;
}
}
// strictTimer 是否严格以超时时间计算回调,否则会计算上次通信时间间隔
void
setOnTimerCallback
(
OnTimerCallback
cb
,
int
seconds
,
bool
strictTimer
=
false
)
{
onTimer_
=
cb
;
if
(
seconds
>
0
)
{
timeout_
=
seconds
;
}
strictTimer_
=
strictTimer
;
}
void
setAutoReconnect
(
bool
b
)
{
autoReconnect_
=
b
;
}
bool
start
(
const
std
::
string
&
ip
,
uint16_t
port
,
std
::
string
&
msg
);
void
stop
();
void
send
(
const
char
*
data
,
size_t
len
);
bool
isStarted
()
const
{
return
started_
;
}
bool
isConnected
()
const
{
return
connected_
;
}
protected
:
bool
started_
=
false
;
bool
connected_
=
false
;
bool
autoReconnect_
=
false
;
void
*
userData_
=
nullptr
;
OnConnectinoCallback
onConn_
=
nullptr
;
OnMessageCallback
onMsg_
=
nullptr
;
OnTimerCallback
onTimer_
=
nullptr
;
int
timeout_
=
5
;
bool
strictTimer_
=
false
;
std
::
mutex
mutex_
=
{};
std
::
chrono
::
steady_clock
::
time_point
lastTimeSendData
=
{};
...
...
jlib/net/Server.cpp
View file @
1a66d846
...
...
@@ -371,7 +371,8 @@ void Server::stop()
if
(
!
impl
)
{
return
;
}
if
(
impl
->
base
)
{
event_base_loopexit
(
impl
->
base
,
nullptr
);
timeval
tv
{
0
,
1000
};
event_base_loopexit
(
impl
->
base
,
&
tv
);
}
if
(
impl
->
thread
.
joinable
())
{
...
...
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