Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
biboumi
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
98
Issues
98
List
Boards
Labels
Service Desk
Milestones
Merge Requests
12
Merge Requests
12
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
louiz’
biboumi
Commits
8b30e312
Commit
8b30e312
authored
Jun 08, 2014
by
louiz’
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add a TimedEvent to cancel the connection to a server after 5 seconds
parent
349dc06e
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
23 additions
and
0 deletions
+23
-0
src/network/socket_handler.cpp
src/network/socket_handler.cpp
+18
-0
src/network/socket_handler.hpp
src/network/socket_handler.hpp
+5
-0
No files found.
src/network/socket_handler.cpp
View file @
8b30e312
#include <network/socket_handler.hpp>
#include <utils/timed_events.hpp>
#include <utils/scopeguard.hpp>
#include <network/poller.hpp>
...
...
@@ -26,6 +27,7 @@
#endif
using
namespace
std
::
string_literals
;
using
namespace
std
::
chrono_literals
;
namespace
ph
=
std
::
placeholders
;
...
...
@@ -117,6 +119,8 @@ void SocketHandler::connect(const std::string& address, const std::string& port,
||
errno
==
EISCONN
)
{
log_info
(
"Connection success."
);
TimedEventsManager
::
instance
().
cancel
(
"connection_timeout"
s
+
std
::
to_string
(
this
->
socket
));
this
->
poller
->
add_socket_handler
(
this
);
this
->
connected
=
true
;
this
->
connecting
=
false
;
...
...
@@ -139,6 +143,12 @@ void SocketHandler::connect(const std::string& address, const std::string& port,
memcpy
(
&
this
->
addrinfo
,
rp
,
sizeof
(
struct
addrinfo
));
this
->
addrinfo
.
ai_addr
=
&
this
->
ai_addr
;
this
->
addrinfo
.
ai_next
=
nullptr
;
// If the connection has not succeeded or failed in 5s, we consider
// it to have failed
TimedEventsManager
::
instance
().
add_event
(
TimedEvent
(
std
::
chrono
::
steady_clock
::
now
()
+
5s
,
std
::
bind
(
&
SocketHandler
::
on_connection_timeout
,
this
),
"connection_timeout"
s
+
std
::
to_string
(
this
->
socket
)));
return
;
}
log_info
(
"Connection failed:"
<<
strerror
(
errno
));
...
...
@@ -149,6 +159,12 @@ void SocketHandler::connect(const std::string& address, const std::string& port,
return
;
}
void
SocketHandler
::
on_connection_timeout
()
{
this
->
close
();
this
->
on_connection_failed
(
"connection timed out"
);
}
void
SocketHandler
::
connect
()
{
this
->
connect
(
this
->
address
,
this
->
port
,
this
->
use_tls
);
...
...
@@ -273,6 +289,8 @@ void SocketHandler::close()
this
->
in_buf
.
clear
();
this
->
out_buf
.
clear
();
this
->
port
.
clear
();
TimedEventsManager
::
instance
().
cancel
(
"connection_timeout"
s
+
std
::
to_string
(
this
->
socket
));
}
socket_t
SocketHandler
::
get_socket
()
const
...
...
src/network/socket_handler.hpp
View file @
8b30e312
...
...
@@ -85,6 +85,11 @@ public:
* Close the connection, remove us from the poller
*/
void
close
();
/**
* Called by a TimedEvent, when the connection did not succeed or fail
* after a given time.
*/
void
on_connection_timeout
();
/**
* Called when the connection is successful.
*/
...
...
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