2009-08-12 09:40:16 +02:00
|
|
|
/*
|
2010-01-05 08:29:35 +01:00
|
|
|
Copyright (c) 2007-2010 iMatix Corporation
|
2009-08-12 09:40:16 +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-12 09:40:16 +02:00
|
|
|
#include "session.hpp"
|
2010-08-11 14:09:56 +02:00
|
|
|
#include "socket_base.hpp"
|
2009-08-30 08:18:31 +02:00
|
|
|
#include "i_engine.hpp"
|
2009-08-12 09:40:16 +02:00
|
|
|
#include "err.hpp"
|
2009-08-27 10:54:28 +02:00
|
|
|
#include "pipe.hpp"
|
2009-08-12 09:40:16 +02:00
|
|
|
|
2010-08-11 14:09:56 +02:00
|
|
|
zmq::session_t::session_t (class io_thread_t *io_thread_,
|
|
|
|
class socket_base_t *socket_, const options_t &options_) :
|
|
|
|
own_t (io_thread_),
|
|
|
|
options (options_),
|
2009-08-27 10:54:28 +02:00
|
|
|
in_pipe (NULL),
|
2010-03-20 15:04:30 +01:00
|
|
|
incomplete_in (false),
|
2009-08-27 16:24:21 +02:00
|
|
|
active (true),
|
2009-08-27 10:54:28 +02:00
|
|
|
out_pipe (NULL),
|
2009-08-21 14:29:22 +02:00
|
|
|
engine (NULL),
|
2010-08-11 14:09:56 +02:00
|
|
|
socket (socket_),
|
|
|
|
io_thread (io_thread_),
|
|
|
|
attach_processed (false),
|
|
|
|
term_processed (false)
|
2010-02-13 13:07:33 +01:00
|
|
|
{
|
2009-08-12 09:40:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
zmq::session_t::~session_t ()
|
|
|
|
{
|
2009-09-02 14:59:53 +02:00
|
|
|
zmq_assert (!in_pipe);
|
|
|
|
zmq_assert (!out_pipe);
|
2009-08-12 09:40:16 +02:00
|
|
|
}
|
|
|
|
|
2010-08-11 14:09:56 +02:00
|
|
|
void zmq::session_t::terminate ()
|
2010-08-06 17:49:37 +02:00
|
|
|
{
|
2010-08-11 17:06:49 +02:00
|
|
|
if (in_pipe)
|
|
|
|
in_pipe->terminate ();
|
|
|
|
if (out_pipe)
|
|
|
|
out_pipe->terminate ();
|
2010-08-06 17:49:37 +02:00
|
|
|
}
|
|
|
|
|
2009-08-21 14:29:22 +02:00
|
|
|
bool zmq::session_t::read (::zmq_msg_t *msg_)
|
2009-08-12 09:40:16 +02:00
|
|
|
{
|
2009-09-21 17:20:13 +02:00
|
|
|
if (!in_pipe || !active)
|
2009-08-27 10:54:28 +02:00
|
|
|
return false;
|
|
|
|
|
2010-08-06 17:49:37 +02:00
|
|
|
if (!in_pipe->read (msg_)) {
|
|
|
|
active = false;
|
2010-03-20 15:04:30 +01:00
|
|
|
return false;
|
2010-08-06 17:49:37 +02:00
|
|
|
}
|
2010-03-20 15:04:30 +01:00
|
|
|
|
2010-03-27 21:25:40 +01:00
|
|
|
incomplete_in = msg_->flags & ZMQ_MSG_MORE;
|
2010-03-20 15:04:30 +01:00
|
|
|
return true;
|
2009-08-12 09:40:16 +02:00
|
|
|
}
|
|
|
|
|
2009-08-21 14:29:22 +02:00
|
|
|
bool zmq::session_t::write (::zmq_msg_t *msg_)
|
2009-08-12 09:40:16 +02:00
|
|
|
{
|
2010-01-21 09:58:49 +01:00
|
|
|
if (out_pipe && out_pipe->write (msg_)) {
|
2009-09-04 16:02:41 +02:00
|
|
|
zmq_msg_init (msg_);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2009-08-12 09:40:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void zmq::session_t::flush ()
|
|
|
|
{
|
2009-09-16 14:02:43 +02:00
|
|
|
if (out_pipe)
|
|
|
|
out_pipe->flush ();
|
2009-08-27 10:54:28 +02:00
|
|
|
}
|
|
|
|
|
2010-08-11 14:09:56 +02:00
|
|
|
void zmq::session_t::clean_pipes ()
|
2009-08-27 16:24:21 +02:00
|
|
|
{
|
2010-03-20 15:04:30 +01:00
|
|
|
// Get rid of half-processed messages in the out pipe. Flush any
|
|
|
|
// unflushed messages upstream.
|
|
|
|
if (out_pipe) {
|
|
|
|
out_pipe->rollback ();
|
|
|
|
out_pipe->flush ();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove any half-read message from the in pipe.
|
|
|
|
if (in_pipe) {
|
|
|
|
while (incomplete_in) {
|
|
|
|
zmq_msg_t msg;
|
|
|
|
zmq_msg_init (&msg);
|
|
|
|
if (!read (&msg)) {
|
|
|
|
zmq_assert (!incomplete_in);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
zmq_msg_close (&msg);
|
|
|
|
}
|
|
|
|
}
|
2009-12-15 17:49:40 +01:00
|
|
|
}
|
|
|
|
|
2009-09-21 14:39:59 +02:00
|
|
|
void zmq::session_t::attach_pipes (class reader_t *inpipe_,
|
2010-02-16 18:30:38 +01:00
|
|
|
class writer_t *outpipe_, const blob_t &peer_identity_)
|
2009-09-02 10:22:23 +02:00
|
|
|
{
|
2009-09-21 14:39:59 +02:00
|
|
|
if (inpipe_) {
|
|
|
|
zmq_assert (!in_pipe);
|
|
|
|
in_pipe = inpipe_;
|
|
|
|
active = true;
|
2010-08-06 17:49:37 +02:00
|
|
|
in_pipe->set_event_sink (this);
|
2009-09-21 14:39:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (outpipe_) {
|
|
|
|
zmq_assert (!out_pipe);
|
|
|
|
out_pipe = outpipe_;
|
2010-08-06 17:49:37 +02:00
|
|
|
out_pipe->set_event_sink (this);
|
2009-09-21 14:39:59 +02:00
|
|
|
}
|
2010-08-11 14:09:56 +02:00
|
|
|
|
|
|
|
attach_processed = true;
|
|
|
|
finalise ();
|
2009-09-02 10:22:23 +02:00
|
|
|
}
|
2009-09-02 14:59:53 +02:00
|
|
|
|
2010-08-06 17:49:37 +02:00
|
|
|
void zmq::session_t::terminated (reader_t *pipe_)
|
2009-09-02 10:22:23 +02:00
|
|
|
{
|
2009-09-21 14:39:59 +02:00
|
|
|
active = false;
|
|
|
|
in_pipe = NULL;
|
2010-08-06 20:55:37 +02:00
|
|
|
finalise ();
|
2009-09-02 10:22:23 +02:00
|
|
|
}
|
|
|
|
|
2010-08-06 17:49:37 +02:00
|
|
|
void zmq::session_t::terminated (writer_t *pipe_)
|
2009-08-27 10:54:28 +02:00
|
|
|
{
|
2009-09-21 14:39:59 +02:00
|
|
|
out_pipe = NULL;
|
2010-08-06 20:55:37 +02:00
|
|
|
finalise ();
|
2009-08-12 09:40:16 +02:00
|
|
|
}
|
|
|
|
|
2010-08-06 17:49:37 +02:00
|
|
|
void zmq::session_t::activated (reader_t *pipe_)
|
2009-08-28 16:51:46 +02:00
|
|
|
{
|
2009-09-21 14:39:59 +02:00
|
|
|
zmq_assert (in_pipe == pipe_);
|
|
|
|
active = true;
|
|
|
|
if (engine)
|
2010-08-11 14:09:56 +02:00
|
|
|
engine->activate_out ();
|
2009-08-28 16:51:46 +02:00
|
|
|
}
|
|
|
|
|
2010-08-06 17:49:37 +02:00
|
|
|
void zmq::session_t::activated (writer_t *pipe_)
|
2010-03-01 10:13:26 +01:00
|
|
|
{
|
|
|
|
zmq_assert (out_pipe == pipe_);
|
|
|
|
if (engine)
|
2010-08-11 14:09:56 +02:00
|
|
|
engine->activate_in ();
|
2010-03-01 10:13:26 +01:00
|
|
|
}
|
|
|
|
|
2009-08-12 09:40:16 +02:00
|
|
|
void zmq::session_t::process_plug ()
|
|
|
|
{
|
2010-02-13 13:07:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void zmq::session_t::process_unplug ()
|
|
|
|
{
|
2010-08-06 17:49:37 +02:00
|
|
|
// TODO: There may be a problem here. The called ensures that all the
|
|
|
|
// commands on the fly have been delivered. However, given that the
|
|
|
|
// session is unregistered from the global repository only at this point
|
|
|
|
// there may be some commands being sent to the session right now.
|
|
|
|
|
2010-02-13 13:07:33 +01:00
|
|
|
// Unregister the session from the socket.
|
2010-08-11 14:09:56 +02:00
|
|
|
// if (!peer_identity.empty () && peer_identity [0] != 0)
|
|
|
|
// unregister_session (peer_identity);
|
|
|
|
// TODO: Should be done in named session.
|
2010-02-13 13:07:33 +01:00
|
|
|
|
|
|
|
// Ask associated pipes to terminate.
|
2010-08-06 17:49:37 +02:00
|
|
|
if (in_pipe)
|
|
|
|
in_pipe->terminate ();
|
|
|
|
if (out_pipe)
|
|
|
|
out_pipe->terminate ();
|
2010-02-13 13:07:33 +01:00
|
|
|
|
|
|
|
if (engine) {
|
|
|
|
engine->unplug ();
|
|
|
|
delete engine;
|
|
|
|
engine = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-11 14:09:56 +02:00
|
|
|
void zmq::session_t::finalise ()
|
|
|
|
{
|
|
|
|
// If all conditions are met, proceed with termination:
|
|
|
|
// 1. Owner object already asked us to terminate.
|
|
|
|
// 2. The pipes were already attached to the session.
|
|
|
|
// 3. Both pipes have already terminated. Note that inbound pipe
|
|
|
|
// is terminated after delimiter is read, i.e. all messages
|
|
|
|
// were already sent to the wire.
|
|
|
|
if (term_processed && attach_processed && !in_pipe && !out_pipe)
|
|
|
|
own_t::process_term ();
|
|
|
|
}
|
|
|
|
|
2010-02-13 13:07:33 +01:00
|
|
|
void zmq::session_t::process_attach (i_engine *engine_,
|
2010-02-13 14:07:30 +01:00
|
|
|
const blob_t &peer_identity_)
|
2010-02-13 13:07:33 +01:00
|
|
|
{
|
2010-02-14 13:34:48 +01:00
|
|
|
// Check whether the required pipes already exist. If not so, we'll
|
|
|
|
// create them and bind them to the socket object.
|
|
|
|
reader_t *socket_reader = NULL;
|
|
|
|
writer_t *socket_writer = NULL;
|
|
|
|
|
|
|
|
if (options.requires_in && !out_pipe) {
|
2010-08-11 14:09:56 +02:00
|
|
|
create_pipe (socket, this, options.hwm, options.swap, &socket_reader,
|
2010-08-06 17:49:37 +02:00
|
|
|
&out_pipe);
|
|
|
|
out_pipe->set_event_sink (this);
|
2010-02-14 13:34:48 +01:00
|
|
|
}
|
2009-09-21 17:20:13 +02:00
|
|
|
|
2010-02-14 13:34:48 +01:00
|
|
|
if (options.requires_out && !in_pipe) {
|
2010-08-11 14:09:56 +02:00
|
|
|
create_pipe (this, socket, options.hwm, options.swap, &in_pipe,
|
2010-08-06 17:49:37 +02:00
|
|
|
&socket_writer);
|
|
|
|
in_pipe->set_event_sink (this);
|
2009-08-27 10:54:28 +02:00
|
|
|
}
|
2009-08-21 14:29:22 +02:00
|
|
|
|
2010-02-14 13:34:48 +01:00
|
|
|
if (socket_reader || socket_writer)
|
2010-08-11 14:09:56 +02:00
|
|
|
send_bind (socket, socket_reader, socket_writer, peer_identity);
|
2010-02-14 13:34:48 +01:00
|
|
|
|
2010-02-13 13:07:33 +01:00
|
|
|
// Plug in the engine.
|
2009-09-11 13:20:24 +02:00
|
|
|
zmq_assert (!engine);
|
2009-08-21 14:29:22 +02:00
|
|
|
zmq_assert (engine_);
|
|
|
|
engine = engine_;
|
2010-08-11 14:09:56 +02:00
|
|
|
engine->plug (io_thread, this);
|
|
|
|
|
|
|
|
// Trigger the notfication about the attachment.
|
|
|
|
attached (peer_identity_);
|
|
|
|
}
|
|
|
|
|
2010-08-11 17:06:49 +02:00
|
|
|
void zmq::session_t::detach ()
|
|
|
|
{
|
|
|
|
// Engine is dead. Let's forget about it.
|
|
|
|
engine = NULL;
|
|
|
|
|
|
|
|
detached ();
|
|
|
|
}
|
|
|
|
|
2010-08-11 14:09:56 +02:00
|
|
|
void zmq::session_t::process_term ()
|
|
|
|
{
|
|
|
|
// Here we are pugging into the own_t's termination mechanism.
|
|
|
|
// The goal is to postpone the termination till all the pending messages
|
|
|
|
// are sent to the peer.
|
|
|
|
term_processed = true;
|
|
|
|
finalise ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void zmq::session_t::attached (const blob_t &peer_identity_)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void zmq::session_t::detached ()
|
|
|
|
{
|
2009-08-12 09:40:16 +02:00
|
|
|
}
|
2010-08-06 17:49:37 +02:00
|
|
|
|