33#include <netinet/in.h>
37#include <sys/socket.h>
52 int fl = fcntl(fd, F_GETFL, 0);
53 (void)fcntl(fd, F_SETFL, fl | O_NONBLOCK);
61 printf(
"server: accept failed: %s\n", a.maybe_error->msg);
68 printf(
"server: read failed: %s\n", r.maybe_error->msg);
70 printf(
"server: got %zu bytes, echoing them back\n", r.
count);
80 int fd = socket(AF_INET, SOCK_STREAM, 0);
87 struct sockaddr_in to = {.sin_family = AF_INET, .sin_port = c->
port};
88 to.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
92 printf(
"client: connect failed: %s\n", err->
msg);
97 const char *msg =
"ping";
105 printf(
"client: sent \"%s\", got back \"%s\"\n", msg, buf);
114 int lst = socket(AF_INET, SOCK_STREAM, 0);
115 struct sockaddr_in a = {.sin_family = AF_INET};
116 a.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
117 if (lst < 0 || bind(lst, (
struct sockaddr *)&a,
sizeof(a)) != 0 || listen(lst, 4) != 0) {
118 perror(
"listener setup");
122 socklen_t al =
sizeof(a);
123 if (getsockname(lst, (
struct sockaddr *)&a, &al) != 0) {
124 perror(
"getsockname");
static void set_nonblock(int fd)
static srn_fiber_result_t client(srn_context_t *ctx, void *arg)
static srn_fiber_result_t server(srn_context_t *ctx, void *arg)
srn_context_t * srn_context_make(srn_engine_t *engine)
Make an empty context, by allocating a new memory block.
int srn_context_release(srn_context_t *ctx)
#define HAS_ERROR(x)
True when x – a value carrying an ERROR_HEADER – has an error attached.
srn_fiber_t * srn_fiber_spawn(srn_context_t *ctx, srn_fiber_entry_t entry, void *arg)
Make and schedule a fiber with every default, the engine's scheduler, the configured stack size,...
void * srn_fiber_result_t
What a fiber's entry produces, type-erased.
srn_io_size_result_t srn_fiber_write(int fd, const void *buf, size_t len, int64_t offset)
Write len bytes from buf to fd, suspending the calling fiber until the operation completes rather tha...
srn_error_t * srn_fiber_connect(int fd, const struct sockaddr *addr, socklen_t addrlen)
Connect the socket fd to addr/addrlen, suspending the calling fiber until the connection completes ra...
srn_io_accept_result_t srn_fiber_accept(int fd, struct sockaddr *addr, socklen_t addr_cap)
Accept a pending connection on the listening socket fd, suspending the calling fiber until one arrive...
srn_io_size_result_t srn_fiber_read(int fd, void *buf, size_t len, int64_t offset)
Read up to len bytes from fd into buf, suspending the calling fiber until the operation completes rat...
The fiber facing IO API: blocking looking calls a fiber uses to do IO.
#define SERENE_RUNTIME_SHUTDOWN(engine)
Tear down what SERENE_RUNTIME_INIT brought up, in reverse order, the engine (reactor,...
#define SERENE_RUNTIME_INIT_WITH_SCHED(engine, sched, config)
SERENE_RUNTIME_INIT plus a sched variable bound to the engine's scheduler, which every run and fiber ...
void srn_sched_run(srn_scheduler_t *sched, size_t nworkers)
Run the scheduler with nworkers os threads draining it, returning once the pool goes quiescent (every...
A runtime error, a tag classifying the failure and a human-readable message.
The result of srn_fiber_accept.
The result of a byte transfer op (read/write/sendto/recvmsg/sendmsg).