Kea: io_asio_socket.h Source File

| Kea 3.2.0-git

io_asio_socket.h
Go to the documentation of this file.
1// Copyright (C) 2010-2025 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
16#include <exceptions/exceptions.h>
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#include <boost/asio/coroutine.hpp>
27
28namespace isc {
29namespace asiolink {
30
34 class SocketNotOpen : public IOError {
35public:
36 SocketNotOpen(const char* file, size_t line, const char* what) :
37 IOError(file, line, what) {}
38};
39
43 class SocketSetError : public IOError {
44public:
45 SocketSetError(const char* file, size_t line, const char* what) :
46 IOError(file, line, what) {}
47};
48
53 class BufferOverflow : public IOError {
54public:
55 BufferOverflow(const char* file, size_t line, const char* what) :
56 IOError(file, line, what) {}
57};
58
60class IOEndpoint;
61
62
80
81template <typename C>
82 class IOAsioSocket : public IOSocket {
83
89
90private:
91 IOAsioSocket(const IOAsioSocket<C>& source);
92 IOAsioSocket& operator=(const IOAsioSocket<C>& source);
93protected:
98 IOAsioSocket() {}
99public:
101 virtual ~IOAsioSocket() = default;
103
123 virtual int getNative() const = 0;
124
133 virtual int getProtocol() const = 0;
134
161 virtual bool isOpenSynchronous() const = 0;
162
175 virtual void open(const IOEndpoint* endpoint, C& callback) = 0;
176
187 virtual void asyncSend(const void* data, size_t length,
188 const IOEndpoint* endpoint, C& callback) = 0;
189
206 virtual void asyncReceive(void* data, size_t length, size_t offset,
207 IOEndpoint* endpoint, C& callback) = 0;
208
263 virtual bool processReceivedData(const void* staging, size_t length,
264 size_t& cumulative,
265 size_t& offset, size_t& expected,
266 isc::util::OutputBufferPtr& buff) = 0;
267
269 virtual void cancel() = 0;
270
272 virtual void close() = 0;
273};
274
275
284
285template <typename C>
286 class DummyAsioSocket : public IOAsioSocket<C> {
287private:
288 DummyAsioSocket(const DummyAsioSocket<C>& source);
289 DummyAsioSocket& operator=(const DummyAsioSocket<C>& source);
290public:
297 DummyAsioSocket(const int protocol) : protocol_(protocol) {}
298
303 virtual int getNative() const { return (-1); }
304
308 virtual int getProtocol() const { return (protocol_); }
309
310
314 bool isOpenSynchronous() const {
315 return true;
316 }
317
325 virtual bool open(const IOEndpoint*, C&) {
326 return (false);
327 }
328
333 virtual void asyncSend(const void*, size_t, const IOEndpoint*, C&) {
334 }
335
340 virtual void asyncReceive(void*, size_t, size_t, IOEndpoint*, C&) {
341 }
342
347 virtual bool receiveComplete(const void*, size_t, size_t&, size_t&,
348 size_t&, isc::util::OutputBufferPtr&)
349 {
350 return (true);
351 }
352
353
357 virtual void cancel() {
358 }
359
363 virtual void close() {
364 }
365
366private:
367 const int protocol_;
368};
369
370} // namespace asiolink
371} // namespace isc
372
373#endif // IO_ASIO_SOCKET_H
buffer.h
isc::Exception::what
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Definition exceptions/exceptions.cc:32
isc::asiolink::BufferOverflow::BufferOverflow
BufferOverflow(const char *file, size_t line, const char *what)
Definition io_asio_socket.h:55
isc::asiolink::DummyAsioSocket::open
virtual bool open(const IOEndpoint *, C &)
Open AsioSocket.
Definition io_asio_socket.h:325
isc::asiolink::DummyAsioSocket::receiveComplete
virtual bool receiveComplete(const void *, size_t, size_t &, size_t &, size_t &, isc::util::OutputBufferPtr &)
Checks if the data received is complete.
Definition io_asio_socket.h:347
isc::asiolink::DummyAsioSocket::getProtocol
virtual int getProtocol() const
A dummy derived method of IOAsioSocket::getProtocol().
Definition io_asio_socket.h:308
isc::asiolink::DummyAsioSocket::asyncReceive
virtual void asyncReceive(void *, size_t, size_t, IOEndpoint *, C &)
Receive Asynchronously.
Definition io_asio_socket.h:340
isc::asiolink::DummyAsioSocket::cancel
virtual void cancel()
Cancel I/O On AsioSocket.
Definition io_asio_socket.h:357
isc::asiolink::DummyAsioSocket::DummyAsioSocket
DummyAsioSocket(const int protocol)
Constructor from the protocol number.
Definition io_asio_socket.h:297
isc::asiolink::DummyAsioSocket::asyncSend
virtual void asyncSend(const void *, size_t, const IOEndpoint *, C &)
Send Asynchronously.
Definition io_asio_socket.h:333
isc::asiolink::DummyAsioSocket::close
virtual void close()
Close socket.
Definition io_asio_socket.h:363
isc::asiolink::DummyAsioSocket::getNative
virtual int getNative() const
A dummy derived method of IOAsioSocket::getNative().
Definition io_asio_socket.h:303
isc::asiolink::DummyAsioSocket::isOpenSynchronous
bool isOpenSynchronous() const
Is socket opening synchronous?
Definition io_asio_socket.h:314
isc::asiolink::IOAsioSocket::close
virtual void close()=0
Close socket.
isc::asiolink::IOAsioSocket::getProtocol
virtual int getProtocol() const =0
Return the transport protocol of the socket.
isc::asiolink::IOAsioSocket::open
virtual void open(const IOEndpoint *endpoint, C &callback)=0
Open AsioSocket.
isc::asiolink::IOAsioSocket::cancel
virtual void cancel()=0
Cancel I/O On AsioSocket.
isc::asiolink::IOAsioSocket::~IOAsioSocket
virtual ~IOAsioSocket()=default
The destructor.
isc::asiolink::IOAsioSocket::IOAsioSocket
IOAsioSocket()
The default constructor.
Definition io_asio_socket.h:98
isc::asiolink::IOAsioSocket::isOpenSynchronous
virtual bool isOpenSynchronous() const =0
Is Open() synchronous?
isc::asiolink::IOAsioSocket::processReceivedData
virtual bool processReceivedData(const void *staging, size_t length, size_t &cumulative, size_t &offset, size_t &expected, isc::util::OutputBufferPtr &buff)=0
Processes received data.
isc::asiolink::IOAsioSocket::asyncSend
virtual void asyncSend(const void *data, size_t length, const IOEndpoint *endpoint, C &callback)=0
Send Asynchronously.
isc::asiolink::IOAsioSocket::getNative
virtual int getNative() const =0
Return the "native" representation of the socket.
isc::asiolink::IOAsioSocket::asyncReceive
virtual void asyncReceive(void *data, size_t length, size_t offset, IOEndpoint *endpoint, C &callback)=0
Receive Asynchronously.
isc::asiolink::IOEndpoint
The IOEndpoint class is an abstract base class to represent a communication endpoint.
Definition io_endpoint.h:38
isc::asiolink::IOError::IOError
IOError(const char *file, size_t line, const char *what)
Definition io_error.h:21
isc::asiolink::SocketNotOpen::SocketNotOpen
SocketNotOpen(const char *file, size_t line, const char *what)
Definition io_asio_socket.h:36
isc::asiolink::SocketSetError::SocketSetError
SocketSetError(const char *file, size_t line, const char *what)
Definition io_asio_socket.h:45
exceptions.h
io_error.h
io_socket.h
isc::asiolink
Definition addr_utilities.cc:170
isc::data
Definition base_stamped_element.cc:12
isc::util::OutputBufferPtr
boost::shared_ptr< OutputBuffer > OutputBufferPtr
Type of pointers to output buffers.
Definition buffer.h:574
isc
Defines the logger used by the top-level component of kea-lfc.
Definition check_exists_add.cc:18