1 /*
2     Copyright (c) 2009-2012 250bpm s.r.o.
3     Copyright (c) 2007-2009 iMatix Corporation
4     Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
5 
6     This file is part of Crossroads I/O project.
7 
8     Crossroads I/O is free software; you can redistribute it and/or modify it
9     under the terms of the GNU Lesser General Public License as published by
10     the Free Software Foundation; either version 3 of the License, or
11     (at your option) any later version.
12 
13     Crossroads is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU Lesser General Public License for more details.
17 
18     You should have received a copy of the GNU Lesser General Public License
19     along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 #ifndef __XS_STREAM_ENGINE_HPP_INCLUDED__
23 #define __XS_STREAM_ENGINE_HPP_INCLUDED__
24 
25 #include <stddef.h>
26 
27 #include "fd.hpp"
28 #include "i_engine.hpp"
29 #include "io_object.hpp"
30 #include "encoder.hpp"
31 #include "decoder.hpp"
32 #include "options.hpp"
33 
34 namespace xs
35 {
36 
37     class io_thread_t;
38     class session_base_t;
39 
40     //  This engine handles any socket with SOCK_STREAM semantics,
41     //  e.g. TCP socket or an UNIX domain socket.
42 
43     class stream_engine_t : public io_object_t, public i_engine
44     {
45     public:
46 
47         stream_engine_t (fd_t fd_, const options_t &options_);
48         ~stream_engine_t ();
49 
50         //  i_engine interface implementation.
51         void plug (xs::io_thread_t *io_thread_,
52            xs::session_base_t *session_);
53         void unplug ();
54         void terminate ();
55         void activate_in ();
56         void activate_out ();
57 
58         //  i_poll_events interface implementation.
59         void in_event (fd_t fd_);
60         void out_event (fd_t fd_);
61 
62     private:
63 
64         //  Function to handle network disconnections.
65         void error ();
66 
67         //  Writes data to the socket. Returns the number of bytes actually
68         //  written (even zero is to be considered to be a success). In case
69         //  of error or orderly shutdown by the other peer -1 is returned.
70         int write (const void *data_, size_t size_);
71 
72         //  Reads data from the socket (up to 'size' bytes). Returns the number
73         //  of bytes actually read (even zero is to be considered to be
74         //  a success). In case of error or orderly shutdown by the other
75         //  peer -1 is returned.
76         int read (void *data_, size_t size_);
77 
78         //  Underlying socket.
79         fd_t s;
80 
81         handle_t handle;
82 
83         unsigned char *inpos;
84         size_t insize;
85         decoder_t decoder;
86 
87         unsigned char *outpos;
88         size_t outsize;
89         encoder_t encoder;
90 
91         //  The session this engine is attached to.
92         xs::session_base_t *session;
93 
94         //  Detached transient session.
95         xs::session_base_t *leftover_session;
96 
97         options_t options;
98 
99         bool plugged;
100 
101         stream_engine_t (const stream_engine_t&);
102         const stream_engine_t &operator = (const stream_engine_t&);
103     };
104 
105 }
106 
107 #endif
108