Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
socket_listener.hxx
Go to the documentation of this file.
1
36#ifndef _UTILS_SOCKET_LISTENER_HXX_
37#define _UTILS_SOCKET_LISTENER_HXX_
38
39#include <functional>
40
41#include "os/OS.hxx"
42
53{
54public:
57 typedef std::function<void(int)> connection_callback_t;
58
65 const char *thread_name = "accept_thread");
67
70
74 void shutdown();
75
78 {
79 return startupComplete_;
80 }
81
82private:
84 volatile unsigned startupComplete_ : 1;
86 volatile unsigned shutdownRequested_ : 1;
88 volatile unsigned shutdownComplete_ : 1;
90 int port_;
95};
96
105int ConnectSocket(const char *host, int port);
106
115int ConnectSocket(const char *host, const char* port_str);
116
117#endif //_UTILS_SOCKET_LISTENER_HXX_
This class provides a threading API.
Definition OS.hxx:46
OSThread accept_thread_
Thread handle / instance for running the listen/accept loop.
volatile unsigned startupComplete_
1 if we have completed bind.
std::function< void(int)> connection_callback_t
Callback type that will be called when a new incoming connection is established.
int port_
Port to listen on.
void shutdown()
Shuts down the socket listener.
connection_callback_t callback_
Callback to call with each incoming conneciton.
SocketListener(int port, connection_callback_t callback, const char *thread_name="accept_thread")
Constructor.
void AcceptThreadBody()
Implementation of the accept thread.
volatile unsigned shutdownRequested_
1 if shutting down.
volatile unsigned shutdownComplete_
1 if accept thread is exited.
int ConnectSocket(const char *host, int port)
Connects a tcp socket to the specified remote host:port.