ServerSocketHandle.cti
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
6 //
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
11 //
12 
13 
14 /** \file
15  \brief ServerSocketHandle inline template implementation
16  */
17 
18 // Definition of inline template functions
19 
20 //#include "ServerSocketHandle.ih"
21 
22 // Custom includes
23 #include <typeinfo>
24 
25 #define prefix_ inline
26 //-/////////////////////////////////////////////////////////////////////////////////////////////////
27 
28 template <class SPolicy>
29 prefix_ senf::ServerSocketHandle<SPolicy>::ServerSocketHandle()
30 {}
31 
32 template <class SPolicy>
33 template <class OtherPolicy>
34 prefix_ senf::ServerSocketHandle<SPolicy>::
35 ServerSocketHandle(ServerSocketHandle<OtherPolicy> other,
36  typename SocketHandle<SPolicy>::template IsCompatible<OtherPolicy>::type *)
37  : SocketHandle<SPolicy>(other,true)
38 {}
39 
40 template <class SPolicy>
41 prefix_ senf::ServerSocketHandle<SPolicy>::
42 ServerSocketHandle(std::unique_ptr<SocketBody> body)
43  : SocketHandle<SPolicy>(std::move(body))
44 {}
45 
46 template <class SPolicy>
47 template <class OtherPolicy>
48 prefix_ typename senf::SocketHandle<SPolicy>::template IsCompatible<OtherPolicy>::type const &
49 senf::ServerSocketHandle<SPolicy>::operator=(ServerSocketHandle<OtherPolicy> other)
50 {
51  assign(other);
52  return *this;
53 }
54 
55 //-/////////////////////////////////////////////////////////////////////////////////////////////////
56 // Server socket interface
57 
58 template <class SPolicy>
59 prefix_ void senf::ServerSocketHandle<SPolicy>::bind(AddressParam addr)
60 {
61  SPolicy::AddressingPolicy::bind(*this,addr);
62 }
63 
64 template <class SPolicy>
65 prefix_ void senf::ServerSocketHandle<SPolicy>::listen(unsigned backlog)
66 {
67  SPolicy::CommunicationPolicy::listen(*this,backlog);
68 }
69 
70 template <class SPolicy>
71 prefix_ typename senf::ServerSocketHandle<SPolicy>::Address
72 senf::ServerSocketHandle<SPolicy>::local()
73 {
74  typename SPolicy::AddressingPolicy::Address addr;
75  this->local(addr);
76  return addr;
77 }
78 
79 template <class SPolicy>
80 prefix_ void senf::ServerSocketHandle<SPolicy>::local(Address & addr)
81 {
82  SPolicy::AddressingPolicy::local(*this,addr);
83 }
84 
85 template <class SPolicy>
86 prefix_ typename senf::ServerSocketHandle<SPolicy>::ClientHandle
87 senf::ServerSocketHandle<SPolicy>::accept()
88 {
89  return ClientHandle(this->body().clone(
90  SPolicy::CommunicationPolicy::accept(*this), false));
91 }
92 
93 template <class SPolicy>
94 prefix_ std::pair<typename senf::ServerSocketHandle<SPolicy>::ClientHandle,
95  typename senf::ServerSocketHandle<SPolicy>::Address>
96 senf::ServerSocketHandle<SPolicy>::acceptfrom()
97 {
98 
99  Address address;
100  ClientHandle handle = acceptfrom(address);
101  return std::make_pair(handle,address);
102 }
103 
104 template <class SPolicy>
105 prefix_ typename senf::ServerSocketHandle<SPolicy>::ClientHandle
106 senf::ServerSocketHandle<SPolicy>::acceptfrom(Address & addr)
107 {
108  return ClientHandle(this->body().clone(
109  SPolicy::CommunicationPolicy::accept(*this,addr), false));
110 }
111 
112 //-/////////////////////////////////////////////////////////////////////////////////////////////////
113 
114 template <class SPolicy>
115 prefix_ senf::ServerSocketHandle<SPolicy>::ServerSocketHandle(FileHandle other,
116  bool isChecked)
117  : SocketHandle<SPolicy>(other, isChecked)
118 {}
119 
120 template <class SPolicy>
121 prefix_ senf::ServerSocketHandle<SPolicy>
122 senf::ServerSocketHandle<SPolicy>::cast_static(FileHandle handle)
123 {
124  return ServerSocketHandle(handle,true);
125 }
126 
127 template <class SPolicy>
128 prefix_ senf::ServerSocketHandle<SPolicy>
129 senf::ServerSocketHandle<SPolicy>::cast_dynamic(FileHandle handle)
130 {
131  SocketHandle<SPolicy> h (SocketHandle<SPolicy>::cast_dynamic(handle));
132  if (! static_cast<SocketBody&>(FileHandle::body(handle)).isServer())
133  throw std::bad_cast();
134  return cast_static(handle);
135 }
136 
137 template <class SPolicy>
138 prefix_ void senf::ServerSocketHandle<SPolicy>::state(SocketStateMap & map, unsigned lod)
139 {
140  map["handle"] = prettyName(typeid(*this));
141  if (this->valid()) {
142  map["valid"] << "true";
143  this->body().state(map,lod);
144  } else
145  map["valid"] << "false";
146 }
147 
148 template <class SPolicy>
149 prefix_ std::string senf::ServerSocketHandle<SPolicy>::dumpState(unsigned lod)
150 {
151  SocketStateMap map;
152  state(map,lod);
153  return detail::dumpState(map);
154 }
155 
156 //-/////////////////////////////////////////////////////////////////////////////////////////////////
157 #undef prefix_
158 
159 
160 // Local Variables:
161 // mode: c++
162 // fill-column: 100
163 // c-file-style: "senf"
164 // indent-tabs-mode: nil
165 // ispell-local-dictionary: "american"
166 // compile-command: "scons -u test"
167 // comment-column: 40
168 // End: