pgm_sender.cpp 6.15 KB
Newer Older
malosek's avatar
malosek committed
1
/*
2
    Copyright (c) 2007-2014 Contributors as noted in the AUTHORS file
malosek's avatar
malosek committed
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
malosek's avatar
malosek committed
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.
malosek's avatar
malosek committed
15

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

#include "platform.hpp"

malosek's avatar
malosek committed
22
#if defined ZMQ_HAVE_OPENPGM
malosek's avatar
malosek committed
23

24 25 26 27
#ifdef ZMQ_HAVE_WINDOWS
#include "windows.hpp"
#endif

Martin Sustrik's avatar
Martin Sustrik committed
28
#include <stdlib.h>
malosek's avatar
malosek committed
29 30 31

#include "io_thread.hpp"
#include "pgm_sender.hpp"
32
#include "session_base.hpp"
malosek's avatar
malosek committed
33 34
#include "err.hpp"
#include "wire.hpp"
Martin Sustrik's avatar
Martin Sustrik committed
35
#include "stdint.hpp"
malosek's avatar
malosek committed
36 37

zmq::pgm_sender_t::pgm_sender_t (io_thread_t *parent_, 
38
      const options_t &options_) :
malosek's avatar
malosek committed
39
    io_object_t (parent_),
40 41
    has_tx_timer (false),
    has_rx_timer (false),
42
    session (NULL),
43
    encoder (0),
44
    more_flag (false),
malosek's avatar
malosek committed
45 46 47 48
    pgm_socket (false, options_),
    options (options_),
    out_buffer (NULL),
    out_buffer_size (0),
Martin Sustrik's avatar
Martin Sustrik committed
49
    write_size (0)
malosek's avatar
malosek committed
50
{
51 52
    int rc = msg.init ();
    errno_assert (rc == 0);
malosek's avatar
malosek committed
53 54
}

55
int zmq::pgm_sender_t::init (bool udp_encapsulation_, const char *network_)
malosek's avatar
malosek committed
56
{
Martin Sustrik's avatar
Martin Sustrik committed
57 58 59 60 61 62
    int rc = pgm_socket.init (udp_encapsulation_, network_);
    if (rc != 0)
        return rc;

    out_buffer_size = pgm_socket.get_max_tsdu_size ();
    out_buffer = (unsigned char*) malloc (out_buffer_size);
63
    alloc_assert (out_buffer);
64 65

    return rc;
malosek's avatar
malosek committed
66 67
}

68
void zmq::pgm_sender_t::plug (io_thread_t *io_thread_, session_base_t *session_)
malosek's avatar
malosek committed
69 70
{
    //  Alocate 2 fds for PGM socket.
71 72 73 74
    fd_t downlink_socket_fd = retired_fd;
    fd_t uplink_socket_fd = retired_fd;
    fd_t rdata_notify_fd = retired_fd;
    fd_t pending_notify_fd = retired_fd;
malosek's avatar
malosek committed
75

76
    session = session_;
malosek's avatar
malosek committed
77

Martin Sustrik's avatar
Martin Sustrik committed
78 79
    //  Fill fds from PGM transport and add them to the poller.
    pgm_socket.get_sender_fds (&downlink_socket_fd, &uplink_socket_fd,
80 81
        &rdata_notify_fd, &pending_notify_fd);

malosek's avatar
malosek committed
82 83
    handle = add_fd (downlink_socket_fd);
    uplink_handle = add_fd (uplink_socket_fd);
malosek's avatar
malosek committed
84
    rdata_notify_handle = add_fd (rdata_notify_fd);   
85
    pending_notify_handle = add_fd (pending_notify_fd);
malosek's avatar
malosek committed
86

malosek's avatar
malosek committed
87 88 89
    //  Set POLLIN. We wont never want to stop polling for uplink = we never
    //  want to stop porocess NAKs.
    set_pollin (uplink_handle);
malosek's avatar
malosek committed
90
    set_pollin (rdata_notify_handle);
91
    set_pollin (pending_notify_handle);
malosek's avatar
malosek committed
92 93 94 95 96 97 98

    //  Set POLLOUT for downlink_socket_handle.
    set_pollout (handle);
}

void zmq::pgm_sender_t::unplug ()
{
99 100 101 102 103 104 105 106 107 108
    if (has_rx_timer) {
        cancel_timer (rx_timer_id);
        has_rx_timer = false;
    }

    if (has_tx_timer) {
        cancel_timer (tx_timer_id);
        has_tx_timer = false;
    }

malosek's avatar
malosek committed
109 110
    rm_fd (handle);
    rm_fd (uplink_handle);
malosek's avatar
malosek committed
111
    rm_fd (rdata_notify_handle);
112
    rm_fd (pending_notify_handle);
113
    session = NULL;
malosek's avatar
malosek committed
114 115
}

116 117 118 119 120 121
void zmq::pgm_sender_t::terminate ()
{
    unplug ();
    delete this;
}

122
void zmq::pgm_sender_t::restart_output ()
malosek's avatar
malosek committed
123 124
{
    set_pollout (handle);
125
    out_event ();
malosek's avatar
malosek committed
126 127
}

128
void zmq::pgm_sender_t::restart_input ()
Martin Hurton's avatar
Martin Hurton committed
129 130 131 132
{
    zmq_assert (false);
}

malosek's avatar
malosek committed
133 134
zmq::pgm_sender_t::~pgm_sender_t ()
{
135 136 137
    int rc = msg.close ();
    errno_assert (rc == 0);

malosek's avatar
malosek committed
138
    if (out_buffer) {
Martin Sustrik's avatar
Martin Sustrik committed
139
        free (out_buffer);
malosek's avatar
malosek committed
140
        out_buffer = NULL;
malosek's avatar
malosek committed
141 142 143 144 145
    }
}

void zmq::pgm_sender_t::in_event ()
{
146 147 148 149 150
    if (has_rx_timer) {
        cancel_timer (rx_timer_id);
        has_rx_timer = false;
    }

151
    //  In-event on sender side means NAK or SPMR receiving from some peer.
malosek's avatar
malosek committed
152
    pgm_socket.process_upstream ();
153
    if (errno == ENOMEM || errno == EBUSY) {
154 155 156 157
        const long timeout = pgm_socket.get_rx_timeout ();
        add_timer (timeout, rx_timer_id);
        has_rx_timer = true;
    }
malosek's avatar
malosek committed
158 159 160 161 162 163
}

void zmq::pgm_sender_t::out_event ()
{
    //  POLLOUT event from send socket. If write buffer is empty, 
    //  try to read new data from the encoder.
Martin Sustrik's avatar
Martin Sustrik committed
164
    if (write_size == 0) {
malosek's avatar
malosek committed
165

Martin Sustrik's avatar
Martin Sustrik committed
166 167 168
        //  First two bytes (sizeof uint16_t) are used to store message 
        //  offset in following steps. Note that by passing our buffer to
        //  the get data function we prevent it from returning its own buffer.
Martin Sustrik's avatar
Martin Sustrik committed
169
        unsigned char *bf = out_buffer + sizeof (uint16_t);
Martin Sustrik's avatar
Martin Sustrik committed
170
        size_t bfsz = out_buffer_size - sizeof (uint16_t);
171 172 173 174 175 176 177 178 179 180 181 182 183 184
        uint16_t offset = 0xffff;

        size_t bytes = encoder.encode (&bf, bfsz);
        while (bytes < bfsz) {
            if (!more_flag && offset == 0xffff)
                offset = static_cast <uint16_t> (bytes);
            int rc = session->pull_msg (&msg);
            if (rc == -1)
                break;
            more_flag = msg.flags () & msg_t::more;
            encoder.load_msg (&msg);
            bf = out_buffer + sizeof (uint16_t) + bytes;
            bytes += encoder.encode (&bf, bfsz - bytes);
        }
malosek's avatar
malosek committed
185 186

        //  If there are no data to write stop polling for output.
187
        if (bytes == 0) {
malosek's avatar
malosek committed
188
            reset_pollout (handle);
Martin Sustrik's avatar
Martin Sustrik committed
189
            return;
malosek's avatar
malosek committed
190 191
        }

192 193
        write_size = sizeof (uint16_t) + bytes;

Martin Sustrik's avatar
Martin Sustrik committed
194
        //  Put offset information in the buffer.
195
        put_uint16 (out_buffer, offset);
malosek's avatar
malosek committed
196 197
    }

198 199 200
    if (has_tx_timer) {
        cancel_timer (tx_timer_id);
        has_tx_timer = false;
201 202
    }

Martin Sustrik's avatar
Martin Sustrik committed
203 204
    //  Send the data.
    size_t nbytes = pgm_socket.send (out_buffer, write_size);
malosek's avatar
malosek committed
205

Martin Sustrik's avatar
Martin Sustrik committed
206
    //  We can write either all data or 0 which means rate limit reached.
207
    if (nbytes == write_size)
Martin Sustrik's avatar
Martin Sustrik committed
208
        write_size = 0;
209
    else {
Martin Sustrik's avatar
Martin Sustrik committed
210
        zmq_assert (nbytes == 0);
211 212 213 214 215

        if (errno == ENOMEM) {
            const long timeout = pgm_socket.get_tx_timeout ();
            add_timer (timeout, tx_timer_id);
            has_tx_timer = true;
216 217
        }
        else
218
            errno_assert (errno == EBUSY);
219 220 221 222 223
    }
}

void zmq::pgm_sender_t::timer_event (int token)
{
224 225 226
    //  Timer cancels on return by poller_base.
    if (token == rx_timer_id) {
        has_rx_timer = false;
227
        in_event ();
228 229 230
    }
    else
    if (token == tx_timer_id) {
231 232
        has_tx_timer = false;
        out_event ();
233 234
    }
    else
235
        zmq_assert (false);
malosek's avatar
malosek committed
236
}
Martin Sustrik's avatar
Martin Sustrik committed
237

malosek's avatar
malosek committed
238 239
#endif