|
Open Model Railroad Network (OpenMRN)
|
TCP server. More...
#include <socket_listener.hxx>
Public Types | |
| typedef std::function< void(int)> | connection_callback_t |
| Callback type that will be called when a new incoming connection is established. | |
Public Member Functions | |
| SocketListener (int port, connection_callback_t callback, const char *thread_name="accept_thread") | |
| Constructor. | |
| void | AcceptThreadBody () |
| Implementation of the accept thread. | |
| void | shutdown () |
| Shuts down the socket listener. | |
| bool | is_started () |
Private Attributes | |
| volatile unsigned | startupComplete_: 1 |
| 1 if we have completed bind. | |
| volatile unsigned | shutdownRequested_: 1 |
| 1 if shutting down. | |
| volatile unsigned | shutdownComplete_: 1 |
| 1 if accept thread is exited. | |
| int | port_ |
| Port to listen on. | |
| connection_callback_t | callback_ |
| Callback to call with each incoming conneciton. | |
| OSThread | accept_thread_ |
| Thread handle / instance for running the listen/accept loop. | |
TCP server.
Listends to a specific TCP port, accepts every incomingconnection, and calls an application-specific callback with the new file descriptor.
usage: Instantiate SocketListener and pass in a function object (by either std::bind or a C++11 lambda function) to decide what to do with the new incoming connections.
Starts a separate thread for the accept loop.
Definition at line 52 of file socket_listener.hxx.
| typedef std::function<void(int)> SocketListener::connection_callback_t |
Callback type that will be called when a new incoming connection is established.
The argument is the filedescriptor of the new connection.
Definition at line 57 of file socket_listener.hxx.
| SocketListener::SocketListener | ( | int | port, |
| connection_callback_t | callback, | ||
| const char * | thread_name = "accept_thread" |
||
| ) |
Constructor.
Startsthe listener.
| port | which TCP port number to listen upon. |
| callback | will be called on each incoming connection. |
| thread_name | name to assign to the OSThread. |
|
inline |
Definition at line 77 of file socket_listener.hxx.
| void SocketListener::shutdown | ( | ) |
Shuts down the socket listener.
Blocks until the accept thread is stopped. This call can take up to 100 msec as of now. It is safe to delete *this after this call is returned.
|
private |
Thread handle / instance for running the listen/accept loop.
Definition at line 94 of file socket_listener.hxx.
|
private |
Callback to call with each incoming conneciton.
Definition at line 92 of file socket_listener.hxx.
|
private |
Port to listen on.
Definition at line 90 of file socket_listener.hxx.
|
private |
1 if accept thread is exited.
Definition at line 88 of file socket_listener.hxx.
|
private |
1 if shutting down.
Definition at line 86 of file socket_listener.hxx.
|
private |
1 if we have completed bind.
Definition at line 84 of file socket_listener.hxx.