cfa7bc80a8b0c7407c65f6e3aba87e6d3bcf537a
[senf.git] / Socket / SocketHandle.ih
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 //     Stefan Bund <stefan.bund@fokus.fraunhofer.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 ConvertibleString class is used to simplify creating a text representation of
48             arbitrary values. ConvertibleString 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 ConvertibleString : public std::string
57         {
58         public:
59             ConvertibleString();
60             ConvertibleString(bool v);  ///< Bool conversion constructor
61                                         /**< The bool conversion is defined explicitly to use a
62                                            specialized representation (the strings 'true' and
63                                            'false') */
64             template <class T>
65             ConvertibleString(T const & other);
66                                         ///< Conversion constructor
67                                         /**< This constructor will assign the string from any
68                                            arbitrary type. It will use boost::lexical_cast to
69                                            convert the argument to its string representation. */
70
71             template <class T>
72             ConvertibleString & operator+= (ConvertibleString const & other);
73                                         ///< Add additional values with separator
74                                         /**< This operator facilitates the representation of
75                                            multiple values in a single string. Each value is first
76                                            converted to a string (using the type conversion
77                                            machinery of C++ and the ConvertibleString conversion
78                                            constructors). It is then appended to the current string
79                                            with ', ' as a separator (if the current string is
80                                            non-empty). */
81         };
82
83     }
84
85     typedef std::map< std::string, detail::ConvertibleString > SocketStateMap;
86
87     namespace detail {
88         /** \brief Helper to convert SocketStateMap to multiline string representation
89             \internal
90          */
91         std::string dumpState(SocketStateMap const & map);
92     }
93
94     /** \brief SocketHandle referenced body
95
96         \internal
97
98         senf::SocketBody is the extended (relatively to senf::FileBody) body of
99         senf::SocketHandle. Every SocketHandle must have a SocketBody as it's body (and not a simple
100         FileBody). The casting and conversion operators defined will ensure this if used
101         properly. If this invariant is violated, your Program will probably crash.
102      */
103     class SocketBody
104         : public FileBody
105     {
106     public:
107         ///////////////////////////////////////////////////////////////////////////
108         // Types
109
110         typedef boost::intrusive_ptr<SocketBody> ptr;
111
112         ///////////////////////////////////////////////////////////////////////////
113         ///\name Structors and default members
114         ///@{
115
116         SocketBody(std::auto_ptr<SocketProtocol> protocol, bool isServer);
117                                         /**<
118                                            \param protocol Protocol class implementing the desired
119                                            protocol
120                                            \param isServer \c true, if this socket is a server
121                                            socket, false otherwise */
122         SocketBody(std::auto_ptr<SocketProtocol> protocol, bool isServer, int fd);
123                                         /**<
124                                            \param protocol Protocol class implementing the desired
125                                            protocol
126                                            \param isServer \c true, if this socket is a server
127                                            socket, false otherwise
128                                            \param fd socket file descriptor */
129
130         // no copy
131         // no conversion constructors
132
133         ///@}
134         ///////////////////////////////////////////////////////////////////////////
135
136         SocketProtocol const & protocol() const;
137                                         ///< Access the protocol instance
138         bool isServer();                ///< Check socket type
139                                         /**< \return \c true, if this is a server socket, \c false
140                                            otherwise */
141
142         void state(SocketStateMap & map, unsigned lod);
143
144     private:
145         virtual void v_close();         ///< Close socket
146                                         /**< This override will automatically \c shutdown() the
147                                            socket whenever it is closed.
148                                            \throws senf::SystemException */
149         virtual void v_terminate();     ///< Forcibly close socket
150                                         /**< This override will automatically \c shutfown() the
151                                            socket whenever it is called. Additionally it will
152                                            disable SO_LINGER to ensure, that v_terminate will not
153                                            block. Like the overriden method, this member will ignore
154                                            failures and will never throw. It therefore safe to be
155                                            called from a destructor. */
156         virtual bool v_eof() const;     ///< Check for eof condition
157                                         /**< Since the eof check for sockets is very protocol
158                                            dependent, this member will forward the call to
159                                            senf::SocketPolicy::eof() */
160
161         boost::scoped_ptr<SocketProtocol> protocol_;
162         bool isServer_;
163     };
164
165 }
166
167 ///////////////////////////////ih.e////////////////////////////////////////
168 #endif
169
170 \f
171 // Local Variables:
172 // mode: c++
173 // fill-column: 100
174 // c-file-style: "senf"
175 // indent-tabs-mode: nil
176 // ispell-local-dictionary: "american"
177 // End: