pianod2
multisource multiuser scriptable networked music player
Classes | Macros | Typedefs | Enumerations | Functions | Variables
fb_socketmgr.c File Reference

Football socket management and dispatching. More...

#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/select.h>
#include <assert.h>
#include "fb_service.h"
Include dependency graph for fb_socketmgr.c:

Classes

struct  socket_data_t
 

Macros

#define _BSD_SOURCE   /* strdup() */
 
#define _DARWIN_C_SOURCE   /* strdup() on OS X */
 
#define _DEFAULT_SOURCE
 
#define FD_COPY(from, to)   memmove(to, from, sizeof(*(from)))
 
#define fb_lock_socketmanager_mutex()   true
 

Typedefs

typedef struct socket_data_t FB_SOCKET_DATA
 
typedef enum select_action_t ACTION
 

Enumerations

enum  select_action_t {
  ACTION_READING , ACTION_WRITING , ACTION_FAULTING , ACTION_BUFFERING ,
  ACTION_COUNT , ACTION_SELECT_COUNT = 3
}
 

Functions

void fb_schedule_reap (FB_SERVICE *service)
 
void fb_queue_event (FB_EVENT *event)
 
void fb_dispose_event (FB_EVENT *event)
 
void fb_interrupt (void)
 Return an FB_EVENT_INTERRUPTED on current or next poll. More...
 
static bool expand_socket_registry (int required_size)
 
bool fb_register (int socket_fd, FB_SOCKETTYPE type, void *thing)
 Add a socket to the registry. More...
 
void fb_unregister (int socket_fd)
 Remove a socket from the registry. More...
 
void fb_set_socket_select_flags (int socket_fd, ACTION group, bool enable)
 
void fb_set_buffering (int socket_fd, bool enable)
 
void fb_set_writable (int socket_fd, bool enable)
 
void fb_set_readable (int socket_fd, bool enable)
 
void fb_accept_input (FB_CONNECTION *connection, bool input)
 Public interface to enable/disable reading on a connection. More...
 
FB_EVENTfb_process_event (int socket_fd, ACTION action)
 
static FB_EVENTfb_perform_reaping ()
 
static void subtract_from_timeval (struct timeval *minuend, const struct timeval *subtrahend)
 
static FB_EVENTfb_poll_for (struct timeval *timeout)
 
FB_EVENTfb_poll (void)
 Quick poll with no waiting. More...
 
FB_EVENTfb_poll_with_timeout (double timeout)
 Poll with a duration. More...
 
FB_EVENTfb_wait (void)
 Poll indefinitely. More...
 
FB_EVENTfb_poll_until (time_t untilwhen)
 Poll with a timeout at a specific time. More...
 

Variables

static FB_SERVICEreapq = NULL
 
static FB_SOCKET_DATA ** sockets
 Index by socket number to service/connection/etc. More...
 
static int maxsockets = 0
 
static int activesockets = 0
 
static fd_set select_state [ACTION_COUNT]
 What we'll use on the next select() More...
 
static fd_set last_state [ACTION_SELECT_COUNT]
 What came back on the last select() More...
 
static FB_EVENTqueued_event = NULL
 Pending event (NULL if none, or single event. More...
 
static bool tls_currently_buffering
 Set when TLS has stuff in its buffers. More...
 
static volatile bool interrupt_pending = false
 Return EVENT_INTERRUPTED on EINTR. More...
 

Detailed Description

Football socket management and dispatching.

Macro Definition Documentation

◆ _BSD_SOURCE

#define _BSD_SOURCE   /* strdup() */

◆ _DARWIN_C_SOURCE

#define _DARWIN_C_SOURCE   /* strdup() on OS X */

◆ _DEFAULT_SOURCE

#define _DEFAULT_SOURCE

◆ fb_lock_socketmanager_mutex

#define fb_lock_socketmanager_mutex ( )    true

◆ FD_COPY

#define FD_COPY (   from,
  to 
)    memmove(to, from, sizeof(*(from)))

Typedef Documentation

◆ ACTION

typedef enum select_action_t ACTION

◆ FB_SOCKET_DATA

typedef struct socket_data_t FB_SOCKET_DATA

Enumeration Type Documentation

◆ select_action_t

Enumerator
ACTION_READING 
ACTION_WRITING 
ACTION_FAULTING 
ACTION_BUFFERING 
ACTION_COUNT 
ACTION_SELECT_COUNT 

Function Documentation

◆ expand_socket_registry()

static bool expand_socket_registry ( int  required_size)
static

Increase the registry to accommodate more sockets.

Parameters
required_sizeThe minimum size required. This function may choose to go larger.
Returns
True on success, false on failure.
Here is the caller graph for this function:

◆ fb_accept_input()

void fb_accept_input ( FB_CONNECTION connection,
bool  input 
)

Public interface to enable/disable reading on a connection.

Parameters
connection- the connection to adjust.
inputtrue to enable reading, false to disable.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fb_dispose_event()

void fb_dispose_event ( FB_EVENT event)

Destroy an event and free its resources. Events are all statically allocated, so we never free them in the current implementation. However, dispose frees the dynamically allocated elements and clears out the structure.

Parameters
eventthe event to free
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fb_interrupt()

void fb_interrupt ( void  )

Return an FB_EVENT_INTERRUPTED on current or next poll.

Here is the caller graph for this function:

◆ fb_perform_reaping()

static FB_EVENT* fb_perform_reaping ( )
static

Remove items from the reap queue. Defer parent services until all children are dead.

Returns
an event structure, or NULL; neither is an error.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fb_poll()

FB_EVENT* fb_poll ( void  )

Quick poll with no waiting.

Returns
An event.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fb_poll_for()

static FB_EVENT* fb_poll_for ( struct timeval *  timeout)
static

Poll the stuff in the registry. This is the worker function; the four exposed functions with various timeout mechanisms follow.

Parameters
timeoutDuration to poll, or NULL for indefinite wait.
Returns
an event structure, or NULL on failure.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fb_poll_until()

FB_EVENT* fb_poll_until ( time_t  untilwhen)

Poll with a timeout at a specific time.

Parameters
untilwhenTime at which timeout should occur.
Returns
An event.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fb_poll_with_timeout()

FB_EVENT* fb_poll_with_timeout ( double  timeout)

Poll with a duration.

Parameters
timeoutin seconds.
Returns
An event.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fb_process_event()

FB_EVENT* fb_process_event ( int  socket_fd,
ACTION  action 
)

Called by the poll routine, this function takes a socket and an action and processes it, either inline or by calling another function to do the work.

Parameters
socket_fdfile descriptor of a socket that is ready/wants attention.
actionindicates if socket is ready for reading, writing, etc.
Returns
an event, when appropriate.
If things are handled completely internally, returns NULL.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fb_queue_event()

void fb_queue_event ( FB_EVENT event)

Queue an event. Only used for fallback mode on HTTP port, when we need to deliver a connection arrival and then the command, so size is limited to 1.

Parameters
eventthe event to queue
Here is the caller graph for this function:

◆ fb_register()

bool fb_register ( int  socket_fd,
FB_SOCKETTYPE  type,
void *  thing 
)

Add a socket to the registry.

Parameters
socket_fdThe file descriptor of the socket being added.
typeType of socket (service, connection, user...)
thingPointer of type indicated by 'type'.
Returns
true on success, false on failure
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fb_schedule_reap()

void fb_schedule_reap ( FB_SERVICE service)

Schedule a service for reaping when it has no connections left.

Here is the caller graph for this function:

◆ fb_set_buffering()

void fb_set_buffering ( int  socket_fd,
bool  enable 
)

Enable/disable writing on a socket. This is used to remember TLS data buffering.

Parameters
socket_fdthe file descriptor to apply flags to
enabletrue if TLS is buffering, false if it is not.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fb_set_readable()

void fb_set_readable ( int  socket_fd,
bool  enable 
)

Enable/disable reading on a socket.

Parameters
socket_fdthe file descriptor to apply flags to
enabletrue to enable reads from the socket, false to disable
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fb_set_socket_select_flags()

void fb_set_socket_select_flags ( int  socket_fd,
ACTION  group,
bool  enable 
)

Enable/disable the flags for selecting, or the buffering flags.

Parameters
socket_fdthe file descriptor to set flags for
groupwhich flags to adjust (read, write, ...)
enabletrue to enable the flag, false to clear it
Here is the caller graph for this function:

◆ fb_set_writable()

void fb_set_writable ( int  socket_fd,
bool  enable 
)

Enable/disable writing on a socket. This is used within Football, enabled when fresh data is queued for a socket and disabled when the buffer is empty.

Parameters
socket_fdthe file descriptor to apply flags to
enabletrue to enable write-polling on the socket
Here is the call graph for this function:
Here is the caller graph for this function:

◆ fb_unregister()

void fb_unregister ( int  socket_fd)

Remove a socket from the registry.

Always succeeds, unless there's a bug.

Parameters
socket_fdthe file descriptor to unregister
Here is the caller graph for this function:

◆ fb_wait()

FB_EVENT* fb_wait ( void  )

Poll indefinitely.

Returns
An event.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ subtract_from_timeval()

static void subtract_from_timeval ( struct timeval *  minuend,
const struct timeval *  subtrahend 
)
static

Subtract time values.

Parameters
[in,out]minuendThe time being subtracted from.
subtrahendThe value being subtracted.
Here is the caller graph for this function:

Variable Documentation

◆ activesockets

int activesockets = 0
static

◆ interrupt_pending

volatile bool interrupt_pending = false
static

Return EVENT_INTERRUPTED on EINTR.

◆ last_state

fd_set last_state[ACTION_SELECT_COUNT]
static

What came back on the last select()

◆ maxsockets

int maxsockets = 0
static

◆ queued_event

FB_EVENT* queued_event = NULL
static

Pending event (NULL if none, or single event.

◆ reapq

FB_SERVICE* reapq = NULL
static

◆ select_state

fd_set select_state[ACTION_COUNT]
static

What we'll use on the next select()

◆ sockets

FB_SOCKET_DATA** sockets
static

Index by socket number to service/connection/etc.

◆ tls_currently_buffering

bool tls_currently_buffering
static

Set when TLS has stuff in its buffers.