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#ifdef __windows__
45 if (gethostname(hostname->data, (int)hostname->length) != 0) {
46#else
47 if (gethostname(hostname->data, hostname->length) != 0) {
48#endif
49 n_log(LOG_ERR, "Couldn't resolve hostname. Error:%s", strerror(errno));
50 free_nstr(&hostname);
51 return FALSE;
52 }
53 hostname->written = strlen(hostname->data);
54
56
57 add_nstrptr_to_msg(msg, hostname);
58
59 n_log(LOG_DEBUG, "Adding string %s", data->data);
60
61 add_nstrdup_to_msg(msg, data);
62
63 for (int i = 0; i < 10; i++) {
64 int val = (rand() % 20) - 10;
65 add_int_to_msg(msg, val);
66 n_log(LOG_DEBUG, "adding %d to message", val);
67 }
68
69 tmpstr = make_str_from_msg(msg);
70
71 delete_msg(&msg);
72
73 __n_assert(tmpstr, return FALSE);
74
75 return netw_add_msg(netw, tmpstr);
76} /* send_net_datas */
77
85int get_net_datas(N_STR* str, N_STR** hostname, N_STR** data) {
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 n_log(LOG_ERR, "Error: message is not NETMSG_DATA(%d) but %d !", NETMSG_DATA, type);
97 delete_msg(&netmsg);
98 return FALSE;
99 }
100 get_nstr_from_msg(netmsg, &(*hostname));
101 get_nstr_from_msg(netmsg, &(*data));
102 n_log(LOG_DEBUG, "getting string %s", (*data)->data);
103
104 for (int i = 0; i < 10; i++) {
105 int val = 0;
106 get_int_from_msg(netmsg, &val);
107 n_log(LOG_DEBUG, "getting %d from message", val);
108 }
109
110 delete_msg(&netmsg);
111
112 return TRUE;
113} /* get_net_datas( ... ) */
114
120void* manage_client(void* ptr) {
121 NETWORK* netw = (NETWORK*)ptr;
122 N_STR* netw_exchange = NULL;
123 uint32_t state = 0;
124 int thr_engine_state = 0;
125
126 n_log(LOG_NOTICE, "manage_client started for netw %d", netw->link.sock);
128
129 int DONE = 0;
130 while (!DONE) {
131 if ((netw_exchange = netw_get_msg(netw))) {
132 N_STR *hostname = NULL, *data = NULL;
133
134 int type = netw_msg_get_type(netw_exchange);
135 if (type == -1) {
136 n_log(LOG_ERR, "Error getting message type from network exchange");
137 free_nstr(&netw_exchange);
138 continue;
139 }
140 switch (type) {
141 case NETMSG_DATA:
142 get_net_datas(netw_exchange, &hostname, &data);
143 if (hostname && hostname->data && data && data->data) {
144 n_log(LOG_NOTICE, "RECV: %s: %s , %s", netw->link.ip, hostname->data, data->data);
145 send_net_datas(netw, data);
146 } else {
147 n_log(LOG_ERR, "Error decoding request");
148 }
149 break;
150 default:
151 n_log(LOG_ERR, "Unknow message type %d", type);
152 DONE = 1;
153 break;
154 }
155 if (data)
156 free_nstr(&data);
157 if (hostname)
158 free_nstr(&hostname);
159 if (netw_exchange)
160 free_nstr(&netw_exchange);
161 } else {
162 u_sleep(500);
163 }
164 netw_get_state(netw, &state, &thr_engine_state);
165 if ((state & NETW_EXITED) || (state & NETW_ERROR) || (state & NETW_EXIT_ASKED))
166 DONE = 1;
167 } /* while( !DONE ) */
168
169 SOCKET sockid = netw->link.sock;
171 n_log(LOG_NOTICE, "network closed for netw %d !", sockid);
172
173 return NULL;
174} /* manage_client(...) */
#define NETMSG_DATA
type of data message
int DONE
Definition ex_fluid.c:59
NETWORK * netw
Network for server mode, accepting incomming.
Definition ex_network.c:38
void * manage_client(void *ptr)
recv/send datas if any for the client
Definition ex_network.h:120
int get_net_datas(N_STR *str, N_STR **hostname, N_STR **data)
decode data we got from network
Definition ex_network.h:85
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:278
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:88
#define LOG_DEBUG
debug-level messages
Definition n_log.h:83
#define LOG_ERR
error conditions
Definition n_log.h:75
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:79
size_t written
size of the written data inside the string
Definition n_str.h:66
char * data
the string
Definition n_str.h:62
size_t length
length of string (in case we wanna keep information after the 0 end of string value)
Definition n_str.h:64
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
Definition n_str.h:201
N_STR * new_nstr(NSTRBYTE size)
create a new N_STR string
Definition n_str.c:206
A box including a string and his lenght.
Definition n_str.h:60
void u_sleep(unsigned int usec)
wrapper around usleep for API consistency
Definition n_time.c:53
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 an int from a message int list.
network message, array of char and int
char * ip
ip of the connected socket
Definition n_network.h:244
N_SOCKET link
networking socket
Definition n_network.h:326
SOCKET sock
a normal socket
Definition n_network.h:242
N_STR * netw_get_msg(NETWORK *netw)
Get a message from aimed NETWORK.
Definition n_network.c:2977
int netw_add_msg(NETWORK *netw, N_STR *msg)
Add a message to send in aimed NETWORK.
Definition n_network.c:2914
int netw_start_thr_engine(NETWORK *netw)
Start the NETWORK netw Threaded Engine.
Definition n_network.c:3050
int SOCKET
default socket declaration
Definition n_network.h:88
int netw_get_state(NETWORK *netw, uint32_t *state, int *thr_engine_status)
Get the state of a network.
Definition n_network.c:1924
int netw_close(NETWORK **netw)
Closing a specified Network, destroy queues, free the structure.
Definition n_network.c:2041
@ NETW_EXITED
Definition n_network.h:232
@ NETW_ERROR
Definition n_network.h:232
@ NETW_EXIT_ASKED
Definition n_network.h:232
Structure of a NETWORK.
Definition n_network.h:258
Common headers and low-level functions & define.
Generic log system.
Network Engine.
Network messages , serialization tools.
Thread pool declaration.