zmq_connecter.cpp 3.93 KB
Newer Older
1
/*
2
    Copyright (c) 2007-2010 iMatix Corporation
3 4 5 6

    This file is part of 0MQ.

    0MQ is free software; you can redistribute it and/or modify it under
7
    the terms of the GNU Lesser General Public License as published by
8 9 10 11 12 13
    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
14
    GNU Lesser General Public License for more details.
15

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

20 21
#include <new>

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

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

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

zmq::zmq_connecter_t::~zmq_connecter_t ()
{
51
    if (wait)
52
        cancel_timer (reconnect_timer_id);
53 54
    if (handle_valid)
        rm_fd (handle);
55 56
}

57
int zmq::zmq_connecter_t::get_reconnect_ivl ()
58 59
{
#if defined ZMQ_HAVE_WINDOWS
60 61
    return (options.reconnect_ivl + (((int) GetCurrentProcessId () * 13)
        % options.reconnect_ivl));
62
#else
63 64
    return (options.reconnect_ivl + (((int) getpid () * 13)
        % options.reconnect_ivl));
65 66 67
#endif
}

68 69
void zmq::zmq_connecter_t::process_plug ()
{
70
    if (wait)
71
        add_timer (get_reconnect_ivl (), reconnect_timer_id);
72 73
    else
        start_connecting ();
74 75 76 77 78 79 80 81 82 83 84 85 86
}

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 ();
87 88
    rm_fd (handle);
    handle_valid = false;
89

90 91 92 93
    //  Handle the error condition by attempt to reconnect.
    if (fd == retired_fd) {
        tcp_connecter.close ();
        wait = true;
94
        add_timer (get_reconnect_ivl (), reconnect_timer_id);
95 96
        return;
    }
97

98 99 100 101 102
    //  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);

103
    //  Create an init object. 
104 105
    zmq_init_t *init = new (std::nothrow) zmq_init_t (io_thread, NULL,
        session, fd, options);
106
    zmq_assert (init);
107
    launch_sibling (init);
108

109 110
    //  Shut the connecter down.
    terminate ();
111 112
}

113
void zmq::zmq_connecter_t::timer_event (int id_)
114
{
115
    zmq_assert (id_ == reconnect_timer_id);
116 117
    wait = false;
    start_connecting ();
118 119 120 121 122 123 124 125 126
}

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) {
127 128
        handle = add_fd (tcp_connecter.get_fd ());
        handle_valid = true;
129 130 131 132 133
        out_event ();
        return;
    }

    //  Connection establishment may be dealyed. Poll for its completion.
134
    else if (rc == -1 && errno == EAGAIN) {
135
        handle = add_fd (tcp_connecter.get_fd ());
136
        handle_valid = true;
137 138 139 140
        set_pollout (handle);
        return;
    }

141 142
    //  Handle any other error condition by eventual reconnect.
    wait = true;
143
    add_timer (get_reconnect_ivl (), reconnect_timer_id);
144
}