libzmq master
The Intelligent Transport Layer

stream_engine.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) 2007-2011 Other contributors as noted in the AUTHORS file
00005 
00006     This file is part of 0MQ.
00007 
00008     0MQ is free software; you can redistribute it and/or modify it under
00009     the terms of the GNU Lesser General Public License as published by
00010     the Free Software Foundation; either version 3 of the License, or
00011     (at your option) any later version.
00012 
00013     0MQ is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016     GNU Lesser General Public License for more details.
00017 
00018     You should have received a copy of the GNU Lesser General Public License
00019     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00020 */
00021 
00022 #ifndef __ZMQ_STREAM_ENGINE_HPP_INCLUDED__
00023 #define __ZMQ_STREAM_ENGINE_HPP_INCLUDED__
00024 
00025 #include <stddef.h>
00026 
00027 #include "fd.hpp"
00028 #include "i_engine.hpp"
00029 #include "io_object.hpp"
00030 #include "encoder.hpp"
00031 #include "decoder.hpp"
00032 #include "options.hpp"
00033 
00034 namespace zmq
00035 {
00036 
00037     //  This engine handles any socket with SOCK_STREAM semantics,
00038     //  e.g. TCP socket or an UNIX domain socket.
00039 
00040     class stream_engine_t : public io_object_t, public i_engine
00041     {
00042     public:
00043 
00044         stream_engine_t (fd_t fd_, const options_t &options_);
00045         ~stream_engine_t ();
00046 
00047         //  i_engine interface implementation.
00048         void plug (class io_thread_t *io_thread_,
00049            class session_base_t *session_);
00050         void unplug ();
00051         void terminate ();
00052         void activate_in ();
00053         void activate_out ();
00054 
00055         //  i_poll_events interface implementation.
00056         void in_event ();
00057         void out_event ();
00058 
00059     private:
00060 
00061         //  Function to handle network disconnections.
00062         void error ();
00063 
00064         //  Writes data to the socket. Returns the number of bytes actually
00065         //  written (even zero is to be considered to be a success). In case
00066         //  of error or orderly shutdown by the other peer -1 is returned.
00067         int write (const void *data_, size_t size_);
00068 
00069         //  Reads data from the socket (up to 'size' bytes). Returns the number
00070         //  of bytes actually read (even zero is to be considered to be
00071         //  a success). In case of error or orderly shutdown by the other
00072         //  peer -1 is returned.
00073         int read (void *data_, size_t size_);
00074 
00075         //  Underlying socket.
00076         fd_t s;
00077 
00078         handle_t handle;
00079 
00080         unsigned char *inpos;
00081         size_t insize;
00082         decoder_t decoder;
00083 
00084         unsigned char *outpos;
00085         size_t outsize;
00086         encoder_t encoder;
00087 
00088         //  The session this engine is attached to.
00089         class session_base_t *session;
00090 
00091         //  Detached transient session.
00092         class session_base_t *leftover_session;
00093 
00094         options_t options;
00095 
00096         bool plugged;
00097 
00098         stream_engine_t (const stream_engine_t&);
00099         const stream_engine_t &operator = (const stream_engine_t&);
00100     };
00101 
00102 }
00103 
00104 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines