37#if OPENMRN_FEATURE_BSD_SOCKETS
39#ifndef _DEFAULT_SOURCE
40#define _DEFAULT_SOURCE
61static void* accept_thread_start(
void* arg)
69 const char *thread_name)
70 : startupComplete_(0),
71 shutdownRequested_(0),
75 accept_thread_(thread_name, 0, config_socket_listener_stack_size(),
76 accept_thread_start, this)
78#if OPENMRN_FEATURE_BSD_SOCKETS_IGNORE_SIGPIPE
81 signal(SIGPIPE, SIG_IGN);
85SocketListener::~SocketListener()
120#if OPENMRN_HAVE_BSD_SOCKETS_GETSOCKNAME
121 namelen =
sizeof(addr);
133 LOG(
INFO,
"Listening on port %d, fd %d",
ntohs(addr.sin_port), listenfd);
135#if OPENMRN_HAVE_BSD_SOCKETS_RX_TIMEOUT
151 namelen =
sizeof(addr);
157 if (errno == EINTR || errno == EAGAIN || errno == EMFILE)
161 else if (errno == ECONNABORTED)
const char * inet_ntoa(struct in_addr addr)
Converts internet address into a dotted decimal string.
int getsockname(int socket, struct sockaddr &addr, socklen_t &namelen)
Get the socket name.
int accept(int socket, struct sockaddr *address, socklen_t *address_len)
Accept a new connection on a socket.
int setsockopt(int socket, int level, int option_name, const void *option_value, socklen_t option_len)
Set the socket options.
int bind(int socket, const struct sockaddr *address, socklen_t address_len)
Bind a name to a socket.
int listen(int socket, int backlog)
Mark a connection-mode socket, specified by the socket argument, as accepting connections.
int socket(int domain, int type, int protocol)
Create an unbound socket in a communications domain.
volatile unsigned startupComplete_
1 if we have completed bind.
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.
void print_errno_and_exit(const char *where)
Prints an error message about errno to std error and terminates the current program.
#define htons(x)
Converts a host endian short value to network endian.
#define INADDR_ANY
Listen on all network interfaces for incoming connections.
#define IPPROTO_TCP
TCP Raw Socket.
#define ntohs(x)
Converts a network endian short value to host endian.
#define ERRNOCHECK(where, x...)
Calls the function x, and if the return value is negative, prints errno as error message to stderr an...
#define LOG(level, message...)
Conditionally write a message to the logging output.
static const int INFO
Loglevel that is printed by default, reporting some status information.
#define MSEC_TO_USEC(_msec)
Convert a millisecond value to a microsecond value.
#define SOCK_STREAM
TCP Socket.
uint32_t socklen_t
type of sockaddr lenth
#define SO_REUSEADDR
socket option to reuse address
#define SOL_SOCKET
socket option category
#define AF_INET
IPv4 Socket (UDP, TCP, etc...)
#define SO_RCVTIMEO
socket option receive timout
Structure describing an Internet socket address.
#define TCP_NODELAY
don't delay send to coalesce packets