zmq_connecter.cpp 4.62 KB
Newer Older
1
/*
2 3
    Copyright (c) 2007-2011 iMatix Corporation
    Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
4 5 6 7

    This file is part of 0MQ.

    0MQ is free software; you can redistribute it and/or modify it under
8
    the terms of the GNU Lesser General Public License as published by
9 10 11 12 13 14
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    0MQ is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
    GNU Lesser General Public License for more details.
16

17
    You should have received a copy of the GNU Lesser General Public License
18 19 20
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

21 22
#include <new>

23
#include "platform.hpp"
24 25 26 27 28 29 30
#if defined ZMQ_HAVE_WINDOWS
#include "windows.hpp"
#else
#include <sys/types.h>
#include <unistd.h>
#endif

31
#include "zmq_connecter.hpp"
32 33
#include "zmq_engine.hpp"
#include "zmq_init.hpp"
34
#include "io_thread.hpp"
35 36
#include "err.hpp"

37 38
zmq::zmq_connecter_t::zmq_connecter_t (class io_thread_t *io_thread_,
      class session_t *session_, const options_t &options_,
39
      const char *protocol_, const char *address_, bool wait_) :
40
    own_t (io_thread_, options_),
41
    io_object_t (io_thread_),
42
    handle_valid (false),
43
    wait (wait_),
44 45
    session (session_),
    current_reconnect_ivl(options.reconnect_ivl)
46
{
47 48
    int rc = tcp_connecter.set_address (protocol_, address_);
    zmq_assert (rc == 0);
49 50 51 52
}

zmq::zmq_connecter_t::~zmq_connecter_t ()
{
53
    if (wait)
54
        cancel_timer (reconnect_timer_id);
55 56
    if (handle_valid)
        rm_fd (handle);
57 58 59 60
}

void zmq::zmq_connecter_t::process_plug ()
{
61
    if (wait)
62
        add_reconnect_timer();
63 64
    else
        start_connecting ();
65 66 67 68 69 70 71 72 73 74 75 76 77
}

void zmq::zmq_connecter_t::in_event ()
{
    //  We are not polling for incomming data, so we are actually called
    //  because of error here. However, we can get error on out event as well
    //  on some platforms, so we'll simply handle both events in the same way.
    out_event ();
}

void zmq::zmq_connecter_t::out_event ()
{
    fd_t fd = tcp_connecter.connect ();
78 79
    rm_fd (handle);
    handle_valid = false;
80

81 82 83 84
    //  Handle the error condition by attempt to reconnect.
    if (fd == retired_fd) {
        tcp_connecter.close ();
        wait = true;
85
        add_reconnect_timer();
86 87
        return;
    }
88

89 90 91 92 93
    //  Choose I/O thread to run connecter in. Given that we are already
    //  running in an I/O thread, there must be at least one available.
    io_thread_t *io_thread = choose_io_thread (options.affinity);
    zmq_assert (io_thread);

94
    //  Create an init object. 
95 96
    zmq_init_t *init = new (std::nothrow) zmq_init_t (io_thread, NULL,
        session, fd, options);
97
    alloc_assert (init);
98
    launch_sibling (init);
99

100 101
    //  Shut the connecter down.
    terminate ();
102 103
}

104
void zmq::zmq_connecter_t::timer_event (int id_)
105
{
106
    zmq_assert (id_ == reconnect_timer_id);
107 108
    wait = false;
    start_connecting ();
109 110 111 112 113 114 115 116 117
}

void zmq::zmq_connecter_t::start_connecting ()
{
    //  Open the connecting socket.
    int rc = tcp_connecter.open ();

    //  Connect may succeed in synchronous manner.
    if (rc == 0) {
118 119
        handle = add_fd (tcp_connecter.get_fd ());
        handle_valid = true;
120 121 122 123 124
        out_event ();
        return;
    }

    //  Connection establishment may be dealyed. Poll for its completion.
125
    else if (rc == -1 && errno == EAGAIN) {
126
        handle = add_fd (tcp_connecter.get_fd ());
127
        handle_valid = true;
128 129 130 131
        set_pollout (handle);
        return;
    }

132 133
    //  Handle any other error condition by eventual reconnect.
    wait = true;
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
    add_reconnect_timer();
}

void zmq::zmq_connecter_t::add_reconnect_timer()
{
    add_timer (get_new_reconnect_ivl(), reconnect_timer_id);
}

int zmq::zmq_connecter_t::get_new_reconnect_ivl ()
{
#if defined ZMQ_HAVE_WINDOWS
    int pid = (int) GetCurrentProcessId ();
#else
    int pid = (int) getpid ();
#endif

    //  The new interval is the current interval + random value.
    int this_interval = current_reconnect_ivl +
        ((pid * 13) % 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) {

        //  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;
166
}