Nilorea Library
C utilities for networking, threading, graphics
n_network.c File Reference

Network Engine. More...

#include <errno.h>
#include <limits.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include "nilorea/n_network.h"
#include "nilorea/n_network_msg.h"
#include "nilorea/n_log.h"
#include "nilorea/n_hash.h"
#include <sys/wait.h>
+ Include dependency graph for n_network.c:

Go to the source code of this file.

Macros

#define neterrno   errno
 Keep it compatible with bsd like. More...
 
#define netstrerror(code)
 BSD style errno string NO WORKING ON REDHAT. More...
 

Functions

int deplete_send_buffer (int fd, size_t timeout)
 wait until the socket is empty or timeout, checking each 100 msec. More...
 
char * get_in_addr (struct sockaddr *sa)
 get sockaddr, IPv4 or IPv6 More...
 
NETWORKnetw_accept_from (NETWORK *from)
 make a normal blocking 'accept' . More...
 
NETWORKnetw_accept_from_ex (NETWORK *from, int send_list_limit, int recv_list_limit, int non_blocking, int *retval)
 make a normal 'accept' . More...
 
NETWORKnetw_accept_nonblock_from (NETWORK *from, int blocking)
 make a normal blocking 'accept' . More...
 
int netw_add_msg (NETWORK *netw, N_STR *msg)
 Add a message to send in aimed NETWORK. More...
 
int netw_add_msg_ex (NETWORK *netw, char *str, unsigned int length)
 Add a message to send in aimed NETWORK. More...
 
int netw_close (NETWORK **netw)
 Closing a specified Network, destroy queues, free the structure. More...
 
int netw_connect (NETWORK **netw, char *host, char *port, int ip_version)
 Use this to connect a NETWORK to any listening one, unrestricted send/recv lists. More...
 
int netw_connect_ex (NETWORK **netw, char *host, char *port, int send_list_limit, int recv_list_limit, int ip_version)
 Use this to connect a NETWORK to any listening one. More...
 
int netw_destroy_pool (NETWORK_POOL **netw_pool)
 free a NETWORK_POOL *pool More...
 
N_STRnetw_get_msg (NETWORK *netw)
 Get a message from aimed NETWORK. More...
 
int netw_get_queue_status (NETWORK *netw, int *nb_to_send, int *nb_to_read)
 retrieve network send queue status More...
 
int netw_get_state (NETWORK *netw, int *state, int *thr_engine_status)
 Get the state of a network. More...
 
int netw_init_wsa (int mode, int v1, int v2)
 Do not directly use, internal api. More...
 
int netw_make_listening (NETWORK **netw, char *addr, char *port, int nbpending, int ip_version)
 Make a NETWORK be a Listening network. More...
 
NETWORKnetw_new (int send_list_limit, int recv_list_limit)
 Return an empty allocated network ready to be netw_closed. More...
 
NETWORK_POOLnetw_new_pool (int nb_min_element)
 return a new network pool of nb_min_element More...
 
int netw_pool_add (NETWORK_POOL *netw_pool, NETWORK *netw)
 add a NETWORK *netw to a NETWORK_POOL *pool More...
 
int netw_pool_broadcast (NETWORK_POOL *netw_pool, NETWORK *from, N_STR *net_msg)
 add net_msg to all network in netork pool More...
 
int netw_pool_nbclients (NETWORK_POOL *netw_pool)
 return the number of networks in netw_pool More...
 
void netw_pool_netw_close (void *netw_ptr)
 close a network from a network pool More...
 
int netw_pool_remove (NETWORK_POOL *netw_pool, NETWORK *netw)
 remove a NETWORK *netw to a NETWORK_POOL *pool More...
 
void * netw_recv_func (void *NET)
 To Thread Receiving function. More...
 
void * netw_send_func (void *NET)
 Thread send function. More...
 
int netw_send_ident (NETWORK *netw, int type, int id, N_STR *name, N_STR *passwd)
 Add a formatted NETWMSG_IDENT message to the specified network. More...
 
int netw_send_ping (NETWORK *netw, int type, int id_from, int id_to, int time)
 Add a ping reply to the network. More...
 
int netw_send_position (NETWORK *netw, int id, double X, double Y, double vx, double vy, double acc_x, double acc_y, int time_stamp)
 Add a formatted NETWMSG_IDENT message to the specified network. More...
 
int netw_send_quit (NETWORK *netw)
 Add a formatted NETMSG_QUIT message to the specified network. More...
 
int netw_send_string_to (NETWORK *netw, int id_to, N_STR *name, N_STR *chan, N_STR *txt, int color)
 Add a string to the network, aiming a specific user. More...
 
int netw_send_string_to_all (NETWORK *netw, N_STR *name, N_STR *chan, N_STR *txt, int color)
 Add a string to the network, aiming all server-side users. More...
 
int netw_set (NETWORK *netw, int flag)
 Restart or reset the specified network ability. More...
 
int netw_set_blocking (NETWORK *netw, unsigned long int is_blocking)
 Modify blocking socket mode. More...
 
int netw_set_user_id (NETWORK *netw, int id)
 associate an id and a network More...
 
int netw_setsockopt (NETWORK *netw, int optname, int value)
 Modify common socket options on the given netw. More...
 
void netw_sigchld_handler (int sig)
 signal handler to reap zombies when forking More...
 
int netw_start_thr_engine (NETWORK *netw)
 Start the NETWORK netw Threaded Engine. More...
 
int netw_stop_thr_engine (NETWORK *netw)
 Stop a NETWORK connection sending and receing thread. More...
 
int netw_wait_close (NETWORK **netw)
 Wait for peer closing a specified Network, destroy queues, free the structure. More...
 
int netw_wait_close_timed (NETWORK **netw, size_t timeout)
 Wait for peer closing a specified Network, destroy queues, free the structure. More...
 
N_STRnetw_wait_msg (NETWORK *netw, size_t refresh, size_t timeout)
 Wait a message from aimed NETWORK. More...
 
int recv_data (SOCKET s, char *buf, NSTRBYTE n)
 recv data from the socket More...
 
int recv_php (SOCKET s, int *_code, char **buf)
 recv data from the socket More...
 
int send_data (SOCKET s, char *buf, NSTRBYTE n)
 send data onto the socket More...
 
int send_php (SOCKET s, int _code, char *buf, int n)
 send data onto the socket More...
 

Variables

 N_ENUM_netw_code_type
 network erro code More...
 

Detailed Description

Network Engine.

Author
Castagnier Mickael
Version
1.0
Date
10/05/2005

Definition in file n_network.c.

Macro Definition Documentation

◆ neterrno

#define neterrno   errno

Keep it compatible with bsd like.

Definition at line 550 of file n_network.c.

◆ netstrerror

#define netstrerror (   code)
Value:
({ \
char *errmsg = NULL ; \
errno = 0 ; \
errmsg = strdup( strerror( code ) ); \
if( errno == ENOMEM ) \
{ \
errmsg = NULL ; \
} \
errmsg ; \
})

BSD style errno string NO WORKING ON REDHAT.

get last error code string

Definition at line 565 of file n_network.c.

Function Documentation

◆ deplete_send_buffer()

int deplete_send_buffer ( int  fd,
size_t  timeout 
)

wait until the socket is empty or timeout, checking each 100 msec.

Not as reliable as expected.

Parameters
fdsocket descriptor
timeouttimeout value in msec , zero => disabled
Returns
0 or the amount of remaining datas in bytes

Definition at line 1541 of file n_network.c.

Referenced by netw_wait_close_timed().

+ Here is the caller graph for this function:

◆ get_in_addr()

char * get_in_addr ( struct sockaddr *  sa)

get sockaddr, IPv4 or IPv6

Parameters
saaddrinfo to get
Returns
socket address

Definition at line 704 of file n_network.c.

Referenced by netw_accept_from_ex(), netw_connect_ex(), and netw_make_listening().

+ Here is the caller graph for this function:

◆ netw_new()

NETWORK * netw_new ( int  send_list_limit,
int  recv_list_limit 
)

Return an empty allocated network ready to be netw_closed.

Parameters
send_list_limitThread engine number of tosend message limit
recv_list_limitThread engine number of received message limit
Returns
NULL or a new empty network

Definition at line 598 of file n_network.c.

References __n_assert, Free, LOG_ERR, Malloc, n_log, netw_close(), new_generic_list(), recv_data(), and send_data().

Referenced by netw_accept_from_ex(), netw_connect_ex(), and netw_make_listening().

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

◆ netw_sigchld_handler()

void netw_sigchld_handler ( int  sig)

signal handler to reap zombies when forking

Parameters
sigthe received signal

Definition at line 581 of file n_network.c.

References LOG_DEBUG, and n_log.

Variable Documentation

◆ N_ENUM_netw_code_type

N_ENUM_netw_code_type

network erro code

Definition at line 21 of file n_network.c.