2009-09-16 10:11:01 +02:00
|
|
|
/*
|
2010-01-05 08:29:35 +01:00
|
|
|
Copyright (c) 2007-2010 iMatix Corporation
|
2009-09-16 10:11:01 +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 Lesser GNU General Public License as published by
|
|
|
|
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
|
|
|
|
Lesser GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the Lesser GNU General Public License
|
|
|
|
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-16 10:11:01 +02:00
|
|
|
|
2009-12-15 23:49:55 +01:00
|
|
|
#include <new>
|
|
|
|
|
2009-10-06 12:57:24 +02:00
|
|
|
#ifdef ZMQ_HAVE_WINDOWS
|
|
|
|
#include "windows.hpp"
|
|
|
|
#endif
|
|
|
|
|
2009-09-16 10:11:01 +02:00
|
|
|
#include "pgm_receiver.hpp"
|
|
|
|
#include "err.hpp"
|
|
|
|
#include "stdint.hpp"
|
|
|
|
#include "wire.hpp"
|
|
|
|
#include "i_inout.hpp"
|
|
|
|
|
|
|
|
zmq::pgm_receiver_t::pgm_receiver_t (class io_thread_t *parent_,
|
2009-12-23 19:37:56 +01:00
|
|
|
const options_t &options_) :
|
2009-09-16 10:11:01 +02:00
|
|
|
io_object_t (parent_),
|
|
|
|
pgm_socket (true, options_),
|
|
|
|
options (options_),
|
2010-03-02 09:02:40 +01:00
|
|
|
inout (NULL),
|
|
|
|
mru_decoder (NULL),
|
|
|
|
pending_bytes (0)
|
2009-09-16 10:11:01 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
zmq::pgm_receiver_t::~pgm_receiver_t ()
|
|
|
|
{
|
2009-09-22 15:12:51 +02:00
|
|
|
// Destructor should not be called before unplug.
|
|
|
|
zmq_assert (peers.empty ());
|
2009-09-16 10:11:01 +02:00
|
|
|
}
|
|
|
|
|
2009-09-16 15:36:38 +02:00
|
|
|
int zmq::pgm_receiver_t::init (bool udp_encapsulation_, const char *network_)
|
2009-09-16 10:11:01 +02:00
|
|
|
{
|
2009-09-16 15:36:38 +02:00
|
|
|
return pgm_socket.init (udp_encapsulation_, network_);
|
2009-09-16 10:11:01 +02:00
|
|
|
}
|
|
|
|
|
2010-08-14 08:37:38 +02:00
|
|
|
void zmq::pgm_receiver_t::plug (io_thread_t *io_thread_, i_inout *inout_)
|
2009-09-16 10:11:01 +02:00
|
|
|
{
|
2009-12-28 11:51:06 +01:00
|
|
|
// Retrieve PGM fds and start polling.
|
2009-09-16 10:11:01 +02:00
|
|
|
int socket_fd;
|
|
|
|
int waiting_pipe_fd;
|
|
|
|
pgm_socket.get_receiver_fds (&socket_fd, &waiting_pipe_fd);
|
|
|
|
socket_handle = add_fd (socket_fd);
|
|
|
|
pipe_handle = add_fd (waiting_pipe_fd);
|
|
|
|
set_pollin (pipe_handle);
|
|
|
|
set_pollin (socket_handle);
|
|
|
|
|
|
|
|
inout = inout_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void zmq::pgm_receiver_t::unplug ()
|
|
|
|
{
|
2009-09-22 15:12:51 +02:00
|
|
|
// Delete decoders.
|
2009-12-28 11:51:06 +01:00
|
|
|
for (peers_t::iterator it = peers.begin (); it != peers.end (); it++) {
|
2009-09-22 15:12:51 +02:00
|
|
|
if (it->second.decoder != NULL)
|
|
|
|
delete it->second.decoder;
|
|
|
|
}
|
|
|
|
peers.clear ();
|
|
|
|
|
2010-03-02 09:02:40 +01:00
|
|
|
mru_decoder = NULL;
|
|
|
|
pending_bytes = 0;
|
|
|
|
|
2009-12-28 11:51:06 +01:00
|
|
|
// Stop polling.
|
2009-09-16 10:11:01 +02:00
|
|
|
rm_fd (socket_handle);
|
|
|
|
rm_fd (pipe_handle);
|
2009-12-28 11:51:06 +01:00
|
|
|
|
2009-09-16 10:11:01 +02:00
|
|
|
inout = NULL;
|
|
|
|
}
|
|
|
|
|
2010-08-14 08:37:38 +02:00
|
|
|
void zmq::pgm_receiver_t::terminate ()
|
|
|
|
{
|
|
|
|
unplug ();
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void zmq::pgm_receiver_t::activate_out ()
|
2009-09-16 10:11:01 +02:00
|
|
|
{
|
|
|
|
zmq_assert (false);
|
|
|
|
}
|
|
|
|
|
2010-08-14 08:37:38 +02:00
|
|
|
void zmq::pgm_receiver_t::activate_in ()
|
2010-03-01 10:13:26 +01:00
|
|
|
{
|
2010-03-02 09:02:40 +01:00
|
|
|
// It is possible that the most recently used decoder
|
|
|
|
// processed the whole buffer but failed to write
|
|
|
|
// the last message into the pipe.
|
|
|
|
if (pending_bytes == 0) {
|
|
|
|
if (mru_decoder != NULL)
|
|
|
|
mru_decoder->process_buffer (NULL, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
zmq_assert (mru_decoder != NULL);
|
|
|
|
zmq_assert (pending_ptr != NULL);
|
|
|
|
|
|
|
|
// Ask the decoder to process remaining data.
|
|
|
|
size_t n = mru_decoder->process_buffer (pending_ptr, pending_bytes);
|
|
|
|
pending_bytes -= n;
|
|
|
|
|
|
|
|
if (pending_bytes > 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Resume polling.
|
|
|
|
set_pollin (pipe_handle);
|
|
|
|
set_pollin (socket_handle);
|
|
|
|
|
|
|
|
in_event ();
|
2010-03-01 10:13:26 +01:00
|
|
|
}
|
|
|
|
|
2009-09-16 10:11:01 +02:00
|
|
|
void zmq::pgm_receiver_t::in_event ()
|
|
|
|
{
|
2009-12-28 11:51:06 +01:00
|
|
|
// Read data from the underlying pgm_socket.
|
|
|
|
unsigned char *data = NULL;
|
2009-09-22 15:12:51 +02:00
|
|
|
const pgm_tsi_t *tsi = NULL;
|
2009-09-16 10:11:01 +02:00
|
|
|
|
2010-03-02 09:02:40 +01:00
|
|
|
zmq_assert (pending_bytes == 0);
|
|
|
|
|
2010-09-28 22:46:56 +02:00
|
|
|
if (has_rx_timer) {
|
|
|
|
cancel_timer (rx_timer_id);
|
|
|
|
has_rx_timer = false;
|
|
|
|
}
|
|
|
|
|
2010-02-02 15:11:25 +01:00
|
|
|
// TODO: This loop can effectively block other engines in the same I/O
|
|
|
|
// thread in the case of high load.
|
|
|
|
while (true) {
|
|
|
|
|
|
|
|
// Get new batch of data.
|
|
|
|
ssize_t received = pgm_socket.receive ((void**) &data, &tsi);
|
|
|
|
|
|
|
|
// No data to process. This may happen if the packet received is
|
|
|
|
// neither ODATA nor ODATA.
|
2010-09-28 22:46:56 +02:00
|
|
|
if (received == 0) {
|
|
|
|
const int last_errno = errno;
|
|
|
|
if (last_errno == ENOMEM || last_errno == EBUSY) {
|
|
|
|
const long timeout = pgm_socket.get_rx_timeout ();
|
|
|
|
add_timer (timeout, rx_timer_id);
|
|
|
|
has_rx_timer = true;
|
|
|
|
}
|
2010-02-02 15:11:25 +01:00
|
|
|
break;
|
2010-09-28 22:46:56 +02:00
|
|
|
}
|
2010-02-02 15:11:25 +01:00
|
|
|
|
|
|
|
// Find the peer based on its TSI.
|
|
|
|
peers_t::iterator it = peers.find (*tsi);
|
|
|
|
|
|
|
|
// Data loss. Delete decoder and mark the peer as disjoint.
|
|
|
|
if (received == -1) {
|
2010-04-08 11:07:22 +02:00
|
|
|
if (it != peers.end ()) {
|
|
|
|
it->second.joined = false;
|
|
|
|
if (it->second.decoder == mru_decoder)
|
|
|
|
mru_decoder = NULL;
|
|
|
|
if (it->second.decoder != NULL) {
|
|
|
|
delete it->second.decoder;
|
|
|
|
it->second.decoder = NULL;
|
|
|
|
}
|
2010-02-02 15:11:25 +01:00
|
|
|
}
|
|
|
|
break;
|
2009-09-22 15:12:51 +02:00
|
|
|
}
|
2009-09-16 10:11:01 +02:00
|
|
|
|
2010-02-02 15:11:25 +01:00
|
|
|
// New peer. Add it to the list of know but unjoint peers.
|
|
|
|
if (it == peers.end ()) {
|
|
|
|
peer_info_t peer_info = {false, NULL};
|
|
|
|
it = peers.insert (std::make_pair (*tsi, peer_info)).first;
|
|
|
|
}
|
2009-09-16 10:11:01 +02:00
|
|
|
|
2010-02-02 15:11:25 +01:00
|
|
|
// Read the offset of the fist message in the current packet.
|
|
|
|
zmq_assert ((size_t) received >= sizeof (uint16_t));
|
|
|
|
uint16_t offset = get_uint16 (data);
|
|
|
|
data += sizeof (uint16_t);
|
|
|
|
received -= sizeof (uint16_t);
|
2009-09-16 10:11:01 +02:00
|
|
|
|
2010-02-02 15:11:25 +01:00
|
|
|
// Join the stream if needed.
|
|
|
|
if (!it->second.joined) {
|
2009-09-16 10:11:01 +02:00
|
|
|
|
2010-02-02 15:11:25 +01:00
|
|
|
// There is no beginning of the message in current packet.
|
|
|
|
// Ignore the data.
|
|
|
|
if (offset == 0xffff)
|
|
|
|
continue;
|
2009-09-16 10:11:01 +02:00
|
|
|
|
2010-02-02 15:11:25 +01:00
|
|
|
zmq_assert (offset <= received);
|
|
|
|
zmq_assert (it->second.decoder == NULL);
|
2009-09-16 10:11:01 +02:00
|
|
|
|
2010-02-02 15:11:25 +01:00
|
|
|
// We have to move data to the begining of the first message.
|
|
|
|
data += offset;
|
|
|
|
received -= offset;
|
2009-09-22 15:12:51 +02:00
|
|
|
|
2010-02-02 15:11:25 +01:00
|
|
|
// Mark the stream as joined.
|
|
|
|
it->second.joined = true;
|
2009-09-22 15:12:51 +02:00
|
|
|
|
2010-02-02 15:11:25 +01:00
|
|
|
// Create and connect decoder for the peer.
|
2010-08-31 07:01:40 +02:00
|
|
|
it->second.decoder = new (std::nothrow) decoder_t (0);
|
2010-02-02 15:11:25 +01:00
|
|
|
it->second.decoder->set_inout (inout);
|
|
|
|
}
|
2009-09-22 15:12:51 +02:00
|
|
|
|
2010-03-02 09:02:40 +01:00
|
|
|
mru_decoder = it->second.decoder;
|
|
|
|
|
2009-12-28 11:51:06 +01:00
|
|
|
// Push all the data to the decoder.
|
2009-12-31 16:35:04 +01:00
|
|
|
ssize_t processed = it->second.decoder->process_buffer (data, received);
|
2010-03-02 09:02:40 +01:00
|
|
|
if (processed < received) {
|
|
|
|
// Save some state so we can resume the decoding process later.
|
|
|
|
pending_bytes = received - processed;
|
|
|
|
pending_ptr = data + processed;
|
|
|
|
// Stop polling.
|
|
|
|
reset_pollin (pipe_handle);
|
|
|
|
reset_pollin (socket_handle);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2009-12-28 11:51:06 +01:00
|
|
|
}
|
2010-02-02 15:11:25 +01:00
|
|
|
|
|
|
|
// Flush any messages decoder may have produced.
|
|
|
|
inout->flush ();
|
2009-09-16 10:11:01 +02:00
|
|
|
}
|
2009-09-22 15:12:51 +02:00
|
|
|
|
2010-09-28 22:46:56 +02:00
|
|
|
void zmq::pgm_receiver_t::timer_event (int token)
|
|
|
|
{
|
|
|
|
zmq_assert (token == rx_timer_id);
|
|
|
|
|
2010-10-11 17:59:58 +02:00
|
|
|
// Timer cancels on return by poller_base.
|
|
|
|
has_rx_timer = false;
|
2010-09-28 22:46:56 +02:00
|
|
|
in_event ();
|
|
|
|
}
|
|
|
|
|
2009-09-16 10:11:01 +02:00
|
|
|
#endif
|
|
|
|
|