libzmq/src/pgm_sender.cpp

245 lines
6.4 KiB
C++
Raw Normal View History

2009-09-11 17:58:37 +02:00
/*
Copyright (c) 2007-2015 Contributors as noted in the AUTHORS file
2009-09-11 17:58:37 +02:00
This file is part of 0MQ.
0MQ is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by
2009-09-11 17:58:37 +02:00
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
GNU Lesser General Public License for more details.
2009-09-11 17:58:37 +02:00
You should have received a copy of the GNU Lesser General Public License
2009-09-11 17:58:37 +02:00
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "platform.hpp"
2009-09-24 16:23:49 +02:00
#if defined ZMQ_HAVE_OPENPGM
2009-09-11 17:58:37 +02:00
#ifdef ZMQ_HAVE_WINDOWS
#include "windows.hpp"
#endif
2009-12-28 11:51:06 +01:00
#include <stdlib.h>
2009-09-11 17:58:37 +02:00
#include "io_thread.hpp"
#include "pgm_sender.hpp"
#include "session_base.hpp"
2009-09-11 17:58:37 +02:00
#include "err.hpp"
#include "wire.hpp"
2009-12-28 11:51:06 +01:00
#include "stdint.hpp"
2009-09-11 17:58:37 +02:00
zmq::pgm_sender_t::pgm_sender_t (io_thread_t *parent_,
const options_t &options_) :
2009-09-11 17:58:37 +02:00
io_object_t (parent_),
has_tx_timer (false),
has_rx_timer (false),
session (NULL),
encoder (0),
more_flag (false),
2009-09-11 17:58:37 +02:00
pgm_socket (false, options_),
options (options_),
out_buffer (NULL),
out_buffer_size (0),
2009-12-28 11:51:06 +01:00
write_size (0)
2009-09-11 17:58:37 +02:00
{
int rc = msg.init ();
errno_assert (rc == 0);
2009-09-11 17:58:37 +02:00
}
int zmq::pgm_sender_t::init (bool udp_encapsulation_, const char *network_)
2009-09-11 17:58:37 +02:00
{
2009-12-28 11:51:06 +01:00
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);
alloc_assert (out_buffer);
return rc;
2009-09-11 17:58:37 +02:00
}
void zmq::pgm_sender_t::plug (io_thread_t *io_thread_, session_base_t *session_)
2009-09-11 17:58:37 +02:00
{
// Alocate 2 fds for PGM socket.
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;
2009-09-11 17:58:37 +02:00
session = session_;
2009-09-11 17:58:37 +02:00
2009-12-28 11:51:06 +01:00
// Fill fds from PGM transport and add them to the poller.
pgm_socket.get_sender_fds (&downlink_socket_fd, &uplink_socket_fd,
&rdata_notify_fd, &pending_notify_fd);
2009-09-11 17:58:37 +02:00
handle = add_fd (downlink_socket_fd);
uplink_handle = add_fd (uplink_socket_fd);
2009-09-28 18:06:06 +02:00
rdata_notify_handle = add_fd (rdata_notify_fd);
pending_notify_handle = add_fd (pending_notify_fd);
2009-09-28 18:06:06 +02:00
2009-09-11 17:58:37 +02:00
// Set POLLIN. We wont never want to stop polling for uplink = we never
// want to stop porocess NAKs.
set_pollin (uplink_handle);
2009-09-28 18:06:06 +02:00
set_pollin (rdata_notify_handle);
set_pollin (pending_notify_handle);
2009-09-11 17:58:37 +02:00
// Set POLLOUT for downlink_socket_handle.
set_pollout (handle);
}
void zmq::pgm_sender_t::unplug ()
{
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;
}
2009-09-11 17:58:37 +02:00
rm_fd (handle);
rm_fd (uplink_handle);
2009-09-28 18:06:06 +02:00
rm_fd (rdata_notify_handle);
rm_fd (pending_notify_handle);
session = NULL;
2009-09-11 17:58:37 +02:00
}
void zmq::pgm_sender_t::terminate ()
{
unplug ();
delete this;
}
void zmq::pgm_sender_t::restart_output ()
2009-09-11 17:58:37 +02:00
{
set_pollout (handle);
out_event ();
2009-09-11 17:58:37 +02:00
}
void zmq::pgm_sender_t::restart_input ()
{
zmq_assert (false);
}
2009-09-11 17:58:37 +02:00
zmq::pgm_sender_t::~pgm_sender_t ()
{
int rc = msg.close ();
errno_assert (rc == 0);
2009-09-11 17:58:37 +02:00
if (out_buffer) {
2009-12-28 11:51:06 +01:00
free (out_buffer);
2009-11-30 16:45:18 +01:00
out_buffer = NULL;
2009-09-11 17:58:37 +02:00
}
}
void zmq::pgm_sender_t::in_event ()
{
2010-09-28 22:46:56 +02:00
if (has_rx_timer) {
cancel_timer (rx_timer_id);
has_rx_timer = false;
}
// In-event on sender side means NAK or SPMR receiving from some peer.
2009-09-11 17:58:37 +02:00
pgm_socket.process_upstream ();
if (errno == ENOMEM || errno == EBUSY) {
2010-09-28 22:46:56 +02:00
const long timeout = pgm_socket.get_rx_timeout ();
add_timer (timeout, rx_timer_id);
has_rx_timer = true;
}
2009-09-11 17:58:37 +02:00
}
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.
2009-12-28 11:51:06 +01:00
if (write_size == 0) {
2009-09-11 17:58:37 +02:00
2009-12-28 11:51:06 +01:00
// 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.
2009-12-13 09:11:08 +01:00
unsigned char *bf = out_buffer + sizeof (uint16_t);
2009-12-28 11:51:06 +01:00
size_t bfsz = out_buffer_size - sizeof (uint16_t);
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);
}
2009-09-11 17:58:37 +02:00
// If there are no data to write stop polling for output.
if (bytes == 0) {
2009-09-11 17:58:37 +02:00
reset_pollout (handle);
2009-12-28 11:51:06 +01:00
return;
2009-09-11 17:58:37 +02:00
}
write_size = sizeof (uint16_t) + bytes;
2009-12-28 11:51:06 +01:00
// Put offset information in the buffer.
put_uint16 (out_buffer, offset);
2009-09-11 17:58:37 +02:00
}
if (has_tx_timer) {
cancel_timer (tx_timer_id);
set_pollout (handle);
has_tx_timer = false;
2010-09-28 22:46:56 +02:00
}
2009-12-28 11:51:06 +01:00
// Send the data.
size_t nbytes = pgm_socket.send (out_buffer, write_size);
2009-09-11 17:58:37 +02:00
2009-12-28 11:51:06 +01:00
// We can write either all data or 0 which means rate limit reached.
2012-10-24 09:18:52 +09:00
if (nbytes == write_size)
2009-12-28 11:51:06 +01:00
write_size = 0;
2012-10-24 09:18:52 +09:00
else {
2009-12-28 11:51:06 +01:00
zmq_assert (nbytes == 0);
2010-09-28 22:46:56 +02:00
if (errno == ENOMEM) {
// Stop polling handle and wait for tx timeout
2010-09-28 22:46:56 +02:00
const long timeout = pgm_socket.get_tx_timeout ();
add_timer (timeout, tx_timer_id);
reset_pollout (handle);
2010-09-28 22:46:56 +02:00
has_tx_timer = true;
2012-10-24 09:18:52 +09:00
}
else
errno_assert (errno == EBUSY);
2010-09-28 22:46:56 +02:00
}
}
void zmq::pgm_sender_t::timer_event (int token)
{
// Timer cancels on return by poller_base.
if (token == rx_timer_id) {
has_rx_timer = false;
2010-09-28 22:46:56 +02:00
in_event ();
2012-10-24 09:18:52 +09:00
}
else
if (token == tx_timer_id) {
// Restart polling handle and retry sending
has_tx_timer = false;
set_pollout (handle);
out_event ();
2012-10-24 09:18:52 +09:00
}
else
zmq_assert (false);
2009-09-11 17:58:37 +02:00
}
2009-12-28 11:51:06 +01:00
2009-09-11 17:58:37 +02:00
#endif