00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00027 #include "SocketHandle.ih"
00028
00029
00030 #include <typeinfo>
00031 #include <senf/Utils/senfassert.hh>
00032 #include <senf/Utils/TypeInfo.hh>
00033
00034 #define prefix_ inline
00035
00036
00037
00038
00039
00040 template <class SPolicy>
00041 prefix_ senf::SocketHandle<SPolicy>::SocketHandle()
00042 {}
00043
00044 template <class SPolicy>
00045 template <class OtherPolicy>
00046 prefix_ senf::SocketHandle<SPolicy>::SocketHandle(SocketHandle<OtherPolicy> other,
00047 typename IsCompatible<OtherPolicy>::type *)
00048 : FileHandle(other)
00049 {}
00050
00051 template <class SPolicy>
00052 template <class OtherPolicy>
00053 prefix_ typename senf::SocketHandle<SPolicy>::template IsCompatible<OtherPolicy>::type const &
00054 senf::SocketHandle<SPolicy>::operator=(SocketHandle<OtherPolicy> other)
00055 {
00056 assign(other);
00057 return *this;
00058 }
00059
00060 template <class SPolicy>
00061 prefix_
00062 senf::SocketHandle<SPolicy>::SocketHandle(std::auto_ptr<SocketBody> body)
00063 : FileHandle(std::auto_ptr<FileBody>(body.release()))
00064 {}
00065
00066 template <class SPolicy>
00067 prefix_ senf::SocketHandle<SPolicy>::SocketHandle(FileHandle other, bool isChecked)
00068 : FileHandle(other)
00069 {
00070 SENF_ASSERT( isChecked, "Internal failure: Wrong overload called !!" );
00071 SENF_ASSERT( ! valid() || dynamic_cast<SocketBody *>(&FileHandle::body()),
00072 "Internal failure: Replacing or re-assigning non-empty incompatible handle");
00073 }
00074
00075 template <class SPolicy>
00076 prefix_ senf::SocketBody & senf::SocketHandle<SPolicy>::body()
00077 {
00078 SENF_ASSERT( dynamic_cast<SocketBody *>(&FileHandle::body()),
00079 "Internal failure: Invalid body found it's way into SocketHandle");
00080 return static_cast<SocketBody &>(FileHandle::body());
00081 }
00082
00083 template <class SPolicy>
00084 prefix_ senf::SocketBody const & senf::SocketHandle<SPolicy>::body()
00085 const
00086 {
00087 SENF_ASSERT( dynamic_cast<SocketBody const *>(&FileHandle::body()),
00088 "Internal failure: Invalid body found it's way into SocketHandle");
00089 return static_cast<SocketBody const &>(FileHandle::body());
00090 }
00091
00092 template <class SPolicy>
00093 prefix_ senf::SocketProtocol & senf::SocketHandle<SPolicy>::protocol()
00094 const
00095 {
00096 return body().protocol();
00097 }
00098
00099 template <class SPolicy>
00100 prefix_ void senf::SocketHandle<SPolicy>::assign(FileHandle other)
00101 {
00102 FileHandle::operator=(other);
00103 }
00104
00105 template <class SPolicy>
00106 prefix_ senf::SocketHandle<SPolicy>
00107 senf::SocketHandle<SPolicy>::cast_static(FileHandle handle)
00108 {
00109 return SocketHandle(handle,true);
00110 }
00111
00112 template <class SPolicy>
00113 prefix_ senf::SocketHandle<SPolicy>
00114 senf::SocketHandle<SPolicy>::cast_dynamic(FileHandle handle)
00115 {
00116
00117 SocketBody & body (dynamic_cast<SocketBody&>(FileHandle::body(handle)));
00118
00119 SPolicy::checkBaseOf(body.protocol().policy());
00120 return cast_static(handle);
00121 }
00122
00123 template <class Target, class Source>
00124 prefix_ Target senf::static_socket_cast(Source handle)
00125 {
00126 BOOST_STATIC_ASSERT((
00127 boost::is_convertible<Source*,FileHandle*>::value &&
00128 boost::is_convertible<Target*,FileHandle*>::value &&
00129 ( boost::is_convertible<Source,Target>::value ||
00130 boost::is_convertible<Target,Source>::value ) ));
00131 SENF_ASSERT( check_socket_cast<Target>(handle),
00132 "Invalid static_socket_cast" );
00133 return Target::cast_static(handle);
00134 }
00135
00136 template <class Target, class Source>
00137 prefix_ Target senf::dynamic_socket_cast(Source handle)
00138 {
00139
00140
00141
00142
00143
00144 try {
00145 return Target::cast_dynamic(handle);
00146 }
00147 SENF_WRAP_EXC(std::bad_cast)
00148 }
00149
00150 template <class Target, class Source>
00151 prefix_ bool senf::check_socket_cast(Source handle)
00152 {
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164 try {
00165 Target::cast_dynamic(handle);
00166 }
00167 catch (std::bad_cast const &) {
00168 return false;
00169 }
00170 return true;
00171 }
00172
00173 template <class SPolicy>
00174 prefix_ void senf::SocketHandle<SPolicy>::state(SocketStateMap & map, unsigned lod)
00175 {
00176
00177
00178
00179
00180
00181 map["handle"] << prettyName(typeid(*this));
00182 if (valid()) {
00183 map["valid"] << "true";
00184 body().state(map,lod);
00185 } else
00186 map["valid"] << "false";
00187 }
00188
00189 template <class SPolicy>
00190 prefix_ std::string senf::SocketHandle<SPolicy>::dumpState(unsigned lod)
00191 {
00192 SocketStateMap map;
00193 state(map,lod);
00194 return detail::dumpState(map);
00195 }
00196
00197 template <class SPolicy>
00198 template <class Facet>
00199 prefix_ Facet & senf::SocketHandle<SPolicy>::facet()
00200
00201 {
00202 try {
00203 return dynamic_cast<Facet &>(protocol());
00204 }
00205 SENF_WRAP_EXC(std::bad_cast)
00206 }
00207
00208
00209
00210
00211 template <class SProtocol>
00212 prefix_ senf::ProtocolSocketBody<SProtocol>::ProtocolSocketBody(bool isServer)
00213 : SocketBody(isServer)
00214 {}
00215
00216 template <class SProtocol>
00217 prefix_ senf::ProtocolSocketBody<SProtocol>::ProtocolSocketBody(bool isServer, int fd)
00218 : SocketBody(isServer, fd)
00219 {}
00220
00221
00222
00223 template <class SPolicy>
00224 prefix_ std::ostream & senf::operator<<(std::ostream & os, SocketHandle<SPolicy> handle)
00225 {
00226 os << handle.dumpState();
00227 return os;
00228 }
00229
00230
00231 #undef prefix_
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242