otpimisation d'un code en c
Répondre à la discussion
Affichage des résultats 1 à 5 sur 5

otpimisation d'un code en c



  1. #1
    bpm37093

    otpimisation d'un code en c


    ------

    Bonjour a tous,
    Je pose cette question sur ce forum car je veux otpimiser un code en C mais je sais pas par ou commencer. Si c'est possible d'avoir un peu d'aide ou des tutos ce serait sympa.
    Merci par avance.



    Voici le code: (si la mise en page du code ne vous convient pas prévener moi)
    Pour le dossier SIM_SNIFFER (serveur) les fichiers que l'ont va devoir crée sont:
    • SIM_SNIFFER_main.c
    • bin.mk
    Puis nous commençons le code en C par le fichier SIM_SNIFFER_main.c
    /**
    * @file
    */
    #include <unistd.h> // en-tête standard bibliothèque
    #include <stdio.h>
    #include <signal.h>
    #include <sys/time.h>
    #include <errno.h>
    #include <stdio.h>
    #include <string.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include "L_LOG.h"
    #include "L_MOD.h"
    #include "L_SIO.h"
    #ifndef MAX
    #define MAX(a,b) ((a) > (b) ? (a) : (b))
    #endif

    ///Control the main loop, if 1 main loop runs, when go to 0 main loop stop.
    int KeepRunning = 1;
    /// Configuration parameters file path, max lenght of L_MOD_RUN_PARAM_MAX_SIZE.
    char CfgPath[L_MOD_RUN_PARAM_MAX_SIZE]; // Path to config file
    fd_set fd_set_read,fd_set_write;
    int fd_listen = -1;
    //int fd_connection[L_SIO_MAX_NR_OF_PORTS] = {-1};
    //int fd_connection_nr = 0;
    //int fd_writable[L_SIO_MAX_NR_OF_PORTS] = {XRWS_FALSE};
    int fd_max = 0;
    /**
    * @brief ascessor to the configuration parameters file path.
    * @return the configuration parameters file path.
    */
    char * getCfgParameterPath ()
    {
    return CfgPath;
    }
    /**
    * @brief This function is executed at exit, it permit to stop the main loop
    * @param[in] sig the signal recieve to stop appication.
    */
    void do_at_exit(int sig)
    {
    DEBUG("do at exit");
    KeepRunning = 0;
    }
    /**
    * @brief Map the interuption signal with do_at_exit function
    */
    void handle_SIGINT()
    {
    struct sigaction orig_action;
    struct sigaction action;
    action.sa_flags = 0;
    action.sa_handler = do_at_exit;
    if(-1 == sigaction(SIGINT, &action, &orig_action))
    {
    ERROR("Error in fu. sigaction");
    }
    }
    static XRWS_STATUS SIM_SNIFFER_API_handle_data(in t p_fd,char* p_data, int
    p_nr_of_bytes, int* p_nr_of_bytes_parsed){
    INFO("connection call back");
    unsigned char msg[5]={2,1,2,12,1};
    if(L_SIO_write(p_fd,msg,5)!=XR WS_OK){
    ERROR("Error in fu. L_SIO_write");
    return XRWS_ERROR;
    } return XRWS_OK;
    }
    static void build_select_fd_set(){
    int i;
    L_SIO_port_t *l_port;
    L_SIO_TCP_config_t *l_cfg;
    FD_ZERO(&fd_set_read);
    FD_ZERO(&fd_set_write);
    FD_SET(fd_listen, &fd_set_read);

    fd_max = fd_listen;
    for(i=0;i<L_SIO_get_port_nr(); i++){
    l_port = L_SIO_get_port(i);
    if(l_port->type == L_SIO_PORT_TYPE_TCP){
    l_cfg = (L_SIO_TCP_config_t *)(&(l_port->cfg));
    FD_SET(l_port->fd, &fd_set_read);
    if(l_cfg->writable==XRWS_FALSE){
    FD_SET(l_port->fd, &fd_set_write);
    } fd_max =
    MAX(fd_max,l_port->fd);
    }
    }
    }
    /**
    * @brief Main function
    *
    * @param[in] argc argument count.
    * @param[in] argv arguments
    * @return exit code
    */
    int main(int argc, char **argv)
    {
    sigset_t orig_sigmask;
    int rc = 0;
    int i,l_read_byte_nr,l_client_fd;
    struct timeval l_timeout; /* Timeout for
    select */
    L_SIO_TCP_config_t l_cfg_server = {0};
    L_SIO_serial_read_CB
    l_CB[]={(L_SIO_serial_read_CB)SIM_SN IFFER_API_handle_data};
    L_SIO_port_t *l_port = NULL;
    L_SIO_TCP_config_t *l_cfg = NULL;
    //****** INITIALIZATION ******
    handle_SIGINT();
    if(L_MOD_RUN_PARAM_retrieve(ar gc
    ,argv
    ,CfgPath)!=0)
    {
    ERROR("Error in fu. L_MOD_RUN_PARAM_retrieve");
    return L_MOD_EXIT_ERROR;
    }
    // init signals
    if(L_MOD_SIG_init(&orig_sigmas k)!=0)
    {
    ERROR_EC("Error in fu. L_MOD_SIG_init");
    return L_MOD_EXIT_ERROR;
    } DEBUG("\tSIG init OK");
    // config for server
    l_cfg_server.type = L_SIO_SOCKET_TYPE_LISTENER;
    //configuration des port pour la connection
    l_cfg_server.address.sin_famil y = AF_INET;
    l_cfg_server.address.sin_addr. s_addr = INADDR_ANY;
    l_cfg_server.address.sin_port = htons(2010);
    // open server
    if(L_SIO_TCP_open_listener_soc ket(&l_cfg_server, &fd_listen)!=XRWS_OK){
    //appel de la bibliothèque L_SIO pour l'ouverture des sockets
    ERROR("Error in function L_SIO_TCP_open");

    return XRWS_ERROR;
    }
    DEBUG("Module was initialized correctly");
    //****** MAIN LOOP ******
    while(KeepRunning==1)
    {
    l_timeout.tv_sec = 5;
    l_timeout.tv_usec = 0;
    build_select_fd_set();
    rc = select(fd_max+1,&fd_set_read,& fd_set_write,NULL,&l_timeout);
    //fonction SELECT alternative au mutex
    DEBUG("Select (%d)",rc);
    if(rc<0){
    ERROR("Error in select function: '%s'",strerror(errno));
    }else if(rc == 0){
    ERROR("Select time out");
    }else{
    if(FD_ISSET(fd_listen,&fd_set_ read)){
    DEBUG("Listening Server socket is set");
    if(L_SIO_TCP_accept(&l_client_ fd,l_CB,1,fd_listen)!
    =XRWS_OK){
    ERROR("Error in function L_SIO_TCP_accept");
    return XRWS_ERROR;
    } printf("New client on fd: %
    d\n",l_client_fd);
    fflush(stdout);
    DEBUG("client connection was accepted on fd:
    %d",l_client_fd);
    rc --;
    } if(rc >
    0){
    for(i=0;i<L_SIO_get_port_nr(); i++){
    l_port = L_SIO_get_port(i);
    if(l_port->type == L_SIO_PORT_TYPE_TCP){
    l_cfg = (L_SIO_TCP_config_t *)(&(l_port-
    >cfg));
    if(FD_ISSET(l_port->fd,&fd_set_read) && rc >
    0){
    // Client is notify that there is
    something to read
    DEBUG("Client connection (%d) has
    something to read", i);
    if(L_SIO_read(l_port-
    >fd,&l_read_byte_nr)!=XRWS_OK) {
    New client on fd: 5
    New client on fd: 5
    New client on fd: 5
    New client on fd: 5 ERROR("Error in fu L_SIO_read");
    return XRWS_ERROR;
    } rc --;
    } if(l_port->fd != -
    1){ //this protect from a
    previously closed fd
    if(FD_ISSET(l_port->fd,&fd_set_write)
    && rc > 0){
    DEBUG("Client connection (%d) is
    ready to write", i);
    l_cfg->writable = XRWS_TRUE;
    rc --;

    } if(rc <= 0){
    break; // stop the loop because
    there's nothing to read anymore
    }
    }
    }
    }
    }
    } //close server connection
    //fermeture de la connection
    for (i=0;i<L_SIO_get_port_nr();i++ ){
    if(L_SIO_close(L_SIO_get_port( i)->fd)!=XRWS_OK){
    ERROR("Error in fu L_SIO_close on fd: %d",L_SIO_get_port(i)-
    >fd);
    return XRWS_ERROR;
    }
    } //close server
    if(close(fd_listen)!=XRWS_OK){
    ERROR("Error in fu L_SIO_close on fd: %d",fd_listen);
    return XRWS_ERROR;
    } DEBUG("Exit properly");
    return 0;
    }

    -----

  2. #2
    ventilopomme

    Re : otpimisation d'un code en c

    merci de nous dire ce que tu dois faire au lieu de nous balancer ton code


    Code:
    /**
    * @file
    */
    #include <unistd.h> // en-tête standard bibliothèque
    #include <stdio.h>
    #include <signal.h>
    #include <sys/time.h>
    #include <errno.h>
    #include <stdio.h>
    #include <string.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include "L_LOG.h"
    #include "L_MOD.h"
    #include "L_SIO.h"
    #ifndef MAX
    #define MAX(a,b) ((a) > (b) ? (a) : (b))
    #endif
    
    ///Control the main loop, if 1 main loop runs, when go to 0 main loop stop.
    int KeepRunning = 1;
    /// Configuration parameters file path, max lenght of L_MOD_RUN_PARAM_MAX_SIZE.
    char CfgPath[L_MOD_RUN_PARAM_MAX_SIZE]; // Path to config file
    fd_set fd_set_read,fd_set_write;
    int fd_listen = -1;
    //int fd_connection[L_SIO_MAX_NR_OF_PORTS] = {-1};
    //int fd_connection_nr = 0;
    //int fd_writable[L_SIO_MAX_NR_OF_PORTS] = {XRWS_FALSE};
    int fd_max = 0;
    /**
    * @brief ascessor to the configuration parameters file path.
    * @return the configuration parameters file path.
    */
    char * getCfgParameterPath ()
    {
    return CfgPath;
    }
    /**
    * @brief This function is executed at exit, it permit to stop the main loop
    * @param[in] sig the signal recieve to stop appication.
    */
    void do_at_exit(int sig)
    {
    DEBUG("do at exit");
    KeepRunning = 0;
    }
    /**
    * @brief Map the interuption signal with do_at_exit function
    */
    void handle_SIGINT()
    {
    struct sigaction orig_action;
    struct sigaction action;
    action.sa_flags = 0;
    action.sa_handler = do_at_exit;
    if(-1 == sigaction(SIGINT, &action, &orig_action))
    {
    ERROR("Error in fu. sigaction");
    }
    }
    static XRWS_STATUS SIM_SNIFFER_API_handle_data(in t p_fd,char* p_data, int
    p_nr_of_bytes, int* p_nr_of_bytes_parsed){
    INFO("connection call back");
    unsigned char msg[5]={2,1,2,12,1};
    if(L_SIO_write(p_fd,msg,5)!=XR WS_OK){
    ERROR("Error in fu. L_SIO_write");
    return XRWS_ERROR;
    } return XRWS_OK;
    }
    static void build_select_fd_set(){
    int i;
    L_SIO_port_t *l_port;
    L_SIO_TCP_config_t *l_cfg;
    FD_ZERO(&fd_set_read);
    FD_ZERO(&fd_set_write);
    FD_SET(fd_listen, &fd_set_read);
    
    fd_max = fd_listen;
    for(i=0;i<L_SIO_get_port_nr(); i++){
    l_port = L_SIO_get_port(i);
    if(l_port->type == L_SIO_PORT_TYPE_TCP){
    l_cfg = (L_SIO_TCP_config_t *)(&(l_port->cfg));
    FD_SET(l_port->fd, &fd_set_read);
    if(l_cfg->writable==XRWS_FALSE){
    FD_SET(l_port->fd, &fd_set_write);
    } fd_max =
    MAX(fd_max,l_port->fd);
    }
    }
    }
    /**
    * @brief Main function
    *
    * @param[in] argc argument count.
    * @param[in] argv arguments
    * @return exit code
    */
    int main(int argc, char **argv)
    {
    sigset_t orig_sigmask;
    int rc = 0;
    int i,l_read_byte_nr,l_client_fd;
    struct timeval l_timeout; /* Timeout for
    select */
    L_SIO_TCP_config_t l_cfg_server = {0};
    L_SIO_serial_read_CB
    l_CB[]={(L_SIO_serial_read_CB)SIM_SN IFFER_API_handle_data};
    L_SIO_port_t *l_port = NULL;
    L_SIO_TCP_config_t *l_cfg = NULL;
    //****** INITIALIZATION ******
    handle_SIGINT();
    if(L_MOD_RUN_PARAM_retrieve(ar gc
    ,argv
    ,CfgPath)!=0)
    {
    ERROR("Error in fu. L_MOD_RUN_PARAM_retrieve");
    return L_MOD_EXIT_ERROR;
    }
    // init signals
    if(L_MOD_SIG_init(&orig_sigmas k)!=0)
    {
    ERROR_EC("Error in fu. L_MOD_SIG_init");
    return L_MOD_EXIT_ERROR;
    } DEBUG("\tSIG init OK");
    // config for server
    l_cfg_server.type = L_SIO_SOCKET_TYPE_LISTENER;
    //configuration des port pour la connection
    l_cfg_server.address.sin_famil y = AF_INET;
    l_cfg_server.address.sin_addr. s_addr = INADDR_ANY;
    l_cfg_server.address.sin_port = htons(2010);
    // open server
    if(L_SIO_TCP_open_listener_soc ket(&l_cfg_server, &fd_listen)!=XRWS_OK){
    //appel de la bibliothèque L_SIO pour l'ouverture des sockets
    ERROR("Error in function L_SIO_TCP_open");
    
    return XRWS_ERROR;
    }
    DEBUG("Module was initialized correctly");
    //****** MAIN LOOP ******
    while(KeepRunning==1)
    {
    l_timeout.tv_sec = 5;
    l_timeout.tv_usec = 0;
    build_select_fd_set();
    rc = select(fd_max+1,&fd_set_read,& fd_set_write,NULL,&l_timeout);
    //fonction SELECT alternative au mutex
    DEBUG("Select (%d)",rc);
    if(rc<0){
    ERROR("Error in select function: '%s'",strerror(errno));
    }else if(rc == 0){
    ERROR("Select time out");
    }else{
    if(FD_ISSET(fd_listen,&fd_set_ read)){
    DEBUG("Listening Server socket is set");
    if(L_SIO_TCP_accept(&l_client_ fd,l_CB,1,fd_listen)!
    =XRWS_OK){
    ERROR("Error in function L_SIO_TCP_accept");
    return XRWS_ERROR;
    } printf("New client on fd: %
    d\n",l_client_fd);
    fflush(stdout);
    DEBUG("client connection was accepted on fd:
    %d",l_client_fd);
    rc --;
    } if(rc >
    0){
    for(i=0;i<L_SIO_get_port_nr(); i++){
    l_port = L_SIO_get_port(i);
    if(l_port->type == L_SIO_PORT_TYPE_TCP){
    l_cfg = (L_SIO_TCP_config_t *)(&(l_port-
    >cfg));
    if(FD_ISSET(l_port->fd,&fd_set_read) && rc >
    0){
    // Client is notify that there is
    something to read
    DEBUG("Client connection (%d) has
    something to read", i);
    if(L_SIO_read(l_port-
    >fd,&l_read_byte_nr)!=XRWS_OK) {
    New client on fd: 5
    New client on fd: 5
    New client on fd: 5
    New client on fd: 5 ERROR("Error in fu L_SIO_read");
    return XRWS_ERROR;
    } rc --;
    } if(l_port->fd != -
    1){ //this protect from a
    previously closed fd
    if(FD_ISSET(l_port->fd,&fd_set_write)
    && rc > 0){
    DEBUG("Client connection (%d) is
    ready to write", i);
    l_cfg->writable = XRWS_TRUE;
    rc --;
    
    } if(rc <= 0){
    break; // stop the loop because
    there's nothing to read anymore
    }
    }
    }
    }
    }
    } //close server connection
    //fermeture de la connection
    for (i=0;i<L_SIO_get_port_nr();i++ ){
    if(L_SIO_close(L_SIO_get_port( i)->fd)!=XRWS_OK){
    ERROR("Error in fu L_SIO_close on fd: %d",L_SIO_get_port(i)-
    >fd);
    return XRWS_ERROR;
    }
    } //close server
    if(close(fd_listen)!=XRWS_OK){
    ERROR("Error in fu L_SIO_close on fd: %d",fd_listen);
    return XRWS_ERROR;
    } DEBUG("Exit properly");
    return 0;
    }
    Dernière modification par ventilopomme ; 16/06/2011 à 07h58.
    exclu à jamais du présent

  3. #3
    doul11

    Re : otpimisation d'un code en c

    Bonjour,

    Citation Envoyé par bpm37093 Voir le message
    Voici le code: (si la mise en page du code ne vous convient pas prévener moi)
    Normalement on indente le code, ça le rends un peut plus lisible

    a moins que tu veuille faire l'IOCCC ? http://fr.wikipedia.org/wiki/Interna...C_Code_Contest
    La logique est une méthode systématique d’arriver en confiance à la mauvaise conclusion.

  4. #4
    ventilopomme

    Re : otpimisation d'un code en c

    le probleme est ce que s'il nous avait au depart ce qu'il devait faire cela serait plus simple de lui proposer qq chose plutot que de partir de son resultat de son raisonnement
    exclu à jamais du présent

  5. A voir en vidéo sur Futura
  6. #5
    bpm37093

    Re : otpimisation d'un code en c

    Je veux faire un programme qui extrait les données d'un SNIFFER (c'est un projet pour l'ecole) il ya plusieurs dossier dans ce programme et moi je m'occupe de celui qui va extraire et les données et les stocké.
    J'ai deux dossiers dans mon code T_SNIFFER et SIM_SNIFFER et dans chacun de ces deux dossiers il un main.c (T_SNIFFER_MAIN.c et SIM_SNIFFER_MAIN.c)
    Je précise que je suis sous ubuntu et que je code sous Eclipse.

    T_SNIFFER
    #include <unistd.h>
    #include <stdio.h>
    #include <signal.h>
    #include <sys/time.h>
    #include <errno.h>
    #include <stdio.h>
    #include <string.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>

    #include "L_LOG.h"
    #include "L_MOD.h"
    #include "L_SIO.h"


    #ifndef MAX
    #define MAX(a,b) ((a) > (b) ? (a) : (b))
    #endif

    ///Control the main loop, if 1 main loop runs, when go to 0 main loop stop.
    int KeepRunning = XRWS_TRUE;
    /// Configuration parameters file path, max lenght of L_MOD_RUN_PARAM_MAX_SIZE.
    char CfgPath[L_MOD_RUN_PARAM_MAX_SIZE]; // Path to config file
    fd_set fd_set_read,fd_set_write;
    int fd_max = 0;

    /**
    * @brief ascessor to the configuration parameters file path.
    * @return the configuration parameters file path.
    */
    char * getCfgParameterPath ()
    {
    return CfgPath;
    }

    /**
    * @brief This function is executed at exit, it permit to stop the main loop
    * @param[in] sig the signal recieve to stop appication.
    */
    void do_at_exit(int sig)
    {
    DEBUG("do at exit");
    KeepRunning = 0;
    }

    /**
    * @brief Map the interuption signal with do_at_exit function
    */
    void handle_SIGINT()
    {
    struct sigaction orig_action;
    struct sigaction action;
    action.sa_flags = 0;
    action.sa_handler = do_at_exit;
    if(-1 == sigaction(SIGINT, &action, &orig_action))
    {
    ERROR("Error in fu. sigaction");
    }
    }

    static XRWS_STATUS T_SNIFFER_API_handle_data(char * p_data, int p_nr_of_bytes, int* p_nr_of_bytes_parsed){
    INFO("connection call back");
    return XRWS_OK;
    }

    static void build_select_fd_set(){
    int i;
    L_SIO_port_t *l_port = NULL;
    L_SIO_TCP_config_t *l_cfg = NULL;
    fd_max = 0;
    FD_ZERO(&fd_set_read);
    FD_ZERO(&fd_set_write);
    if(L_SIO_get_port_nr()!=0){
    for(i=0;i<L_SIO_get_port_nr(); i++){
    l_port = L_SIO_get_port(i);
    if(l_port->type == L_SIO_PORT_TYPE_TCP){
    l_cfg = (L_SIO_TCP_config_t *)(&(l_port->cfg));
    FD_SET(l_port->fd, &fd_set_read);
    if(l_cfg->writable==XRWS_FALSE){
    FD_SET(l_port->fd, &fd_set_write);
    }
    fd_max = MAX(fd_max,l_port->fd);
    }
    }
    }else{
    INFO("Server connection was broken");
    KeepRunning = XRWS_FALSE;
    }
    }

    /**
    * @brief Main function
    *
    * @param[in] argc argument count.
    * @param[in] argv arguments
    * @return exit code
    */
    int main(int argc, char **argv)
    {
    sigset_t orig_sigmask;
    int rc = 0;
    int i,fd_connection;
    struct timeval l_timeout; /* Timeout for select */
    L_SIO_TCP_config_t l_connection_cfg;
    L_SIO_serial_read_CB l_CB[]={(L_SIO_serial_read_CB)T_SNIF FER_API_handle_data};
    L_SIO_port_t *l_port = NULL;
    L_SIO_TCP_config_t *l_cfg = NULL;

    unsigned char msg[5]={2,1,2,12,1};

    //****** INITIALIZATION ******

    handle_SIGINT();
    if(L_MOD_RUN_PARAM_retrieve(ar gc
    ,argv
    ,CfgPath)!=0)
    {
    ERROR("Error in fu. L_MOD_RUN_PARAM_retrieve");
    return L_MOD_EXIT_ERROR;
    }

    // init signals
    if(L_MOD_SIG_init(&orig_sigmas k)!=0)
    {
    ERROR_EC("Error in fu. L_MOD_SIG_init");
    return L_MOD_EXIT_ERROR;
    }
    DEBUG("\tSIG init OK");

    // config for server
    l_connection_cfg.type = L_SIO_SOCKET_TYPE_SOCKET;
    l_connection_cfg.address.sin_f amily = AF_INET;
    l_connection_cfg.address.sin_a ddr.s_addr = INADDR_ANY;
    l_connection_cfg.address.sin_p ort = htons(2010);
    // open server
    if(L_SIO_TCP_open(&l_connectio n_cfg, l_CB, 1, &fd_connection)!=XRWS_OK){
    ERROR("Error in function L_SIO_TCP_open");
    return XRWS_ERROR;
    }
    build_select_fd_set();
    DEBUG("Module was initialized correctly");

    //****** MAIN LOOP ******

    while(KeepRunning==XRWS_TRUE)
    {
    l_timeout.tv_sec = 5;
    l_timeout.tv_usec = 0;
    rc = select(fd_max+1,&fd_set_read,& fd_set_write,NULL,&l_timeout);
    DEBUG("Select (%d)",rc);
    if(rc<0){
    ERROR("Error in select function: '%s'",strerror(errno));
    }else if(rc == 0){
    ERROR("Select time out");
    }else{
    for(i=0;i<L_SIO_get_port_nr(); i++){
    l_port = L_SIO_get_port(i);
    if(l_port->type == L_SIO_PORT_TYPE_TCP){
    l_cfg = (L_SIO_TCP_config_t *)(&(l_port->cfg));
    // check if there is something to read
    if(FD_ISSET(l_port->fd,&fd_set_read) && rc > 0){
    rc--; // this permits to avoid a false fd_isset on the socket returned by accept function
    // Client is notify that there is something to read
    DEBUG("connection has something to read");
    if(L_SIO_read(l_port->fd,NULL)!=XRWS_OK){
    ERROR("Error in fu L_SIO_read");
    return XRWS_ERROR;
    }
    }
    if(l_port->fd != -1){ //this protect from a previously closed fd
    // check if socket is ready to write
    if(FD_ISSET(l_port->fd,&fd_set_write) && rc > 0){
    rc--; // this permits to avoid a false fd_isset on the socket returned by accept function
    DEBUG("connection is ready to write");
    l_cfg->writable = XRWS_TRUE;
    if(L_SIO_write(l_port->fd,msg,5)!=XRWS_OK){
    ERROR("Error in fu. L_SIO_write");
    return XRWS_ERROR;
    }
    }
    }
    }
    }
    }
    build_select_fd_set(); // re-build the fd set because select() function updated it
    }
    //close server connection
    for (i=0;i<L_SIO_get_port_nr();i++ ){
    l_port = L_SIO_get_port(i);
    if(L_SIO_close(l_port->fd)!=XRWS_OK){
    ERROR("Error in fu L_SIO_close on fd: %d",l_port->fd);
    return XRWS_ERROR;
    }
    }
    DEBUG("Exit properly");
    return 0;
    }



    Et SIM_SNIFFER

    #include <unistd.h>
    #include <stdio.h>
    #include <signal.h>
    #include <sys/time.h>
    #include <errno.h>
    #include <stdio.h>
    #include <string.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>

    #include "L_LOG.h"
    #include "L_MOD.h"
    #include "L_SIO.h"


    #ifndef MAX
    #define MAX(a,b) ((a) > (b) ? (a) : (b))
    #endif

    ///Control the main loop, if 1 main loop runs, when go to 0 main loop stop.
    int KeepRunning = 1;
    /// Configuration parameters file path, max lenght of L_MOD_RUN_PARAM_MAX_SIZE.
    char CfgPath[L_MOD_RUN_PARAM_MAX_SIZE]; // Path to config file
    fd_set fd_set_read,fd_set_write;
    int fd_listen = -1;
    //int fd_connection[L_SIO_MAX_NR_OF_PORTS] = {-1};
    //int fd_connection_nr = 0;
    //int fd_writable[L_SIO_MAX_NR_OF_PORTS] = {XRWS_FALSE};
    int fd_max = 0;
    /**
    * @brief ascessor to the configuration parameters file path.
    * @return the configuration parameters file path.
    */
    char * getCfgParameterPath ()
    {
    return CfgPath;
    }

    /**
    * @brief This function is executed at exit, it permit to stop the main loop
    * @param[in] sig the signal recieve to stop appication.
    */
    void do_at_exit(int sig)
    {
    DEBUG("do at exit");
    KeepRunning = 0;
    }

    /**
    * @brief Map the interuption signal with do_at_exit function
    */
    void handle_SIGINT()
    {
    struct sigaction orig_action;
    struct sigaction action;
    action.sa_flags = 0;
    action.sa_handler = do_at_exit;
    if(-1 == sigaction(SIGINT, &action, &orig_action))
    {
    ERROR("Error in fu. sigaction");
    }
    }

    static XRWS_STATUS SIM_SNIFFER_API_handle_data(in t p_fd,char* p_data, int p_nr_of_bytes, int* p_nr_of_bytes_parsed){
    INFO("connection call back");
    unsigned char msg[5]={2,1,2,12,1};
    if(L_SIO_write(p_fd,msg,5)!=XR WS_OK){
    ERROR("Error in fu. L_SIO_write");
    return XRWS_ERROR;
    }
    return XRWS_OK;
    }

    static void build_select_fd_set(){
    int i;
    L_SIO_port_t *l_port;
    L_SIO_TCP_config_t *l_cfg;

    FD_ZERO(&fd_set_read);
    FD_ZERO(&fd_set_write);
    FD_SET(fd_listen, &fd_set_read);
    fd_max = fd_listen;
    for(i=0;i<L_SIO_get_port_nr(); i++){
    l_port = L_SIO_get_port(i);
    if(l_port->type == L_SIO_PORT_TYPE_TCP){
    l_cfg = (L_SIO_TCP_config_t *)(&(l_port->cfg));
    FD_SET(l_port->fd, &fd_set_read);
    if(l_cfg->writable==XRWS_FALSE){
    FD_SET(l_port->fd, &fd_set_write);
    }
    fd_max = MAX(fd_max,l_port->fd);
    }
    }
    }

    /**
    * @brief Main function
    *
    * @param[in] argc argument count.
    * @param[in] argv arguments
    * @return exit code
    */
    int main(int argc, char **argv)
    {
    sigset_t orig_sigmask;
    int rc = 0;
    int i,l_read_byte_nr,l_client_fd;
    struct timeval l_timeout; /* Timeout for select */
    L_SIO_TCP_config_t l_cfg_server = {0};
    L_SIO_serial_read_CB l_CB[]={(L_SIO_serial_read_CB)SIM_SN IFFER_API_handle_data};
    L_SIO_port_t *l_port = NULL;
    L_SIO_TCP_config_t *l_cfg = NULL;

    //****** INITIALIZATION ******

    handle_SIGINT();
    if(L_MOD_RUN_PARAM_retrieve(ar gc
    ,argv
    ,CfgPath)!=0)
    {
    ERROR("Error in fu. L_MOD_RUN_PARAM_retrieve");
    return L_MOD_EXIT_ERROR;
    }

    // init signals
    if(L_MOD_SIG_init(&orig_sigmas k)!=0)
    {
    ERROR_EC("Error in fu. L_MOD_SIG_init");
    return L_MOD_EXIT_ERROR;
    }
    DEBUG("\tSIG init OK");

    // config for server
    l_cfg_server.type = L_SIO_SOCKET_TYPE_LISTENER;
    l_cfg_server.address.sin_famil y = AF_INET;
    l_cfg_server.address.sin_addr. s_addr = INADDR_ANY;
    l_cfg_server.address.sin_port = htons(2010);
    // open server
    if(L_SIO_TCP_open_listener_soc ket(&l_cfg_server, &fd_listen)!=XRWS_OK){
    ERROR("Error in function L_SIO_TCP_open");
    return XRWS_ERROR;
    }

    DEBUG("Module was initialized correctly");

    //****** MAIN LOOP ******

    while(KeepRunning==1)
    {
    l_timeout.tv_sec = 5;
    l_timeout.tv_usec = 0;
    build_select_fd_set();
    rc = select(fd_max+1,&fd_set_read,& fd_set_write,NULL,&l_timeout);
    DEBUG("Select (%d)",rc);
    if(rc<0){
    ERROR("Error in select function: '%s'",strerror(errno));
    }else if(rc == 0){
    ERROR("Select time out");
    }else{
    if(FD_ISSET(fd_listen,&fd_set_ read)){
    DEBUG("Listening Server socket is set");
    if(L_SIO_TCP_accept(&l_client_ fd,l_CB,1,fd_listen)!=XRWS_OK) {
    ERROR("Error in function L_SIO_TCP_accept");
    return XRWS_ERROR;
    }
    printf("New client on fd: %d\n",l_client_fd);
    fflush(stdout);
    DEBUG("client connection was accepted on fd: %d",l_client_fd);
    rc --;
    }
    if(rc > 0){
    for(i=0;i<L_SIO_get_port_nr(); i++){
    l_port = L_SIO_get_port(i);
    if(l_port->type == L_SIO_PORT_TYPE_TCP){
    l_cfg = (L_SIO_TCP_config_t *)(&(l_port->cfg));
    if(FD_ISSET(l_port->fd,&fd_set_read) && rc > 0){
    // Client is notify that there is something to read
    DEBUG("Client connection (%d) has something to read", i);
    if(L_SIO_read(l_port->fd,&l_read_byte_nr)!=XRWS_OK) {
    ERROR("Error in fu L_SIO_read");
    return XRWS_ERROR;
    }
    rc --;
    }
    if(l_port->fd != -1){ //this protect from a previously closed fd
    if(FD_ISSET(l_port->fd,&fd_set_write) && rc > 0){
    DEBUG("Client connection (%d) is ready to write", i);
    l_cfg->writable = XRWS_TRUE;
    rc --;
    }
    }
    if(rc <= 0){
    break; // stop the loop because there's nothing to read anymore
    }
    }
    }
    }
    }
    }
    //close server connection
    for (i=0;i<L_SIO_get_port_nr();i++ ){
    if(L_SIO_close(L_SIO_get_port( i)->fd)!=XRWS_OK){
    ERROR("Error in fu L_SIO_close on fd: %d",L_SIO_get_port(i)->fd);
    return XRWS_ERROR;
    }
    }
    //close server
    if(close(fd_listen)!=XRWS_OK){
    ERROR("Error in fu L_SIO_close on fd: %d",fd_listen);
    return XRWS_ERROR;
    }
    DEBUG("Exit properly");
    return 0;
    }

Discussions similaires

  1. suppression d'un datetime en sql via un code c#
    Par azerty2010 dans le forum Programmation et langages, Algorithmique
    Réponses: 2
    Dernier message: 24/04/2011, 22h35
  2. Un langage codé léger, 2 caractères non codé=1 caractère codé. Une solution?
    Par invite06e0b926 dans le forum Mathématiques du supérieur
    Réponses: 1
    Dernier message: 30/06/2010, 10h02
  3. code capacité d'un condensateur
    Par dardagnan dans le forum Électronique
    Réponses: 9
    Dernier message: 04/05/2009, 11h29
  4. saisie d'un code (clavier numérique)
    Par Galdon dans le forum Électronique
    Réponses: 1
    Dernier message: 09/03/2008, 15h24
  5. Implémentation d'un code sur FPGA
    Par invite232dbe64 dans le forum Électronique
    Réponses: 5
    Dernier message: 12/07/2007, 13h00