11#error "asio.hpp must be included before including this, see asiolink.h as to why"
14#include <netinet/in.h>
15#include <sys/socket.h>
21#include <boost/numeric/conversion/cast.hpp>
78#if BOOST_VERSION < 106600
79 return (socket_.native());
81 return (socket_.native_handle());
107 if (socket_.is_open()) {
109 const bool non_blocking_orig = socket_.non_blocking();
112 socket_.non_blocking(
true);
114 boost::system::error_code ec;
119 socket_.receive(boost::asio::buffer(data,
sizeof(data)),
120 boost::asio::socket_base::message_peek,
124 socket_.non_blocking(non_blocking_orig);
132 return (!ec || (ec.value() == boost::asio::error::try_again) ||
133 (ec.value() == boost::asio::error::would_block));
175 void asyncSend(
const void* data,
size_t length, C& callback);
207 size_t& cumulative,
size_t& offset,
230 std::unique_ptr<boost::asio::ip::tcp::socket> socket_ptr_;
233 boost::asio::ip::tcp::socket& socket_;
257 socket_ptr_(), socket_(socket), send_buffer_()
265 socket_ptr_(new
boost::asio::ip::tcp::socket(service.get_io_service())),
266 socket_(*socket_ptr_)
279template <
typename C>
void
283 if (socket_.is_open() && !isUsable()) {
289 if (!socket_.is_open()) {
291 socket_.open(boost::asio::ip::tcp::v4());
293 socket_.open(boost::asio::ip::tcp::v6());
300 socket_.set_option(boost::asio::socket_base::reuse_address(
true));
320template <
typename C>
void
323 if (socket_.is_open()) {
327 send_buffer_->writeData(data, length);
330 socket_.async_send(boost::asio::buffer(send_buffer_->getData(),
331 send_buffer_->getLength()),
333 }
catch (
const boost::numeric::bad_numeric_cast&) {
335 "attempt to send buffer larger than 64kB");
340 "attempt to send on a TCP socket that is not open");
344template <
typename C>
void
348 if (socket_.is_open()) {
355 uint16_t count = boost::numeric_cast<uint16_t>(length);
359 send_buffer_->writeUint16(count);
360 send_buffer_->writeData(data, length);
363 socket_.async_send(boost::asio::buffer(send_buffer_->getData(),
364 send_buffer_->getLength()), callback);
365 }
catch (
const boost::numeric::bad_numeric_cast&) {
367 "attempt to send buffer larger than 64kB");
372 "attempt to send on a TCP socket that is not open");
379template <
typename C>
void
383 if (socket_.is_open()) {
402 if (offset >= length) {
404 "TCP receive buffer");
406 void* buffer_start =
static_cast<void*
>(
static_cast<uint8_t*
>(data) + offset);
409 socket_.async_receive(boost::asio::buffer(buffer_start, length - offset), callback);
413 "attempt to receive from a TCP socket that is not open");
419template <
typename C>
bool
421 size_t& cumulative,
size_t& offset,
426 const uint8_t* data =
static_cast<const uint8_t*
>(staging);
427 size_t data_length = length;
431 if (cumulative < 2) {
435 cumulative += length;
436 if (cumulative < 2) {
455 data_length = cumulative - 2;
459 cumulative += length;
469 if (expected >= outbuff->getLength()) {
473 size_t copy_amount = std::min(expected - outbuff->getLength(), data_length);
474 outbuff->writeData(data, copy_amount);
478 return (expected == outbuff->getLength());
483template <
typename C>
void
485 if (socket_.is_open()) {
493template <
typename C>
void
495 if (socket_.is_open() && socket_ptr_) {
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
I/O Socket with asynchronous operations.
The IOEndpoint class is an abstract base class to represent a communication endpoint.
virtual short getFamily() const =0
Returns the address family of the endpoint.
virtual short getProtocol() const =0
Returns the protocol number of the endpoint (TCP, UDP...)
An exception that is thrown if an error occurs within the IO module.
The IOService class is a wrapper for the ASIO io_service class.
The TCPEndpoint class is a concrete derived class of IOEndpoint that represents an endpoint of a TCP ...
const boost::asio::ip::tcp::endpoint & getASIOEndpoint() const
The TCPSocket class is a concrete derived class of IOAsioSocket that represents a TCP socket.
bool isUsable() const
Checks if the connection is usable.
virtual void close()
Close socket.
virtual boost::asio::ip::tcp::socket & getASIOSocket() const
Returns reference to the underlying ASIO socket.
virtual void cancel()
Cancel I/O On Socket.
virtual void open(const IOEndpoint *endpoint, C &callback)
Open Socket.
virtual int getProtocol() const
Return protocol of socket.
virtual void asyncSend(const void *data, size_t length, const IOEndpoint *endpoint, C &callback)
Send Asynchronously.
virtual void asyncReceive(void *data, size_t length, size_t offset, IOEndpoint *endpoint, C &callback)
Receive Asynchronously.
virtual int getNative() const
Return file descriptor of underlying socket.
TCPSocket(IOService &service)
Constructor.
virtual bool isOpenSynchronous() const
Is "open()" synchronous?
virtual ~TCPSocket()
Destructor.
void asyncSend(const void *data, size_t length, C &callback)
Send Asynchronously without count.
virtual bool processReceivedData(const void *staging, size_t length, size_t &cumulative, size_t &offset, size_t &expected, isc::util::OutputBufferPtr &outbuff)
Process received data packet.
TCPSocket(boost::asio::ip::tcp::socket &socket)
Constructor from an ASIO TCP socket.
The OutputBuffer class is a buffer abstraction for manipulating mutable data.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
#define isc_throw_assert(expr)
Replacement for assert() that throws if the expression is false.
A wrapper interface for the ASIO library.
boost::shared_ptr< OutputBuffer > OutputBufferPtr
uint16_t readUint16(const void *buffer, size_t length)
Read Unsigned 16-Bit Integer from Buffer.
Defines the logger used by the top-level component of kea-lfc.