Kea  1.9.9-git
io_asio_socket.h
Go to the documentation of this file.
1 // Copyright (C) 2010-2018 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #ifndef IO_ASIO_SOCKET_H
8 #define IO_ASIO_SOCKET_H 1
9 
10 // IMPORTANT NOTE: only very few ASIO headers files can be included in
11 // this file. In particular, asio.hpp should never be included here.
12 // See the description of the namespace below.
13 #include <config.h>
14 
15 #include <unistd.h> // for some network system calls
16 
17 #include <functional>
18 #include <string>
19 
20 #include <exceptions/exceptions.h>
21 
22 #include <util/buffer.h>
23 
24 #include <asiolink/io_error.h>
25 #include <asiolink/io_socket.h>
26 
27 // We want to use coroutine.hpp from the system's boost headers if possible.
28 // However, very old Boost versions (provided by RHEL 7 or CentOS 7) didn't have
29 // this header. So we can resort to our bundled version, but only if necessary.
30 #ifndef HAVE_BOOST_ASIO_COROUTINE_HPP
31 #include <ext/coroutine/coroutine.hpp>
32 #else
33 #include <boost/asio/coroutine.hpp>
34 #endif
35 
36 namespace isc {
37 namespace asiolink {
38 
42 class SocketNotOpen : public IOError {
43 public:
44  SocketNotOpen(const char* file, size_t line, const char* what) :
45  IOError(file, line, what) {}
46 };
47 
51 class SocketSetError : public IOError {
52 public:
53  SocketSetError(const char* file, size_t line, const char* what) :
54  IOError(file, line, what) {}
55 };
56 
61 class BufferOverflow : public IOError {
62 public:
63  BufferOverflow(const char* file, size_t line, const char* what) :
64  IOError(file, line, what) {}
65 };
66 
68 class IOEndpoint;
69 
70 
88 
89 template <typename C>
90 class IOAsioSocket : public IOSocket {
91 
97 
98 private:
99  IOAsioSocket(const IOAsioSocket<C>& source);
100  IOAsioSocket& operator=(const IOAsioSocket<C>& source);
101 protected:
107 public:
109  virtual ~IOAsioSocket() {}
111 
131  virtual int getNative() const = 0;
132 
141  virtual int getProtocol() const = 0;
142 
169  virtual bool isOpenSynchronous() const = 0;
170 
183  virtual void open(const IOEndpoint* endpoint, C& callback) = 0;
184 
195  virtual void asyncSend(const void* data, size_t length,
196  const IOEndpoint* endpoint, C& callback) = 0;
197 
214  virtual void asyncReceive(void* data, size_t length, size_t offset,
215  IOEndpoint* endpoint, C& callback) = 0;
216 
271  virtual bool processReceivedData(const void* staging, size_t length,
272  size_t& cumulative, size_t& offset,
273  size_t& expected,
274  isc::util::OutputBufferPtr& outbuff) = 0;
275 
277  virtual void cancel() = 0;
278 
280  virtual void close() = 0;
281 };
282 
283 
292 
293 template <typename C>
294 class DummyAsioSocket : public IOAsioSocket<C> {
295 private:
296  DummyAsioSocket(const DummyAsioSocket<C>& source);
297  DummyAsioSocket& operator=(const DummyAsioSocket<C>& source);
298 public:
305  DummyAsioSocket(const int protocol) : protocol_(protocol) {}
306 
311  virtual int getNative() const { return (-1); }
312 
316  virtual int getProtocol() const { return (protocol_); }
317 
318 
322  bool isOpenSynchronous() const {
323  return true;
324  }
325 
333  virtual bool open(const IOEndpoint*, C&) {
334  return (false);
335  }
336 
341  virtual void asyncSend(const void*, size_t, const IOEndpoint*, C&) {
342  }
343 
348  virtual void asyncReceive(void*, size_t, size_t, IOEndpoint*, C&) {
349  }
350 
355  virtual bool receiveComplete(const void*, size_t, size_t&, size_t&,
356  size_t&, isc::util::OutputBufferPtr&)
357  {
358  return (true);
359  }
360 
361 
365  virtual void cancel() {
366  }
367 
371  virtual void close() {
372  }
373 
374 private:
375  const int protocol_;
376 };
377 
378 } // namespace asiolink
379 } // namespace isc
380 
381 #endif // IO_ASIO_SOCKET_H
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Defines the logger used by the top-level component of kea-dhcp-ddns.
boost::shared_ptr< OutputBuffer > OutputBufferPtr
Definition: buffer.h:599