# pion::net::TCPConnection Class Reference

`#include <TCPConnection.hpp>`

List of all members.

## Detailed Description

TCPConnection: represents a single tcp connection

Definition at line 39 of file TCPConnection.hpp.

## Public Types

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
| LIFECYCLE_CLOSE
| LIFECYCLE_KEEPALIVE
| LIFECYCLE_PIPELINED
| READ_BUFFER_SIZE = 8192
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

## 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
ReadBuffer & | getReadBuffer (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
Socket & | getSocket (void)
| returns non-const reference to underlying TCP socket object
SSLSocket & | getSSLSocket (void)
| returns non-const reference to underlying SSL socket object
const Socket & | getSocket (void) const
| returns const reference to underlying TCP socket object
const SSLSocket & | getSSLSocket (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)

## Classes

class | SSLSocket

## Constructor & Destructor Documentation

pion::net::TCPConnection::TCPConnection | ( | boost::asio::io_service & | io_service,
|  | const bool | ssl_flag = `false` |
| ) |  |  | `[inline, explicit]`

creates a new TCPConnection object

Parameters:

  | io_service | asio service associated with the connection
  | ssl_flag | if 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_service | asio service associated with the connection
  | ssl_context | asio 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 |
| ) |  |  | `[inline, protected]`

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

Parameters:

  | io_service | asio service associated with the connection
  | ssl_context | asio ssl context associated with the connection
  | ssl_flag | if true then the connection will be encrypted using SSL
  | finished_handler | function 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_acceptor | object 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.

References pion::net::TCPConnection::SSLSocket::lowest_layer().

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_acceptor | object used to accept new connections
  | handler | called after a new connection has been accepted

See also:
  boost::asio::basic_socket_acceptor::async_accept()

Definition at line 175 of file TCPConnection.hpp.

References pion::net::TCPConnection::SSLSocket::lowest_layer().

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_addr | remote IP address (v4) to connect to
  | remote_port | remote port number to connect to
  | handler | called 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 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_endpoint | remote endpoint to connect to
  | handler | called after a new connection has been established

See also:
  boost::asio::basic_socket_acceptor::async_connect()

Definition at line 205 of file TCPConnection.hpp.

References pion::net::TCPConnection::SSLSocket::lowest_layer().

Referenced by 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:

  | handler | called 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:

  | handler | called after the ssl handshake has completed

See also:
  boost::asio::ssl::stream::async_handshake()

Definition at line 319 of file TCPConnection.hpp.

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:

  | buffers | one or more buffers into which the data will be read
  | completion_condition | determines if the read operation is complete
  | handler | called after the read operation has completed

See also:
  boost::asio::async_read()

Definition at line 468 of file TCPConnection.hpp.

References getSSLFlag(), and pion::net::TCPConnection::SSLSocket::next_layer().

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_condition | determines if the read operation is complete
  | handler | called after the read operation has completed

See also:
  boost::asio::async_read()

Definition at line 444 of file TCPConnection.hpp.

References getSSLFlag(), and pion::net::TCPConnection::SSLSocket::next_layer().

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_buffer | the buffer to read data into
  | handler | called 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(), and pion::net::TCPConnection::SSLSocket::next_layer().

template<typename ReadHandler>

void pion::net::TCPConnection::async_read_some | ( | ReadHandler | handler | ) | `[inline]`

asynchronously reads some data into the connection's read buffer

Parameters:

  | handler | called 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(), and pion::net::TCPConnection::SSLSocket::next_layer().

template<typename ConstBufferSequence, typename WriteHandler>

void pion::net::TCPConnection::async_write | ( | const ConstBufferSequence & | buffers,
|  | WriteHandler | handler |
| ) |  |  | `[inline]`

asynchronously writes data to the connection

Parameters:

  | buffers | one or more buffers containing the data to be written
  | handler | called after the data has been written

See also:
  boost::asio::async_write()

Definition at line 541 of file TCPConnection.hpp.

References getSSLFlag(), and pion::net::TCPConnection::SSLSocket::next_layer().

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_server | hostname of the remote server to connect to
  | remote_port | remote 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(), connect(), and pion::net::TCPConnection::SSLSocket::lowest_layer().

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_addr | remote IP address (v4) to connect to
  | remote_port | remote 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 | ( | boost::asio::ip::tcp::endpoint & | tcp_endpoint | ) | `[inline]`

connects to a remote endpoint (blocks until established)

Parameters:

  | tcp_endpoint | remote 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.

References pion::net::TCPConnection::SSLSocket::lowest_layer().

Referenced by pion::net::TCPStream::connect(), 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 |
| ) |  |  | `[inline, static]`

creates new shared TCPConnection objects

Parameters:

  | io_service | asio service associated with the connection
  | ssl_context | asio ssl context associated with the connection
  | ssl_flag | if true then the connection will be encrypted using SSL
  | finished_handler | function 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]`

This function should be called when a server has finished handling the connection

Definition at line 576 of file TCPConnection.hpp.

Referenced by pion::net::HTTPServer::handleBadRequest(), pion::net::HTTPServer::handleForbiddenRequest(), pion::net::HTTPServer::handleMethodNotAllowed(), pion::net::HTTPServer::handleNotFoundRequest(), pion::net::HTTPCookieAuth::handleOk(), pion::net::HTTPCookieAuth::handleRedirection(), pion::net::HTTPServer::handleServerError(), pion::net::HTTPCookieAuth::handleUnauthorized(), and pion::net::HTTPBasicAuth::handleUnauthorized().

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_ptr | points to the next character to be consumed in the read_buffer
  | read_end_ptr | points 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 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:

  | buffers | one or more buffers into which the data will be read
  | completion_condition | determines if the read operation is complete
  | ec | contains 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 pion::net::TCPConnection::SSLSocket::next_layer().

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_condition | determines if the read operation is complete
  | ec | contains 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 getSSLFlag(), and pion::net::TCPConnection::SSLSocket::next_layer().

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_buffer | the buffer to read data into
  | ec | contains 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(), and pion::net::TCPConnection::SSLSocket::next_layer().

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:

  | ec | contains 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(), and pion::net::TCPConnection::SSLSocket::next_layer().

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

void pion::net::TCPConnection::saveReadPosition | ( | const char * | read_ptr,
|  | const char * | read_end_ptr |
| ) |  |  | `[inline]`

saves a read position bookmark

Parameters:

  | read_ptr | points to the next character to be consumed in the read_buffer
  | read_end_ptr | points 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:

  | buffers | one or more buffers containing the data to be written
  | ec | contains 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(), and pion::net::TCPConnection::SSLSocket::next_layer().

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

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

- net/include/pion/net/TCPConnection.hpp