6 ex__network_8h
gullradriel edited this page 2026-10-09 00:55:18 +02:00

examples/ex_network.h

Functions

Name
int send_net_datas(NETWORK * netw, N_STR * data)
send data to specified network
void * manage_client(void * ptr)
recv/send datas if any for the client
int get_net_datas(N_STR * str, N_STR ** hostname, N_STR ** data)
decode data we got from network

Defines

Name
NETMSG_DATA
type of data message

Functions Documentation

function send_net_datas

int send_net_datas(
    NETWORK * netw,
    N_STR * data
)

send data to specified network

Parameters:

  • netw the target network
  • data the data to send

Return: TRUE or FALSE

function manage_client

void * manage_client(
    void * ptr
)

recv/send datas if any for the client

Parameters:

  • ptr pointer to the client structure to manage

Return: NULL

function get_net_datas

int get_net_datas(
    N_STR * str,
    N_STR ** hostname,
    N_STR ** data
)

decode data we got from network

Parameters:

  • str the string to decode
  • hostname decoded hostname
  • data decoded data

Return: TRUE or FALSE

Macros Documentation

define NETMSG_DATA

#define NETMSG_DATA 1

type of data message

Source code

/*
 * Nilorea Library
 * Copyright (C) 2005-2026 Castagnier Mickael
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 */


#define NETMSG_DATA 1

#include <getopt.h>
#include <sys/time.h>
#include <sys/types.h>

#include "nilorea/n_common.h"
#include "nilorea/n_log.h"
#ifndef __windows__
#include <sys/wait.h>
#endif

#include "nilorea/n_network.h"
#include "nilorea/n_network_msg.h"
#include "nilorea/n_thread_pool.h"

int send_net_datas(NETWORK* netw, N_STR* data) {
    __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);
#ifdef __windows__
    if (gethostname(hostname->data, (int)hostname->length) != 0) {
#else
    if (gethostname(hostname->data, hostname->length) != 0) {
#endif
        n_log(LOG_ERR, "Couldn't resolve hostname. Error:%s", strerror(errno));
        free_nstr(&hostname);
        return FALSE;
    }
    hostname->written = strlen(hostname->data);

    add_int_to_msg(msg, NETMSG_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);
    netw_start_thr_engine(netw);

    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);
            if (type == -1) {
                n_log(LOG_ERR, "Error getting message type from network exchange");
                free_nstr(&netw_exchange);
                continue;
            }
            switch (type) {
                case NETMSG_DATA:
                    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);
                        send_net_datas(netw, 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;
    netw_close(&netw);
    n_log(LOG_NOTICE, "network closed for netw %d !", sockid);

    return NULL;
} /* manage_client(...) */