pion-net  4.0.9
Classes | Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | List of all members
pion::net::TCPConnection Class Reference

#include <TCPConnection.hpp>

Inherits noncopyable.

Classes

class  SSLSocket

Public Types

enum  LifecycleType { LIFECYCLE_CLOSE, LIFECYCLE_KEEPALIVE, LIFECYCLE_PIPELINED }
 data type for the connection's lifecycle state
enum  { READ_BUFFER_SIZE = 8192 }
 size of the read buffer
typedef boost::function1< void,
boost::shared_ptr
< TCPConnection > > 
ConnectionHandler
 data type for a function that handles TCP connection objects
typedef boost::array< char,
READ_BUFFER_SIZE > 
ReadBuffer
 data type for an I/O read buffer
typedef
boost::asio::ip::tcp::socket 
Socket
 data type for a socket connection
typedef int SSLContext

Public Member Functions

 TCPConnection (boost::asio::io_service &io_service, const bool ssl_flag=false)
 TCPConnection (boost::asio::io_service &io_service, SSLContext &ssl_context)
bool is_open (void) const
 returns true if the connection is currently open
void close (void)
 closes the tcp socket and cancels any pending asynchronous operations
virtual ~TCPConnection ()
 virtual destructor
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 (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 WriteHandler >
void async_write (const ConstBufferSequence &buffers, WriteHandler handler)
template<typename ConstBufferSequence >
std::size_t write (const ConstBufferSequence &buffers, boost::system::error_code &ec)
void finish (void)
bool getSSLFlag (void) const
 returns true if the connection is encrypted using SSL
void setLifecycle (LifecycleType t)
 sets the lifecycle type for the connection
LifecycleType getLifecycle (void) const
 returns the lifecycle type for the connection
bool getKeepAlive (void) const
 returns true if the connection should be kept alive
bool getPipelined (void) const
 returns true if the HTTP requests are pipelined
ReadBuffergetReadBuffer (void)
 returns the buffer used for reading data from the TCP connection
void saveReadPosition (const char *read_ptr, const char *read_end_ptr)
void loadReadPosition (const char *&read_ptr, const char *&read_end_ptr) const
boost::asio::ip::tcp::endpoint getRemoteEndpoint (void) const
 returns an ASIO endpoint for the client connection
boost::asio::ip::address getRemoteIp (void) const
 returns the client's IP address
unsigned short getRemotePort (void) const
 returns the client's port number
boost::asio::io_service & getIOService (void)
 returns reference to the io_service used for async operations
SocketgetSocket (void)
 returns non-const reference to underlying TCP socket object
SSLSocketgetSSLSocket (void)
 returns non-const reference to underlying SSL socket object
const SocketgetSocket (void) const
 returns const reference to underlying TCP socket object
const SSLSocketgetSSLSocket (void) const
 returns const reference to underlying SSL socket object

Static Public Member Functions

static boost::shared_ptr
< TCPConnection
create (boost::asio::io_service &io_service, SSLContext &ssl_context, const bool ssl_flag, ConnectionHandler finished_handler)

Protected Member Functions

 TCPConnection (boost::asio::io_service &io_service, SSLContext &ssl_context, const bool ssl_flag, ConnectionHandler finished_handler)

Detailed Description

TCPConnection: represents a single tcp connection

Definition at line 39 of file TCPConnection.hpp.

Constructor & Destructor Documentation

pion::net::TCPConnection::TCPConnection ( boost::asio::io_service &  io_service,
const bool  ssl_flag = false 
)
inlineexplicit

creates a new TCPConnection object

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

Definition at line 107 of file TCPConnection.hpp.

References saveReadPosition().

Referenced by create().

pion::net::TCPConnection::TCPConnection ( boost::asio::io_service &  io_service,
SSLContext &  ssl_context 
)
inline

creates a new TCPConnection object for SSL

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

Definition at line 129 of file TCPConnection.hpp.

References saveReadPosition().

pion::net::TCPConnection::TCPConnection ( boost::asio::io_service &  io_service,
SSLContext &  ssl_context,
const bool  ssl_flag,
ConnectionHandler  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

Definition at line 669 of file TCPConnection.hpp.

References saveReadPosition().

Member Function Documentation

boost::system::error_code pion::net::TCPConnection::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()

Definition at line 189 of file TCPConnection.hpp.

Referenced by pion::net::TCPStream::accept().

template<typename AcceptHandler >
void pion::net::TCPConnection::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()

Definition at line 175 of file TCPConnection.hpp.

template<typename ConnectHandler >
void pion::net::TCPConnection::async_connect ( 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()

Definition at line 205 of file TCPConnection.hpp.

Referenced by async_connect().

template<typename ConnectHandler >
void pion::net::TCPConnection::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()

Definition at line 221 of file TCPConnection.hpp.

References async_connect().

template<typename SSLHandshakeHandler >
void pion::net::TCPConnection::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()

Definition at line 304 of file TCPConnection.hpp.

template<typename SSLHandshakeHandler >
void pion::net::TCPConnection::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()

Definition at line 319 of file TCPConnection.hpp.

template<typename CompletionCondition , typename ReadHandler >
void pion::net::TCPConnection::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()

Definition at line 444 of file TCPConnection.hpp.

References getSSLFlag().

Referenced by async_read(), and read().

template<typename MutableBufferSequence , typename CompletionCondition , typename ReadHandler >
void pion::net::TCPConnection::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()

Definition at line 468 of file TCPConnection.hpp.

References async_read(), and getSSLFlag().

template<typename ReadHandler >
void pion::net::TCPConnection::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()

Definition at line 366 of file TCPConnection.hpp.

References getSSLFlag().

template<typename ReadBufferType , typename ReadHandler >
void pion::net::TCPConnection::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()

Definition at line 386 of file TCPConnection.hpp.

References getSSLFlag().

template<typename ConstBufferSequence , typename WriteHandler >
void pion::net::TCPConnection::async_write ( const ConstBufferSequence &  buffers,
WriteHandler  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()

Definition at line 541 of file TCPConnection.hpp.

References getSSLFlag().

boost::system::error_code pion::net::TCPConnection::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()

Definition at line 237 of file TCPConnection.hpp.

Referenced by connect(), and pion::net::TCPStream::connect().

boost::system::error_code pion::net::TCPConnection::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()

Definition at line 253 of file TCPConnection.hpp.

References connect().

boost::system::error_code pion::net::TCPConnection::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()

Definition at line 269 of file TCPConnection.hpp.

References close(), and connect().

static boost::shared_ptr<TCPConnection> pion::net::TCPConnection::create ( boost::asio::io_service &  io_service,
SSLContext &  ssl_context,
const bool  ssl_flag,
ConnectionHandler  finished_handler 
)
inlinestatic

creates new shared TCPConnection 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

Definition at line 92 of file TCPConnection.hpp.

References TCPConnection().

void pion::net::TCPConnection::finish ( void  )
inline
boost::system::error_code pion::net::TCPConnection::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()

Definition at line 333 of file TCPConnection.hpp.

Referenced by pion::net::TCPStream::connect().

boost::system::error_code pion::net::TCPConnection::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()

Definition at line 349 of file TCPConnection.hpp.

Referenced by pion::net::TCPStream::accept().

void pion::net::TCPConnection::loadReadPosition ( 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)

Definition at line 613 of file TCPConnection.hpp.

Referenced by pion::net::HTTPMessage::receive().

template<typename CompletionCondition >
std::size_t pion::net::TCPConnection::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()

Definition at line 493 of file TCPConnection.hpp.

References async_read(), and getSSLFlag().

Referenced by read().

template<typename MutableBufferSequence , typename CompletionCondition >
std::size_t pion::net::TCPConnection::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()

Definition at line 518 of file TCPConnection.hpp.

References getSSLFlag(), and read().

std::size_t pion::net::TCPConnection::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()

Definition at line 404 of file TCPConnection.hpp.

References getSSLFlag().

Referenced by pion::net::HTTPMessage::receive().

template<typename ReadBufferType >
std::size_t pion::net::TCPConnection::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()

Definition at line 423 of file TCPConnection.hpp.

References getSSLFlag().

void pion::net::TCPConnection::saveReadPosition ( 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)

Definition at line 602 of file TCPConnection.hpp.

Referenced by pion::net::HTTPMessage::receive(), and TCPConnection().

template<typename ConstBufferSequence >
std::size_t pion::net::TCPConnection::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()

Definition at line 560 of file TCPConnection.hpp.

References getSSLFlag().

Referenced by pion::net::HTTPMessage::send().


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