11#error "asio.hpp must be included before including this, see asiolink.h as to why"
26#include <boost/numeric/conversion/cast.hpp>
28#include <netinet/in.h>
29#include <sys/socket.h>
49class TCPSocket :
public IOAsioSocket<C> {
52 TCPSocket(
const TCPSocket&);
53 TCPSocket& operator=(
const TCPSocket&);
62 TCPSocket(boost::asio::ip::tcp::socket& socket);
77 return (socket_.native_handle());
102 if (socket_.is_open()) {
104 const bool non_blocking_orig = socket_.non_blocking();
107 socket_.non_blocking(
true);
109 boost::system::error_code ec;
115 socket_.receive(boost::asio::buffer(
data,
sizeof(
data)),
116 boost::asio::socket_base::message_peek,
120 socket_.non_blocking(non_blocking_orig);
128 return (!ec || (ec.value() == boost::asio::error::try_again) ||
129 (ec.value() == boost::asio::error::would_block));
171 void asyncSend(
const void*
data,
size_t length, C& callback);
203 size_t& cumulative,
size_t& offset,
230 std::unique_ptr<boost::asio::ip::tcp::socket> socket_ptr_;
233 boost::asio::ip::tcp::socket& socket_;
242TCPSocket<C>::TCPSocket(boost::asio::ip::tcp::socket& socket) :
243 socket_ptr_(), socket_(socket), header_() {
249TCPSocket<C>::TCPSocket(
const IOServicePtr& io_service) : io_service_(io_service),
250 socket_ptr_(new boost::asio::ip::
tcp::socket(io_service_->getInternalIOService())),
251 socket_(*socket_ptr_) {
263template <
typename C>
void
267 if (socket_.is_open() && !
isUsable()) {
273 if (!socket_.is_open()) {
275 socket_.open(boost::asio::ip::tcp::v4());
277 socket_.open(boost::asio::ip::tcp::v6());
284 socket_.set_option(boost::asio::socket_base::reuse_address(
true));
309template <
typename C>
void
311 if (socket_.is_open()) {
313 socket_.async_send(boost::asio::buffer(
data, length), callback);
316 "attempt to send on a TCP socket that is not open");
320template <
typename C>
void
323 if (socket_.is_open()) {
329 uint16_t count = boost::numeric_cast<uint16_t>(length);
331 header_->writeUint16(count);
332 std::vector<boost::asio::const_buffer> buffers;
335 buffers.push_back(boost::asio::buffer(header_->getData(),
336 header_->getLength()));
337 buffers.push_back(boost::asio::buffer(
data, length));
340 socket_.async_send(buffers, callback);
341 }
catch (
const boost::numeric::bad_numeric_cast&) {
343 "attempt to send buffer larger than 64kB");
348 "attempt to send on a TCP socket that is not open");
355template <
typename C>
void
358 if (socket_.is_open()) {
377 if (offset >= length) {
379 "TCP receive buffer");
381 void* buffer_start =
static_cast<void*
>(
static_cast<uint8_t*
>(
data) + offset);
384 socket_.async_receive(boost::asio::buffer(buffer_start, length - offset), callback);
388 "attempt to receive from a TCP socket that is not open");
394template <
typename C>
bool
396 size_t& cumulative,
size_t& offset,
400 const uint8_t*
data =
static_cast<const uint8_t*
>(staging);
401 size_t data_length = length;
405 if (cumulative < 2) {
409 cumulative += length;
410 if (cumulative < 2) {
429 data_length = cumulative - 2;
433 cumulative += length;
443 if (expected >= outbuff->getLength()) {
447 size_t copy_amount = std::min(expected - outbuff->getLength(), data_length);
448 outbuff->writeData(
data, copy_amount);
452 return (expected == outbuff->getLength());
457template <
typename C>
void
459 if (socket_.is_open()) {
467template <
typename C>
void
469 if (socket_.is_open() && socket_ptr_) {
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
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...).
IOError(const char *file, size_t line, const char *what)
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
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.
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.
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.
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.