Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
L
libzmq
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
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
libzmq
Commits
668b2c4d
Commit
668b2c4d
authored
Jun 07, 2014
by
Ian Barber
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1083 from hurtonm/master
Code cleanup
parents
8c616290
706eb4da
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
39 additions
and
49 deletions
+39
-49
tcp_connecter.cpp
src/tcp_connecter.cpp
+39
-49
No files found.
src/tcp_connecter.cpp
View file @
668b2c4d
...
...
@@ -59,12 +59,12 @@ zmq::tcp_connecter_t::tcp_connecter_t (class io_thread_t *io_thread_,
delayed_start
(
delayed_start_
),
timer_started
(
false
),
session
(
session_
),
current_reconnect_ivl
(
options
.
reconnect_ivl
)
current_reconnect_ivl
(
options
.
reconnect_ivl
)
{
zmq_assert
(
addr
);
zmq_assert
(
addr
->
protocol
==
"tcp"
);
addr
->
to_string
(
endpoint
);
socket
=
session
->
get_socket
();
socket
=
session
->
get_socket
();
}
zmq
::
tcp_connecter_t
::~
tcp_connecter_t
()
...
...
@@ -110,14 +110,14 @@ void zmq::tcp_connecter_t::in_event ()
void
zmq
::
tcp_connecter_t
::
out_event
()
{
fd_t
fd
=
connect
();
rm_fd
(
handle
);
handle_valid
=
false
;
const
fd_t
fd
=
connect
();
// Handle the error condition by attempt to reconnect.
if
(
fd
==
retired_fd
)
{
close
();
add_reconnect_timer
();
add_reconnect_timer
();
return
;
}
...
...
@@ -125,7 +125,7 @@ void zmq::tcp_connecter_t::out_event ()
tune_tcp_keepalives
(
fd
,
options
.
tcp_keepalive
,
options
.
tcp_keepalive_cnt
,
options
.
tcp_keepalive_idle
,
options
.
tcp_keepalive_intvl
);
// remember our fd for ZMQ_SRCFD in messages
socket
->
set_fd
(
fd
);
socket
->
set_fd
(
fd
);
// Create the engine object for this connection.
stream_engine_t
*
engine
=
new
(
std
::
nothrow
)
...
...
@@ -151,7 +151,7 @@ void zmq::tcp_connecter_t::timer_event (int id_)
void
zmq
::
tcp_connecter_t
::
start_connecting
()
{
// Open the connecting socket.
int
rc
=
open
();
const
int
rc
=
open
();
// Connect may succeed in synchronous manner.
if
(
rc
==
0
)
{
...
...
@@ -177,32 +177,28 @@ void zmq::tcp_connecter_t::start_connecting ()
}
}
void
zmq
::
tcp_connecter_t
::
add_reconnect_timer
()
void
zmq
::
tcp_connecter_t
::
add_reconnect_timer
()
{
int
rc_ivl
=
get_new_reconnect_ivl
();
add_timer
(
rc_iv
l
,
reconnect_timer_id
);
socket
->
event_connect_retried
(
endpoint
,
rc_iv
l
);
const
int
interval
=
get_new_reconnect_ivl
();
add_timer
(
interva
l
,
reconnect_timer_id
);
socket
->
event_connect_retried
(
endpoint
,
interva
l
);
timer_started
=
true
;
}
int
zmq
::
tcp_connecter_t
::
get_new_reconnect_ivl
()
{
// The new interval is the current interval + random value.
int
this_
interval
=
current_reconnect_ivl
+
(
generate_random
()
%
options
.
reconnect_ivl
)
;
const
int
interval
=
current_reconnect_ivl
+
generate_random
()
%
options
.
reconnect_ivl
;
// Only change the current reconnect interval if the maximum reconnect
// interval was set and if it's larger than the reconnect interval.
if
(
options
.
reconnect_ivl_max
>
0
&&
options
.
reconnect_ivl_max
>
options
.
reconnect_ivl
)
{
options
.
reconnect_ivl_max
>
options
.
reconnect_ivl
)
// Calculate the next interval
current_reconnect_ivl
=
current_reconnect_ivl
*
2
;
if
(
current_reconnect_ivl
>=
options
.
reconnect_ivl_max
)
{
current_reconnect_ivl
=
options
.
reconnect_ivl_max
;
}
}
return
this_interval
;
current_reconnect_ivl
=
std
::
min
(
current_reconnect_ivl
*
2
,
options
.
reconnect_ivl_max
);
return
interval
;
}
int
zmq
::
tcp_connecter_t
::
open
()
...
...
@@ -214,7 +210,6 @@ int zmq::tcp_connecter_t::open ()
delete
addr
->
resolved
.
tcp_addr
;
addr
->
resolved
.
tcp_addr
=
NULL
;
}
zmq_assert
(
addr
->
resolved
.
tcp_addr
==
NULL
);
addr
->
resolved
.
tcp_addr
=
new
(
std
::
nothrow
)
tcp_address_t
();
alloc_assert
(
addr
->
resolved
.
tcp_addr
);
...
...
@@ -226,9 +221,10 @@ int zmq::tcp_connecter_t::open ()
return
-
1
;
}
zmq_assert
(
addr
->
resolved
.
tcp_addr
!=
NULL
);
tcp_address_t
*
const
tcp_addr
=
addr
->
resolved
.
tcp_addr
;
// Create the socket.
s
=
open_socket
(
addr
->
resolved
.
tcp_addr
->
family
(),
SOCK_STREAM
,
IPPROTO_TCP
);
s
=
open_socket
(
tcp_addr
->
family
(),
SOCK_STREAM
,
IPPROTO_TCP
);
#ifdef ZMQ_HAVE_WINDOWS
if
(
s
==
INVALID_SOCKET
)
{
errno
=
wsa_error_to_errno
(
WSAGetLastError
());
...
...
@@ -241,7 +237,7 @@ int zmq::tcp_connecter_t::open ()
// On some systems, IPv4 mapping in IPv6 sockets is disabled by default.
// Switch it on in such cases.
if
(
addr
->
resolved
.
tcp_addr
->
family
()
==
AF_INET6
)
if
(
tcp_addr
->
family
()
==
AF_INET6
)
enable_ipv4_mapping
(
s
);
// Set the IP Type-Of-Service priority for this socket
...
...
@@ -262,18 +258,14 @@ int zmq::tcp_connecter_t::open ()
set_ip_type_of_service
(
s
,
options
.
tos
);
// Set a source address for conversations
if
(
addr
->
resolved
.
tcp_addr
->
has_src_addr
())
{
rc
=
::
bind
(
s
,
addr
->
resolved
.
tcp_addr
->
src_addr
(),
addr
->
resolved
.
tcp_addr
->
src_addrlen
());
if
(
rc
==
-
1
)
{
if
(
tcp_addr
->
has_src_addr
())
{
rc
=
::
bind
(
s
,
tcp_addr
->
src_addr
(),
tcp_addr
->
src_addrlen
());
if
(
rc
==
-
1
)
return
-
1
;
}
}
// Connect to the remote peer.
rc
=
::
connect
(
s
,
addr
->
resolved
.
tcp_addr
->
addr
(),
addr
->
resolved
.
tcp_addr
->
addrlen
());
rc
=
::
connect
(
s
,
tcp_addr
->
addr
(),
tcp_addr
->
addrlen
());
// Connect was successfull immediately.
if
(
rc
==
0
)
...
...
@@ -298,33 +290,31 @@ zmq::fd_t zmq::tcp_connecter_t::connect ()
{
// Async connect has finished. Check whether an error occurred
int
err
=
0
;
#if
defined
ZMQ_HAVE_HPUX
int
len
=
sizeof
(
err
)
;
#if
def
ZMQ_HAVE_HPUX
int
len
=
sizeof
err
;
#else
socklen_t
len
=
sizeof
(
err
)
;
socklen_t
len
=
sizeof
err
;
#endif
int
rc
=
getsockopt
(
s
,
SOL_SOCKET
,
SO_ERROR
,
(
char
*
)
&
err
,
&
len
);
const
int
rc
=
getsockopt
(
s
,
SOL_SOCKET
,
SO_ERROR
,
(
char
*
)
&
err
,
&
len
);
// Assert if the error was caused by 0MQ bug.
// Networking problems are OK. No need to assert.
#ifdef ZMQ_HAVE_WINDOWS
zmq_assert
(
rc
==
0
);
if
(
err
!=
0
)
{
if
(
err
==
WSAECONNREFUSED
||
err
==
WSAETIMEDOUT
||
err
==
WSAECONNABORTED
||
err
==
WSAEHOSTUNREACH
||
err
==
WSAENETUNREACH
||
err
==
WSAENETDOWN
||
err
==
WSAEACCES
||
err
==
WSAEINVAL
||
err
==
WSAEADDRINUSE
)
wsa_assert
(
err
==
WSAECONNREFUSED
||
err
==
WSAETIMEDOUT
||
err
==
WSAECONNABORTED
||
err
==
WSAEHOSTUNREACH
||
err
==
WSAENETUNREACH
||
err
==
WSAENETDOWN
||
err
==
WSAEACCES
||
err
==
WSAEINVAL
||
err
==
WSAEADDRINUSE
);
return
retired_fd
;
wsa_assert_no
(
err
);
}
#else
// Following code should handle both Berkeley-derived socket
// implementations and Solaris.
if
(
rc
==
-
1
)
...
...
@@ -344,7 +334,7 @@ zmq::fd_t zmq::tcp_connecter_t::connect ()
#endif
// Return the newly connected socket.
fd_t
result
=
s
;
const
fd_t
result
=
s
;
s
=
retired_fd
;
return
result
;
}
...
...
@@ -353,10 +343,10 @@ void zmq::tcp_connecter_t::close ()
{
zmq_assert
(
s
!=
retired_fd
);
#ifdef ZMQ_HAVE_WINDOWS
int
rc
=
closesocket
(
s
);
const
int
rc
=
closesocket
(
s
);
wsa_assert
(
rc
!=
SOCKET_ERROR
);
#else
int
rc
=
::
close
(
s
);
const
int
rc
=
::
close
(
s
);
errno_assert
(
rc
==
0
);
#endif
socket
->
event_closed
(
endpoint
,
s
);
...
...
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