SocketProtocol.hh
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 
18 // The private inheritance idea should indeed work very well: We just need to change the
19 // implementations of body() and protocol() and that of the ProtocolClient/ServerSocketHandle
20 // constructors and the SocketBody constructor. The body and the protocol would still be visible
21 // like several instances because of the private inheritance but we would save the backwards
22 // pointer.
23 
71 #ifndef HH_SENF_Socket_SocketProtocol_
72 #define HH_SENF_Socket_SocketProtocol_ 1
73 
74 // Custom includes
75 #include <boost/noncopyable.hpp>
76 // Hrmpf ... I have tried very hard, but I just can't find a nice, generic way to clean
77 // up this include
78 #include "SocketHandle.ih"
79 
80 //#include "SocketProtocol.mpp"
81 //-/////////////////////////////////////////////////////////////////////////////////////////////////
82 
83 namespace senf {
84 
86  //\{
87 
88  struct SocketPolicyBase;
89 
97  class SocketProtocol
98  : boost::noncopyable
99  {
100  public:
101  //-////////////////////////////////////////////////////////////////////////
102  // Types
103 
104  //-////////////////////////////////////////////////////////////////////////
106  //\{
107 
108  SocketProtocol();
109  virtual ~SocketProtocol() = 0;
110 
111  // default default constructor
112  // no copy
113  // no conversion constructors
114 
115  //\}
116  //-////////////////////////////////////////////////////////////////////////
117 
118  virtual SocketPolicyBase const & policy() const = 0;
120 
121  //-////////////////////////////////////////////////////////////////////////
122  // Virtual interface
123 
124  virtual unsigned available() const = 0;
127 
143  virtual bool eof() const = 0;
144 
150  virtual void close();
151 
156  virtual void terminate() const;
157 
165  virtual void state(SocketStateMap & map, unsigned lod) const;
167 
204  int fd() const;
205 
209  protected:
210  FileHandle fh() const;
211 
217  void fd(int) const;
218 
224  private:
225  virtual std::unique_ptr<SocketBody> clone(bool isServer) const = 0;
226  virtual std::unique_ptr<SocketBody> clone(int fd, bool isServer) const = 0;
227  virtual SocketBody & body() const = 0;
229  friend class SocketBody;
230  };
231 
232  template <class SPolicy> class ClientSocketHandle;
233  template <class SPolicy> class ServerSocketHandle;
234 
253  template <class SocketPolicy, class Self>
255  : public virtual SocketProtocol
256  {
257  public:
258  //-////////////////////////////////////////////////////////////////////////
259  // Types
261  typedef SocketPolicy Policy;
262 
263  //-////////////////////////////////////////////////////////////////////////
265  //\{
266 
267  ~ConcreteSocketProtocol() = 0;
268 
269  // no default constructor
270  // no copy
271  // no conversion constructors
272 
273  //\}
274  //-////////////////////////////////////////////////////////////////////////
275 
276  Policy const & policy() const;
277 
278  protected:
279  ClientSocketHandle<Policy> clientHandle() const;
281 
283  ServerSocketHandle<Policy> serverHandle() const;
285 
288  private:
289  virtual std::unique_ptr<SocketBody> clone(bool isServer) const;
290  virtual std::unique_ptr<SocketBody> clone(int fd, bool isServer) const;
291  virtual SocketBody & body() const;
292 
293  Policy policy_;
294  };
295 
296  //\}
297 }
298 
299 //-/////////////////////////////////////////////////////////////////////////////////////////////////
300 #include "SocketProtocol.cci"
301 //#include "SocketProtocol.ct"
302 #include "SocketProtocol.cti"
303 #endif
304 
305 
306 // Local Variables:
307 // mode: c++
308 // fill-column: 100
309 // c-file-style: "senf"
310 // indent-tabs-mode: nil
311 // ispell-local-dictionary: "american"
312 // compile-command: "scons -u test"
313 // comment-column: 40
314 // End:
virtual bool eof() const =0
Check for end-of-file condition.
Generic SocketHandle with client interface.
virtual SocketPolicyBase const & policy() const =0
Access the policy instance.
Generic SocketHandle with server interface.
int fd() const
Get file descriptor.
Socket Protocol base class.
virtual void state(SocketStateMap &map, unsigned lod) const
Return socket state information.
virtual unsigned available() const =0
Return (maximum) number of bytes available for reading without < blocking.
FileHandle fh() const
Get a FileHandle for this instance.
Concrete Socket Protocol implementation base class.
Collection of policy classes.
virtual void close()
Close socket.
virtual void terminate() const
Forcibly close socket.
virtual ~SocketProtocol()=0