00001 // $Id: SocketHandle.ih 1772 2011-03-10 12:45:21Z tho $ 00002 // 00003 // Copyright (C) 2006 00004 // Fraunhofer (FOKUS) 00005 // Competence Center NETwork research (NET), St. Augustin, GERMANY 00006 // Stefan Bund <g0dil@berlios.de> 00007 // 00008 // This program is free software; you can redistribute it and/or modify 00009 // it under the terms of the GNU General Public License as published by 00010 // the Free Software Foundation; either version 2 of the License, or 00011 // (at your option) any later version. 00012 // 00013 // This program 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 General Public License for more details. 00017 // 00018 // You should have received a copy of the GNU General Public License 00019 // along with this program; if not, write to the 00020 // Free Software Foundation, Inc., 00021 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00022 00027 #ifndef IH_SENF_Socket_SocketHandle_ 00028 #define IH_SENF_Socket_SocketHandle_ 1 00029 00030 // Custom includes 00031 #include <map> 00032 #include <string> 00033 #include <boost/scoped_ptr.hpp> 00034 #include "FileHandle.hh" 00035 00036 //-///////////////////////////////////////////////////////////////////////////////////////////////// 00037 00038 namespace senf { 00039 00040 00041 class SocketProtocol; 00042 00043 namespace detail { 00044 00056 class StreamableString : public std::string 00057 { 00058 public: 00059 using std::string::operator=; 00060 00061 template <class T> 00062 StreamableString & operator<<(T const & other); 00064 00071 StreamableString & operator<<(bool v); 00072 00075 }; 00076 00077 } 00078 00079 typedef std::map< std::string, detail::StreamableString > SocketStateMap; 00080 00081 namespace detail { 00085 std::string dumpState(SocketStateMap const & map); 00086 } 00087 00088 template <class Policy, class Self> class ConcreteSocketProtocol; 00089 00099 class SocketBody 00100 : public FileBody 00101 { 00102 public: 00103 //-///////////////////////////////////////////////////////////////////////////////////////// 00104 // Types 00105 00106 typedef boost::intrusive_ptr<SocketBody> ptr; 00107 00108 //-///////////////////////////////////////////////////////////////////////////////////////// 00110 //\{ 00111 00112 SocketBody(bool isServer); 00114 SocketBody(bool isServer, int fd); 00119 // no copy 00120 // no conversion constructors 00121 00122 //\} 00123 //-///////////////////////////////////////////////////////////////////////////////////////// 00124 00125 SocketProtocol & protocol() const; 00126 00127 bool isServer(); 00128 00131 void state(SocketStateMap & map, unsigned lod); 00132 00133 std::auto_ptr<SocketBody> clone(bool isServer) const; 00134 std::auto_ptr<SocketBody> clone(int fd, bool isServer) const; 00135 00136 private: 00137 virtual void v_close(); 00138 00141 virtual void v_terminate(); 00142 00148 virtual bool v_eof() const; 00149 00153 virtual SocketProtocol const & v_protocol() const = 0; 00154 virtual std::string v_protocolName() const = 0; 00155 00156 bool isServer_; 00157 }; 00158 00159 template <class SProtocol> 00160 class ProtocolSocketBody 00161 : public SocketBody, 00162 private SProtocol, 00163 public senf::pool_alloc_mixin< ProtocolSocketBody<SProtocol> > 00164 { 00165 public: 00166 typedef SProtocol Protocol; 00167 00168 using senf::pool_alloc_mixin< ProtocolSocketBody<SProtocol> >::operator new; 00169 using senf::pool_alloc_mixin< ProtocolSocketBody<SProtocol> >::operator delete; 00170 00171 ProtocolSocketBody(bool isServer); 00173 ProtocolSocketBody(bool isServer, int fd); 00178 private: 00179 virtual SocketProtocol const & v_protocol() const; 00180 virtual std::string v_protocolName() const; 00181 00182 friend class ConcreteSocketProtocol<typename SProtocol::Policy, SProtocol>; 00183 }; 00184 00185 } 00186 00187 //-///////////////////////////////////////////////////////////////////////////////////////////////// 00188 #endif 00189 00190 00191 // Local Variables: 00192 // mode: c++ 00193 // fill-column: 100 00194 // c-file-style: "senf" 00195 // indent-tabs-mode: nil 00196 // ispell-local-dictionary: "american" 00197 // compile-command: "scons -u test" 00198 // comment-column: 40 00199 // End: