2009-09-11 17:58:37 +02:00
|
|
|
/*
|
|
|
|
Copyright (c) 2007-2009 FastMQ Inc.
|
|
|
|
|
|
|
|
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
|
|
|
#ifdef ZMQ_HAVE_OPENPGM
|
2009-09-11 17:58:37 +02:00
|
|
|
|
2009-10-06 12:57:24 +02:00
|
|
|
#ifdef ZMQ_HAVE_WINDOWS
|
|
|
|
#include "windows.hpp"
|
|
|
|
#endif
|
|
|
|
|
2009-09-11 17:58:37 +02:00
|
|
|
#ifdef ZMQ_HAVE_LINUX
|
2009-10-06 12:57:24 +02:00
|
|
|
#include <poll.h>
|
|
|
|
// Has to be defined befiore including pgm/pgm.h
|
2009-09-24 16:23:49 +02:00
|
|
|
#define CONFIG_HAVE_POLL
|
2009-09-11 17:58:37 +02:00
|
|
|
#endif
|
|
|
|
|
2009-10-06 12:57:24 +02:00
|
|
|
#include <pgm/pgm.h>
|
2009-09-11 17:58:37 +02:00
|
|
|
#include <string>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include "options.hpp"
|
|
|
|
#include "pgm_socket.hpp"
|
|
|
|
#include "config.hpp"
|
|
|
|
#include "err.hpp"
|
2009-09-22 15:12:51 +02:00
|
|
|
#include "uuid.hpp"
|
2009-09-11 17:58:37 +02:00
|
|
|
|
|
|
|
zmq::pgm_socket_t::pgm_socket_t (bool receiver_, const options_t &options_) :
|
2009-12-13 09:11:08 +01:00
|
|
|
transport (NULL),
|
2009-09-11 17:58:37 +02:00
|
|
|
options (options_),
|
|
|
|
receiver (receiver_),
|
|
|
|
port_number (0),
|
|
|
|
udp_encapsulation (false),
|
|
|
|
pgm_msgv (NULL),
|
|
|
|
nbytes_rec (0),
|
|
|
|
nbytes_processed (0),
|
|
|
|
pgm_msgv_processed (0),
|
|
|
|
pgm_msgv_len (0)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-09-16 15:36:38 +02:00
|
|
|
int zmq::pgm_socket_t::init (bool udp_encapsulation_, const char *network_)
|
2009-09-11 17:58:37 +02:00
|
|
|
{
|
2009-09-16 15:36:38 +02:00
|
|
|
udp_encapsulation = udp_encapsulation_;
|
2009-09-11 17:58:37 +02:00
|
|
|
|
|
|
|
// Parse port number.
|
2009-09-16 15:36:38 +02:00
|
|
|
const char *port_delim = strchr (network_, ':');
|
2009-09-11 17:58:37 +02:00
|
|
|
if (!port_delim) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
port_number = atoi (port_delim + 1);
|
|
|
|
|
2009-09-16 15:36:38 +02:00
|
|
|
if (port_delim - network_ >= (int) sizeof (network) - 1) {
|
2009-09-11 17:58:37 +02:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset (network, '\0', sizeof (network));
|
2009-09-16 15:36:38 +02:00
|
|
|
memcpy (network, network_, port_delim - network_);
|
2009-09-11 17:58:37 +02:00
|
|
|
|
|
|
|
// Open PGM transport.
|
|
|
|
int rc = open_transport ();
|
|
|
|
if (rc != 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
// For receiver transport preallocate pgm_msgv array.
|
|
|
|
// in_batch_size configured in confing.hpp
|
|
|
|
if (receiver) {
|
|
|
|
pgm_msgv_len = get_max_apdu_at_once (in_batch_size);
|
|
|
|
pgm_msgv = new pgm_msgv_t [pgm_msgv_len];
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-12-13 11:27:43 +01:00
|
|
|
int zmq::pgm_socket_t::open_transport ()
|
2009-09-11 17:58:37 +02:00
|
|
|
{
|
|
|
|
// Can not open transport before destroying old one.
|
2009-12-13 09:11:08 +01:00
|
|
|
zmq_assert (transport == NULL);
|
2009-09-11 17:58:37 +02:00
|
|
|
|
|
|
|
// Zero counter used in msgrecv.
|
|
|
|
nbytes_rec = 0;
|
|
|
|
nbytes_processed = 0;
|
|
|
|
pgm_msgv_processed = 0;
|
|
|
|
|
2009-12-13 09:56:02 +01:00
|
|
|
// TODO: Converting bool to int? Not nice.
|
2009-09-25 17:50:12 +02:00
|
|
|
int pgm_ok = true;
|
2009-10-05 16:57:26 +02:00
|
|
|
GError *pgm_error = NULL;
|
2009-09-25 17:50:12 +02:00
|
|
|
|
2009-09-11 17:58:37 +02:00
|
|
|
// Init PGM transport.
|
|
|
|
// Ensure threading enabled, ensure timer enabled and find PGM protocol id.
|
|
|
|
//
|
|
|
|
// Note that if you want to use gettimeofday and sleep for openPGM timing,
|
|
|
|
// set environment variables PGM_TIMER to "GTOD"
|
|
|
|
// and PGM_SLEEP to "USLEEP".
|
|
|
|
int rc = pgm_init ();
|
|
|
|
if (rc != 0) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// PGM transport GSI.
|
|
|
|
pgm_gsi_t gsi;
|
|
|
|
|
2009-10-05 16:57:26 +02:00
|
|
|
std::string gsi_base;
|
|
|
|
|
2009-09-22 15:12:51 +02:00
|
|
|
if (options.identity.size () > 0) {
|
|
|
|
|
|
|
|
// Create gsi from identity string.
|
2009-10-05 16:57:26 +02:00
|
|
|
gsi_base = options.identity;
|
2009-09-22 15:12:51 +02:00
|
|
|
} else {
|
|
|
|
|
|
|
|
// Generate random gsi.
|
2009-10-05 16:57:26 +02:00
|
|
|
gsi_base = uuid_t ().to_string ();
|
2009-09-22 15:12:51 +02:00
|
|
|
}
|
|
|
|
|
2009-12-13 09:56:02 +01:00
|
|
|
rc = pgm_gsi_create_from_string (&gsi, gsi_base.c_str (), -1);
|
2009-10-05 16:57:26 +02:00
|
|
|
|
|
|
|
if (rc != pgm_ok) {
|
2009-09-11 17:58:37 +02:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-09-25 17:50:12 +02:00
|
|
|
//zmq_log (1, "Transport GSI: %s, %s(%i)\n", pgm_print_gsi (&gsi),
|
|
|
|
// __FILE__, __LINE__);
|
2009-09-22 15:12:51 +02:00
|
|
|
|
2009-09-25 17:50:12 +02:00
|
|
|
struct pgm_transport_info_t *res = NULL;
|
2009-09-11 17:58:37 +02:00
|
|
|
|
2009-09-25 17:50:12 +02:00
|
|
|
if (!pgm_if_get_transport_info (network, NULL, &res, &pgm_error)) {
|
2009-09-24 16:23:49 +02:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
res->ti_gsi = gsi;
|
2009-09-25 17:50:12 +02:00
|
|
|
res->ti_dport = port_number;
|
2009-09-24 16:23:49 +02:00
|
|
|
|
|
|
|
// If we are using UDP encapsulation update gsr or res.
|
|
|
|
if (udp_encapsulation) {
|
|
|
|
res->ti_udp_encap_ucast_port = port_number;
|
|
|
|
res->ti_udp_encap_mcast_port = port_number;
|
2009-09-11 17:58:37 +02:00
|
|
|
}
|
|
|
|
|
2009-12-13 09:11:08 +01:00
|
|
|
if (!pgm_transport_create (&transport, res, &pgm_error)) {
|
2009-09-24 16:23:49 +02:00
|
|
|
pgm_if_free_transport_info (res);
|
|
|
|
// TODO: tranlate errors from glib into errnos.
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
pgm_if_free_transport_info (res);
|
2009-09-11 17:58:37 +02:00
|
|
|
|
|
|
|
// Common parameters for receiver and sender.
|
|
|
|
|
|
|
|
// Set maximum transport protocol data unit size (TPDU).
|
2009-12-13 09:11:08 +01:00
|
|
|
rc = pgm_transport_set_max_tpdu (transport, pgm_max_tpdu);
|
2009-09-25 17:50:12 +02:00
|
|
|
if (rc != pgm_ok) {
|
2009-09-11 17:58:37 +02:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set maximum number of network hops to cross.
|
2009-12-13 09:11:08 +01:00
|
|
|
rc = pgm_transport_set_hops (transport, 16);
|
2009-09-25 17:50:12 +02:00
|
|
|
if (rc != pgm_ok) {
|
2009-09-11 17:58:37 +02:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-09-25 17:50:12 +02:00
|
|
|
// Set nonblocking send/recv sockets.
|
2009-12-13 09:11:08 +01:00
|
|
|
if (!pgm_transport_set_nonblocking (transport, true)) {
|
2009-09-25 17:50:12 +02:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-09-11 17:58:37 +02:00
|
|
|
if (receiver) {
|
2009-12-13 09:11:08 +01:00
|
|
|
|
|
|
|
// Receiver transport.
|
2009-09-25 17:50:12 +02:00
|
|
|
|
2009-09-11 17:58:37 +02:00
|
|
|
// Set transport->can_send_data = FALSE.
|
|
|
|
// Note that NAKs are still generated by the transport.
|
2009-12-13 09:11:08 +01:00
|
|
|
rc = pgm_transport_set_recv_only (transport, true, false);
|
2009-12-13 09:56:02 +01:00
|
|
|
|
2009-12-13 09:11:08 +01:00
|
|
|
zmq_assert (rc == pgm_ok);
|
2009-09-11 17:58:37 +02:00
|
|
|
|
|
|
|
// Set NAK transmit back-off interval [us].
|
2009-12-13 09:11:08 +01:00
|
|
|
rc = pgm_transport_set_nak_bo_ivl (transport, 50 * 1000);
|
|
|
|
zmq_assert (rc == pgm_ok);
|
2009-09-11 17:58:37 +02:00
|
|
|
|
|
|
|
// Set timeout before repeating NAK [us].
|
2009-12-13 09:11:08 +01:00
|
|
|
rc = pgm_transport_set_nak_rpt_ivl (transport, 200 * 1000);
|
|
|
|
zmq_assert (rc == pgm_ok);
|
2009-09-11 17:58:37 +02:00
|
|
|
|
|
|
|
// Set timeout for receiving RDATA.
|
2009-12-13 09:11:08 +01:00
|
|
|
rc = pgm_transport_set_nak_rdata_ivl (transport, 200 * 1000);
|
|
|
|
zmq_assert (rc == pgm_ok);
|
2009-09-11 17:58:37 +02:00
|
|
|
|
|
|
|
// Set retries for NAK without NCF/DATA (NAK_DATA_RETRIES).
|
2009-12-13 09:11:08 +01:00
|
|
|
rc = pgm_transport_set_nak_data_retries (transport, 5);
|
|
|
|
zmq_assert (rc == pgm_ok);
|
2009-09-11 17:58:37 +02:00
|
|
|
|
|
|
|
// Set retries for NCF after NAK (NAK_NCF_RETRIES).
|
2009-12-13 09:11:08 +01:00
|
|
|
rc = pgm_transport_set_nak_ncf_retries (transport, 2);
|
|
|
|
zmq_assert (rc == pgm_ok);
|
2009-09-11 17:58:37 +02:00
|
|
|
|
|
|
|
// Set timeout for removing a dead peer [us].
|
2009-12-13 09:11:08 +01:00
|
|
|
rc = pgm_transport_set_peer_expiry (transport, 5 * 8192 * 1000);
|
|
|
|
zmq_assert (rc == pgm_ok);
|
2009-09-11 17:58:37 +02:00
|
|
|
|
|
|
|
// Set expiration time of SPM Requests [us].
|
2009-12-13 09:11:08 +01:00
|
|
|
rc = pgm_transport_set_spmr_expiry (transport, 25 * 1000);
|
|
|
|
zmq_assert (rc == pgm_ok);
|
2009-09-11 17:58:37 +02:00
|
|
|
|
|
|
|
// Set the size of the receive window.
|
2009-12-13 09:11:08 +01:00
|
|
|
// Data rate is in [B/s]. options.rate is in [kb/s].
|
2009-09-11 17:58:37 +02:00
|
|
|
if (options.rate <= 0) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2009-12-13 09:11:08 +01:00
|
|
|
rc = pgm_transport_set_rxw_max_rte (transport,
|
2009-09-11 17:58:37 +02:00
|
|
|
options.rate * 1000 / 8);
|
2009-09-25 17:50:12 +02:00
|
|
|
if (rc != pgm_ok) {
|
2009-09-11 17:58:37 +02:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recovery interval [s].
|
|
|
|
if (options.recovery_ivl <= 0) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2009-12-13 09:11:08 +01:00
|
|
|
rc = pgm_transport_set_rxw_secs (transport, options.recovery_ivl);
|
2009-09-25 17:50:12 +02:00
|
|
|
if (rc != pgm_ok) {
|
2009-09-11 17:58:37 +02:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2009-12-13 09:11:08 +01:00
|
|
|
// Sender transport.
|
|
|
|
|
2009-11-30 16:45:18 +01:00
|
|
|
// Set transport->can_recv = FALSE, waiting_pipe will not be read.
|
2009-12-13 09:11:08 +01:00
|
|
|
rc = pgm_transport_set_send_only (transport, TRUE);
|
|
|
|
zmq_assert (rc == pgm_ok);
|
2009-09-11 17:58:37 +02:00
|
|
|
|
|
|
|
// Set the size of the send window.
|
2009-12-13 09:11:08 +01:00
|
|
|
// Data rate is in [B/s] options.rate is in [kb/s].
|
2009-09-11 17:58:37 +02:00
|
|
|
if (options.rate <= 0) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2009-12-13 09:11:08 +01:00
|
|
|
rc = pgm_transport_set_txw_max_rte (transport,
|
2009-09-11 17:58:37 +02:00
|
|
|
options.rate * 1000 / 8);
|
2009-09-25 17:50:12 +02:00
|
|
|
if (rc != pgm_ok) {
|
2009-09-11 17:58:37 +02:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recovery interval [s].
|
|
|
|
if (options.recovery_ivl <= 0) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2009-12-13 09:11:08 +01:00
|
|
|
rc = pgm_transport_set_txw_secs (transport, options.recovery_ivl);
|
2009-09-25 17:50:12 +02:00
|
|
|
if (rc != pgm_ok) {
|
2009-09-11 17:58:37 +02:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set interval of background SPM packets [us].
|
2009-12-13 09:11:08 +01:00
|
|
|
rc = pgm_transport_set_ambient_spm (transport, 8192 * 1000);
|
|
|
|
zmq_assert (rc == pgm_ok);
|
2009-09-11 17:58:37 +02:00
|
|
|
|
|
|
|
// Set intervals of data flushing SPM packets [us].
|
|
|
|
guint spm_heartbeat[] = {4 * 1000, 4 * 1000, 8 * 1000, 16 * 1000,
|
|
|
|
32 * 1000, 64 * 1000, 128 * 1000, 256 * 1000, 512 * 1000,
|
|
|
|
1024 * 1000, 2048 * 1000, 4096 * 1000, 8192 * 1000};
|
2009-12-13 09:11:08 +01:00
|
|
|
rc = pgm_transport_set_heartbeat_spm (transport, spm_heartbeat,
|
2009-09-11 17:58:37 +02:00
|
|
|
G_N_ELEMENTS(spm_heartbeat));
|
2009-12-13 09:11:08 +01:00
|
|
|
zmq_assert (rc == pgm_ok);
|
2009-09-11 17:58:37 +02:00
|
|
|
}
|
2009-09-16 10:11:01 +02:00
|
|
|
|
2009-09-11 17:58:37 +02:00
|
|
|
// Enable multicast loopback.
|
2009-09-16 10:11:01 +02:00
|
|
|
if (options.use_multicast_loop) {
|
2009-12-13 09:11:08 +01:00
|
|
|
rc = pgm_transport_set_multicast_loop (transport, true);
|
|
|
|
zmq_assert (rc == pgm_ok);
|
2009-09-11 17:58:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Bind a transport to the specified network devices.
|
2009-12-13 09:56:02 +01:00
|
|
|
if (!pgm_transport_bind (transport, &pgm_error)) {
|
2009-09-24 16:23:49 +02:00
|
|
|
// TODO: tranlate errors from glib into errnos.
|
|
|
|
return -1;
|
2009-12-13 09:56:02 +01:00
|
|
|
}
|
2009-09-11 17:58:37 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
zmq::pgm_socket_t::~pgm_socket_t ()
|
|
|
|
{
|
|
|
|
// Celanup.
|
|
|
|
if (pgm_msgv) {
|
|
|
|
delete [] pgm_msgv;
|
|
|
|
}
|
|
|
|
|
2009-12-13 09:11:08 +01:00
|
|
|
if (transport)
|
2009-09-11 17:58:37 +02:00
|
|
|
close_transport ();
|
|
|
|
}
|
|
|
|
|
2009-12-13 11:27:43 +01:00
|
|
|
void zmq::pgm_socket_t::close_transport ()
|
2009-09-11 17:58:37 +02:00
|
|
|
{
|
2009-12-13 09:11:08 +01:00
|
|
|
// transport has to be valid.
|
|
|
|
zmq_assert (transport);
|
2009-09-11 17:58:37 +02:00
|
|
|
|
2009-12-13 09:11:08 +01:00
|
|
|
pgm_transport_destroy (transport, TRUE);
|
2009-09-11 17:58:37 +02:00
|
|
|
|
2009-12-13 09:11:08 +01:00
|
|
|
transport = NULL;
|
2009-09-11 17:58:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get receiver fds. recv_fd is from transport->recv_sock
|
|
|
|
// waiting_pipe_fd is from transport->waiting_pipe [0]
|
2009-12-13 11:27:43 +01:00
|
|
|
void zmq::pgm_socket_t::get_receiver_fds (int *receive_fd_,
|
2009-09-11 17:58:37 +02:00
|
|
|
int *waiting_pipe_fd_)
|
|
|
|
{
|
2009-11-03 14:11:53 +01:00
|
|
|
zmq_assert (receive_fd_);
|
|
|
|
zmq_assert (waiting_pipe_fd_);
|
|
|
|
|
|
|
|
// recv_sock2 should not be used - check it.
|
2009-12-13 09:11:08 +01:00
|
|
|
zmq_assert (transport->recv_sock2 == -1);
|
2009-11-03 14:11:53 +01:00
|
|
|
|
|
|
|
// Check if transport can receive data and can not send.
|
2009-12-13 09:11:08 +01:00
|
|
|
zmq_assert (transport->can_recv_data);
|
|
|
|
zmq_assert (!transport->can_send_data);
|
2009-11-03 14:11:53 +01:00
|
|
|
|
|
|
|
// Take FDs directly from transport.
|
2009-12-13 09:11:08 +01:00
|
|
|
*receive_fd_ = pgm_transport_get_recv_fd (transport);
|
|
|
|
*waiting_pipe_fd_ = pgm_transport_get_pending_fd (transport);
|
2009-09-11 17:58:37 +02:00
|
|
|
}
|
|
|
|
|
2009-11-03 14:11:53 +01:00
|
|
|
// Get fds and store them into user allocated memory.
|
|
|
|
// sender_fd is from pgm_transport->send_sock.
|
|
|
|
// receive_fd_ is from transport->recv_sock.
|
|
|
|
// rdata_notify_fd_ is from transport->rdata_notify (PGM2 only).
|
2009-12-13 11:27:43 +01:00
|
|
|
void zmq::pgm_socket_t::get_sender_fds (int *send_fd_, int *receive_fd_,
|
2009-09-28 18:06:06 +02:00
|
|
|
int *rdata_notify_fd_)
|
2009-09-11 17:58:37 +02:00
|
|
|
{
|
2009-09-28 18:06:06 +02:00
|
|
|
zmq_assert (send_fd_);
|
|
|
|
zmq_assert (receive_fd_);
|
2009-11-03 14:11:53 +01:00
|
|
|
|
|
|
|
zmq_assert (rdata_notify_fd_);
|
|
|
|
|
|
|
|
// recv_sock2 should not be used - check it.
|
2009-12-13 09:11:08 +01:00
|
|
|
zmq_assert (transport->recv_sock2 == -1);
|
2009-11-03 14:11:53 +01:00
|
|
|
|
|
|
|
// Check if transport can send data and can not receive.
|
2009-12-13 09:11:08 +01:00
|
|
|
zmq_assert (transport->can_send_data);
|
|
|
|
zmq_assert (!transport->can_recv_data);
|
2009-11-03 14:11:53 +01:00
|
|
|
|
|
|
|
// Take FDs directly from transport.
|
2009-12-13 09:11:08 +01:00
|
|
|
*receive_fd_ = pgm_transport_get_recv_fd (transport);
|
|
|
|
*rdata_notify_fd_ = pgm_transport_get_repair_fd (transport);
|
|
|
|
*send_fd_ = pgm_transport_get_send_fd (transport);
|
2009-09-11 17:58:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send one APDU, transmit window owned memory.
|
|
|
|
size_t zmq::pgm_socket_t::send (unsigned char *data_, size_t data_len_)
|
|
|
|
{
|
2009-09-28 18:06:06 +02:00
|
|
|
size_t nbytes = 0;
|
|
|
|
|
2009-12-13 09:11:08 +01:00
|
|
|
PGMIOStatus status = pgm_send (transport, data_, data_len_, &nbytes);
|
2009-09-28 18:06:06 +02:00
|
|
|
|
|
|
|
if (nbytes != data_len_) {
|
2009-10-05 16:57:26 +02:00
|
|
|
zmq_assert (status == PGM_IO_STATUS_RATE_LIMITED);
|
2009-09-28 18:06:06 +02:00
|
|
|
zmq_assert (nbytes == 0);
|
|
|
|
}
|
2009-09-11 17:58:37 +02:00
|
|
|
|
|
|
|
// We have to write all data as one packet.
|
2009-12-13 11:27:43 +01:00
|
|
|
if (nbytes > 0)
|
2009-09-28 18:06:06 +02:00
|
|
|
zmq_assert ((ssize_t) nbytes == (ssize_t) data_len_);
|
2009-09-11 17:58:37 +02:00
|
|
|
|
|
|
|
return nbytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return max TSDU size without fragmentation from current PGM transport.
|
2009-12-13 11:27:43 +01:00
|
|
|
size_t zmq::pgm_socket_t::get_max_tsdu_size ()
|
2009-09-11 17:58:37 +02:00
|
|
|
{
|
2009-12-13 09:11:08 +01:00
|
|
|
return (size_t)pgm_transport_max_tsdu (transport, false);
|
2009-09-11 17:58:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns how many APDUs are needed to fill reading buffer.
|
|
|
|
size_t zmq::pgm_socket_t::get_max_apdu_at_once (size_t readbuf_size_)
|
|
|
|
{
|
|
|
|
zmq_assert (readbuf_size_ > 0);
|
|
|
|
|
|
|
|
// Read max TSDU size without fragmentation.
|
|
|
|
size_t max_tsdu_size = get_max_tsdu_size ();
|
|
|
|
|
|
|
|
// Calculate number of APDUs needed to fill the reading buffer.
|
|
|
|
size_t apdu_count = (int)readbuf_size_ / max_tsdu_size;
|
|
|
|
|
|
|
|
if ((int) readbuf_size_ % max_tsdu_size)
|
|
|
|
apdu_count ++;
|
|
|
|
|
|
|
|
// Have to have at least one APDU.
|
|
|
|
zmq_assert (apdu_count);
|
|
|
|
|
|
|
|
return apdu_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate buffer for one packet from the transmit window, The memory buffer
|
|
|
|
// is owned by the transmit window and so must be returned to the window with
|
|
|
|
// content via pgm_transport_send() calls or unused with pgm_packetv_free1().
|
|
|
|
void *zmq::pgm_socket_t::get_buffer (size_t *size_)
|
|
|
|
{
|
|
|
|
// Store size.
|
|
|
|
*size_ = get_max_tsdu_size ();
|
|
|
|
|
2009-09-28 18:06:06 +02:00
|
|
|
// Allocate buffer.
|
|
|
|
unsigned char *apdu_buff = new unsigned char [*size_];
|
|
|
|
zmq_assert (apdu_buff);
|
|
|
|
return apdu_buff;
|
2009-09-11 17:58:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return an unused packet allocated from the transmit window
|
|
|
|
// via pgm_packetv_alloc().
|
|
|
|
void zmq::pgm_socket_t::free_buffer (void *data_)
|
|
|
|
{
|
2009-09-28 18:06:06 +02:00
|
|
|
delete [] (unsigned char*) data_;
|
2009-09-25 17:50:12 +02:00
|
|
|
}
|
|
|
|
|
2009-09-11 17:58:37 +02:00
|
|
|
|
|
|
|
// pgm_transport_recvmsgv is called to fill the pgm_msgv array up to
|
|
|
|
// pgm_msgv_len. In subsequent calls data from pgm_msgv structure are
|
|
|
|
// returned.
|
2009-09-22 15:12:51 +02:00
|
|
|
ssize_t zmq::pgm_socket_t::receive (void **raw_data_, const pgm_tsi_t **tsi_)
|
2009-09-11 17:58:37 +02:00
|
|
|
{
|
2009-09-24 16:23:49 +02:00
|
|
|
size_t raw_data_len = 0;
|
|
|
|
|
2009-09-11 17:58:37 +02:00
|
|
|
// We just sent all data from pgm_transport_recvmsgv up
|
|
|
|
// and have to return 0 that another engine in this thread is scheduled.
|
|
|
|
if (nbytes_rec == nbytes_processed && nbytes_rec > 0) {
|
|
|
|
|
|
|
|
// Reset all the counters.
|
|
|
|
nbytes_rec = 0;
|
|
|
|
nbytes_processed = 0;
|
|
|
|
pgm_msgv_processed = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have are going first time or if we have processed all pgm_msgv_t
|
2009-09-16 10:11:01 +02:00
|
|
|
// structure previously read from the pgm socket.
|
2009-09-11 17:58:37 +02:00
|
|
|
if (nbytes_rec == nbytes_processed) {
|
|
|
|
|
|
|
|
// Check program flow.
|
|
|
|
zmq_assert (pgm_msgv_processed == 0);
|
|
|
|
zmq_assert (nbytes_processed == 0);
|
|
|
|
zmq_assert (nbytes_rec == 0);
|
|
|
|
|
|
|
|
// Receive a vector of Application Protocol Domain Unit's (APDUs)
|
|
|
|
// from the transport.
|
2009-09-25 17:50:12 +02:00
|
|
|
GError *pgm_error = NULL;
|
|
|
|
|
2009-12-13 09:11:08 +01:00
|
|
|
const PGMIOStatus status = pgm_recvmsgv (transport, pgm_msgv,
|
2009-09-25 17:50:12 +02:00
|
|
|
pgm_msgv_len, MSG_DONTWAIT, &nbytes_rec, &pgm_error);
|
|
|
|
|
|
|
|
// In a case when no ODATA/RDATA fired POLLIN event (SPM...)
|
|
|
|
// pgm_recvmsg returns ?.
|
2009-11-03 19:54:43 +01:00
|
|
|
if (status == PGM_IO_STATUS_TIMER_PENDING) {
|
|
|
|
|
2009-09-29 13:56:19 +02:00
|
|
|
zmq_assert (nbytes_rec == 0);
|
|
|
|
|
2009-09-25 17:50:12 +02:00
|
|
|
// In case if no RDATA/ODATA caused POLLIN 0 is
|
|
|
|
// returned.
|
|
|
|
nbytes_rec = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
2009-11-03 19:54:43 +01:00
|
|
|
|
2009-09-29 13:56:19 +02:00
|
|
|
// Data loss.
|
|
|
|
if (status == PGM_IO_STATUS_RESET) {
|
|
|
|
|
2009-12-13 09:11:08 +01:00
|
|
|
pgm_peer_t* peer = (pgm_peer_t*) transport->peers_pending->data;
|
2009-09-29 13:56:19 +02:00
|
|
|
|
|
|
|
// Save lost data TSI.
|
|
|
|
*tsi_ = &peer->tsi;
|
|
|
|
nbytes_rec = 0;
|
|
|
|
|
|
|
|
// In case of dala loss -1 is returned.
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Catch the rest of the errors.
|
2009-12-13 11:27:43 +01:00
|
|
|
zmq_assert (status == PGM_IO_STATUS_NORMAL);
|
2009-09-11 17:58:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
zmq_assert (nbytes_rec > 0);
|
|
|
|
|
2009-09-25 17:50:12 +02:00
|
|
|
// Only one APDU per pgm_msgv_t structure is allowed.
|
|
|
|
zmq_assert (pgm_msgv [pgm_msgv_processed].msgv_len == 1);
|
|
|
|
|
|
|
|
struct pgm_sk_buff_t* skb =
|
|
|
|
pgm_msgv [pgm_msgv_processed].msgv_skb [0];
|
|
|
|
|
|
|
|
// Take pointers from pgm_msgv_t structure.
|
|
|
|
*raw_data_ = skb->data;
|
|
|
|
raw_data_len = skb->len;
|
|
|
|
|
|
|
|
// Save current TSI.
|
|
|
|
*tsi_ = &skb->tsi;
|
2009-09-11 17:58:37 +02:00
|
|
|
|
|
|
|
// Move the the next pgm_msgv_t structure.
|
|
|
|
pgm_msgv_processed++;
|
|
|
|
nbytes_processed +=raw_data_len;
|
|
|
|
|
|
|
|
return raw_data_len;
|
|
|
|
}
|
|
|
|
|
2009-12-13 11:27:43 +01:00
|
|
|
void zmq::pgm_socket_t::process_upstream ()
|
2009-09-11 17:58:37 +02:00
|
|
|
{
|
2009-09-28 18:06:06 +02:00
|
|
|
pgm_msgv_t dummy_msg;
|
2009-09-24 16:23:49 +02:00
|
|
|
|
2009-09-28 18:06:06 +02:00
|
|
|
size_t dummy_bytes = 0;
|
|
|
|
GError *pgm_error = NULL;
|
|
|
|
|
2009-12-13 09:11:08 +01:00
|
|
|
PGMIOStatus status = pgm_recvmsgv (transport, &dummy_msg,
|
2009-09-28 18:06:06 +02:00
|
|
|
1, MSG_DONTWAIT, &dummy_bytes, &pgm_error);
|
|
|
|
|
|
|
|
// No data should be returned.
|
2009-11-04 18:59:19 +01:00
|
|
|
zmq_assert (dummy_bytes == 0 && (status == PGM_IO_STATUS_TIMER_PENDING ||
|
|
|
|
status == PGM_IO_STATUS_RATE_LIMITED));
|
2009-09-11 17:58:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|