2009-08-09 09:24:48 +02:00
|
|
|
/*
|
2010-01-05 08:29:35 +01:00
|
|
|
Copyright (c) 2007-2010 iMatix Corporation
|
2009-08-09 09:24:48 +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/>.
|
|
|
|
*/
|
|
|
|
|
2009-12-15 23:49:55 +01:00
|
|
|
#include <new>
|
|
|
|
|
2009-08-09 09:24:48 +02:00
|
|
|
#include "zmq_engine.hpp"
|
2009-09-16 11:02:18 +02:00
|
|
|
#include "zmq_connecter.hpp"
|
2009-08-09 09:24:48 +02:00
|
|
|
#include "io_thread.hpp"
|
2009-08-12 09:40:16 +02:00
|
|
|
#include "i_inout.hpp"
|
|
|
|
#include "config.hpp"
|
|
|
|
#include "err.hpp"
|
2009-08-09 09:24:48 +02:00
|
|
|
|
2009-12-10 09:47:24 +01:00
|
|
|
zmq::zmq_engine_t::zmq_engine_t (io_thread_t *parent_, fd_t fd_,
|
2010-01-15 14:11:39 +01:00
|
|
|
const options_t &options_, bool reconnect_,
|
|
|
|
const char *protocol_, const char *address_) :
|
2009-08-12 09:40:16 +02:00
|
|
|
io_object_t (parent_),
|
2009-12-11 22:29:04 +01:00
|
|
|
inpos (NULL),
|
2009-08-12 09:40:16 +02:00
|
|
|
insize (0),
|
2009-12-15 09:09:19 +01:00
|
|
|
decoder (in_batch_size, NULL, 0),
|
2009-12-11 22:29:04 +01:00
|
|
|
outpos (NULL),
|
2009-08-12 09:40:16 +02:00
|
|
|
outsize (0),
|
2009-12-15 09:09:19 +01:00
|
|
|
encoder (out_batch_size, false),
|
2009-12-10 09:47:24 +01:00
|
|
|
inout (NULL),
|
2009-12-15 17:49:40 +01:00
|
|
|
options (options_),
|
|
|
|
reconnect (reconnect_)
|
2009-08-09 09:24:48 +02:00
|
|
|
{
|
2010-01-15 14:11:39 +01:00
|
|
|
if (reconnect) {
|
|
|
|
protocol = protocol_;
|
2009-12-15 17:49:40 +01:00
|
|
|
address = address_;
|
2010-01-15 14:11:39 +01:00
|
|
|
}
|
2009-12-15 17:49:40 +01:00
|
|
|
|
2009-08-12 09:40:16 +02:00
|
|
|
// Initialise the underlying socket.
|
2009-12-10 09:47:24 +01:00
|
|
|
int rc = tcp_socket.open (fd_, options.sndbuf, options.rcvbuf);
|
2009-08-12 09:40:16 +02:00
|
|
|
zmq_assert (rc == 0);
|
2009-08-09 09:24:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
zmq::zmq_engine_t::~zmq_engine_t ()
|
|
|
|
{
|
2009-08-12 09:40:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void zmq::zmq_engine_t::plug (i_inout *inout_)
|
|
|
|
{
|
2009-09-11 13:20:24 +02:00
|
|
|
zmq_assert (!inout);
|
|
|
|
|
2009-08-12 09:40:16 +02:00
|
|
|
encoder.set_inout (inout_);
|
|
|
|
decoder.set_inout (inout_);
|
|
|
|
|
|
|
|
handle = add_fd (tcp_socket.get_fd ());
|
|
|
|
set_pollin (handle);
|
|
|
|
set_pollout (handle);
|
|
|
|
|
|
|
|
inout = inout_;
|
2009-09-02 11:58:39 +02:00
|
|
|
|
|
|
|
// Flush all the data that may have been already received downstream.
|
|
|
|
in_event ();
|
2009-08-12 09:40:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void zmq::zmq_engine_t::unplug ()
|
|
|
|
{
|
|
|
|
rm_fd (handle);
|
2009-09-10 16:32:06 +02:00
|
|
|
encoder.set_inout (NULL);
|
|
|
|
decoder.set_inout (NULL);
|
2009-08-12 09:40:16 +02:00
|
|
|
inout = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void zmq::zmq_engine_t::in_event ()
|
|
|
|
{
|
2009-12-11 22:29:04 +01:00
|
|
|
// If there's no data to process in the buffer...
|
|
|
|
if (!insize) {
|
2009-08-12 09:40:16 +02:00
|
|
|
|
2009-12-11 22:29:04 +01:00
|
|
|
// Retrieve the buffer and read as much data as possible.
|
|
|
|
decoder.get_buffer (&inpos, &insize);
|
|
|
|
insize = tcp_socket.read (inpos, insize);
|
2009-08-12 09:40:16 +02:00
|
|
|
|
|
|
|
// Check whether the peer has closed the connection.
|
2009-12-08 15:41:50 +01:00
|
|
|
if (insize == (size_t) -1) {
|
2009-08-12 09:40:16 +02:00
|
|
|
insize = 0;
|
|
|
|
error ();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push the data to the decoder.
|
2009-12-11 22:29:04 +01:00
|
|
|
size_t processed = decoder.process_buffer (inpos, insize);
|
|
|
|
|
|
|
|
// Adjust the buffer.
|
|
|
|
inpos += processed;
|
|
|
|
insize -= processed;
|
2009-08-12 09:40:16 +02:00
|
|
|
|
2009-12-11 22:29:04 +01:00
|
|
|
// Stop polling for input if we got stuck.
|
2009-12-28 21:29:31 +01:00
|
|
|
if (processed < insize) {
|
2010-01-05 14:45:10 +01:00
|
|
|
|
|
|
|
// This may happen if queue limits are implemented or when
|
|
|
|
// init object reads all required information from the socket
|
|
|
|
// and rejects to read more data.
|
|
|
|
reset_pollin (handle);
|
2009-12-28 21:29:31 +01:00
|
|
|
}
|
2009-08-12 09:40:16 +02:00
|
|
|
|
2009-09-04 09:51:42 +02:00
|
|
|
// Flush all messages the decoder may have produced.
|
|
|
|
inout->flush ();
|
2009-08-09 09:24:48 +02:00
|
|
|
}
|
|
|
|
|
2009-08-12 09:40:16 +02:00
|
|
|
void zmq::zmq_engine_t::out_event ()
|
2009-08-09 09:24:48 +02:00
|
|
|
{
|
2009-08-12 09:40:16 +02:00
|
|
|
// If write buffer is empty, try to read new data from the encoder.
|
2009-12-11 22:29:04 +01:00
|
|
|
if (!outsize) {
|
2009-12-13 09:11:08 +01:00
|
|
|
|
|
|
|
outpos = NULL;
|
|
|
|
encoder.get_data (&outpos, &outsize);
|
2009-12-11 22:29:04 +01:00
|
|
|
|
2009-08-12 09:40:16 +02:00
|
|
|
// If there is no data to send, stop polling for output.
|
2009-12-11 22:29:04 +01:00
|
|
|
if (outsize == 0) {
|
2009-08-12 09:40:16 +02:00
|
|
|
reset_pollout (handle);
|
2009-12-11 22:29:04 +01:00
|
|
|
return;
|
|
|
|
}
|
2009-08-12 09:40:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// If there are any data to write in write buffer, write as much as
|
|
|
|
// possible to the socket.
|
2009-12-11 22:29:04 +01:00
|
|
|
int nbytes = tcp_socket.write (outpos, outsize);
|
2009-08-12 09:40:16 +02:00
|
|
|
|
2009-12-11 22:29:04 +01:00
|
|
|
// Handle problems with the connection.
|
|
|
|
if (nbytes == -1) {
|
|
|
|
error ();
|
|
|
|
return;
|
2009-08-12 09:40:16 +02:00
|
|
|
}
|
2009-12-11 22:29:04 +01:00
|
|
|
|
|
|
|
outpos += nbytes;
|
|
|
|
outsize -= nbytes;
|
2009-08-09 09:24:48 +02:00
|
|
|
}
|
|
|
|
|
2009-08-27 10:54:28 +02:00
|
|
|
void zmq::zmq_engine_t::revive ()
|
|
|
|
{
|
|
|
|
set_pollout (handle);
|
2009-10-02 09:42:13 +02:00
|
|
|
|
|
|
|
// Speculative write: The assumption is that at the moment new message
|
|
|
|
// was sent by the user the socket is probably available for writing.
|
|
|
|
// Thus we try to write the data to socket avoiding polling for POLLOUT.
|
|
|
|
// Consequently, the latency should be better in request/reply scenarios.
|
|
|
|
out_event ();
|
2009-08-27 10:54:28 +02:00
|
|
|
}
|
|
|
|
|
2009-08-12 09:40:16 +02:00
|
|
|
void zmq::zmq_engine_t::error ()
|
|
|
|
{
|
2009-08-27 16:24:21 +02:00
|
|
|
zmq_assert (inout);
|
2009-12-15 17:49:40 +01:00
|
|
|
|
|
|
|
zmq_connecter_t *reconnecter = NULL;
|
|
|
|
if (reconnect) {
|
|
|
|
|
|
|
|
// Create a connecter object to attempt reconnect.
|
|
|
|
// Ask it to wait for a while before reconnecting.
|
2009-12-15 23:49:55 +01:00
|
|
|
reconnecter = new (std::nothrow) zmq_connecter_t (
|
2009-12-15 17:49:40 +01:00
|
|
|
inout->get_io_thread (), inout->get_owner (),
|
2009-12-23 19:37:56 +01:00
|
|
|
options, inout->get_ordinal (), true);
|
2009-12-15 17:49:40 +01:00
|
|
|
zmq_assert (reconnecter);
|
2010-01-15 14:11:39 +01:00
|
|
|
reconnecter->set_address (protocol.c_str(), address.c_str ());
|
2009-12-15 17:49:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inout->detach (reconnecter);
|
2009-08-27 16:24:21 +02:00
|
|
|
unplug ();
|
|
|
|
delete this;
|
2009-08-12 09:40:16 +02:00
|
|
|
}
|