libzmq master
The Intelligent Transport Layer

session_base.hpp

Go to the documentation of this file.
00001 /*
00002     Copyright (c) 2009-2011 250bpm s.r.o.
00003     Copyright (c) 2007-2009 iMatix Corporation
00004     Copyright (c) 2011 VMware, Inc.
00005     Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
00006 
00007     This file is part of 0MQ.
00008 
00009     0MQ is free software; you can redistribute it and/or modify it under
00010     the terms of the GNU Lesser General Public License as published by
00011     the Free Software Foundation; either version 3 of the License, or
00012     (at your option) any later version.
00013 
00014     0MQ is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017     GNU Lesser General Public License for more details.
00018 
00019     You should have received a copy of the GNU Lesser General Public License
00020     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00021 */
00022 
00023 #ifndef __ZMQ_SESSION_BASE_HPP_INCLUDED__
00024 #define __ZMQ_SESSION_BASE_HPP_INCLUDED__
00025 
00026 #include <string>
00027 
00028 #include "own.hpp"
00029 #include "i_engine.hpp"
00030 #include "io_object.hpp"
00031 #include "pipe.hpp"
00032 
00033 namespace zmq
00034 {
00035 
00036     class session_base_t :
00037         public own_t,
00038         public io_object_t,
00039         public i_pipe_events
00040     {
00041     public:
00042 
00043         //  Create a session of the particular type.
00044         static session_base_t *create (class io_thread_t *io_thread_,
00045             bool connect_, class socket_base_t *socket_,
00046             const options_t &options_, const char *protocol_,
00047             const char *address_);
00048 
00049         //  To be used once only, when creating the session.
00050         void attach_pipe (class pipe_t *pipe_);
00051 
00052         //  Following functions are the interface exposed towards the engine.
00053         virtual int read (msg_t *msg_);
00054         virtual int write (msg_t *msg_);
00055         void flush ();
00056         void detach ();
00057 
00058         //  i_pipe_events interface implementation.
00059         void read_activated (class pipe_t *pipe_);
00060         void write_activated (class pipe_t *pipe_);
00061         void hiccuped (class pipe_t *pipe_);
00062         void terminated (class pipe_t *pipe_);
00063 
00064     protected:
00065 
00066         session_base_t (class io_thread_t *io_thread_, bool connect_,
00067             class socket_base_t *socket_, const options_t &options_,
00068             const char *protocol_, const char *address_);
00069         ~session_base_t ();
00070 
00071     private:
00072 
00073         void start_connecting (bool wait_);
00074 
00075         void detached ();
00076 
00077         //  Handlers for incoming commands.
00078         void process_plug ();
00079         void process_attach (struct i_engine *engine_);
00080         void process_term (int linger_);
00081 
00082         //  i_poll_events handlers.
00083         void timer_event (int id_);
00084 
00085         //  Remove any half processed messages. Flush unflushed messages.
00086         //  Call this function when engine disconnect to get rid of leftovers.
00087         void clean_pipes ();
00088 
00089         //  Call this function to move on with the delayed process_term.
00090         void proceed_with_term ();
00091 
00092         //  If true, this session (re)connects to the peer. Otherwise, it's
00093         //  a transient session created by the listener.
00094         bool connect;
00095 
00096         //  Pipe connecting the session to its socket.
00097         class pipe_t *pipe;
00098 
00099         //  This flag is true if the remainder of the message being processed
00100         //  is still in the in pipe.
00101         bool incomplete_in;
00102 
00103         //  True if termination have been suspended to push the pending
00104         //  messages to the network.
00105         bool pending;
00106 
00107         //  The protocol I/O engine connected to the session.
00108         struct i_engine *engine;
00109 
00110         //  The socket the session belongs to.
00111         class socket_base_t *socket;
00112 
00113         //  I/O thread the session is living in. It will be used to plug in
00114         //  the engines into the same thread.
00115         class io_thread_t *io_thread;
00116 
00117         //  ID of the linger timer
00118         enum {linger_timer_id = 0x20};
00119 
00120         //  True is linger timer is running.
00121         bool has_linger_timer;
00122 
00123         //  If true, identity is to be sent/recvd from the network.
00124         bool send_identity;
00125         bool recv_identity;
00126 
00127         //  Protocol and address to use when connecting.
00128         std::string protocol;
00129         std::string address;
00130 
00131         session_base_t (const session_base_t&);
00132         const session_base_t &operator = (const session_base_t&);
00133     };
00134 
00135 }
00136 
00137 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines