Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_network.h
Go to the documentation of this file.
1
10#define NETMSG_DATA 1
11
12#include <getopt.h>
13#include <sys/time.h>
14#include <sys/types.h>
15
16#include "nilorea/n_common.h"
17#include "nilorea/n_log.h"
18#ifndef __windows__
19#include <sys/wait.h>
20#endif
21
22#include "nilorea/n_network.h"
25
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 n_log(LOG_ERR, "Coudldn't resolve hostname. Error:%s", strerror(errno));
46 free_nstr(&hostname);
47 return FALSE;
48 }
49 hostname->written = strlen(hostname->data);
50
52
53 add_nstrptr_to_msg(msg, hostname);
54
55 n_log(LOG_DEBUG, "Adding string %s", data->data);
56
57 add_nstrdup_to_msg(msg, data);
58
59 for (int i = 0; i < 10; i++) {
60 int val = (rand() % 20) - 10;
61 add_int_to_msg(msg, val);
62 n_log(LOG_DEBUG, "adding %d to message", val);
63 }
64
65 tmpstr = make_str_from_msg(msg);
66
67 delete_msg(&msg);
68
69 __n_assert(tmpstr, return FALSE);
70
71 return netw_add_msg(netw, tmpstr);
72} /* send_net_datas */
73
81int get_net_datas(N_STR* str, N_STR** hostname, N_STR** data) {
82 NETW_MSG* netmsg = NULL;
83 int type = 0;
84
85 __n_assert(str, return FALSE);
86
87 netmsg = make_msg_from_str(str);
88 __n_assert(netmsg, return FALSE);
89
90 get_int_from_msg(netmsg, &type);
91 if (type != NETMSG_DATA) {
92 n_log(LOG_ERR, "Error: message is not NETMSG_DATA(%d) but %d !", NETMSG_DATA, type);
93 delete_msg(&netmsg);
94 return FALSE;
95 }
96 get_nstr_from_msg(netmsg, &(*hostname));
97 get_nstr_from_msg(netmsg, &(*data));
98 n_log(LOG_DEBUG, "getting string %s", (*data)->data);
99
100 for (int i = 0; i < 10; i++) {
101 int val = 0;
102 get_int_from_msg(netmsg, &val);
103 n_log(LOG_DEBUG, "getting %d from message", val);
104 }
105
106 delete_msg(&netmsg);
107
108 return TRUE;
109} /* get_net_datas( ... ) */
110
116void* manage_client(void* ptr) {
117 NETWORK* netw = (NETWORK*)ptr;
118 N_STR* netw_exchange = NULL;
119 uint32_t state = 0;
120 int thr_engine_state = 0;
121
122 n_log(LOG_NOTICE, "manage_client started for netw %d", netw->link.sock);
124
125 int DONE = 0;
126 while (!DONE) {
127 if ((netw_exchange = netw_get_msg(netw))) {
128 N_STR *hostname = NULL, *data = NULL;
129
130 int type = netw_msg_get_type(netw_exchange);
131 switch (type) {
132 case NETMSG_DATA:
133 get_net_datas(netw_exchange, &hostname, &data);
134 if (hostname && hostname->data && data && data->data) {
135 n_log(LOG_NOTICE, "RECV: %s: %s , %s", netw->link.ip, hostname->data, data->data);
136 send_net_datas(netw, data);
137 } else {
138 n_log(LOG_ERR, "Error decoding request");
139 }
140 break;
141 default:
142 n_log(LOG_ERR, "Unknow message type %d", type);
143 DONE = 1;
144 break;
145 }
146 if (data)
147 free_nstr(&data);
148 if (hostname)
149 free_nstr(&hostname);
150 if (netw_exchange)
151 free_nstr(&netw_exchange);
152 } else {
153 u_sleep(500);
154 }
155 netw_get_state(netw, &state, &thr_engine_state);
156 if ((state & NETW_EXITED) || (state & NETW_ERROR) || (state & NETW_EXIT_ASKED))
157 DONE = 1;
158 } /* while( !DONE ) */
159
160 SOCKET sockid = netw->link.sock;
162 n_log(LOG_NOTICE, "network closed for netw %d !", sockid);
163
164 return NULL;
165} /* manage_client(...) */
int DONE
Definition ex_fluid.c:41
NETWORK * netw
Network for server mode, accepting incomming.
Definition ex_network.c:20
void * manage_client(void *ptr)
recv/send datas if any for the client
Definition ex_network.h:116
#define NETMSG_DATA
type of data message
Definition ex_network.h:10
int get_net_datas(N_STR *str, N_STR **hostname, N_STR **data)
decode data we got from network
Definition ex_network.h:81
int send_net_datas(NETWORK *netw, N_STR *data)
send data to specified network
Definition ex_network.h:32
#define __n_assert(__ptr, __ret)
macro to assert things
Definition n_common.h:256
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:70
#define LOG_DEBUG
debug-level messages
Definition n_log.h:65
#define LOG_ERR
error conditions
Definition n_log.h:57
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:61
size_t written
size of the written data inside the string
Definition n_str.h:45
char * data
the string
Definition n_str.h:41
size_t length
length of string (in case we wanna keep information after the 0 end of string value)
Definition n_str.h:43
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
Definition n_str.h:176
N_STR * new_nstr(NSTRBYTE size)
create a new N_STR string
Definition n_str.c:180
A box including a string and his lenght.
Definition n_str.h:39
void u_sleep(unsigned int usec)
wrapper around usleep for API consistency
Definition n_time.c:35
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
char * ip
ip of the connected socket
Definition n_network.h:225
N_SOCKET link
networking socket
Definition n_network.h:305
SOCKET sock
a normal socket
Definition n_network.h:223
N_STR * netw_get_msg(NETWORK *netw)
Get a message from aimed NETWORK.
Definition n_network.c:1997
int netw_add_msg(NETWORK *netw, N_STR *msg)
Add a message to send in aimed NETWORK.
Definition n_network.c:1930
int netw_start_thr_engine(NETWORK *netw)
Start the NETWORK netw Threaded Engine.
Definition n_network.c:2069
int SOCKET
default socket declaration
Definition n_network.h:64
int netw_get_state(NETWORK *netw, uint32_t *state, int *thr_engine_status)
Get the state of a network.
Definition n_network.c:1388
int netw_close(NETWORK **netw)
Closing a specified Network, destroy queues, free the structure.
Definition n_network.c:1503
@ NETW_EXITED
Definition n_network.h:213
@ NETW_ERROR
Definition n_network.h:213
@ NETW_EXIT_ASKED
Definition n_network.h:213
Structure of a NETWORK.
Definition n_network.h:239
Common headers and low-level functions & define.
Generic log system.
Network Engine.
Network messages , serialization tools.
Thread pool declaration.