11#error "asio.hpp must be included before including this, see asiolink.h as to why"
25#include <boost/numeric/conversion/cast.hpp>
27#include <netinet/in.h>
28#include <sys/socket.h>
76#if BOOST_VERSION < 106600
77 return (socket_.native());
79 return (socket_.native_handle());
105 if (socket_.is_open()) {
107 const bool non_blocking_orig = socket_.non_blocking();
110 socket_.non_blocking(
true);
112 boost::system::error_code ec;
117 socket_.receive(boost::asio::buffer(data,
sizeof(data)),
118 boost::asio::socket_base::message_peek,
122 socket_.non_blocking(non_blocking_orig);
130 return (!ec || (ec.value() == boost::asio::error::try_again) ||
131 (ec.value() == boost::asio::error::would_block));
173 void asyncSend(
const void* data,
size_t length, C& callback);
205 size_t& cumulative,
size_t& offset,
232 std::unique_ptr<boost::asio::ip::tcp::socket> socket_ptr_;
235 boost::asio::ip::tcp::socket& socket_;
259 socket_ptr_(), socket_(socket), send_buffer_() {
266 socket_ptr_(new
boost::asio::ip::tcp::socket(io_service_->getInternalIOService())),
267 socket_(*socket_ptr_) {
279template <
typename C>
void
283 if (socket_.is_open() && !isUsable()) {
289 if (!socket_.is_open()) {
290 if (endpoint->getFamily() == AF_INET) {
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));
314 socket_.async_connect(tcp_endpoint->getASIOEndpoint(), callback);
320template <
typename C>
void
322 if (socket_.is_open()) {
326 send_buffer_->writeData(data, length);
329 socket_.async_send(boost::asio::buffer(send_buffer_->getData(),
330 send_buffer_->getLength()),
332 }
catch (
const boost::numeric::bad_numeric_cast&) {
334 "attempt to send buffer larger than 64kB");
339 "attempt to send on a TCP socket that is not open");
343template <
typename C>
void
346 if (socket_.is_open()) {
353 uint16_t count = boost::numeric_cast<uint16_t>(length);
357 send_buffer_->writeUint16(count);
358 send_buffer_->writeData(data, length);
361 socket_.async_send(boost::asio::buffer(send_buffer_->getData(),
362 send_buffer_->getLength()), callback);
363 }
catch (
const boost::numeric::bad_numeric_cast&) {
365 "attempt to send buffer larger than 64kB");
370 "attempt to send on a TCP socket that is not open");
377template <
typename C>
void
380 if (socket_.is_open()) {
399 if (offset >= length) {
401 "TCP receive buffer");
403 void* buffer_start =
static_cast<void*
>(
static_cast<uint8_t*
>(data) + offset);
406 socket_.async_receive(boost::asio::buffer(buffer_start, length - offset), callback);
410 "attempt to receive from a TCP socket that is not open");
416template <
typename C>
bool
418 size_t& cumulative,
size_t& offset,
422 const uint8_t* data =
static_cast<const uint8_t*
>(staging);
423 size_t data_length = length;
427 if (cumulative < 2) {
431 cumulative += length;
432 if (cumulative < 2) {
451 data_length = cumulative - 2;
455 cumulative += length;
465 if (expected >= outbuff->getLength()) {
469 size_t copy_amount = std::min(expected - outbuff->getLength(), data_length);
470 outbuff->writeData(data, copy_amount);
474 return (expected == outbuff->getLength());
479template <
typename C>
void
481 if (socket_.is_open()) {
489template <
typename C>
void
491 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.
An exception that is thrown if an error occurs within the IO module.
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.
TCPSocket(const IOServicePtr &service)
Constructor.
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.
virtual bool processReceivedData(const void *staging, size_t length, size_t &cumulative, size_t &offset, size_t &expected, isc::util::OutputBufferPtr &buff)
Process received data packet.
virtual bool isOpenSynchronous() const
Is "open()" synchronous?
virtual ~TCPSocket()
Destructor.
void asyncSend(const void *data, size_t length, C &callback)
Send Asynchronously without count.
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< IOService > IOServicePtr
Defines a smart pointer to an IOService instance.
uint16_t readUint16(void const *const buffer, size_t const length)
uint16_t wrapper over readUint.
boost::shared_ptr< OutputBuffer > OutputBufferPtr
Type of pointers to output buffers.
Defines the logger used by the top-level component of kea-lfc.