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

Go to the source code of this file.

Data Structures

struct  CALLBACK_DATA
 data passed to accept callback and worker threads More...
 
struct  WORKER_ARG
 data passed to worker thread function More...
 

Macros

#define MODE_POOLED   2
 
#define MODE_SINGLE_INLINE   0
 mode constants
 
#define MODE_SINGLE_POOL   1
 

Functions

static void dispatch_to_pool (NETWORK *client, CALLBACK_DATA *cb_data)
 Dispatch a client to the thread pool for handling.
 
static void handle_client (NETWORK *client)
 Handle a single client: read one message, echo it back, close.
 
static void handle_inline_and_count (NETWORK *client, CALLBACK_DATA *cb_data)
 Handle client inline in the accept thread and update counter.
 
int main (int argc, char **argv)
 
static void on_accept_pooled (NETWORK *conn, void *user_data)
 Accept pool callback: dispatches to thread pool.
 
static void sighandler (int sig)
 
static void usage (void)
 
static void * worker_handle_client (void *ptr)
 Worker thread function: handles a client and updates counter.
 

Variables

static volatile int server_running = 1
 
static int total_target = 500
 

Data Structure Documentation

◆ CALLBACK_DATA

struct CALLBACK_DATA

data passed to accept callback and worker threads

Examples
ex_accept_pool_server.c.

Definition at line 80 of file ex_accept_pool_server.c.

+ Collaboration diagram for CALLBACK_DATA:
Data Fields
size_t handled atomic counter of handled connections
pthread_mutex_t lock mutex for counter
THREAD_POOL * worker_pool thread pool for dispatching (modes single-pool and pooled)

◆ WORKER_ARG

struct WORKER_ARG

data passed to worker thread function

Examples
ex_accept_pool_server.c.

Definition at line 108 of file ex_accept_pool_server.c.

+ Collaboration diagram for WORKER_ARG:
Data Fields
CALLBACK_DATA * cb_data back pointer to callback data for counting
NETWORK * client the accepted connection

Macro Definition Documentation

◆ MODE_POOLED

#define MODE_POOLED   2
Examples
ex_accept_pool_server.c.

Definition at line 77 of file ex_accept_pool_server.c.

◆ MODE_SINGLE_INLINE

#define MODE_SINGLE_INLINE   0

mode constants

Examples
ex_accept_pool_server.c.

Definition at line 75 of file ex_accept_pool_server.c.

◆ MODE_SINGLE_POOL

#define MODE_SINGLE_POOL   1
Examples
ex_accept_pool_server.c.

Definition at line 76 of file ex_accept_pool_server.c.

Function Documentation

◆ dispatch_to_pool()

static void dispatch_to_pool ( NETWORK client,
CALLBACK_DATA cb_data 
)
static

Dispatch a client to the thread pool for handling.

Parameters
clientthe accepted NETWORK connection
cb_datacallback data with thread pool and counters
Examples
ex_accept_pool_server.c.

Definition at line 144 of file ex_accept_pool_server.c.

References __n_assert, add_threaded_process(), LOG_ERR, Malloc, n_log, netw_close(), NORMAL_PROC, worker_handle_client(), and CALLBACK_DATA::worker_pool.

Referenced by main(), and on_accept_pooled().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ handle_client()

static void handle_client ( NETWORK client)
static

Handle a single client: read one message, echo it back, close.

Parameters
clientthe accepted NETWORK connection
Examples
ex_accept_pool_server.c.

Definition at line 93 of file ex_accept_pool_server.c.

References __n_assert, netw_add_msg(), netw_close(), netw_start_thr_engine(), netw_wait_msg(), and u_sleep().

Referenced by handle_inline_and_count(), and worker_handle_client().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ handle_inline_and_count()

static void handle_inline_and_count ( NETWORK client,
CALLBACK_DATA cb_data 
)
static

Handle client inline in the accept thread and update counter.

Parameters
clientthe accepted NETWORK connection
cb_datacallback data with counters
Examples
ex_accept_pool_server.c.

Definition at line 167 of file ex_accept_pool_server.c.

References __n_assert, handle_client(), CALLBACK_DATA::handled, CALLBACK_DATA::lock, LOG_NOTICE, n_log, and netw_close().

Referenced by main().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ main()

◆ on_accept_pooled()

static void on_accept_pooled ( NETWORK conn,
void *  user_data 
)
static

Accept pool callback: dispatches to thread pool.

Parameters
connthe accepted NETWORK connection
user_datapointer to CALLBACK_DATA
Examples
ex_accept_pool_server.c.

Definition at line 187 of file ex_accept_pool_server.c.

References __n_assert, dispatch_to_pool(), and netw_close().

Referenced by main().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ sighandler()

static void sighandler ( int  sig)
static
Examples
ex_accept_pool_server.c.

Definition at line 193 of file ex_accept_pool_server.c.

References server_running.

Referenced by main().

+ Here is the caller graph for this function:

◆ usage()

static void usage ( void  )
static

Definition at line 198 of file ex_accept_pool_server.c.

◆ worker_handle_client()

static void * worker_handle_client ( void *  ptr)
static

Worker thread function: handles a client and updates counter.

Parameters
ptrpointer to WORKER_ARG (freed by this function)
Returns
NULL
Examples
ex_accept_pool_server.c.

Definition at line 120 of file ex_accept_pool_server.c.

References __n_assert, WORKER_ARG::cb_data, WORKER_ARG::client, Free, handle_client(), CALLBACK_DATA::handled, CALLBACK_DATA::lock, LOG_NOTICE, and n_log.

Referenced by dispatch_to_pool().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ server_running

volatile int server_running = 1
static
Examples
ex_accept_pool_server.c.

Definition at line 71 of file ex_accept_pool_server.c.

Referenced by main(), and sighandler().

◆ total_target

int total_target = 500
static
Examples
ex_accept_pool_server.c.

Definition at line 72 of file ex_accept_pool_server.c.

Referenced by main().