1 /*
2     Copyright (c) 2007-2010 iMatix Corporation
3 
4     This file is part of 0MQ.
5 
6     0MQ is free software; you can redistribute it and/or modify it under
7     the terms of the GNU Lesser General Public License as published by
8     the Free Software Foundation; either version 3 of the License, or
9     (at your option) any later version.
10 
11     0MQ is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU Lesser General Public License for more details.
15 
16     You should have received a copy of the GNU Lesser General Public License
17     along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "session.hpp"
21 #include "socket_base.hpp"
22 #include "i_engine.hpp"
23 #include "err.hpp"
24 #include "pipe.hpp"
25 #include "likely.hpp"
26 
session_t(class io_thread_t * io_thread_,class socket_base_t * socket_,const options_t & options_)27 zmq::session_t::session_t (class io_thread_t *io_thread_,
28       class socket_base_t *socket_, const options_t &options_) :
29     own_t (io_thread_, options_),
30     io_object_t (io_thread_),
31     in_pipe (NULL),
32     incomplete_in (false),
33     out_pipe (NULL),
34     engine (NULL),
35     socket (socket_),
36     io_thread (io_thread_),
37     pipes_attached (false),
38     delimiter_processed (false),
39     force_terminate (false),
40     has_linger_timer (false),
41     state (active)
42 {
43 }
44 
~session_t()45 zmq::session_t::~session_t ()
46 {
47     zmq_assert (!in_pipe);
48     zmq_assert (!out_pipe);
49 
50     if (engine)
51         engine->terminate ();
52 }
53 
proceed_with_term()54 void zmq::session_t::proceed_with_term ()
55 {
56     if (state == terminating)
57         return;
58 
59     zmq_assert (state == pending);
60     state = terminating;
61 
62     //  If there's still a pending linger timer, remove it.
63     if (has_linger_timer) {
64         cancel_timer (linger_timer_id);
65         has_linger_timer = false;
66     }
67 
68     if (in_pipe) {
69         register_term_acks (1);
70         in_pipe->terminate ();
71     }
72     if (out_pipe) {
73         register_term_acks (1);
74         out_pipe->terminate ();
75     }
76 
77     //  The session has already waited for the linger period. We don't want
78     //  the child objects to linger any more thus linger is set to zero.
79     own_t::process_term (0);
80 }
81 
read(::zmq_msg_t * msg_)82 bool zmq::session_t::read (::zmq_msg_t *msg_)
83 {
84     if (!in_pipe)
85         return false;
86 
87     if (!in_pipe->read (msg_))
88         return false;
89 
90     incomplete_in = msg_->flags & ZMQ_MSG_MORE;
91     return true;
92 }
93 
write(::zmq_msg_t * msg_)94 bool zmq::session_t::write (::zmq_msg_t *msg_)
95 {
96     if (out_pipe && out_pipe->write (msg_)) {
97         zmq_msg_init (msg_);
98         return true;
99     }
100 
101     return false;
102 }
103 
flush()104 void zmq::session_t::flush ()
105 {
106     if (out_pipe)
107         out_pipe->flush ();
108 }
109 
clean_pipes()110 void zmq::session_t::clean_pipes ()
111 {
112     //  Get rid of half-processed messages in the out pipe. Flush any
113     //  unflushed messages upstream.
114     if (out_pipe) {
115         out_pipe->rollback ();
116         out_pipe->flush ();
117     }
118 
119     //  Remove any half-read message from the in pipe.
120     if (in_pipe) {
121         while (incomplete_in) {
122             zmq_msg_t msg;
123             zmq_msg_init (&msg);
124             if (!read (&msg)) {
125                 zmq_assert (!incomplete_in);
126                 break;
127             }
128             zmq_msg_close (&msg);
129         }
130     }
131 }
132 
attach_pipes(class reader_t * inpipe_,class writer_t * outpipe_,const blob_t & peer_identity_)133 void zmq::session_t::attach_pipes (class reader_t *inpipe_,
134     class writer_t *outpipe_, const blob_t &peer_identity_)
135 {
136     zmq_assert (!pipes_attached);
137     pipes_attached = true;
138 
139     if (inpipe_) {
140         zmq_assert (!in_pipe);
141         in_pipe = inpipe_;
142         in_pipe->set_event_sink (this);
143     }
144 
145     if (outpipe_) {
146         zmq_assert (!out_pipe);
147         out_pipe = outpipe_;
148         out_pipe->set_event_sink (this);
149     }
150 
151     //  If we are already terminating, terminate the pipes straight away.
152     if (state == terminating) {
153         if (in_pipe) {
154             in_pipe->terminate ();
155             register_term_acks (1);
156         }
157         if (out_pipe) {
158             out_pipe->terminate ();
159             register_term_acks (1);
160         }
161     }
162 }
163 
delimited(reader_t * pipe_)164 void zmq::session_t::delimited (reader_t *pipe_)
165 {
166     zmq_assert (in_pipe == pipe_);
167     zmq_assert (!delimiter_processed);
168     delimiter_processed = true;
169 
170     //  If we are in process of being closed, but still waiting for all
171     //  pending messeges being sent, we can terminate here.
172     if (state == pending)
173         proceed_with_term ();
174 }
175 
terminated(reader_t * pipe_)176 void zmq::session_t::terminated (reader_t *pipe_)
177 {
178     zmq_assert (in_pipe == pipe_);
179     in_pipe = NULL;
180     if (state == terminating)
181         unregister_term_ack ();
182 }
183 
terminated(writer_t * pipe_)184 void zmq::session_t::terminated (writer_t *pipe_)
185 {
186     zmq_assert (out_pipe == pipe_);
187     out_pipe = NULL;
188     if (state == terminating)
189         unregister_term_ack ();
190 }
191 
activated(reader_t * pipe_)192 void zmq::session_t::activated (reader_t *pipe_)
193 {
194     zmq_assert (in_pipe == pipe_);
195 
196     if (likely (engine != NULL))
197         engine->activate_out ();
198     else
199         in_pipe->check_read ();
200 }
201 
activated(writer_t * pipe_)202 void zmq::session_t::activated (writer_t *pipe_)
203 {
204     zmq_assert (out_pipe == pipe_);
205     if (engine)
206         engine->activate_in ();
207 }
208 
process_plug()209 void zmq::session_t::process_plug ()
210 {
211 }
212 
process_attach(i_engine * engine_,const blob_t & peer_identity_)213 void zmq::session_t::process_attach (i_engine *engine_,
214     const blob_t &peer_identity_)
215 {
216     //  If some other object (e.g. init) notifies us that the connection failed
217     //  we need to start the reconnection process.
218     if (!engine_) {
219         zmq_assert (!engine);
220         detached ();
221         return;
222     }
223 
224     //  If we are already terminating, we destroy the engine straight away.
225     //  Note that we don't have to unplug it before deleting as it's not
226     //  yet plugged to the session.
227     if (state == terminating) {
228         delete engine_;
229         return;
230     }
231 
232     //  If the session already has an engine attached, destroy new one.
233     //  Note new engine is not plugged in yet, we don't have to unplug it.
234     if (engine) {
235         log ("DPID: duplicate peer identity - disconnecting peer");
236         delete engine_;
237         return;
238     }
239 
240     //  Check whether the required pipes already exist. If not so, we'll
241     //  create them and bind them to the socket object.
242     if (!pipes_attached) {
243         zmq_assert (!in_pipe && !out_pipe);
244         pipes_attached = true;
245         reader_t *socket_reader = NULL;
246         writer_t *socket_writer = NULL;
247 
248         //  Create the pipes, as required.
249         if (options.requires_in) {
250             create_pipe (socket, this, options.hwm, options.swap, &socket_reader,
251                 &out_pipe);
252             out_pipe->set_event_sink (this);
253         }
254         if (options.requires_out) {
255             create_pipe (this, socket, options.hwm, options.swap, &in_pipe,
256                 &socket_writer);
257             in_pipe->set_event_sink (this);
258         }
259 
260         //  Bind the pipes to the socket object.
261         if (socket_reader || socket_writer)
262             send_bind (socket, socket_reader, socket_writer, peer_identity_);
263     }
264 
265     //  Plug in the engine.
266     engine = engine_;
267     engine->plug (io_thread, this);
268 
269     //  Trigger the notfication about the attachment.
270     attached (peer_identity_);
271 }
272 
detach()273 void zmq::session_t::detach ()
274 {
275     //  Engine is dead. Let's forget about it.
276     engine = NULL;
277 
278     //  Remove any half-done messages from the pipes.
279     clean_pipes ();
280 
281     //  Send the event to the derived class.
282     detached ();
283 
284     //  Just in case, there's only a delimiter in the inbound pipe.
285     if (in_pipe)
286         in_pipe->check_read ();
287 }
288 
process_term(int linger_)289 void zmq::session_t::process_term (int linger_)
290 {
291     zmq_assert (state == active);
292     state = pending;
293 
294     //  If linger is set to zero, we can terminate the session straight away
295     //  not waiting for the pending messages to be sent.
296     if (linger_ == 0) {
297         proceed_with_term ();
298         return;
299     }
300 
301     //  If there's finite linger value, set up a timer.
302     if (linger_ > 0) {
303        zmq_assert (!has_linger_timer);
304        add_timer (linger_, linger_timer_id);
305        has_linger_timer = true;
306     }
307 
308     //  If there's no engine and there's only delimiter in the pipe it wouldn't
309     //  be ever read. Thus we check for it explicitly.
310     if (in_pipe)
311         in_pipe->check_read ();
312 
313     //  If there's no in pipe there are no pending messages to send.
314     //  We can proceed with the shutdown straight away. Also, if there is
315     //  inbound pipe, but the delimiter was already processed, we can
316     //  terminate immediately. Alternatively, if the derived session type have
317     //  called 'terminate' we'll finish straight away.
318     if (!options.requires_out || delimiter_processed || force_terminate ||
319         (!options.immediate_connect && !in_pipe))
320         proceed_with_term ();
321 }
322 
timer_event(int id_)323 void zmq::session_t::timer_event (int id_)
324 {
325     //  Linger period expired. We can proceed with termination even though
326     //  there are still pending messages to be sent.
327     zmq_assert (id_ == linger_timer_id);
328     has_linger_timer = false;
329     proceed_with_term ();
330 }
331 
register_session(const blob_t & name_,session_t * session_)332 bool zmq::session_t::register_session (const blob_t &name_, session_t *session_)
333 {
334     return socket->register_session (name_, session_);
335 }
336 
unregister_session(const blob_t & name_)337 void zmq::session_t::unregister_session (const blob_t &name_)
338 {
339     socket->unregister_session (name_);
340 }
341 
terminate()342 void zmq::session_t::terminate ()
343 {
344     force_terminate = true;
345     own_t::terminate ();
346 }
347