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;
}
-----