Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_network_reactor.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <getopt.h>
#include <signal.h>
#include <pthread.h>
#include "nilorea/n_common.h"
#include "nilorea/n_log.h"
#include "nilorea/n_network.h"
#include "nilorea/n_reactor.h"
+ Include dependency graph for ex_network_reactor.c:

Go to the source code of this file.

Macros

#define MODE_CLIENT   1
 
#define MODE_SERVER   0
 

Functions

int main (int argc, char **argv)
 
static int run_client (const char *host, const char *port, int attempts)
 Client-side: connect, send one message, wait for the echo, close.
 
static int run_server (const char *addr, const char *port, int target)
 Server-side: accept up to target connections into the reactor, echo one message back per client, close.
 
static void sighandler (int sig)
 
static void usage (void)
 

Variables

static volatile sig_atomic_t g_running = 1
 

Macro Definition Documentation

◆ MODE_CLIENT

#define MODE_CLIENT   1

Definition at line 64 of file ex_network_reactor.c.

◆ MODE_SERVER

#define MODE_SERVER   0

Definition at line 63 of file ex_network_reactor.c.

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 254 of file ex_network_reactor.c.

References addr, FreeNoLog, LOG_DEBUG, LOG_ERR, LOG_INFO, log_level, LOG_NOTICE, mode, MODE_CLIENT, MODE_SERVER, port, run_client(), run_server(), set_log_level(), sighandler(), and usage().

+ Here is the call graph for this function:

◆ run_client()

static int run_client ( const char *  host,
const char *  port,
int  attempts 
)
static

Client-side: connect, send one message, wait for the echo, close.

Repeats attempts times. Uses the standard thread engine (the reactor is server-side opt-in only).

Parameters
hostserver address
portserver port
attemptshow many sequential connect/echo/close cycles to run
Returns
0 on success, non-zero on error

Definition at line 222 of file ex_network_reactor.c.

References char_to_nstr(), free_nstr, g_running, N_STR::length, LOG_ERR, LOG_NOTICE, n_log, netw, netw_add_msg(), netw_close(), netw_connect(), netw_start_thr_engine(), netw_wait_msg(), NETWORK_IPALL, and port.

+ Here is the call graph for this function:

◆ run_server()

static int run_server ( const char *  addr,
const char *  port,
int  target 
)
static

Server-side: accept up to target connections into the reactor, echo one message back per client, close.

The reactor itself runs on a dedicated thread; this function is the "game thread" that drains recv queues and queues replies.

Parameters
addrbind address (may be NULL/empty for all interfaces)
portport to listen on
targetnumber of connections to handle before stopping
Returns
0 on success, non-zero on error

Definition at line 94 of file ex_network_reactor.c.

References addr, n_reactor_stats::events_processed, n_reactor_stats::fds_registered, n_reactor_stats::fds_unregistered, free_nstr, g_running, N_STR::length, NETWORK::link, list_destroy(), list_push(), LOG_ERR, LOG_INFO, LOG_NOTICE, MAX_LIST_ITEMS, n_log, n_reactor_destroy(), n_reactor_get_stats(), n_reactor_new(), n_reactor_run_thread_entry(), n_reactor_stop(), LIST::nb_items, netw_accept_into_reactor(), netw_add_msg(), netw_close(), netw_get_msg(), netw_make_listening(), NETWORK_IPALL, new_generic_list(), LIST_NODE::next, port, LIST_NODE::ptr, n_reactor_stats::reads_partial, remove_list_node_f(), N_SOCKET::sock, LIST::start, u_sleep(), n_reactor_stats::wake_signals, and n_reactor_stats::writes_partial.

+ Here is the call graph for this function:

◆ sighandler()

static void sighandler ( int  sig)
static

Definition at line 68 of file ex_network_reactor.c.

References g_running.

◆ usage()

static void usage ( void  )
static

Definition at line 73 of file ex_network_reactor.c.

Variable Documentation

◆ g_running

volatile sig_atomic_t g_running = 1
static
Examples
ex_network_reactor.c.

Definition at line 66 of file ex_network_reactor.c.

Referenced by run_client(), run_server(), and sighandler().