repo_name
string
dataset
string
owner
string
lang
string
func_name
string
code
string
docstring
string
url
string
sha
string
zap
github_2023
zigzap
c
on_repl_message
static void on_repl_message(fio_msg_s *msg) { fio_write((intptr_t)msg->udata1, msg->msg.data, msg->msg.len); }
/* Forward REPL messages to the socket - pub/sub callback */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/examples/raw-client.c#L119-L121
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
main
int main(int argc, char const *argv[]) { /* Setup CLI arguments */ fio_cli_start(argc, argv, 1, 2, "use:\n\tclient <args> hostname port\n", FIO_CLI_BOOL("-tls use TLS to establish a secure connection."), FIO_CLI_STRING("-tls-alpn set the ALPN extension for TLS."), FIO...
/* ***************************************************************************** Main ***************************************************************************** */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/examples/raw-client.c#L154-L216
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
echo_on_data
static void echo_on_data(intptr_t uuid, fio_protocol_s *prt) { // echo buffer char buffer[1024] = {'E', 'c', 'h', 'o', ':', ' '}; ssize_t len; // Read to the buffer, starting after the "Echo: " while ((len = fio_read(uuid, buffer + 6, 1018)) > 0) { fprintf(stderr, "Read: %.*s", (int)len, buffer + 6); ...
/* ***************************************************************************** Echo connection callbacks ***************************************************************************** */ // A callback to be called whenever data is available on the socket
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/examples/raw-echo.c#L30-L48
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
echo_ping
static void echo_ping(intptr_t uuid, fio_protocol_s *prt) { fio_write(uuid, "Server: Are you there?\n", 23); (void)prt; // we can ignore the unused argument }
// A callback called whenever a timeout is reach
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/examples/raw-echo.c#L51-L54
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
echo_on_shutdown
static uint8_t echo_on_shutdown(intptr_t uuid, fio_protocol_s *prt) { fio_write(uuid, "Echo server shutting down\nGoodbye.\n", 35); return 0; (void)prt; // we can ignore the unused argument }
// A callback called if the server is shutting down... // ... while the connection is still open
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/examples/raw-echo.c#L58-L62
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
echo_on_open
static void echo_on_open(intptr_t uuid, void *udata) { // Protocol objects MUST be dynamically allocated when multi-threading. fio_protocol_s *echo_proto = malloc(sizeof(*echo_proto)); *echo_proto = (fio_protocol_s){.on_data = echo_on_data, .on_shutdown = echo_on_shutdown, ...
/* ***************************************************************************** The main echo protocol creation callback ***************************************************************************** */ // A callback called for new connections
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/examples/raw-echo.c#L75-L89
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
main
int main(int argc, char const *argv[]) { /* Setup CLI arguments */ fio_cli_start(argc, argv, 0, 0, "this example accepts the following options:", FIO_CLI_INT("-t -thread number of threads to run."), FIO_CLI_INT("-w -workers number of workers to run."), "-b, -address t...
/* ***************************************************************************** The main function (listens to the `echo` connections and handles CLI) ***************************************************************************** */ // The main function starts listening to echo connections
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/examples/raw-echo.c#L96-L117
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
on_http_request
int on_http_request(light_http_s *http) { /* handle a request for `http->path` */ if (1) { /* a simple, hardcoded HTTP/1.1 response */ static char HTTP_RESPONSE[] = "HTTP/1.1 200 OK\r\n" "Content-Length: 13\r\n" "Connection: keep-alive\r\n"...
/* ***************************************************************************** The HTTP/1.1 Request Handler - change this to whateve you feel like. ***************************************************************************** */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/examples/raw-http.c#L85-L107
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
main
int main(int argc, char const *argv[]) { /* A simple CLI interface. */ fio_cli_start(argc, argv, 0, 0, "Custom HTTP example for the facil.io framework.", FIO_CLI_INT("-port -p Port to bind to. Default: 3000"), FIO_CLI_INT("-workers -w Number of workers (processes)."),...
/* our main function / starting point */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/examples/raw-http.c#L117-L137
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
light_http1_on_request
int light_http1_on_request(http1_parser_s *parser) { int ret = on_http_request(parser2pr(parser)); fio_str_free(&parser2pr(parser)->body); parser2pr(parser)->reset = 1; return ret; }
/* ***************************************************************************** The HTTP/1.1 Parsing Callbacks - we need to implememnt everything for the parser ***************************************************************************** */ /** called when a request was received. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/examples/raw-http.c#L144-L149
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
light_http1_on_response
int light_http1_on_response(http1_parser_s *parser) { return -1; (void)parser; }
/** called when a response was received, this is for HTTP clients (error). */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/examples/raw-http.c#L152-L155
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
light_http1_on_method
int light_http1_on_method(http1_parser_s *parser, char *method, size_t method_len) { parser2pr(parser)->method = method; return 0; (void)method_len; }
/** called when a request method is parsed. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/examples/raw-http.c#L158-L163
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
light_http1_on_status
int light_http1_on_status(http1_parser_s *parser, size_t status, char *status_str, size_t len) { return -1; (void)parser; (void)status; (void)status_str; (void)len; }
/** called when a response status is parsed. the status_str is the string * without the prefixed numerical status indicator.*/
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/examples/raw-http.c#L167-L174
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
light_http1_on_path
int light_http1_on_path(http1_parser_s *parser, char *path, size_t path_len) { parser2pr(parser)->path = path; return 0; (void)path_len; }
/** called when a request path (excluding query) is parsed. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/examples/raw-http.c#L176-L180
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
light_http1_on_query
int light_http1_on_query(http1_parser_s *parser, char *query, size_t query_len) { parser2pr(parser)->query = query; return 0; (void)query_len; }
/** called when a request path (excluding query) is parsed. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/examples/raw-http.c#L182-L187
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
light_http1_on_http_version
int light_http1_on_http_version(http1_parser_s *parser, char *version, size_t len) { parser2pr(parser)->http_version = version; return 0; (void)len; }
/** called when a the HTTP/1.x version is parsed. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/examples/raw-http.c#L189-L194
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
light_http1_on_header
int light_http1_on_header(http1_parser_s *parser, char *name, size_t name_len, char *data, size_t data_len) { if (parser2pr(parser)->header_count >= MAX_HTTP_HEADER_COUNT) return -1; parser2pr(parser)->headers[parser2pr(parser)->header_count] = name; parser2pr(parser)->values[parser2...
/** called when a header is parsed. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/examples/raw-http.c#L196-L206
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
light_http1_on_body_chunk
int light_http1_on_body_chunk(http1_parser_s *parser, char *data, size_t data_len) { if (parser->state.content_length >= MAX_HTTP_BODY_MAX) return -1; if (fio_str_write(&parser2pr(parser)->body, data, data_len).len >= MAX_HTTP_BODY_MAX) return -1; return 0; }
/** called when a body chunk is parsed. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/examples/raw-http.c#L209-L217
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
light_http1_on_error
int light_http1_on_error(http1_parser_s *parser) { /* close the connection */ fio_close(parser2pr(parser)->uuid); return 0; }
/** called when a protocol error occurred. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/examples/raw-http.c#L220-L224
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
light_http_on_open
void light_http_on_open(intptr_t uuid, void *udata) { /* * we should allocate a protocol object for this connection. * * since protocol objects are stateful (the parsing, internal locks, etc'), we * need a different protocol object per connection. * * NOTE: the extra length in the memory will be the...
/* this will be called when a connection is opened. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/examples/raw-http.c#L235-L261
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
light_http_on_data
void light_http_on_data(intptr_t uuid, fio_protocol_s *pr) { /* We will read some / all of the data */ light_http_s *h = (light_http_s *)pr; ssize_t tmp = fio_read(uuid, (char *)(h + 1) + h->buf_writer, (MAX_HTTP_HEADER_LENGTH + MIN_HTTP_READFILE) - h->buf_writer); if (tmp <= 0) { /* re...
/* this will be called when the connection has incoming data. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/examples/raw-http.c#L264-L316
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
light_http_on_close
void light_http_on_close(intptr_t uuid, fio_protocol_s *pr) { /* in case we lost connection midway */ fio_str_free(&((light_http_s *)pr)->body); /* free our protocol data and resources */ free(pr); (void)uuid; }
/* this will be called when the connection is closed. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/examples/raw-http.c#L319-L325
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
light_http_send_response
void light_http_send_response(intptr_t uuid, int status, fio_str_info_s status_str, size_t header_count, fio_str_info_s headers[][2], fio_str_info_s body) { static size_t date_len = 0; /* TODO: implement a date header when missi...
/* ***************************************************************************** Fast HTTP response handling ***************************************************************************** */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/examples/raw-http.c#L331-L369
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
main
int main(int argc, char const *argv[]) { /* initialize the CLI helper and options */ cli_init(argc, argv); /* sertup routes */ route_add("/json", on_request_json); route_add("/plaintext", on_request_plain_text); /* Server name and header */ HTTP_HEADER_SERVER = fiobj_str_new("server", 6); HTTP_VALUE_S...
/* ***************************************************************************** The main function ***************************************************************************** */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/examples/benchmarks/framework_benchmark.c#L54-L87
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
on_request_json
static void on_request_json(http_s *h) { http_set_header(h, HTTP_HEADER_CONTENT_TYPE, http_mimetype_find("json", 4)); FIOBJ json; /* create a new Hash to be serialized for every request */ FIOBJ hash = fiobj_hash_new2(1); fiobj_hash_set(hash, JSON_KEY, fiobj_dup(JSON_VALUE)); json = fiobj_obj2json(hash, 0);...
/* ***************************************************************************** Request handlers ***************************************************************************** */ /* handles JSON requests */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/examples/benchmarks/framework_benchmark.c#L94-L105
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
on_request_plain_text
static void on_request_plain_text(http_s *h) { http_set_header(h, HTTP_HEADER_CONTENT_TYPE, http_mimetype_find("txt", 3)); http_send_body(h, "Hello, World!", 13); }
/* handles plain text requests (Hello World) */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/examples/benchmarks/framework_benchmark.c#L108-L111
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
cli_init
static void cli_init(int argc, char const *argv[]) { fio_cli_start(argc, argv, 0, 0, "This is a facil.io framework benchmark application.\n" "\nFor details about the benchmarks visit:\n" "http://frameworkbenchmarks.readthedocs.io/en/latest/\n" "\nThe fol...
/* ***************************************************************************** CLI ***************************************************************************** */ /* initialize CLI helper and manage it's default options */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/examples/benchmarks/framework_benchmark.c#L118-L154
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
route_add
static void route_add(char *path, void (*handler)(http_s *)) { /* add handler to the hash map */ fio_str_s tmp = FIO_STR_INIT_STATIC(path); /* fio hash maps support up to 96 full collisions, we can use len as hash */ fio_router_insert(&routes, fio_str_len(&tmp), tmp, handler, NULL); }
/* adds a route to our simple router */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/examples/benchmarks/framework_benchmark.c#L174-L179
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
route_perform
static void route_perform(http_s *h) { /* add required Serevr header */ http_set_header(h, HTTP_HEADER_SERVER, fiobj_dup(HTTP_VALUE_SERVER)); /* collect path from hash map */ fio_str_info_s tmp_i = fiobj_obj2cstr(h->path); fio_str_s tmp = FIO_STR_INIT_EXISTING(tmp_i.data, tmp_i.len, 0); fio_router_handler_f...
/* routes a request to the correct handler */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/examples/benchmarks/framework_benchmark.c#L182-L195
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
route_clear
static void route_clear(void) { fio_router_free(&routes); }
/* cleanup for our router */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/examples/benchmarks/framework_benchmark.c#L198-L198
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
cleanup
static void cleanup(void) { fio_cli_end(); fiobj_free(HTTP_HEADER_SERVER); fiobj_free(HTTP_VALUE_SERVER); fiobj_free(JSON_KEY); fiobj_free(JSON_VALUE); route_clear(); }
/* ***************************************************************************** Cleanup ***************************************************************************** */ /* cleanup any leftovers */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/examples/benchmarks/framework_benchmark.c#L205-L213
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
on_open_shootout_websocket
static void on_open_shootout_websocket(ws_s *ws) { fio_atomic_add(&sub_count, 2); websocket_subscribe(ws, .channel = CHANNEL_TEXT, .force_text = 1, .on_unsubscribe = on_websocket_unsubscribe); websocket_subscribe(ws, .channel = CHANNEL_BINARY, .force_binary = 1, .on_uns...
/* ***************************************************************************** WebSocket event callbacks ***************************************************************************** */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/examples/benchmarks/websocket_shootout.c#L61-L67
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
answer_http_request
static void answer_http_request(http_s *request) { http_set_header(request, HTTP_HEADER_CONTENT_TYPE, http_mimetype_find("txt", 3)); http_send_body(request, "This is a Websocket-Shootout example!", 37); }
/* ***************************************************************************** HTTP events ***************************************************************************** */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/examples/benchmarks/websocket_shootout.c#L100-L104
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
logger_subscribe
static void logger_subscribe(const fio_pubsub_engine_s *eng, fio_str_info_s channel, fio_match_fn match) { FIO_LOG_INFO("(%d) Channel subscription created: %s", getpid(), channel.data); (void)eng; (void)match; }
/* ***************************************************************************** Pub/Sub logging (for debugging) ***************************************************************************** */ /** Should subscribe channel. Failures are ignored. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/examples/benchmarks/websocket_shootout.c#L120-L125
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
logger_unsubscribe
static void logger_unsubscribe(const fio_pubsub_engine_s *eng, fio_str_info_s channel, fio_match_fn match) { FIO_LOG_INFO("(%d) Channel subscription destroyed: %s", getpid(), channel.data); fflush(stderr); (void)eng; (void)match; }
/** Should unsubscribe channel. Failures are ignored. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/examples/benchmarks/websocket_shootout.c#L127-L134
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
logger_publish
static void logger_publish(const fio_pubsub_engine_s *eng, fio_str_info_s channel, fio_str_info_s msg, uint8_t is_json) { (void)eng; (void)channel; (void)msg; (void)is_json; }
/** Should publish a message through the engine. Failures are ignored. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/examples/benchmarks/websocket_shootout.c#L136-L143
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
redis_cleanup
static void redis_cleanup(void *e_) { redis_engine_destroy(e_); FIO_LOG_DEBUG("Cleaned up redis engine object."); FIO_PUBSUB_DEFAULT = FIO_PUBSUB_CLUSTER; }
/* ***************************************************************************** Redis cleanup helpers ***************************************************************************** */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/examples/benchmarks/websocket_shootout.c#L155-L159
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
main
int main(int argc, char const *argv[]) { const char *port = "3000"; const char *public_folder = NULL; uint32_t threads = 0; uint32_t workers = 0; uint8_t print_log = 0; /* **** Command line arguments **** */ fio_cli_start( argc, argv, 0, 0, "This is a facil.io example application.\n"...
/* ***************************************************************************** The main function ***************************************************************************** */ /* Read available command line details using "-?". */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/examples/benchmarks/websocket_shootout.c#L186-L255
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
on_http_request
static void on_http_request(http_s *h) { /* set a response and send it (finnish vs. destroy). */ http_send_body(h, "Hello World!", 12); }
/* TODO: edit this function to handle HTTP data and answer Websocket requests.*/
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/examples/boiler_plate/src/http_service.c#L5-L8
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
initialize_http_service
void initialize_http_service(void) { /* listen for inncoming connections */ if (http_listen(fio_cli_get("-p"), fio_cli_get("-b"), .on_request = on_http_request, .max_body_size = fio_cli_get_i("-maxbd") * 1024 * 1024, .ws_max_msg_size = fio_cli_get_i("-max-msg") ...
/* starts a listeninng socket for HTTP connections. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/examples/boiler_plate/src/http_service.c#L11-L25
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_capa
size_t fio_capa(void) { if (fio_data) return fio_data->capa; return 0; }
/** * Returns the maximum number of open files facil.io can handle per worker * process. * * Total OS limits might apply as well but aren't shown. * * The value of 0 indicates either that the facil.io library wasn't initialized * yet or that it's resources were released. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L256-L260
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_packet_free
static inline void fio_packet_free(fio_packet_s *packet) { packet->dealloc(packet->data.buffer); fio_free(packet); }
/* ***************************************************************************** Packet allocation (for socket's user-buffer) ***************************************************************************** */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L266-L269
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_max_fd_min
static void fio_max_fd_min(uint32_t fd) { if (fio_data->max_protocol_fd > fd) return; fio_lock(&fio_data->lock); if (fio_data->max_protocol_fd < fd) fio_data->max_protocol_fd = fd; fio_unlock(&fio_data->lock); }
/* ***************************************************************************** Core Connection Data Clearing ***************************************************************************** */ /* set the minimal max_protocol_fd */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L281-L288
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_max_fd_shrink
static void fio_max_fd_shrink(void) { fio_lock(&fio_data->lock); uint32_t fd = fio_data->max_protocol_fd; while (fd && fd_data(fd).protocol == NULL) --fd; fio_data->max_protocol_fd = fd; fio_unlock(&fio_data->lock); }
/* set the minimal max_protocol_fd */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L291-L298
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_clear_fd
static inline int fio_clear_fd(intptr_t fd, uint8_t is_open) { fio_packet_s *packet; fio_protocol_s *protocol; fio_rw_hook_s *rw_hooks; void *rw_udata; fio_uuid_links_s links; fio_lock(&(fd_data(fd).sock_lock)); links = fd_data(fd).links; packet = fd_data(fd).packet; protocol = fd_data(fd).protocol; ...
/* resets connection data, marking it as either open or closed. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L301-L342
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
protocol_unlock
inline static void protocol_unlock(fio_protocol_s *pr, enum fio_protocol_lock_e type) { fio_unlock(&prt_meta(pr).locks[type]); }
/** See `fio_protocol_try_lock` for details. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L381-L384
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_protocol_unlock
void fio_protocol_unlock(fio_protocol_s *pr, enum fio_protocol_lock_e type) { protocol_unlock(pr, type); }
/* public API. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L403-L405
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_fd2uuid
intptr_t fio_fd2uuid(int fd) { if (fd < 0 || (size_t)fd >= fio_data->capa) return -1; if (!fd_data(fd).open) { fio_lock(&fd_data(fd).protocol_lock); fio_clear_fd(fd, 1); fio_unlock(&fd_data(fd).protocol_lock); } return fd2uuid(fd); }
/* ***************************************************************************** UUID validation and state ***************************************************************************** */ /* public API. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L412-L421
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_is_valid
int fio_is_valid(intptr_t uuid) { return uuid_is_valid(uuid); }
/* public API. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L424-L424
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_is_closed
int fio_is_closed(intptr_t uuid) { return !uuid_is_valid(uuid) || !uuid_data(uuid).open || uuid_data(uuid).close; }
/* public API. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L427-L429
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_is_running
int16_t fio_is_running(void) { return fio_data && fio_data->active; }
/* public API. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L437-L437
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_last_tick
struct timespec fio_last_tick(void) { return fio_data->last_cycle; }
/* public API. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L440-L442
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_touch
void fio_touch(intptr_t uuid) { if (uuid_is_valid(uuid)) touchfd(fio_uuid2fd(uuid)); }
/* public API. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L447-L450
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_peer_addr
fio_str_info_s fio_peer_addr(intptr_t uuid) { if (fio_is_closed(uuid) || !uuid_data(uuid).addr_len) return (fio_str_info_s){.data = NULL, .len = 0, .capa = 0}; return (fio_str_info_s){.data = (char *)uuid_data(uuid).addr, .len = uuid_data(uuid).addr_len, .capa...
/* public API. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L453-L459
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_local_addr
size_t fio_local_addr(char *dest, size_t limit) { if (gethostname(dest, limit)) return 0; struct addrinfo hints, *info; memset(&hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; // don't care IPv4 or IPv6 hints.ai_socktype = SOCK_STREAM; // TCP stream sockets hints.ai_flags = AI_CANONNAME; //...
/** * Writes the local machine address (qualified host name) to the buffer. * * Returns the amount of data written (excluding the NUL byte). * * `limit` is the maximum number of bytes in the buffer, including the NUL byte. * * If the returned value == limit - 1, the result might have been truncated. * * If 0 i...
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L473-L500
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_uuid_link
void fio_uuid_link(intptr_t uuid, void *obj, void (*on_close)(void *obj)) { if (!uuid_is_valid(uuid)) goto invalid; fio_lock(&uuid_data(uuid).sock_lock); if (!uuid_is_valid(uuid)) goto locked_invalid; fio_uuid_links_overwrite(&uuid_data(uuid).links, (uintptr_t)obj, on_close, N...
/* ***************************************************************************** UUID attachments (linking objects to the UUID's lifetime) ***************************************************************************** */ /* public API. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L507-L522
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_uuid_unlink
int fio_uuid_unlink(intptr_t uuid, void *obj) { if (!uuid_is_valid(uuid)) goto invalid; fio_lock(&uuid_data(uuid).sock_lock); if (!uuid_is_valid(uuid)) goto locked_invalid; /* default object comparison is always true */ int ret = fio_uuid_links_remove(&uuid_data(uuid).links, (uintptr_t)obj, NULL...
/* public API. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L525-L543
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_thread_suspend
FIO_FUNC void fio_thread_suspend(void) { fio_lock(&fio_thread_lock); fio_ls_embd_push(&fio_thread_queue, &fio_thread_data.node); fio_unlock(&fio_thread_lock); struct pollfd list = { .events = (POLLPRI | POLLIN), .fd = fio_thread_data.fd_wait, }; if (poll(&list, 1, 5000) > 0) { /* thread was ...
/* suspend thread execution (might be resumed unexpectedly) */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L700-L719
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_thread_signal
FIO_FUNC void fio_thread_signal(void) { fio_thread_queue_s *t; int fd = -2; fio_lock(&fio_thread_lock); t = (fio_thread_queue_s *)fio_ls_embd_shift(&fio_thread_queue); if (t) fd = t->fd_signal; fio_unlock(&fio_thread_lock); if (fd >= 0) { uint64_t data = 1; int r = write(fd, (void *)&data, siz...
/* wake up a single thread */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L722-L738
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_thread_broadcast
FIO_FUNC void fio_thread_broadcast(void) { while (fio_ls_embd_any(&fio_thread_queue)) { fio_thread_signal(); } }
/* wake up all threads */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L741-L745
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_defer_thread_wait
static void fio_defer_thread_wait(void) { #if FIO_ENGINE_POLL fio_poll(); return; #endif if (FIO_DEFER_THROTTLE_POLL) { fio_thread_suspend(); } else { /* keeps threads active (concurrent), but reduces performance */ static __thread size_t static_throttle = 262143UL; fio_throttle_thread(static_th...
/** * A thread entering this function should wait for new evennts. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L751-L767
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_defer_clear_tasks_for_queue
static inline void fio_defer_clear_tasks_for_queue(fio_task_queue_s *queue) { fio_lock(&queue->lock); while (queue->reader) { fio_defer_queue_block_s *tmp = queue->reader; queue->reader = queue->reader->next; if (tmp != &queue->static_queue) { COUNT_DEALLOC; free(tmp); } } queue->sta...
/* same as fio_defer_clear_queue , just inlined */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L988-L1001
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_defer_perform_single_task_for_queue
static inline int fio_defer_perform_single_task_for_queue(fio_task_queue_s *queue) { fio_defer_task_s task = fio_defer_pop_task(queue); if (!task.func) return -1; task.func(task.arg1, task.arg2); return 0; }
/** * Performs a single task from the queue, returning -1 if the queue was empty. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L1006-L1013
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_defer
int fio_defer(void (*func)(void *, void *), void *arg1, void *arg2) { /* must have a task to defer */ if (!func) goto call_error; fio_defer_push_task(func, arg1, arg2); return 0; call_error: return -1; }
/* ***************************************************************************** External Task API ***************************************************************************** */ /** Defer an execution of a function for later. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L1034-L1043
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_defer_perform
void fio_defer_perform(void) { #if FIO_USE_URGENT_QUEUE while (fio_defer_perform_single_task_for_queue(&task_queue_urgent) == 0 || fio_defer_perform_single_task_for_queue(&task_queue_normal) == 0) ; #else while (fio_defer_perform_single_task_for_queue(&task_queue_normal) == 0) ; #endif // for (...
/** Performs all deferred functions until the queue had been depleted. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L1046-L1067
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_defer_has_queue
int fio_defer_has_queue(void) { #if FIO_USE_URGENT_QUEUE return task_queue_urgent.reader != task_queue_urgent.writer || task_queue_urgent.reader->write != task_queue_urgent.reader->read || task_queue_normal.reader != task_queue_normal.writer || task_queue_normal.reader->write != task_queue_...
/** Returns true if there are deferred functions waiting for execution. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L1070-L1080
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_defer_clear_queue
void fio_defer_clear_queue(void) { fio_defer_clear_tasks(); }
/** Clears the queue. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L1083-L1083
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_defer_thread_pool_join
static void fio_defer_thread_pool_join(fio_defer_thread_pool_s *pool) { for (size_t i = 0; i < pool->thread_count; ++i) { fio_thread_join(pool->threads[i]); } free(pool); }
/* joins a thread pool */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L1105-L1110
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_mark_time
static inline void fio_mark_time(void) { clock_gettime(CLOCK_REALTIME, &fio_data->last_cycle); }
/** Marks the current time as facil.io's cycle time */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L1174-L1176
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_timer_calc_due
static struct timespec fio_timer_calc_due(size_t interval) { struct timespec now = fio_last_tick(); if (interval >= 1000) { unsigned long long secs = interval / 1000; now.tv_sec += secs; interval -= secs * 1000; } now.tv_nsec += (interval * 1000000UL); if (now.tv_nsec >= 1000000000L) { now.tv_...
/** Calculates the due time for a task, given it's interval */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L1179-L1192
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_timer_calc_first_interval
static size_t fio_timer_calc_first_interval(void) { if (fio_defer_has_queue()) return 0; if (fio_ls_embd_is_empty(&fio_timers)) { return FIO_POLL_TICK; } struct timespec now = fio_last_tick(); struct timespec due = FIO_LS_EMBD_OBJ(fio_timer_s, node, fio_timers.next)->due; if (due.tv_sec < now....
/** Returns the number of miliseconds until the next event, up to FIO_POLL_TICK */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L1196-L1217
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_timer_compare
static int fio_timer_compare(struct timespec a, struct timespec b) { if (a.tv_sec == b.tv_sec) { if (a.tv_nsec < b.tv_nsec) return 1; if (a.tv_nsec > b.tv_nsec) return -1; return 0; } if (a.tv_sec < b.tv_sec) return 1; return -1; }
/* simple a<=>b if "a" is bigger a negative result is returned, eq == 0. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L1220-L1231
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_timer_add_order
static void fio_timer_add_order(fio_timer_s *timer) { timer->due = fio_timer_calc_due(timer->interval); // fio_ls_embd_s *pos = &fio_timers; fio_lock(&fio_timer_lock); FIO_LS_EMBD_FOR(&fio_timers, node) { fio_timer_s *t2 = FIO_LS_EMBD_OBJ(fio_timer_s, node, node); if (fio_timer_compare(timer->due, t2->d...
/** Places a timer in an ordered linked list. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L1234-L1248
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_timer_perform_single
static void fio_timer_perform_single(void *timer_, void *ignr) { fio_timer_s *timer = timer_; timer->task(timer->arg); if (!timer->repetitions || fio_atomic_sub(&timer->repetitions, 1)) goto reschedule; if (timer->on_finish) timer->on_finish(timer->arg); free(timer); return; (void)ignr; reschedule...
/** Performs a timer task and re-adds it to the queue (or cleans it up) */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L1251-L1263
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_timer_schedule
static void fio_timer_schedule(void) { struct timespec now = fio_last_tick(); fio_lock(&fio_timer_lock); while (fio_ls_embd_any(&fio_timers) && fio_timer_compare( FIO_LS_EMBD_OBJ(fio_timer_s, node, fio_timers.next)->due, now) >= 0) { fio_ls_embd_s *tmp = fio_ls_embd_remove(f...
/** schedules all timers that are due to be performed. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L1266-L1278
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_run_every
int fio_run_every(size_t milliseconds, size_t repetitions, void (*task)(void *), void *arg, void (*on_finish)(void *)) { if (!task || (milliseconds == 0 && !repetitions)) return -1; fio_timer_s *timer = malloc(sizeof(*timer)); FIO_ASSERT_ALLOC(timer); fio_mark_time(); *timer = (fio_timer...
/** * Creates a timer to run a task at the specified interval. * * The task will repeat `repetitions` times. If `repetitions` is set to 0, task * will repeat forever. * * Returns -1 on error. * * The `on_finish` handler is always called (even on error). */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L1302-L1319
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
reap_child_handler
static void reap_child_handler(int sig) { (void)(sig); int old_errno = errno; while (waitpid(-1, NULL, WNOHANG) > 0) ; errno = old_errno; if (fio_old_sig_chld.sa_handler != SIG_IGN && fio_old_sig_chld.sa_handler != SIG_DFL) fio_old_sig_chld.sa_handler(sig); }
/* * Zombie Reaping * With thanks to Dr Graham D Shaw. * http://www.microhowto.info/howto/reap_zombie_processes_using_a_sigchld_handler.html */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L1365-L1374
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_reap_children
void fio_reap_children(void) { struct sigaction sa; if (fio_old_sig_chld.sa_handler) return; sa.sa_handler = reap_child_handler; sigemptyset(&sa.sa_mask); sa.sa_flags = SA_RESTART | SA_NOCLDSTOP; if (sigaction(SIGCHLD, &sa, &fio_old_sig_chld) == -1) { perror("Child reaping initialization failed"); ...
/* initializes zombie reaping for the process */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L1377-L1389
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
sig_int_handler
static void sig_int_handler(int sig) { struct sigaction *old = NULL; switch (sig) { #if !FIO_DISABLE_HOT_RESTART case SIGUSR1: fio_signal_children_flag = 1; old = &fio_old_sig_usr1; break; #endif /* fallthrough */ case SIGINT: if (!old) old = &fio_old_sig_int; /* fallthrough */ c...
/* handles the SIGUSR1, SIGINT and SIGTERM signals. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L1392-L1421
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_signal_handler_setup
static void fio_signal_handler_setup(void) { /* setup signal handling */ struct sigaction act; if (fio_trylock(&fio_signal_set_flag)) return; memset(&act, 0, sizeof(act)); act.sa_handler = sig_int_handler; sigemptyset(&act.sa_mask); act.sa_flags = SA_RESTART | SA_NOCLDSTOP; if (sigaction(SIGINT, ...
/* setup handling for the SIGUSR1, SIGPIPE, SIGINT and SIGTERM signals. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L1424-L1457
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_is_worker
int fio_is_worker(void) { return fio_data->is_worker; }
/** * Returns 1 if the current process is a worker process or a single process. * * Otherwise returns 0. * * NOTE: When cluster mode is off, the root process is also the worker process. * This means that single process instances don't automatically respawn * after critical errors. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L1489-L1489
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_is_master
int fio_is_master(void) { return fio_data->is_worker == 0 || fio_data->workers == 1; }
/** * Returns 1 if the current process is the master (root) process. * * Otherwise returns 0. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L1496-L1498
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_parent_pid
pid_t fio_parent_pid(void) { return fio_data->parent; }
/** returns facil.io's parent (root) process pid. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L1501-L1501
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_expected_concurrency
void fio_expected_concurrency(int16_t *threads, int16_t *processes) { if (!threads || !processes) return; if (!*threads && !*processes) { /* both options set to 0 - default to cores*cores matrix */ ssize_t cpu_count = fio_detect_cpu_cores(); #if FIO_CPU_CORES_LIMIT if (cpu_count > FIO_CPU_CORES_LIMI...
/** * Returns the number of expected threads / processes to be used by facil.io. * * The pointers should start with valid values that match the expected threads / * processes values passed to `fio_run`. * * The data in the pointers will be overwritten with the result. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L1525-L1592
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_poll
static size_t fio_poll(void) { /* shrink fd poll range */ size_t end = fio_data->capa; // max_protocol_fd might break TLS size_t start = 0; struct pollfd *list = NULL; fio_lock(&fio_data->lock); while (start < end && fio_data->poll[start].fd == -1) ++start; while (start < end && fio_data->poll[end - 1...
/** returns non-zero if events were scheduled, 0 if idle */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L1983-L2040
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
mock_on_ev
static void mock_on_ev(intptr_t uuid, fio_protocol_s *protocol) { (void)uuid; (void)protocol; }
/* FIO_ENGINE_POLL */ /* ***************************************************************************** Section Start Marker IO Callbacks / Event Handling ***************************************************************************** */ /* ********************************...
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L2077-L2080
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
deferred_on_close
static void deferred_on_close(void *uuid_, void *pr_) { fio_protocol_s *pr = pr_; if (pr->rsv) goto postpone; pr->on_close((intptr_t)uuid_, pr); return; postpone: fio_defer_push_task(deferred_on_close, uuid_, pr_); }
/* ***************************************************************************** Deferred event handlers - these tasks safely forward the events to the Protocol ***************************************************************************** */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L2123-L2131
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_force_event
void fio_force_event(intptr_t uuid, enum fio_io_event ev) { if (!uuid_is_valid(uuid)) return; switch (ev) { case FIO_EVENT_ON_DATA: fio_trylock(&uuid_data(uuid).scheduled); fio_defer_push_task(deferred_on_data, (void *)uuid, (void *)1); break; case FIO_EVENT_ON_TIMEOUT: fio_defer_push_task(d...
/* ***************************************************************************** Forcing / Suspending IO events ***************************************************************************** */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L2258-L2273
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_set_non_block
int fio_set_non_block(int fd) { /* If they have O_NONBLOCK, use the Posix way to do it */ #if defined(O_NONBLOCK) /* Fixme: O_NONBLOCK is defined but broken on SunOS 4.1.x and AIX 3.2.5. */ int flags; if (-1 == (flags = fcntl(fd, F_GETFL, 0))) flags = 0; #ifdef O_CLOEXEC return fcntl(fd, F_SETFL, flags | O_...
/* ***************************************************************************** Section Start Marker IO Socket Layer Read / Write / Accept / Connect / etc' ***************************************************************************** */ /* *...
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L2322-L2341
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_accept
intptr_t fio_accept(intptr_t srv_uuid) { struct sockaddr_in6 addrinfo[2]; /* grab a slice of stack (aligned) */ socklen_t addrlen = sizeof(addrinfo); int client; #ifdef SOCK_NONBLOCK client = accept4(fio_uuid2fd(srv_uuid), (struct sockaddr *)addrinfo, &addrlen, SOCK_NONBLOCK | SOCK_CLOEXEC); ...
/** * `fio_accept` accepts a new socket connection from a server socket - see the * server flag on `fio_socket`. * * NOTE: this function does NOT attach the socket to the IO reactor -see * `fio_attach`. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L2365-L2417
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_unix_socket
static intptr_t fio_unix_socket(const char *address, uint8_t server) { /* Unix socket */ struct sockaddr_un addr = {0}; size_t addr_len = strlen(address); if (addr_len >= sizeof(addr.sun_path)) { FIO_LOG_ERROR("(fio_unix_socket) address too long (%zu bytes > %zu bytes).", addr_len, sizeof(...
/* Creates a Unix socket - returning it's uuid (or -1) */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L2420-L2473
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_tcp_socket
static intptr_t fio_tcp_socket(const char *address, const char *port, uint8_t server) { /* TCP/IP socket */ // setup the address struct addrinfo hints = {0}; struct addrinfo *addrinfo; // will point to the results memset(&hints, 0, sizeof hints); // make sure the struct is...
/* Creates a TCP/IP socket - returning it's uuid (or -1) */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L2476-L2553
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_socket
intptr_t fio_socket(const char *address, const char *port, uint8_t server) { intptr_t uuid; if (port) { char *pos = (char *)port; int64_t n = fio_atol(&pos); /* make sure port is only numerical */ if (*pos) { FIO_LOG_ERROR("(fio_socket) port %s is not a number.", port); errno = EINVAL; ...
/* PUBLIC API: opens a server or client socket */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L2556-L2592
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_read
ssize_t fio_read(intptr_t uuid, void *buffer, size_t count) { if (!uuid_is_valid(uuid) || !uuid_data(uuid).open) { errno = EBADF; return -1; } if (count == 0) return 0; fio_lock(&uuid_data(uuid).sock_lock); ssize_t (*rw_read)(intptr_t, void *, void *, size_t) = uuid_data(uuid).rw_hooks->read...
/* ***************************************************************************** Socket / Connection Functions ***************************************************************************** */ /** * Returns the information available about the socket's peer address. * * If no information is available, the struct will ...
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L2766-L2795
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_write2_fn
ssize_t fio_write2_fn(intptr_t uuid, fio_write_args_s options) { if (!uuid_is_valid(uuid)) goto error; /* create packet */ fio_packet_s *packet = fio_packet_alloc(); *packet = (fio_packet_s){ .length = options.length, .offset = options.offset, .data.buffer = (void *)options.data.buffer, ...
/** * `fio_write2_fn` is the actual function behind the macro `fio_write2`. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L2800-L2862
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
FIO_DEALLOC_NOOP
void FIO_DEALLOC_NOOP(void *arg) { (void)arg; }
/** A noop function for fio_write2 in cases not deallocation is required. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L2865-L2865
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_pending
size_t fio_pending(intptr_t uuid) { if (!uuid_is_valid(uuid)) return 0; return uuid_data(uuid).packet_count; }
/** * Returns the number of `fio_write` calls that are waiting in the socket's * queue and haven't been processed. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L2871-L2875
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_close
void fio_close(intptr_t uuid) { if (!uuid_is_valid(uuid)) { errno = EBADF; return; } if (uuid_data(uuid).packet || uuid_data(uuid).sock_lock) { uuid_data(uuid).close = 1; fio_poll_add_write(fio_uuid2fd(uuid)); return; } fio_force_close(uuid); }
/** * `fio_close` marks the connection for disconnection once all the data was * sent. The actual disconnection will be managed by the `fio_flush` function. * * `fio_flash` will be automatically scheduled. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L2883-L2894
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_force_close
void fio_force_close(intptr_t uuid) { if (!uuid_is_valid(uuid)) { errno = EBADF; return; } // FIO_LOG_DEBUG("fio_force_close called for uuid %p", (void *)uuid); /* make sure the close marker is set */ if (!uuid_data(uuid).close) uuid_data(uuid).close = 1; /* clear away any packets in case we wan...
/** * `fio_force_close` closes the connection immediately, without adhering to any * protocol restrictions and without sending any remaining data in the * connection buffer. */
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L2901-L2940
675c65b509d48c21a8d1fa4c5ec53fc407643a3b
zap
github_2023
zigzap
c
fio_flush
ssize_t fio_flush(intptr_t uuid) { if (!uuid_is_valid(uuid)) goto invalid; errno = 0; ssize_t flushed = 0; int tmp; /* start critical section */ if (fio_trylock(&uuid_data(uuid).sock_lock)) goto would_block; if (!uuid_data(uuid).packet) goto flush_rw_hook; const fio_packet_s *old_packet = ...
/** * `fio_flush` attempts to write any remaining data in the internal buffer to * the underlying file descriptor and closes the underlying file descriptor once * if it's marked for closure (and all the data was sent). * * Return values: 1 will be returned if data remains in the buffer. 0 * will be returned if th...
https://github.com/zigzap/zap/blob/675c65b509d48c21a8d1fa4c5ec53fc407643a3b/facil.io/lib/facil/fio.c#L2951-L3051
675c65b509d48c21a8d1fa4c5ec53fc407643a3b