PionNetworkLibrary
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
pion::tcp::connection Class Reference

#include <connection.hpp>

Inheritance diagram for pion::tcp::connection:
Collaboration diagram for pion::tcp::connection:

Classes

class  ssl_socket_type
 

Public Types

enum  lifecycle_type { LIFECYCLE_CLOSE, LIFECYCLE_KEEPALIVE, LIFECYCLE_PIPELINED }
 data type for the connection's lifecycle state More...
 
enum  { READ_BUFFER_SIZE = 8192 }
 size of the read buffer More...
 
typedef boost::function1< void,
boost::shared_ptr< connection > > 
connection_handler
 data type for a function that handles TCP connection objects More...
 
typedef boost::array< char,
READ_BUFFER_SIZE
read_buffer_type
 data type for an I/O read buffer More...
 
typedef
boost::asio::ip::tcp::socket 
socket_type
 data type for a socket connection More...
 
typedef int ssl_context_type
 

Public Member Functions

 connection (boost::asio::io_service &io_service, const bool ssl_flag=false)
 
 connection (boost::asio::io_service &io_service, ssl_context_type &ssl_context)
 
bool is_open (void) const
 returns true if the connection is currently open More...
 
void close (void)
 closes the tcp socket and cancels any pending asynchronous operations More...
 
void cancel (void)
 
virtual ~connection ()
 virtual destructor More...
 
template<typename AcceptHandler >
void async_accept (boost::asio::ip::tcp::acceptor &tcp_acceptor, AcceptHandler handler)
 
boost::system::error_code accept (boost::asio::ip::tcp::acceptor &tcp_acceptor)
 
template<typename ConnectHandler >
void async_connect (const boost::asio::ip::tcp::endpoint &tcp_endpoint, ConnectHandler handler)
 
template<typename ConnectHandler >
void async_connect (const boost::asio::ip::address &remote_addr, const unsigned int remote_port, ConnectHandler handler)
 
boost::system::error_code connect (boost::asio::ip::tcp::endpoint &tcp_endpoint)
 
boost::system::error_code connect (const boost::asio::ip::address &remote_addr, const unsigned int remote_port)
 
boost::system::error_code connect (const std::string &remote_server, const unsigned int remote_port)
 
template<typename SSLHandshakeHandler >
void async_handshake_client (SSLHandshakeHandler handler)
 
template<typename SSLHandshakeHandler >
void async_handshake_server (SSLHandshakeHandler handler)
 
boost::system::error_code handshake_client (void)
 
boost::system::error_code handshake_server (void)
 
template<typename ReadHandler >
void async_read_some (ReadHandler handler)
 
template<typename ReadBufferType , typename ReadHandler >
void async_read_some (ReadBufferType read_buffer, ReadHandler handler)
 
std::size_t read_some (boost::system::error_code &ec)
 
template<typename ReadBufferType >
std::size_t read_some (ReadBufferType read_buffer, boost::system::error_code &ec)
 
template<typename CompletionCondition , typename ReadHandler >
void async_read (CompletionCondition completion_condition, ReadHandler handler)
 
template<typename MutableBufferSequence , typename CompletionCondition , typename ReadHandler >
void async_read (const MutableBufferSequence &buffers, CompletionCondition completion_condition, ReadHandler handler)
 
template<typename CompletionCondition >
std::size_t read (CompletionCondition completion_condition, boost::system::error_code &ec)
 
template<typename MutableBufferSequence , typename CompletionCondition >
std::size_t read (const MutableBufferSequence &buffers, CompletionCondition completion_condition, boost::system::error_code &ec)
 
template<typename ConstBufferSequence , typename write_handler_t >
void async_write (const ConstBufferSequence &buffers, write_handler_t handler)
 
template<typename ConstBufferSequence >
std::size_t write (const ConstBufferSequence &buffers, boost::system::error_code &ec)
 
void finish (void)
 
bool get_ssl_flag (void) const
 returns true if the connection is encrypted using SSL More...
 
void set_lifecycle (lifecycle_type t)
 sets the lifecycle type for the connection More...
 
lifecycle_type get_lifecycle (void) const
 returns the lifecycle type for the connection More...
 
bool get_keep_alive (void) const
 returns true if the connection should be kept alive More...
 
bool get_pipelined (void) const
 returns true if the HTTP requests are pipelined More...
 
read_buffer_typeget_read_buffer (void)
 returns the buffer used for reading data from the TCP connection More...
 
void save_read_pos (const char *read_ptr, const char *read_end_ptr)
 
void load_read_pos (const char *&read_ptr, const char *&read_end_ptr) const
 
boost::asio::ip::tcp::endpoint get_remote_endpoint (void) const
 returns an ASIO endpoint for the client connection More...
 
boost::asio::ip::address get_remote_ip (void) const
 returns the client's IP address More...
 
unsigned short get_remote_port (void) const
 returns the client's port number More...
 
boost::asio::io_service & get_io_service (void)
 returns reference to the io_service used for async operations More...
 
socket_typeget_socket (void)
 returns non-const reference to underlying TCP socket object More...
 
ssl_socket_typeget_ssl_socket (void)
 returns non-const reference to underlying SSL socket object More...
 
const socket_typeget_socket (void) const
 returns const reference to underlying TCP socket object More...
 
const ssl_socket_typeget_ssl_socket (void) const
 returns const reference to underlying SSL socket object More...
 

Static Public Member Functions

static boost::shared_ptr
< connection
create (boost::asio::io_service &io_service, ssl_context_type &ssl_context, const bool ssl_flag, connection_handler finished_handler)
 

Protected Member Functions

 connection (boost::asio::io_service &io_service, ssl_context_type &ssl_context, const bool ssl_flag, connection_handler finished_handler)
 

Detailed Description

connection: represents a single tcp connection

Member Typedef Documentation

typedef boost::function1<void, boost::shared_ptr<connection> > pion::tcp::connection::connection_handler

data type for a function that handles TCP connection objects

data type for an I/O read buffer

typedef boost::asio::ip::tcp::socket pion::tcp::connection::socket_type

data type for a socket connection

Member Enumeration Documentation

anonymous enum

size of the read buffer

Enumerator
READ_BUFFER_SIZE 

data type for the connection's lifecycle state

Enumerator
LIFECYCLE_CLOSE 
LIFECYCLE_KEEPALIVE 
LIFECYCLE_PIPELINED 

Constructor & Destructor Documentation

pion::tcp::connection::connection ( boost::asio::io_service &  io_service,
const bool  ssl_flag = false 
)
inlineexplicit

creates a new connection object

Parameters
io_serviceasio service associated with the connection
ssl_flagif true then the connection will be encrypted using SSL

References save_read_pos().

Referenced by create().

pion::tcp::connection::connection ( boost::asio::io_service &  io_service,
ssl_context_type ssl_context 
)
inline

creates a new connection object for SSL

Parameters
io_serviceasio service associated with the connection
ssl_contextasio ssl context associated with the connection

References save_read_pos().

virtual pion::tcp::connection::~connection ( )
inlinevirtual

virtual destructor

References close().

pion::tcp::connection::connection ( boost::asio::io_service &  io_service,
ssl_context_type ssl_context,
const bool  ssl_flag,
connection_handler  finished_handler 
)
inlineprotected

protected constructor restricts creation of objects (use create())

Parameters
io_serviceasio service associated with the connection
ssl_contextasio ssl context associated with the connection
ssl_flagif true then the connection will be encrypted using SSL
finished_handlerfunction called when a server has finished handling the connection

References save_read_pos().

Member Function Documentation

boost::system::error_code pion::tcp::connection::accept ( boost::asio::ip::tcp::acceptor &  tcp_acceptor)
inline

accepts a new tcp connection (blocks until established)

Parameters
tcp_acceptorobject used to accept new connections
Returns
boost::system::error_code contains error code if the connection fails
See Also
boost::asio::basic_socket_acceptor::accept()

References pion::tcp::connection::ssl_socket_type::lowest_layer().

Referenced by pion::tcp::stream::accept().

template<typename AcceptHandler >
void pion::tcp::connection::async_accept ( boost::asio::ip::tcp::acceptor &  tcp_acceptor,
AcceptHandler  handler 
)
inline

asynchronously accepts a new tcp connection

Parameters
tcp_acceptorobject used to accept new connections
handlercalled after a new connection has been accepted
See Also
boost::asio::basic_socket_acceptor::async_accept()

References pion::tcp::connection::ssl_socket_type::lowest_layer().

template<typename ConnectHandler >
void pion::tcp::connection::async_connect ( const boost::asio::ip::tcp::endpoint &  tcp_endpoint,
ConnectHandler  handler 
)
inline

asynchronously connects to a remote endpoint

Parameters
tcp_endpointremote endpoint to connect to
handlercalled after a new connection has been established
See Also
boost::asio::basic_socket_acceptor::async_connect()

References pion::tcp::connection::ssl_socket_type::lowest_layer().

Referenced by async_connect().

template<typename ConnectHandler >
void pion::tcp::connection::async_connect ( const boost::asio::ip::address &  remote_addr,
const unsigned int  remote_port,
ConnectHandler  handler 
)
inline

asynchronously connects to a (IPv4) remote endpoint

Parameters
remote_addrremote IP address (v4) to connect to
remote_portremote port number to connect to
handlercalled after a new connection has been established
See Also
boost::asio::basic_socket_acceptor::async_connect()

References async_connect().

template<typename SSLHandshakeHandler >
void pion::tcp::connection::async_handshake_client ( SSLHandshakeHandler  handler)
inline

asynchronously performs client-side SSL handshake for a new connection

Parameters
handlercalled after the ssl handshake has completed
See Also
boost::asio::ssl::stream::async_handshake()
template<typename SSLHandshakeHandler >
void pion::tcp::connection::async_handshake_server ( SSLHandshakeHandler  handler)
inline

asynchronously performs server-side SSL handshake for a new connection

Parameters
handlercalled after the ssl handshake has completed
See Also
boost::asio::ssl::stream::async_handshake()
template<typename CompletionCondition , typename ReadHandler >
void pion::tcp::connection::async_read ( CompletionCondition  completion_condition,
ReadHandler  handler 
)
inline

asynchronously reads data into the connection's read buffer until completion_condition is met

Parameters
completion_conditiondetermines if the read operation is complete
handlercalled after the read operation has completed
See Also
boost::asio::async_read()

References get_ssl_flag(), and pion::tcp::connection::ssl_socket_type::next_layer().

template<typename MutableBufferSequence , typename CompletionCondition , typename ReadHandler >
void pion::tcp::connection::async_read ( const MutableBufferSequence &  buffers,
CompletionCondition  completion_condition,
ReadHandler  handler 
)
inline

asynchronously reads data from the connection until completion_condition is met

Parameters
buffersone or more buffers into which the data will be read
completion_conditiondetermines if the read operation is complete
handlercalled after the read operation has completed
See Also
boost::asio::async_read()

References get_ssl_flag(), and pion::tcp::connection::ssl_socket_type::next_layer().

template<typename ReadHandler >
void pion::tcp::connection::async_read_some ( ReadHandler  handler)
inline

asynchronously reads some data into the connection's read buffer

Parameters
handlercalled after the read operation has completed
See Also
boost::asio::basic_stream_socket::async_read_some()

References get_ssl_flag(), and pion::tcp::connection::ssl_socket_type::next_layer().

template<typename ReadBufferType , typename ReadHandler >
void pion::tcp::connection::async_read_some ( ReadBufferType  read_buffer,
ReadHandler  handler 
)
inline

asynchronously reads some data into the connection's read buffer

Parameters
read_bufferthe buffer to read data into
handlercalled after the read operation has completed
See Also
boost::asio::basic_stream_socket::async_read_some()

References get_ssl_flag(), and pion::tcp::connection::ssl_socket_type::next_layer().

template<typename ConstBufferSequence , typename write_handler_t >
void pion::tcp::connection::async_write ( const ConstBufferSequence &  buffers,
write_handler_t  handler 
)
inline

asynchronously writes data to the connection

Parameters
buffersone or more buffers containing the data to be written
handlercalled after the data has been written
See Also
boost::asio::async_write()

References get_ssl_flag(), and pion::tcp::connection::ssl_socket_type::next_layer().

void pion::tcp::connection::cancel ( void  )
inline

cancels any asynchronous operations pending on the socket. there is no good way to do this on windows until vista or later (0x0600) see http://www.boost.org/doc/libs/1_53_0/doc/html/boost_asio/reference/basic_stream_socket/cancel/overload2.html note that the asio docs are misleading because close() is not thread-safe, and the suggested #define statements cause WAY too much trouble and heartache

References pion::tcp::connection::ssl_socket_type::next_layer().

void pion::tcp::connection::close ( void  )
inline

closes the tcp socket and cancels any pending asynchronous operations

References is_open(), and pion::tcp::connection::ssl_socket_type::next_layer().

Referenced by pion::tcp::stream::close(), connect(), pion::tcp::server::stop(), and ~connection().

boost::system::error_code pion::tcp::connection::connect ( boost::asio::ip::tcp::endpoint &  tcp_endpoint)
inline

connects to a remote endpoint (blocks until established)

Parameters
tcp_endpointremote endpoint to connect to
Returns
boost::system::error_code contains error code if the connection fails
See Also
boost::asio::basic_socket_acceptor::connect()

References pion::tcp::connection::ssl_socket_type::lowest_layer().

Referenced by connect(), and pion::tcp::stream::connect().

boost::system::error_code pion::tcp::connection::connect ( const boost::asio::ip::address &  remote_addr,
const unsigned int  remote_port 
)
inline

connects to a (IPv4) remote endpoint (blocks until established)

Parameters
remote_addrremote IP address (v4) to connect to
remote_portremote port number to connect to
Returns
boost::system::error_code contains error code if the connection fails
See Also
boost::asio::basic_socket_acceptor::connect()

References connect().

boost::system::error_code pion::tcp::connection::connect ( const std::string &  remote_server,
const unsigned int  remote_port 
)
inline

connects to a remote endpoint with hostname lookup

Parameters
remote_serverhostname of the remote server to connect to
remote_portremote port number to connect to
Returns
boost::system::error_code contains error code if the connection fails
See Also
boost::asio::basic_socket_acceptor::connect()

References close(), connect(), and pion::tcp::connection::ssl_socket_type::lowest_layer().

static boost::shared_ptr<connection> pion::tcp::connection::create ( boost::asio::io_service &  io_service,
ssl_context_type ssl_context,
const bool  ssl_flag,
connection_handler  finished_handler 
)
inlinestatic

creates new shared connection objects

Parameters
io_serviceasio service associated with the connection
ssl_contextasio ssl context associated with the connection
ssl_flagif true then the connection will be encrypted using SSL
finished_handlerfunction called when a server has finished handling the connection

References connection().

boost::asio::io_service& pion::tcp::connection::get_io_service ( void  )
inline

returns reference to the io_service used for async operations

References pion::tcp::connection::ssl_socket_type::lowest_layer().

bool pion::tcp::connection::get_keep_alive ( void  ) const
inline

returns true if the connection should be kept alive

References LIFECYCLE_CLOSE.

Referenced by pion::http::message::send().

lifecycle_type pion::tcp::connection::get_lifecycle ( void  ) const
inline

returns the lifecycle type for the connection

bool pion::tcp::connection::get_pipelined ( void  ) const
inline

returns true if the HTTP requests are pipelined

References LIFECYCLE_PIPELINED.

Referenced by pion::http::message::receive().

read_buffer_type& pion::tcp::connection::get_read_buffer ( void  )
inline

returns the buffer used for reading data from the TCP connection

Referenced by pion::http::message::receive().

boost::asio::ip::tcp::endpoint pion::tcp::connection::get_remote_endpoint ( void  ) const
inline

returns an ASIO endpoint for the client connection

Referenced by get_remote_ip(), and get_remote_port().

boost::asio::ip::address pion::tcp::connection::get_remote_ip ( void  ) const
inline

returns the client's IP address

References get_remote_endpoint().

Referenced by pion::tcp::stream::get_remote_ip().

unsigned short pion::tcp::connection::get_remote_port ( void  ) const
inline

returns the client's port number

References get_remote_endpoint().

socket_type& pion::tcp::connection::get_socket ( void  )
inline

returns non-const reference to underlying TCP socket object

References pion::tcp::connection::ssl_socket_type::next_layer().

const socket_type& pion::tcp::connection::get_socket ( void  ) const
inline

returns const reference to underlying TCP socket object

bool pion::tcp::connection::get_ssl_flag ( void  ) const
inline

returns true if the connection is encrypted using SSL

Referenced by async_read(), async_read_some(), async_write(), pion::tcp::stream::get_ssl_flag(), read(), read_some(), and write().

ssl_socket_type& pion::tcp::connection::get_ssl_socket ( void  )
inline

returns non-const reference to underlying SSL socket object

const ssl_socket_type& pion::tcp::connection::get_ssl_socket ( void  ) const
inline

returns const reference to underlying SSL socket object

boost::system::error_code pion::tcp::connection::handshake_client ( void  )
inline

performs client-side SSL handshake for a new connection (blocks until finished)

Returns
boost::system::error_code contains error code if the connection fails
See Also
boost::asio::ssl::stream::handshake()

Referenced by pion::tcp::stream::connect().

boost::system::error_code pion::tcp::connection::handshake_server ( void  )
inline

performs server-side SSL handshake for a new connection (blocks until finished)

Returns
boost::system::error_code contains error code if the connection fails
See Also
boost::asio::ssl::stream::handshake()

Referenced by pion::tcp::stream::accept().

bool pion::tcp::connection::is_open ( void  ) const
inline

returns true if the connection is currently open

Referenced by close(), and pion::tcp::stream::is_open().

void pion::tcp::connection::load_read_pos ( const char *&  read_ptr,
const char *&  read_end_ptr 
) const
inline

loads a read position bookmark

Parameters
read_ptrpoints to the next character to be consumed in the read_buffer
read_end_ptrpoints to the end of the read_buffer (last byte + 1)

Referenced by pion::http::message::receive().

template<typename CompletionCondition >
std::size_t pion::tcp::connection::read ( CompletionCondition  completion_condition,
boost::system::error_code &  ec 
)
inline

reads data into the connection's read buffer until completion_condition is met (blocks until finished)

Parameters
completion_conditiondetermines if the read operation is complete
eccontains error code if the read fails
Returns
std::size_t number of bytes read
See Also
boost::asio::read()

References get_ssl_flag(), and pion::tcp::connection::ssl_socket_type::next_layer().

template<typename MutableBufferSequence , typename CompletionCondition >
std::size_t pion::tcp::connection::read ( const MutableBufferSequence &  buffers,
CompletionCondition  completion_condition,
boost::system::error_code &  ec 
)
inline

reads data from the connection until completion_condition is met (blocks until finished)

Parameters
buffersone or more buffers into which the data will be read
completion_conditiondetermines if the read operation is complete
eccontains error code if the read fails
Returns
std::size_t number of bytes read
See Also
boost::asio::read()

References get_ssl_flag(), and pion::tcp::connection::ssl_socket_type::next_layer().

std::size_t pion::tcp::connection::read_some ( boost::system::error_code &  ec)
inline

reads some data into the connection's read buffer (blocks until finished)

Parameters
eccontains error code if the read fails
Returns
std::size_t number of bytes read
See Also
boost::asio::basic_stream_socket::read_some()

References get_ssl_flag(), and pion::tcp::connection::ssl_socket_type::next_layer().

Referenced by pion::http::message::receive().

template<typename ReadBufferType >
std::size_t pion::tcp::connection::read_some ( ReadBufferType  read_buffer,
boost::system::error_code &  ec 
)
inline

reads some data into the connection's read buffer (blocks until finished)

Parameters
read_bufferthe buffer to read data into
eccontains error code if the read fails
Returns
std::size_t number of bytes read
See Also
boost::asio::basic_stream_socket::read_some()

References get_ssl_flag(), and pion::tcp::connection::ssl_socket_type::next_layer().

void pion::tcp::connection::save_read_pos ( const char *  read_ptr,
const char *  read_end_ptr 
)
inline

saves a read position bookmark

Parameters
read_ptrpoints to the next character to be consumed in the read_buffer
read_end_ptrpoints to the end of the read_buffer (last byte + 1)

Referenced by connection(), and pion::http::message::receive().

void pion::tcp::connection::set_lifecycle ( lifecycle_type  t)
inline

sets the lifecycle type for the connection

Referenced by pion::http::message::receive().

template<typename ConstBufferSequence >
std::size_t pion::tcp::connection::write ( const ConstBufferSequence &  buffers,
boost::system::error_code &  ec 
)
inline

writes data to the connection (blocks until finished)

Parameters
buffersone or more buffers containing the data to be written
eccontains error code if the write fails
Returns
std::size_t number of bytes written
See Also
boost::asio::write()

References get_ssl_flag(), and pion::tcp::connection::ssl_socket_type::next_layer().

Referenced by pion::http::message::send().


The documentation for this class was generated from the following file: