TCP server that handles a single client connection.
More...
#include <CTcpServer.hpp>
|
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< CTcpServer > | Pointer |
| Convenience type for a shared pointer to self. More...
|
|
|
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...
|
|
|
| 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...
|
|
A TCP server that redirects clients to the registered connection handler.
- Limitations:
- None.
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
-
ios | The I/O service used by the server. |
port | The listen port of the server. |
address | The address of the interface used for listening. |
- Limitations:
- None.
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
-
ios | The I/O service used by the server. |
port | The listen port of the server. |
address | The 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
-
error | The 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.
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
-
h | The 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.
boost::asio::ip::tcp::acceptor freedm::broker::device::CTcpServer::m_acceptor |
|
private |
Connection freedm::broker::device::CTcpServer::m_client |
|
protected |
unsigned short freedm::broker::device::CTcpServer::m_port |
|
private |
The documentation for this class was generated from the following files:
- /home/docs/checkouts/readthedocs.org/user_builds/freedm/checkouts/latest/Broker/src/device/CTcpServer.hpp
- /home/docs/checkouts/readthedocs.org/user_builds/freedm/checkouts/latest/Broker/src/device/CTcpServer.cpp