Skip to content

Commit

Permalink
BREAKING CHANGE - change srv name to io and enforce namespace + m…
Browse files Browse the repository at this point in the history
…inor fixes
  • Loading branch information
boazsegev committed Nov 23, 2024
1 parent 0580409 commit 507eacd
Show file tree
Hide file tree
Showing 27 changed files with 8,791 additions and 8,364 deletions.
60 changes: 30 additions & 30 deletions examples/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ This program uses a single thread, which reduces complexity.
#include "fio-stl/include.h"

/** Called When the client socket is attached to the server. */
FIO_SFUNC void on_attach(fio_s *io);
FIO_SFUNC void on_attach(fio_io_s *io);
/** Called there's incoming data (from the server). */
FIO_SFUNC void on_data(fio_s *io);
FIO_SFUNC void on_data(fio_io_s *io);
/** Called when the monitored IO is closed or has a fatal error. */
FIO_SFUNC void on_close(void *udata);
FIO_SFUNC void on_close(void *buf, void *udata);

/** Socket client protocol */
static fio_protocol_s CLIENT_PROTOCOL = {
static fio_io_protocol_s CLIENT_PROTOCOL = {
.on_attach = on_attach,
.on_data = on_data,
.on_close = on_close,
Expand Down Expand Up @@ -56,14 +56,14 @@ FIO_SFUNC void client_on_finish(fio_http_s *h);
FIO_SFUNC void client_on_ready(fio_http_s *h);

/** Called there's incoming data from STDIN. */
FIO_SFUNC void on_input(fio_s *io);
FIO_SFUNC void on_input(fio_io_s *io);
/** Called when STDIN closed. */
FIO_SFUNC void on_input_closed(void *udata);
FIO_SFUNC void on_input_closed(void *buf, void *udata);
/** Called if connection failed to establish. */
FIO_SFUNC void on_failed(fio_protocol_s *p, void *arg);
FIO_SFUNC void on_failed(fio_io_protocol_s *p, void *arg);

/** STDIN protocol (REPL) */
static fio_protocol_s STDIN_PROTOCOL = {
static fio_io_protocol_s STDIN_PROTOCOL = {
.on_data = on_input,
.on_close = on_input_closed,
};
Expand Down Expand Up @@ -134,7 +134,7 @@ int main(int argc, char const *argv[]) {
/* set connection task in master process. */
fio_state_callback_add(FIO_CALL_ON_START, open_client_connection, is_http);
/* start server, connection termination will stop it. */
fio_srv_start(0);
fio_io_start(0);
return 0;
}

Expand All @@ -161,10 +161,10 @@ FIO_SFUNC void open_client_connection(void *is_http) {
} else {
/* Raw TCP/IP / UDP Client */
CLIENT_PROTOCOL.timeout = (fio_cli_get_i("-t") * 1000);
FIO_ASSERT(fio_srv_connect(fio_cli_unnamed(0),
.protocol = &CLIENT_PROTOCOL,
.on_failed = on_failed,
.timeout = (fio_cli_get_i("-w") * 1000)),
FIO_ASSERT(fio_io_connect(fio_cli_unnamed(0),
.protocol = &CLIENT_PROTOCOL,
.on_failed = on_failed,
.timeout = (fio_cli_get_i("-w") * 1000)),
"Connection error!");
}
}
Expand All @@ -174,7 +174,7 @@ Input from STDIN - directed to the client's socket using pub/sub
***************************************************************************** */

/** Called there's incoming data (from STDIN / the client socket). */
FIO_SFUNC void on_input(fio_s *io) {
FIO_SFUNC void on_input(fio_io_s *io) {
struct {
size_t len;
char buf[4080];
Expand All @@ -192,10 +192,10 @@ FIO_SFUNC void on_input(fio_s *io) {
}

/** Called when STDIN closed. */
FIO_SFUNC void on_input_closed(void *udata) {
FIO_SFUNC void on_input_closed(void *buf, void *udata) {
FIO_LOG_DEBUG2("STDIN input stream closed.");
fio_srv_stop();
(void)udata;
fio_io_stop();
(void)buf, (void)udata;
}

/* Debug messages for STDIN round-trip */
Expand All @@ -208,7 +208,7 @@ void debug_subscriber(fio_msg_s *msg) {
/* Attach STDIN */
FIO_SFUNC void attach_stdin(void) {
FIO_LOG_DEBUG2("listening to user input on STDIN.");
fio_srv_attach_fd(fileno(stdin), &STDIN_PROTOCOL, NULL, NULL);
fio_io_attach_fd(fileno(stdin), &STDIN_PROTOCOL, NULL, NULL);
if (fio_cli_get_bool("-V"))
fio_subscribe(.channel = FIO_BUF_INFO1("client"),
.on_message = debug_subscriber,
Expand All @@ -220,36 +220,36 @@ IO callback(s)
***************************************************************************** */

/** Called When the client socket is attached to the server. */
FIO_SFUNC void on_attach(fio_s *io) {
FIO_SFUNC void on_attach(fio_io_s *io) {
fio_subscribe(.io = io, .channel = FIO_BUF_INFO1("client"));
fio_udata_set(io, (void *)1);
fio_io_udata_set(io, (void *)1);
FIO_LOG_DEBUG2("* connection established.\n");
FIO_LOG_DEBUG2("Connected client IO to pub/sub");
attach_stdin();
}
/** Called there's incoming data from the client socket. */
FIO_SFUNC void on_data(fio_s *io) {
FIO_SFUNC void on_data(fio_io_s *io) {
FIO_LOG_DEBUG2("on_data callback called for: %p", io);
char buf[4080];
for (;;) { /* read until done */
size_t l = fio_read(io, buf, 4080);
size_t l = fio_io_read(io, buf, 4080);
if (!l)
return;
fwrite(buf, 1, l, stdout); /* test for incomplete `write`? */
}
}

/** Called when the monitored IO is closed or has a fatal error. */
FIO_SFUNC void on_close(void *arg) {
FIO_SFUNC void on_close(void *buf, void *udata) {
FIO_LOG_DEBUG2("Connection lost, shutting down client.");
fio_srv_stop();
(void)arg;
fio_io_stop();
(void)buf, (void)udata;
}

/** Called if connection failed to establish. */
FIO_SFUNC void on_failed(fio_protocol_s *p, void *arg) {
FIO_SFUNC void on_failed(fio_io_protocol_s *p, void *arg) {
FIO_LOG_ERROR("Connection failed / no data received: %s", fio_cli_unnamed(0));
p->on_close(arg);
p->on_close(arg, NULL);
}

/* *****************************************************************************
Expand Down Expand Up @@ -287,7 +287,7 @@ FIO_SFUNC void client_on_http(fio_http_s *h) {
printf("%.*s", (int)buf.len, buf.buf);
}

fio_srv_stop();
fio_io_stop();
}

/** Called once a WebSocket / SSE connection upgrade is complete. */
Expand Down Expand Up @@ -335,13 +335,13 @@ FIO_SFUNC void client_on_eventsource(fio_http_s *h,
FIO_SFUNC void client_on_finish(fio_http_s *h) {
if (!fio_cli_get_bool("-b"))
FIO_LOG_INFO("Connection Closed");
fio_srv_stop();
fio_io_stop();
(void)h;
}

/** Called for show. */
FIO_SFUNC void client_on_ready(fio_http_s *h) {
FIO_LOG_DEBUG2("ON_READY Called! %zu bytes in outgoing buffer.",
fio_srv_backlog(fio_http_io(h)));
fio_io_backlog(fio_http_io(h)));
(void)h;
}
72 changes: 36 additions & 36 deletions examples/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ Main
***************************************************************************** */

int main(int argc, char const *argv[]) {
static fio_srv_async_s http_queue; /* async queue for worker threads. */
static fio_io_async_s http_queue; /* async queue for worker threads. */

/* setup CLI options */
fio_cli_start(
Expand Down Expand Up @@ -162,18 +162,18 @@ int main(int argc, char const *argv[]) {
}

/* Debug data: print type sizes */
FIO_LOG_DEBUG2("IO overhead: %zu bytes", sizeof(fio_s) + 8);
FIO_LOG_DEBUG2("IO overhead: %zu bytes", sizeof(fio_io_s) + 8);
FIO_LOG_DEBUG2("HTTP connection overhead: %zu bytes state + %zu bytes buffer",
sizeof(fio___http_connection_s) + 8,
FIO_HTTP_DEFAULT_MAX_LINE_LEN);
FIO_LOG_DEBUG2("HTTP handle overhead: %zu", sizeof(fio_http_s) + 8);
FIO_LOG_DEBUG2("Total HTTP overhead: %zu+%zu bytes",
sizeof(fio_s) + sizeof(fio___http_connection_s) +
sizeof(fio_io_s) + sizeof(fio___http_connection_s) +
sizeof(fio_http_s) + 24,
FIO_HTTP_DEFAULT_MAX_LINE_LEN);

/* initialize Async HTTP queue */
fio_srv_async_init(&http_queue, fio_srv_workers(fio_cli_get_i("-t")));
fio_io_async_attach(&http_queue, fio_io_workers(fio_cli_get_i("-t")));

/* Clustering */
if (fio_cli_get_i("-bp") > 0) {
Expand All @@ -183,19 +183,19 @@ int main(int argc, char const *argv[]) {
}

/* Test for TLS */
fio_tls_s *tls = (fio_cli_get("--tls-cert") && fio_cli_get("--tls-key"))
? fio_tls_cert_add(fio_tls_new(),
fio_cli_get("--tls-name"),
fio_cli_get("--tls-cert"),
fio_cli_get("--tls-key"),
fio_cli_get("-tls-pass"))
: fio_cli_get("-tls")
? fio_tls_cert_add(fio_tls_new(),
fio_cli_get("-tls-name"),
NULL,
NULL,
NULL)
: NULL;
fio_io_tls_s *tls = (fio_cli_get("--tls-cert") && fio_cli_get("--tls-key"))
? fio_io_tls_cert_add(fio_io_tls_new(),
fio_cli_get("--tls-name"),
fio_cli_get("--tls-cert"),
fio_cli_get("--tls-key"),
fio_cli_get("-tls-pass"))
: fio_cli_get("-tls")
? fio_io_tls_cert_add(fio_io_tls_new(),
fio_cli_get("-tls-name"),
NULL,
NULL,
NULL)
: NULL;
/* support -b and -p for when a URL isn't provided */
if (!fio_cli_get("-b"))
fio_cli_set(fio_cli_get("-b"), fio_cli_unnamed(0));
Expand Down Expand Up @@ -251,19 +251,19 @@ int main(int argc, char const *argv[]) {
.log = fio_cli_get_bool("-v")),
"Could not open listening socket as requested.");
/* we don't need the tls object any more. */
fio_tls_free(tls);
fio_io_tls_free(tls);

FIO_LOG_INFO("\n\tStarting HTTP echo server example app."
"\n\tEngine: " FIO_POLL_ENGINE_STR "\n\tWorkers: %d\t(%s)"
"\n\tThreads: 1+%d\t(per worker)"
"\n\tPress ^C to exit.",
fio_srv_workers(fio_cli_get_i("-w")),
(fio_srv_workers(fio_cli_get_i("-w")) ? "cluster mode"
: "single process"),
(int)http_queue.count);
FIO_LOG_INFO(
"\n\tStarting HTTP echo server example app."
"\n\tEngine: " FIO_POLL_ENGINE_STR "\n\tWorkers: %d\t(%s)"
"\n\tThreads: 1+%d\t(per worker)"
"\n\tPress ^C to exit.",
fio_io_workers(fio_cli_get_i("-w")),
(fio_io_workers(fio_cli_get_i("-w")) ? "cluster mode" : "single process"),
(int)http_queue.count);

/* start server reactor */
fio_srv_start(fio_cli_get_i("-w"));
fio_io_start(fio_cli_get_i("-w"));

/* shutdown starts here */
FIO_LOG_INFO("Shutdown complete.");
Expand Down Expand Up @@ -325,12 +325,12 @@ static void http_respond(fio_http_s *h) {
FIO_STRING_WRITE_STR2(body.buf, body.len),
FIO_STRING_WRITE_STR2("\r\n", 2));
}
/* fio_env_set(io, ...) example */
/* fio_io_env_set(io, ...) example */
if (0) {
fio_env_set(fio_http_io(h),
.name = FIO_BUF_INFO2("my key", 6),
.udata = fio_bstr_write(NULL, "my env data", 11),
.on_close = (void (*)(void *))fio_bstr_free);
fio_io_env_set(fio_http_io(h),
.name = FIO_BUF_INFO2("my key", 6),
.udata = fio_bstr_write(NULL, "my env data", 11),
.on_close = (void (*)(void *))fio_bstr_free);
}
/* ETag header example */
if (1) {
Expand All @@ -348,22 +348,22 @@ static void http_respond(fio_http_s *h) {
.dealloc = (void (*)(void *))fio_bstr_free,
.copy = 0,
.finish = 1);
#else
#else /* HTTP_RESPONSE_ECHO */
fio_http_write(h, .buf = "Hello World!", .len = 12, .finish = 1);
#endif
#endif /* HTTP_RESPONSE_ECHO */
}

/* *****************************************************************************
Pub/Sub Logger / Recorder
***************************************************************************** */
#if 0
FIO_SFUNC void logger_detached(const fio_pubsub_engine_s *eng) {
FIO_LOG_INFO("%d (logger) detached", fio_srv_pid());
FIO_LOG_INFO("%d (logger) detached", fio_io_pid());
(void)eng;
}
FIO_SFUNC void logger_on_msg(fio_msg_s *msg) {
FIO_LOG_INFO("%d (logger) pub/sub message for %s (%d):\n%s",
fio_srv_pid(),
fio_io_pid(),
(msg->channel.len ? msg->channel.buf : "<null>"),
(int)msg->filter,
(msg->message.len ? msg->message.buf : "<null>"));
Expand Down
Loading

0 comments on commit 507eacd

Please sign in to comment.