Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
SocketListener Class Reference

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.
 

Detailed Description

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.

Member Typedef Documentation

◆ connection_callback_t

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.

Constructor & Destructor Documentation

◆ SocketListener()

SocketListener::SocketListener ( int  port,
connection_callback_t  callback,
const char *  thread_name = "accept_thread" 
)

Constructor.

Startsthe listener.

Parameters
portwhich TCP port number to listen upon.
callbackwill be called on each incoming connection.
thread_namename to assign to the OSThread.

Member Function Documentation

◆ is_started()

bool SocketListener::is_started ( )
inline
Returns
true if the accept thread is already listening.

Definition at line 77 of file socket_listener.hxx.

◆ shutdown()

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.

Member Data Documentation

◆ accept_thread_

OSThread SocketListener::accept_thread_
private

Thread handle / instance for running the listen/accept loop.

Definition at line 94 of file socket_listener.hxx.

◆ callback_

connection_callback_t SocketListener::callback_
private

Callback to call with each incoming conneciton.

Definition at line 92 of file socket_listener.hxx.

◆ port_

int SocketListener::port_
private

Port to listen on.

Definition at line 90 of file socket_listener.hxx.

◆ shutdownComplete_

volatile unsigned SocketListener::shutdownComplete_
private

1 if accept thread is exited.

Definition at line 88 of file socket_listener.hxx.

◆ shutdownRequested_

volatile unsigned SocketListener::shutdownRequested_
private

1 if shutting down.

Definition at line 86 of file socket_listener.hxx.

◆ startupComplete_

volatile unsigned SocketListener::startupComplete_
private

1 if we have completed bind.

Definition at line 84 of file socket_listener.hxx.


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