7#ifndef TCP_CONNECTION_H
8#define TCP_CONNECTION_H
15#include <boost/enable_shared_from_this.hpp>
16#include <boost/system/error_code.hpp>
17#include <boost/shared_ptr.hpp>
72 virtual size_t postBuffer(
const void* buf,
const size_t nbytes) = 0;
96 std::string logFormatRequestSafe(
const size_t limit = 0)
const;
107 : send_in_progress_(false) {};
130 return (send_in_progress_);
135 bool send_in_progress_;
151class TcpConnectionPool;
162 typedef std::function<void(boost::system::error_code ec,
size_t length)>
163 SocketCallbackFunction;
168 class SocketCallback {
175 SocketCallback(SocketCallbackFunction socket_callback)
176 : callback_(socket_callback) {
188 void operator()(boost::system::error_code ec,
size_t length = 0);
192 SocketCallbackFunction callback_;
217 const long idle_timeout,
218 const size_t read_max = 32768);
235 virtual void close();
310 static boost::asio::ip::tcp::endpoint endpoint;
370 boost::system::error_code ec,
380 boost::system::error_code ec,
439 std::unique_ptr<asiolink::TCPSocket<SocketCallback> >
tcp_socket_;
442 std::unique_ptr<asiolink::TLSSocket<SocketCallback> >
tls_socket_;
This is a base class for exceptions thrown from the DNS library module.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
A generic exception that is thrown if a function is called in a prohibited way.
The IntervalTimer class is a wrapper for the ASIO boost::asio::deadline_timer class.
Generic error reported within TcpConnection class.
TcpConnectionError(const char *file, size_t line, const char *what)
Pool of active TCP connections.
Accepts and handles a single TCP connection.
void doWrite(TcpResponsePtr response)
Starts asynchronous write to the socket.
virtual TcpRequestPtr createRequest()=0
Creates a new, empty request.
void shutdownCallback(const boost::system::error_code &ec)
Callback invoked when TLS shutdown is performed.
unsigned char * getInputBufData()
Returns pointer to the first byte of the input buffer.
void acceptorCallback(const boost::system::error_code &ec)
Local callback invoked when new connection is accepted.
void asyncAccept()
Asynchronously accepts new connection.
size_t getInputBufSize() const
Returns input buffer size.
virtual void shutdown()
Shutdown the socket.
size_t getReadMax() const
Fetches the maximum number of bytes read during single socket read.
boost::asio::ip::tcp::endpoint remote_endpoint_
Remote endpoint.
virtual void requestReceived(TcpRequestPtr request)=0
Processes a request once it has been completely received.
virtual void shutdownConnection()
Shuts down current connection.
virtual void stopThisConnection()
Stops current connection.
void setReadMax(const size_t read_max)
Sets the maximum number of bytes read during single socket read.
TcpConnectionAcceptorPtr acceptor_
Pointer to the TCP acceptor used to accept new connections.
virtual void close()
Closes the socket.
asiolink::TlsContextPtr tls_context_
TLS context.
WireData input_buf_
Buffer for a single socket read.
void socketReadCallback(TcpRequestPtr request, boost::system::error_code ec, size_t length)
Callback invoked when new data is received over the socket.
void setupIdleTimer()
Reset timer for detecting idle timeout in connections.
virtual void socketWriteCallback(TcpResponsePtr request, boost::system::error_code ec, size_t length)
Callback invoked when data is sent over the socket.
void asyncSendResponse(TcpResponsePtr response)
Sends TCP response asynchronously.
asiolink::IOServicePtr io_service_
The IO service used to handle events.
std::string getRemoteEndpointAddressAsText() const
returns remote address in textual form
TcpConnectionFilterCallback connection_filter_
External callback for filtering connections by IP address.
const boost::asio::ip::tcp::endpoint getRemoteEndpoint() const
Fetches the remote endpoint for the connection's socket.
std::unique_ptr< asiolink::TCPSocket< SocketCallback > > tcp_socket_
TCP socket used by this connection.
void doRead(TcpRequestPtr request=TcpRequestPtr())
Starts asynchronous read from the socket.
static const boost::asio::ip::tcp::endpoint & NO_ENDPOINT()
Returns an empty end point.
virtual ~TcpConnection()
Destructor.
asiolink::IntervalTimer idle_timer_
Timer used to detect idle Timeout.
virtual bool responseSent(TcpResponsePtr response)=0
Determines behavior after a response has been sent.
TcpConnectionAcceptorCallback acceptor_callback_
External TCP acceptor callback.
size_t read_max_
Maximum bytes to read in a single socket read.
TcpConnectionPool & connection_pool_
Connection pool holding this connection.
void doHandshake()
Asynchronously performs TLS handshake.
TcpRequestPtr postData(TcpRequestPtr request, WireData &input_data)
Appends newly received raw data to the given request.
std::unique_ptr< asiolink::TLSSocket< SocketCallback > > tls_socket_
TLS socket used by this connection.
void handshakeCallback(const boost::system::error_code &ec)
Local callback invoked when TLS handshake is performed.
void idleTimeoutCallback()
Callback invoked when the client has been idle.
TcpConnection(const asiolink::IOServicePtr &io_service, const TcpConnectionAcceptorPtr &acceptor, const asiolink::TlsContextPtr &tls_context, TcpConnectionPool &connection_pool, const TcpConnectionAcceptorCallback &acceptor_callback, const TcpConnectionFilterCallback &connection_filter, const long idle_timeout, const size_t read_max=32768)
Constructor.
long idle_timeout_
Timeout after which the a TCP connection is shut down by the server.
Base class for TCP messages.
virtual ~TcpMessage()
Destructor.
WireData wire_data_
Buffer used for data in wire format data.
size_t getWireDataSize() const
Returns current size of the wire data.
const uint8_t * getWireData() const
Returns pointer to the first byte of the wire data.
Abstract class used to receive an inbound message.
virtual bool needData() const =0
Returns true if the request is incomplete.
virtual std::string logFormatRequest(const size_t limit=0) const =0
Returns request contents formatted for log output.
virtual void unpack()=0
Unpacks wire data once the message has been completely received.
virtual size_t postBuffer(const void *buf, const size_t nbytes)=0
Adds data to an incomplete request.
virtual ~TcpRequest()
Destructor.
Abstract class used to create and send an outbound response.
virtual ~TcpResponse()
Destructor.
virtual void consumeWireData(const size_t length)
Erases n bytes from the beginning of the wire data.
virtual void pack()=0
Prepares the wire data content for writing.
bool wireDataAvail() const
Checks if the output buffer contains some data to be sent.
TcpResponse()
Constructor.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
boost::shared_ptr< TlsContext > TlsContextPtr
The type of shared pointers to TlsContext objects.
boost::shared_ptr< IOService > IOServicePtr
Defines a smart pointer to an IOService instance.
boost::shared_ptr< TcpConnectionAcceptor > TcpConnectionAcceptorPtr
Type of shared pointer to TCP acceptors.
boost::shared_ptr< TcpRequest > TcpRequestPtr
Defines a smart pointer to a TcpRequest.
std::function< bool(const boost::asio::ip::tcp::endpoint &) TcpConnectionFilterCallback)
Type of the callback for filtering new connections by ip address.
boost::shared_ptr< TcpConnection > TcpConnectionPtr
Pointer to the TcpConnection.
std::function< void(const boost::system::error_code &) TcpConnectionAcceptorCallback)
Type of the callback for the TCP acceptor used in this library.
boost::shared_ptr< TcpResponse > TcpResponsePtr
std::vector< uint8_t > WireData
Defines a data structure for storing raw bytes of data on the wire.
boost::shared_ptr< WireData > WireDataPtr
Defines the logger used by the top-level component of kea-lfc.