Kea 2.7.0
io_asio_socket.h
Go to the documentation of this file.
1// Copyright (C) 2010-2024 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
17#include <asiolink/io_error.h>
18#include <asiolink/io_socket.h>
19#include <util/buffer.h>
20
21#include <functional>
22#include <string>
23
24#include <unistd.h> // for some network system calls
25
26// We want to use coroutine.hpp from the system's boost headers if possible.
27// However, very old Boost versions (provided by RHEL 7 or CentOS 7) didn't have
28// this header. So we can resort to our bundled version, but only if necessary.
29#ifndef HAVE_BOOST_ASIO_COROUTINE_HPP
30#include <ext/coroutine/coroutine.hpp>
31#else
32#include <boost/asio/coroutine.hpp>
33#endif
34
35namespace isc {
36namespace asiolink {
37
41class SocketNotOpen : public IOError {
42public:
43 SocketNotOpen(const char* file, size_t line, const char* what) :
44 IOError(file, line, what) {}
45};
46
50class SocketSetError : public IOError {
51public:
52 SocketSetError(const char* file, size_t line, const char* what) :
53 IOError(file, line, what) {}
54};
55
60class BufferOverflow : public IOError {
61public:
62 BufferOverflow(const char* file, size_t line, const char* what) :
63 IOError(file, line, what) {}
64};
65
67class IOEndpoint;
68
69
87
88template <typename C>
89class IOAsioSocket : public IOSocket {
90
96
97private:
98 IOAsioSocket(const IOAsioSocket<C>& source);
99 IOAsioSocket& operator=(const IOAsioSocket<C>& source);
100protected:
106public:
108 virtual ~IOAsioSocket() = default;
110
130 virtual int getNative() const = 0;
131
140 virtual int getProtocol() const = 0;
141
168 virtual bool isOpenSynchronous() const = 0;
169
182 virtual void open(const IOEndpoint* endpoint, C& callback) = 0;
183
194 virtual void asyncSend(const void* data, size_t length,
195 const IOEndpoint* endpoint, C& callback) = 0;
196
213 virtual void asyncReceive(void* data, size_t length, size_t offset,
214 IOEndpoint* endpoint, C& callback) = 0;
215
270 virtual bool processReceivedData(const void* staging, size_t length,
271 size_t& cumulative,
272 size_t& offset, size_t& expected,
274
276 virtual void cancel() = 0;
277
279 virtual void close() = 0;
280};
281
282
291
292template <typename C>
293class DummyAsioSocket : public IOAsioSocket<C> {
294private:
295 DummyAsioSocket(const DummyAsioSocket<C>& source);
296 DummyAsioSocket& operator=(const DummyAsioSocket<C>& source);
297public:
304 DummyAsioSocket(const int protocol) : protocol_(protocol) {}
305
310 virtual int getNative() const { return (-1); }
311
315 virtual int getProtocol() const { return (protocol_); }
316
317
321 bool isOpenSynchronous() const {
322 return true;
323 }
324
332 virtual bool open(const IOEndpoint*, C&) {
333 return (false);
334 }
335
340 virtual void asyncSend(const void*, size_t, const IOEndpoint*, C&) {
341 }
342
347 virtual void asyncReceive(void*, size_t, size_t, IOEndpoint*, C&) {
348 }
349
354 virtual bool receiveComplete(const void*, size_t, size_t&, size_t&,
356 {
357 return (true);
358 }
359
360
364 virtual void cancel() {
365 }
366
370 virtual void close() {
371 }
372
373private:
374 const int protocol_;
375};
376
377} // namespace asiolink
378} // namespace isc
379
380#endif // IO_ASIO_SOCKET_H
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
boost::shared_ptr< OutputBuffer > OutputBufferPtr
Type of pointers to output buffers.
Definition buffer.h:571
Defines the logger used by the top-level component of kea-lfc.