Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_network.h

Nilorea Library n_network api test.

Nilorea Library n_network api test

Author
Castagnier Mickael
Version
1.0
Date
26/05/2015
#define NETMSG_DATA 1
#include <getopt.h>
#include <sys/time.h>
#include <sys/types.h>
#include "nilorea/n_log.h"
#ifndef __windows__
#include <sys/wait.h>
#endif
__n_assert(netw, return FALSE);
__n_assert(data, return FALSE);
NETW_MSG* msg = NULL;
N_STR* tmpstr = NULL;
N_STR* hostname = NULL;
create_msg(&msg);
__n_assert(msg, return FALSE);
hostname = new_nstr(1024);
if (gethostname(hostname->data, hostname->length) != 0) {
n_log(LOG_ERR, "Coudldn't resolve hostname. Error:%s", strerror(errno));
free_nstr(&hostname);
return FALSE;
}
hostname->written = strlen(hostname->data);
add_nstrptr_to_msg(msg, hostname);
n_log(LOG_DEBUG, "Adding string %s", data->data);
add_nstrdup_to_msg(msg, data);
for (int i = 0; i < 10; i++) {
int val = (rand() % 20) - 10;
add_int_to_msg(msg, val);
n_log(LOG_DEBUG, "adding %d to message", val);
}
tmpstr = make_str_from_msg(msg);
delete_msg(&msg);
__n_assert(tmpstr, return FALSE);
return netw_add_msg(netw, tmpstr);
} /* send_net_datas */
int get_net_datas(N_STR* str, N_STR** hostname, N_STR** data) {
NETW_MSG* netmsg = NULL;
int type = 0;
__n_assert(str, return FALSE);
netmsg = make_msg_from_str(str);
__n_assert(netmsg, return FALSE);
get_int_from_msg(netmsg, &type);
if (type != NETMSG_DATA) {
n_log(LOG_ERR, "Error: message is not NETMSG_DATA(%d) but %d !", NETMSG_DATA, type);
delete_msg(&netmsg);
return FALSE;
}
get_nstr_from_msg(netmsg, &(*hostname));
get_nstr_from_msg(netmsg, &(*data));
n_log(LOG_DEBUG, "getting string %s", (*data)->data);
for (int i = 0; i < 10; i++) {
int val = 0;
get_int_from_msg(netmsg, &val);
n_log(LOG_DEBUG, "getting %d from message", val);
}
delete_msg(&netmsg);
return TRUE;
} /* get_net_datas( ... ) */
void* manage_client(void* ptr) {
NETWORK* netw = (NETWORK*)ptr;
N_STR* netw_exchange = NULL;
uint32_t state = 0;
int thr_engine_state = 0;
n_log(LOG_NOTICE, "manage_client started for netw %d", netw->link.sock);
int DONE = 0;
while (!DONE) {
if ((netw_exchange = netw_get_msg(netw))) {
N_STR *hostname = NULL, *data = NULL;
int type = netw_msg_get_type(netw_exchange);
switch (type) {
get_net_datas(netw_exchange, &hostname, &data);
if (hostname && hostname->data && data && data->data) {
n_log(LOG_NOTICE, "RECV: %s: %s , %s", netw->link.ip, hostname->data, data->data);
} else {
n_log(LOG_ERR, "Error decoding request");
}
break;
default:
n_log(LOG_ERR, "Unknow message type %d", type);
DONE = 1;
break;
}
if (data)
free_nstr(&data);
if (hostname)
free_nstr(&hostname);
if (netw_exchange)
free_nstr(&netw_exchange);
} else {
u_sleep(500);
}
netw_get_state(netw, &state, &thr_engine_state);
if ((state & NETW_EXITED) || (state & NETW_ERROR) || (state & NETW_EXIT_ASKED))
DONE = 1;
} /* while( !DONE ) */
SOCKET sockid = netw->link.sock;
n_log(LOG_NOTICE, "network closed for netw %d !", sockid);
return NULL;
} /* 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.