Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
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
31int send_net_datas( NETWORK *netw, N_STR *data )
32{
33 __n_assert( netw, return FALSE );
34 __n_assert( data, return FALSE );
35
36 NETW_MSG *msg = NULL ;
37 N_STR *tmpstr = NULL ;
38 N_STR *hostname = NULL ;
39
40 create_msg( &msg );
41 __n_assert( msg, return FALSE );
42
43 hostname = new_nstr( 1024 );
44 if( gethostname( hostname -> data, hostname -> length ) != 0 )
45 {
46 n_log( LOG_ERR, "Coudldn't resolve hostname. Error:%s", strerror( errno ) );
47 free_nstr( &hostname );
48 return FALSE;
49 }
50 hostname -> written = strlen( hostname -> data );
51
53
54 add_nstrptr_to_msg( msg, hostname );
55
56 n_log( LOG_DEBUG, "Adding string %s", data -> data );
57
58 add_nstrdup_to_msg( msg, data );
59
60 for( int i = 0 ; i < 10 ; i ++ )
61 {
62 int val = ( rand()%20 ) - 10 ;
63 add_int_to_msg( msg, val );
64 n_log( LOG_DEBUG, "adding %d to message", val );
65 }
66
67 tmpstr = make_str_from_msg( msg );
68
69 delete_msg( &msg );
70
71 __n_assert( tmpstr, return FALSE );
72
73 return netw_add_msg( netw, tmpstr );
74} /* send_net_datas */
75
76
84int get_net_datas( N_STR *str, N_STR **hostname, N_STR **data )
85{
86 NETW_MSG *netmsg = NULL ;
87 int type = 0;
88
89 __n_assert( str, return FALSE );
90
91 netmsg = make_msg_from_str( str );
92 __n_assert( netmsg, return FALSE );
93
94 get_int_from_msg( netmsg, &type );
95 if( type != NETMSG_DATA )
96 {
97 n_log( LOG_ERR, "Error: message is not NETMSG_DATA(%d) but %d !", NETMSG_DATA, type );
98 delete_msg( &netmsg );
99 return FALSE;
100 }
101 get_nstr_from_msg( netmsg, &(*hostname) );
102 get_nstr_from_msg( netmsg, &(*data) );
103 n_log( LOG_DEBUG, "getting string %s", (*data) -> data );
104
105 for( int i = 0 ; i < 10 ; i ++ )
106 {
107 int val = 0 ;
108 get_int_from_msg( netmsg, &val );
109 n_log( LOG_DEBUG, "getting %d from message", val );
110 }
111
112
113 delete_msg( &netmsg );
114
115 return TRUE;
116} /* get_net_datas( ... ) */
117
118
119
125void* manage_client( void *ptr )
126{
127 NETWORK *netw = (NETWORK *)ptr ;
128 N_STR *netw_exchange = NULL ;
129 int state = 0, thr_engine_state = 0 ;
130
131 n_log( LOG_NOTICE, "manage_client started for netw %d", netw -> link . sock );
132 netw_start_thr_engine( netw );
133
134 int DONE = 0 ;
135 while( !DONE )
136 {
137 if( ( netw_exchange = netw_get_msg( netw ) ) )
138 {
139 N_STR *hostname = NULL, *data = NULL ;
140
141 int type = netw_msg_get_type( netw_exchange ) ;
142 switch( type )
143 {
144 case NETMSG_DATA:
145 get_net_datas( netw_exchange, &hostname, &data );
146 if( hostname && hostname -> data && data && data -> data )
147 {
148 n_log( LOG_NOTICE, "RECV: %s: %s , %s", netw -> link . ip, hostname -> data, data -> data );
149 send_net_datas( netw, data );
150 }
151 else
152 {
153 n_log( LOG_ERR, "Error decoding request" );
154 }
155 break ;
156 default:
157 n_log( LOG_ERR, "Unknow message type %d", type );
158 DONE = 1 ;
159 break ;
160 }
161 if( data )
162 free_nstr( &data );
163 if( hostname )
164 free_nstr( &hostname );
165 if( netw_exchange )
166 free_nstr( &netw_exchange );
167 }
168 else
169 {
170 u_sleep( 500 );
171 }
172 netw_get_state( netw, &state, &thr_engine_state );
173 if( (state&NETW_EXITED) || (state&NETW_ERROR ) || (state&NETW_EXIT_ASKED) )
174 DONE = 1 ;
175 }/* while( !DONE ) */
176
177 SOCKET sockid = netw -> link . sock ;
178 n_log( LOG_NOTICE, "manage_client stopping for netw %d...", sockid );
179 netw_wait_close( &netw );
180 n_log( LOG_NOTICE, "network closed for netw %d !", sockid );
181
182 return NULL ;
183} /* manage_client(...) */
184
185
186
187
188
189
190
void * manage_client(void *ptr)
recv/send datas if any for the client
Definition ex_network.h:125
#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:84
int send_net_datas(NETWORK *netw, N_STR *data)
send data to specified network
Definition ex_network.h:31
#define __n_assert(__ptr, __ret)
macro to assert things
Definition n_common.h:284
#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.
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
N_STR * netw_get_msg(NETWORK *netw)
Get a message from aimed NETWORK.
Definition n_network.c:2197
int netw_add_msg(NETWORK *netw, N_STR *msg)
Add a message to send in aimed NETWORK.
Definition n_network.c:2118
int netw_start_thr_engine(NETWORK *netw)
Start the NETWORK netw Threaded Engine.
Definition n_network.c:2278
int netw_wait_close(NETWORK **netw)
Wait for peer closing a specified Network, destroy queues, free the structure.
Definition n_network.c:1706
int SOCKET
default socket declaration
Definition n_network.h:52
int netw_get_state(NETWORK *netw, int *state, int *thr_engine_status)
Get the state of a network.
Definition n_network.c:1484
Structure of a NETWORK.
Definition n_network.h:233
Common headers and low-level hugly functions & define.
Generic log system.
Network Engine.
Network messages , serialization tools.
Thread pool declaration.