Football I/O event handlers.
More...
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdarg.h>
#include <ctype.h>
#include <errno.h>
#include <assert.h>
#include <sys/types.h>
#include <stdbool.h>
#include "fb_service.h"
|
| static FB_MESSAGE * | fb_create_message (const char *format, va_list parameters) |
| |
| static ssize_t | fb_queue_single (FB_CONNECTION *connection, FB_MESSAGE *message) |
| |
| static ssize_t | fb_queue_broadcast (FB_SERVICE *service, FB_MESSAGE *message, bool(*callback)(FB_CONNECTION *)) |
| |
| ssize_t | fb_queue_message (void *thing, FB_MESSAGE *message, bool broadcast) |
| |
| ssize_t | fb_fprint (void *thing, const char *bytes, ssize_t length) |
| | Adds an unformatted message to the output queue. More...
|
| |
| ssize_t | fb_fprintf (void *thing, const char *format,...) |
| | Add messages to output queues in various forms and styles. More...
|
| |
| ssize_t | fb_vfprintf (void *thing, const char *format, va_list parameters) |
| |
| ssize_t | fb_bfprintf (void *thing, const char *format,...) |
| |
| ssize_t | fb_bvfprintf (void *thing, const char *format, va_list parameters) |
| |
| ssize_t | fb_cbfprintf (void *thing, bool(*callback)(FB_CONNECTION *), const char *format,...) |
| |
| ssize_t | fb_cbvfprintf (void *thing, bool(*callback)(FB_CONNECTION *), const char *format, va_list parameters) |
| |
| FB_EVENT * | fb_send_output (FB_EVENT *event, FB_CONNECTION *connection) |
| |
| bool | fb_set_input_buffer_size (FB_CONNECTION *connection, size_t size) |
| |
| bool | fb_recv_input (FB_CONNECTION *connection, ssize_t byte_count) |
| |
| static char * | fb_get_line_bytes (FB_CONNECTION *connection, size_t *length) |
| |
| FB_EVENT * | fb_read_line_input (FB_EVENT *event, FB_CONNECTION *connection) |
| |
| FB_EVENT * | fb_read_input (FB_EVENT *ev, FB_CONNECTION *connection) |
| |
| FB_EVENT * | fb_new_connect (FB_EVENT *event, FB_SERVICE *service) |
| |
Football I/O event handlers.
- Football socket abstraction layer
- Author
- Perette Barella
- Date
- 2012-03-03
- Copyright
- Copyright 2012-2021 Devious Fish. All rights reserved.
◆ _BSD_SOURCE
| #define _BSD_SOURCE /* snprintf() */ |
◆ _DEFAULT_SOURCE
◆ _POSIX_C_SOURCE
| #define _POSIX_C_SOURCE 200112L |
◆ fb_bfprintf()
| ssize_t fb_bfprintf |
( |
void * |
thing, |
|
|
const char * |
format, |
|
|
|
... |
|
) |
| |
◆ fb_bvfprintf()
| ssize_t fb_bvfprintf |
( |
void * |
thing, |
|
|
const char * |
format, |
|
|
va_list |
parameters |
|
) |
| |
◆ fb_cbfprintf()
| ssize_t fb_cbfprintf |
( |
void * |
thing, |
|
|
bool(*)(FB_CONNECTION *) |
callback, |
|
|
const char * |
format, |
|
|
|
... |
|
) |
| |
◆ fb_cbvfprintf()
| ssize_t fb_cbvfprintf |
( |
void * |
thing, |
|
|
bool(*)(FB_CONNECTION *) |
callback, |
|
|
const char * |
format, |
|
|
va_list |
parameters |
|
) |
| |
◆ fb_create_message()
| static FB_MESSAGE* fb_create_message |
( |
const char * |
format, |
|
|
va_list |
parameters |
|
) |
| |
|
static |
Create and populate a new message block.
- Parameters
-
| format | a printf-style format string |
| parameters | parameters to the format string |
◆ fb_fprint()
| ssize_t fb_fprint |
( |
void * |
thing, |
|
|
const char * |
bytes, |
|
|
ssize_t |
length |
|
) |
| |
Adds an unformatted message to the output queue.
- Parameters
-
| thing | the destined service, connection or event. |
| bytes | the bytes to transmit. |
| length | number of bytes in the message. |
- Returns
- -1 on error, number of bytes written on success or partial success.
◆ fb_fprintf()
| ssize_t fb_fprintf |
( |
void * |
thing, |
|
|
const char * |
format, |
|
|
|
... |
|
) |
| |
Add messages to output queues in various forms and styles.
All forms take an opaque type and determine if it's a connection or service, so you can write code once and deal with both single and broadcast messages.
- 'b' variants broadcast, given either a connection or service. Services always broadcast, regardless of service.
- 'cb' variants broadcast only to those connections identified buy the supplied callback function returning
true.
- 'v' accepts a pointer to the format arguments.
- Parameters
-
| thing | the destined service, connection or event. |
| format | a printf format string, with values to follow. |
- Returns
- -1 on error, number of bytes written on success or partial success.
◆ fb_get_line_bytes()
| static char* fb_get_line_bytes |
( |
FB_CONNECTION * |
connection, |
|
|
size_t * |
length |
|
) |
| |
|
static |
Read line-oriented input until encountering a newline. If there isn't a newline, the read input is kept in the connection's input buffer between invocations.
- Parameters
-
| connection | the connection to read from. |
| length | the number of bytes in the completed input |
- Returns
- a pointer to the input, or NULL if it is not complete yet.
◆ fb_new_connect()
Accept a new connection and return an event announcing it if applicable.
- Parameters
-
| event | a partially filled event applicable to the new connection. |
| service | the service on which the connection is arriving. |
- Returns
- FB_EVENT_CONNECTED for successful line-oriented connections, NULL otherwise.
◆ fb_queue_broadcast()
Put a message into all a service's queues.
- Parameters
-
| service | the service to which the message will be sent. |
| message | the message to send. |
| callback | If NULL, broadcast to all connections. Otherwise, broadcast only if the specified callback function returns true. |
- Returns
- length on success (queued to all connections successfully). On failure or partial failure, return -1.
◆ fb_queue_message()
| ssize_t fb_queue_message |
( |
void * |
thing, |
|
|
FB_MESSAGE * |
message, |
|
|
bool |
broadcast |
|
) |
| |
Add a message message to a service, connection, or event.
- Parameters
-
| thing | The service, connection or event to which message is sent. |
| message | The message to send. |
| broadcast | True if the message should be sent to all connections. Broadcast is inherently true if 'thing' is a service. |
- Returns
- the length of the message, or -1 on error, as per stdio. In case of only partial success... Success.
◆ fb_queue_single()
Put a message into a connection's output queue.
- Parameters
-
| connection | the connection to which the message is to be sent. |
| message | a message to send. |
- Returns
- length of message queued on success, or -1 on failure.
Add the message to the queue
◆ fb_read_input()
Read input based on protocol and state. If applicable, returns an event for the input.
- FB_EVENT_INPUT is returned when there is input
- FB_EVENT_CONNECT is returned on greeting or WebSocket establishment
- Parameters
-
| ev | a partially-filled event applicable to this input |
| connection | the connection from which input is read |
- Returns
- an event (if applicable), or NULL.
◆ fb_read_line_input()
Read input from a connection, return it as an event. The event has the raw command in 'command'. The event has a broken up command in 'argv'. On failure, 'argv' may be null.
- Parameters
-
| event | a partially filled event applicable to the connection. |
| connection | the connection from which input is read |
- Returns
- An FB_EVENT_INPUT event, or input is incomplete.
◆ fb_recv_input()
| bool fb_recv_input |
( |
FB_CONNECTION * |
connection, |
|
|
ssize_t |
byte_count |
|
) |
| |
Read some bytes directly from a socket or via TLS if encrypted. Handle connection closing and errors.
- Parameters
-
| connection | the connection to read input from. |
| byte_count | the number of bytes to read |
- Returns
- true if the byte count is met, false otherwise.
◆ fb_send_output()
Write output to a connection from the queue. Events are generated for connection closure only; there are none for writing.
If the socket is in a closing state, when the buffer is empty, close the connection.
- Parameters
-
| event | A partially complete event record, or NULL. If NULL, events are delayed (along with moving state toward closure) until a later time, when an event can be returned for those state changes. |
| connection | The connection whose queued data is to be sent. |
- Returns
- FB_EVENT_CLOSE or NULL
◆ fb_set_input_buffer_size()
| bool fb_set_input_buffer_size |
( |
FB_CONNECTION * |
connection, |
|
|
size_t |
size |
|
) |
| |
Expand the input buffer to a specified size.
- Parameters
-
| connection | The connection whose buffer size is being set. |
| size | the desired size. |
- Returns
- true on success, false on failure.
◆ fb_vfprintf()
| ssize_t fb_vfprintf |
( |
void * |
thing, |
|
|
const char * |
format, |
|
|
va_list |
parameters |
|
) |
| |