Nilorea Library
C utilities for networking, threading, graphics
ex_network.h
Go to the documentation of this file.
1
9#define NETMSG_DATA 1
10
11#include <getopt.h>
12#include <sys/time.h>
13#include <sys/types.h>
14
15#include "nilorea/n_common.h"
16#include "nilorea/n_log.h"
17#ifndef __windows__
18#include <sys/wait.h>
19#endif
20
21#include "nilorea/n_network.h"
24
25#ifdef __linux__
26void sigchld_handler( int sig )
27{
28 // waitpid() might overwrite errno, so we save and restore it:
29 int saved_errno = errno;
30
31 while(waitpid(-1, NULL, WNOHANG) > 0);
32
33 errno = saved_errno;
34 n_log( LOG_DEBUG, "Signal %d", sig );
35}
36#endif
37
38
45int send_net_datas( NETWORK *netw, N_STR *data )
46{
47 __n_assert( netw, return FALSE );
48 __n_assert( data, return FALSE );
49
50 NETW_MSG *msg = NULL ;
51 N_STR *tmpstr = NULL ;
52 N_STR *hostname = NULL ;
53
54 create_msg( &msg );
55 __n_assert( msg, return FALSE );
56
57 hostname = new_nstr( 1024 );
58 if( gethostname( hostname -> data, hostname -> length ) != 0 )
59 {
60 n_log( LOG_ERR, "Coudldn't resolve hostname. Error:%s", strerror( errno ) );
61 free_nstr( &hostname );
62 return FALSE;
63 }
64 hostname -> written = strlen( hostname -> data );
65
67
68 add_nstrptr_to_msg( msg, hostname );
69
70 n_log( LOG_DEBUG, "Adding string %s", data -> data );
71
72 add_nstrdup_to_msg( msg, data );
73
74 for( int i = 0 ; i < 10 ; i ++ )
75 {
76 int val = ( rand()%20 ) - 10 ;
77 add_int_to_msg( msg, val );
78 n_log( LOG_DEBUG, "adding %d to message", val );
79 }
80
81 tmpstr = make_str_from_msg( msg );
82
83 delete_msg( &msg );
84
85 __n_assert( tmpstr, return FALSE );
86
87 return netw_add_msg( netw, tmpstr );
88} /* send_net_datas */
89
90
98int get_net_datas( N_STR *str, N_STR **hostname, N_STR **data )
99{
100 NETW_MSG *netmsg = NULL ;
101 int type = 0;
102
103 __n_assert( str, return FALSE );
104
105 netmsg = make_msg_from_str( str );
106 __n_assert( netmsg, return FALSE );
107
108 get_int_from_msg( netmsg, &type );
109 if( type != NETMSG_DATA )
110 {
111 n_log( LOG_ERR, "Error: message is not NETMSG_DATA(%d) but %d !", NETMSG_DATA, type );
112 delete_msg( &netmsg );
113 return FALSE;
114 }
115 get_nstr_from_msg( netmsg, &(*hostname) );
116 get_nstr_from_msg( netmsg, &(*data) );
117 n_log( LOG_DEBUG, "getting string %s", (*data) -> data );
118
119 for( int i = 0 ; i < 10 ; i ++ )
120 {
121 int val = 0 ;
122 get_int_from_msg( netmsg, &val );
123 n_log( LOG_DEBUG, "getting %d from message", val );
124 }
125
126
127 delete_msg( &netmsg );
128
129 return TRUE;
130} /* get_net_datas( ... ) */
131
132
133
139void* manage_client( void *ptr )
140{
141 NETWORK *netw = (NETWORK *)ptr ;
142 N_STR *netw_exchange = NULL ;
143 int state = 0, thr_engine_state = 0 ;
144
145 n_log( LOG_NOTICE, "manage_client started for netw %d", netw -> link . sock );
146 netw_start_thr_engine( netw );
147
148 int DONE = 0 ;
149 while( !DONE )
150 {
151 if( ( netw_exchange = netw_get_msg( netw ) ) )
152 {
153 N_STR *hostname = NULL, *data = NULL ;
154
155 int type = netw_msg_get_type( netw_exchange ) ;
156 switch( type )
157 {
158 case NETMSG_DATA:
159 get_net_datas( netw_exchange, &hostname, &data );
160 if( hostname && hostname -> data && data && data -> data )
161 {
162 n_log( LOG_NOTICE, "RECV: %s: %s , %s", netw -> link . ip, hostname -> data, data -> data );
163 send_net_datas( netw, data );
164 }
165 else
166 {
167 n_log( LOG_ERR, "Error decoding request" );
168 }
169 break ;
170 default:
171 n_log( LOG_ERR, "Unknow message type %d", type );
172 DONE = 1 ;
173 break ;
174 }
175 if( data )
176 free_nstr( &data );
177 if( hostname )
178 free_nstr( &hostname );
179 if( netw_exchange )
180 free_nstr( &netw_exchange );
181 }
182 else
183 {
184 u_sleep( 500 );
185 }
186 netw_get_state( netw, &state, &thr_engine_state );
187 if( (state&NETW_EXITED) || (state&NETW_ERROR ) || (state&NETW_EXIT_ASKED) )
188 DONE = 1 ;
189 }/* while( !DONE ) */
190
191 SOCKET sockid = netw -> link . sock ;
192 n_log( LOG_NOTICE, "manage_client stopping for netw %d...", sockid );
193 netw_wait_close( &netw );
194 n_log( LOG_NOTICE, "network closed for netw %d !", sockid );
195
196 return NULL ;
197} /* manage_client(...) */
198
199
200
201
202
203
204
void * manage_client(void *ptr)
recv/send datas if any for the client
Definition: ex_network.h:139
#define NETMSG_DATA
type of data message
Definition: ex_network.h:9
int get_net_datas(N_STR *str, N_STR **hostname, N_STR **data)
decode data we got from network
Definition: ex_network.h:98
int send_net_datas(NETWORK *netw, N_STR *data)
send data to specified network
Definition: ex_network.h:45
#define __n_assert(__ptr, __ret)
macro to assert things
Definition: n_common.h:276
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition: n_log.h:74
#define LOG_DEBUG
debug-level messages
Definition: n_log.h:66
#define LOG_ERR
error conditions
Definition: n_log.h:58
#define LOG_NOTICE
normal but significant condition
Definition: n_log.h:62
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
Definition: n_str.h:222
N_STR * new_nstr(NSTRBYTE size)
create a new N_STR string
Definition: n_str.c:215
A box including a string and his lenght.
Definition: n_str.h:173
void u_sleep(unsigned int usec)
wrapper around usleep for API consistency
Definition: n_time.c:38
NETW_MSG * make_msg_from_str(N_STR *str)
Make a single message of the string.
N_STR * make_str_from_msg(NETW_MSG *msg)
Make a single string of the message.
int add_nstrptr_to_msg(NETW_MSG *msg, N_STR *str)
Add a string to the string list in the message.
int create_msg(NETW_MSG **msg)
Create a NETW_MSG *object.
Definition: n_network_msg.c:78
int netw_msg_get_type(N_STR *msg)
Get the type of message without killing the first number. Use with netw_get_XXX.
int get_nstr_from_msg(NETW_MSG *msg, N_STR **value)
Get a string from a message string list.
int add_nstrdup_to_msg(NETW_MSG *msg, N_STR *str)
Add a copy of str to the string list in the message.
int add_int_to_msg(NETW_MSG *msg, int value)
Add an int to the int list int the message.
int delete_msg(NETW_MSG **msg)
Delete a NETW_MSG *object.
int get_int_from_msg(NETW_MSG *msg, int *value)
Get a number from a message number list.
network message, array of char and int
Definition: n_network_msg.h:49
N_STR * netw_get_msg(NETWORK *netw)
Get a message from aimed NETWORK.
Definition: n_network.c:2044
int netw_add_msg(NETWORK *netw, N_STR *msg)
Add a message to send in aimed NETWORK.
Definition: n_network.c:1965
int netw_start_thr_engine(NETWORK *netw)
Start the NETWORK netw Threaded Engine.
Definition: n_network.c:2125
int netw_wait_close(NETWORK **netw)
Wait for peer closing a specified Network, destroy queues, free the structure.
Definition: n_network.c:1574
int netw_get_state(NETWORK *netw, int *state, int *thr_engine_status)
Get the state of a network.
Definition: n_network.c:1376
#define SOCKET
socket macro shortcut
Definition: n_network.h:54
Structure of a NETWORK.
Definition: n_network.h:252
Common headers and low-level hugly functions & define.
Generic log system.
Network Engine.
Network messages , serialization tools.
Thread pool declaration.