NEW FILE HEADER / COPYRIGHT FORMAT
[senf.git] / Socket / SocketHandle.ih
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institute for Open Communication Systems (FOKUS) 
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY 
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program 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 General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief SocketHandle internal header
25  */
26
27 #ifndef IH_SocketHandle_
28 #define IH_SocketHandle_ 1
29
30 // Custom includes
31 #include <map>
32 #include <string>
33 #include <boost/scoped_ptr.hpp>
34 #include "FileHandle.hh"
35
36 ///////////////////////////////ih.p////////////////////////////////////////
37
38 namespace senf {
39
40
41     class SocketProtocol;
42
43     namespace detail {
44
45         /** \brief String supporting automatic type conversion
46
47             The StreamableString class is used to simplify creating a text representation of
48             arbitrary values. StreamableString is an ordinary string with an additional constructor
49             which allows constructing the string from any arbitrary, streamable type.
50
51             \note It is generally not advisable to derive from the standard library container
52             classes. However, in this concrete case, the derivation is safe since only the
53             additional functionality is added. It is absolutely safe to convert the derived class
54             back to the base type.
55          */
56         class StreamableString : public std::string
57         {
58         public:
59             using std::string::operator=;
60
61             template <class T>
62             StreamableString & operator<<(T const & other);
63                                         ///< Value assigment
64                                         /**< This operator will assign the string from any
65                                              arbitrary type. It will use boost::lexical_cast to
66                                              convert the argument to its string representation. 
67
68                                              If the string is non-empty, an additional separating
69                                              comma is added to the string. */
70
71             StreamableString & operator<<(bool v);  ///< Bool assignment
72                                         /**< The bool assignment is defined explicitly to use a
73                                              specialized representation (the strings 'true' and
74                                              'false'). */
75         };
76
77     }
78
79     typedef std::map< std::string, detail::StreamableString > SocketStateMap;
80
81     namespace detail {
82         /** \brief Helper to convert SocketStateMap to multiline string representation
83             \internal
84          */
85         std::string dumpState(SocketStateMap const & map);
86     }
87
88     /** \brief SocketHandle referenced body
89
90         \internal
91
92         senf::SocketBody is the extended (relatively to senf::FileBody) body of
93         senf::SocketHandle. Every SocketHandle must have a SocketBody as it's body (and not a simple
94         FileBody). The casting and conversion operators defined will ensure this if used
95         properly. If this invariant is violated, your Program will probably crash.
96      */
97     class SocketBody
98         : public FileBody, 
99           public senf::pool_alloc_mixin<SocketBody>
100     {
101     public:
102         using senf::pool_alloc_mixin<SocketBody>::operator new;
103         using senf::pool_alloc_mixin<SocketBody>::operator delete;
104
105         ///////////////////////////////////////////////////////////////////////////
106         // Types
107
108         typedef boost::intrusive_ptr<SocketBody> ptr;
109
110         ///////////////////////////////////////////////////////////////////////////
111         ///\name Structors and default members
112         ///@{
113
114         SocketBody(std::auto_ptr<SocketProtocol> protocol, bool isServer);
115                                         /**<
116                                            \param protocol Protocol class implementing the desired
117                                            protocol
118                                            \param isServer \c true, if this socket is a server
119                                            socket, false otherwise */
120         SocketBody(std::auto_ptr<SocketProtocol> protocol, bool isServer, int fd);
121                                         /**<
122                                            \param protocol Protocol class implementing the desired
123                                            protocol
124                                            \param isServer \c true, if this socket is a server
125                                            socket, false otherwise
126                                            \param fd socket file descriptor */
127
128         // no copy
129         // no conversion constructors
130
131         ///@}
132         ///////////////////////////////////////////////////////////////////////////
133
134         SocketProtocol const & protocol() const;
135                                         ///< Access the protocol instance
136         bool isServer();                ///< Check socket type
137                                         /**< \return \c true, if this is a server socket, \c false
138                                            otherwise */
139
140         void state(SocketStateMap & map, unsigned lod);
141
142     private:
143         virtual void v_close();         ///< Close socket
144                                         /**< This override will automatically \c shutdown() the
145                                            socket whenever it is closed.
146                                            \throws senf::SystemException */
147         virtual void v_terminate();     ///< Forcibly close socket
148                                         /**< This override will automatically \c shutfown() the
149                                            socket whenever it is called. Additionally it will
150                                            disable SO_LINGER to ensure, that v_terminate will not
151                                            block. Like the overriden method, this member will ignore
152                                            failures and will never throw. It therefore safe to be
153                                            called from a destructor. */
154         virtual bool v_eof() const;     ///< Check for eof condition
155                                         /**< Since the eof check for sockets is very protocol
156                                            dependent, this member will forward the call to
157                                            senf::SocketPolicy::eof() */
158
159         boost::scoped_ptr<SocketProtocol> protocol_;
160         bool isServer_;
161     };
162
163 }
164
165 ///////////////////////////////ih.e////////////////////////////////////////
166 #endif
167
168 \f
169 // Local Variables:
170 // mode: c++
171 // fill-column: 100
172 // c-file-style: "senf"
173 // indent-tabs-mode: nil
174 // ispell-local-dictionary: "american"
175 // compile-command: "scons -u test"
176 // comment-column: 40
177 // End: