FREEDM DGI
Public Types | Public Member Functions | Static Public Member Functions | Protected Attributes | Private Member Functions | Private Attributes | List of all members
freedm::broker::device::CTcpServer Class Reference

TCP server that handles a single client connection. More...

#include <CTcpServer.hpp>

Inheritance diagram for freedm::broker::device::CTcpServer:

Public Types

typedef boost::shared_ptr< boost::asio::ip::tcp::socket > Connection
 Convenient type for the client socket. More...
 
typedef boost::function< void()> ConnectionHandler
 Type of the callback function for client connections. More...
 
typedef boost::shared_ptr< CTcpServerPointer
 Convenience type for a shared pointer to self. More...
 

Public Member Functions

virtual ~CTcpServer ()
 Virtual destructor for derived classes. More...
 
Connection GetClient ()
 Gets the current client. More...
 
void RegisterHandler (ConnectionHandler h)
 Registers a callback function for client connections. More...
 
void StartAccept ()
 Prepares to accept the next client. More...
 
void Stop ()
 Stops the TCP server. More...
 

Static Public Member Functions

static Pointer Create (boost::asio::io_service &ios, unsigned short port, const std::string address="")
 Creates a new TCP server on the specified port number. More...
 

Protected Attributes

Connection m_client
 Socket for the current client. More...
 

Private Member Functions

 CTcpServer (boost::asio::io_service &ios, unsigned short port, const std::string address="")
 Constructs the TCP server on the specified port number. More...
 
void HandleAccept (const boost::system::error_code &error)
 Handles an accepted client connection. More...
 
std::string hdr () const
 Gets a log header. More...
 

Private Attributes

boost::asio::ip::tcp::acceptor m_acceptor
 Acceptor for new client connections. More...
 
ConnectionHandler m_handler
 Callback function to handle clients. More...
 
unsigned short m_port
 Port number of the server. More...
 

Detailed Description

A TCP server that redirects clients to the registered connection handler.

Limitations:
None.

Member Typedef Documentation

typedef boost::shared_ptr<boost::asio::ip::tcp::socket> freedm::broker::device::CTcpServer::Connection

Constructor & Destructor Documentation

freedm::broker::device::CTcpServer::~CTcpServer ( )
virtual

Stops the server prior to destruction.

Precondition:
None.
Postcondition:
Calls CTcpServer::Stop.
Limitations:
None.
freedm::broker::device::CTcpServer::CTcpServer ( boost::asio::io_service &  ios,
unsigned short  port,
const std::string  address = "" 
)
private

Constructor for a TCP server.

Precondition:
The specified port number must be valid.
Postcondition:
Constructs a TCP server that accepts connections on the given port.
Parameters
iosThe I/O service used by the server.
portThe listen port of the server.
addressThe address of the interface used for listening.
Limitations:
None.

Member Function Documentation

CTcpServer::Pointer freedm::broker::device::CTcpServer::Create ( boost::asio::io_service &  ios,
unsigned short  port,
const std::string  address = "" 
)
static

Creates a new TCP server.

Precondition:
The specified port number must be valid.
Postcondition:
Creates a TCP server that accepts connections on the given port.
Parameters
iosThe I/O service used by the server.
portThe listen port of the server.
addressThe address of the interface used for listening.
Returns
Shared pointer to the server.
Limitations:
None.
Connection freedm::broker::device::CTcpServer::GetClient ( )
inline
void freedm::broker::device::CTcpServer::HandleAccept ( const boost::system::error_code &  error)
private

Redirects an accepted client to the connection handler.

Error Handling:
Throws a std::runtime_error if the connection handler has not been defined with CTcpServer::RegisterHandler.
Precondition:
CTcpServer::RegisterHandler must be called prior to this function.
Postcondition:
Calls m_handler to handle the client connection.
Parameters
errorThe error code if the connection failed.
Limitations:
This function will not schedule the next accept. The owner of the handler must call CTcpServer::StartAccept when done with the client. This limitation is because the server handles at most one connection, and that connection must be closed before the next accept can be scheduled.
std::string freedm::broker::device::CTcpServer::hdr ( ) const
private

Gets a log header for this object.

Precondition:
None.
Postcondition:
A string "(m_port)" unique to this server.
Returns
A header to use with the logger.
Limitations:
None.
void freedm::broker::device::CTcpServer::RegisterHandler ( ConnectionHandler  h)

Registers a client connection handler with the server.

Error Handling:
Throws a std::runtime_error if either m_handler has already been initialized or the passed function is null.
Precondition:
m_handler must not be initialized.
Postcondition:
Assigns the passed function to m_handler.
Parameters
hThe callback function to handle client connections.
Limitations:
This function can only be called once.
void freedm::broker::device::CTcpServer::StartAccept ( )

Prepares to accept the next client connection.

Precondition:
None.
Postcondition:
Closes the current client connection on m_socket.
Limitations:
None.
void freedm::broker::device::CTcpServer::Stop ( )

Stops the TCP server from accepting new clients.

Precondition:
None.
Postcondition:
m_acceptor and m_socket are closed.
Error Handling:
throws boost::system::system_error if it fails
Limitations:
None.

Member Data Documentation

boost::asio::ip::tcp::acceptor freedm::broker::device::CTcpServer::m_acceptor
private
Connection freedm::broker::device::CTcpServer::m_client
protected
ConnectionHandler freedm::broker::device::CTcpServer::m_handler
private
unsigned short freedm::broker::device::CTcpServer::m_port
private

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