From 726ab7b4e3c5a6545fad1f1f9d32c7829fa43245 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sun, 9 Apr 2017 18:41:44 +0200 Subject: [PATCH] =?UTF-8?q?[done]=20Ajout=20timeout=20lors=20de=20cr=C3=A9?= =?UTF-8?q?ation=20sockets=20+=20fonction=20de=20traitement=20dynamique=20?= =?UTF-8?q?pour=20chaque=20thread=20du=20type=20"LISTEN=5F"=20+=20debug=20?= =?UTF-8?q?TCP=20+=20fix=20`make=20clean`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- central-manager/Makefile | 2 +- central-manager/central-manager.c | 20 ++++++++++++-------- central-manager/central-manager.h | 9 +++++---- central-manager/lib/header.h | 2 ++ central-manager/lib/network/tcp/server.c | 23 ++++++++++++++++++----- central-manager/lib/network/udp/server.c | 12 +++++++++++- 6 files changed, 49 insertions(+), 19 deletions(-) diff --git a/central-manager/Makefile b/central-manager/Makefile index 6cb0ee0..372acc1 100644 --- a/central-manager/Makefile +++ b/central-manager/Makefile @@ -24,4 +24,4 @@ all: boot # cleans the compiled files clean: rm boot; - rm ./**/*.o + rm lib/network/**/*.o; diff --git a/central-manager/central-manager.c b/central-manager/central-manager.c index d042b27..bece44a 100644 --- a/central-manager/central-manager.c +++ b/central-manager/central-manager.c @@ -29,9 +29,10 @@ int main(int argc, char* argv[]){ sgca_index.update = 0; /* Variables locales */ - struct listn_thrd_arg tcp_listn_arg = { SERV_HOST, TCP_LIST }; - struct listn_thrd_arg udp_vterm_arg = { MCST_VTER, UDP_VTER }; - struct listn_thrd_arg udp_cterm_arg = { MCST_CTER, UDP_CTER }; + struct listn_thrd_arg tcp_listn_arg = { SERV_HOST, TCP_LIST, &managePlane }; + struct listn_thrd_arg udp_mcast_arg = { MCST_HOST, UDP_MCST, NULL }; + struct listn_thrd_arg udp_vterm_arg = { MCST_VTER, UDP_VTER, &manageTerminal }; + struct listn_thrd_arg udp_cterm_arg = { MCST_CTER, UDP_CTER, &manageTerminal }; /* [1] Lancement des THREADS d'écoute @@ -41,8 +42,8 @@ int main(int argc, char* argv[]){ if( DEBUGMOD&THR ) printf("{tcp_listn} démarré\n"); /* (2) Ecoute UDP multicast */ - // pthread_create(&listenManagers[1], NULL, LISTEN_UDP, (void*)(intptr_t) UDP_MCST); - // if( DEBUGMOD&THR ) printf("{udp_mcast} émarré\n\n"); + pthread_create(&listenManagers[1], NULL, LISTEN_UDP, (void*) &udp_mcast_arg); + if( DEBUGMOD&THR ) printf("{udp_mcast} démarré\n\n"); /* (3) Ecoute UDP viewTerm */ pthread_create(&listenManagers[2], NULL, LISTEN_UDP, (void*) &udp_vterm_arg); @@ -122,6 +123,7 @@ void* LISTEN_TCP(void* THREADABLE_ARG){ index = -1; /* 2. On attends une connection TCP */ + len = sizeof(struct sockaddr_in); CLIENT_SOCKET = accept(LISTENSOCK, (struct sockaddr*) &clientInfo, &len); /* 3. Si erreur, on attend une nouvelle connection */ @@ -130,6 +132,8 @@ void* LISTEN_TCP(void* THREADABLE_ARG){ break; } + if( DEBUGMOD&SCK ) printf("{tcp_listn} %s:%d connecté\n", inet_ntoa(clientInfo.sin_addr), ntohs(clientInfo.sin_port)); + /* 4. On cherche un "manager" libre (inactif) */ for( i = 0 ; i < MAX_TCP_THR ; i++ ) if( activeTCPManagers[i] == 0 ){ index = i; break; } @@ -138,7 +142,7 @@ void* LISTEN_TCP(void* THREADABLE_ARG){ if( index != -1 ){ /* 5. On lance un thread pour le traitement de ce client */ - pthread_create(&TCPManagers[index], NULL, managePlane, (void*)(intptr_t) CLIENT_SOCKET); + pthread_create(&TCPManagers[index], NULL, arg->func, (void*)(intptr_t) CLIENT_SOCKET); if( DEBUGMOD&THR ) printf("{tcp_listn}{com}(%d) démarré\n", index); @@ -246,7 +250,7 @@ void* LISTEN_UDP(void* THREADABLE_ARG){ } /* 3. On récupère l'adresse IP du client */ - if( DEBUGMOD&SCK ) printf("{%s} '%s' connecté\n", entity, inet_ntoa(clientInfo.sin_addr)); + if( DEBUGMOD&SCK ) printf("{%s} %s:%d connecté\n", entity, inet_ntoa(clientInfo.sin_addr), ntohs(clientInfo.sin_port)); @@ -327,7 +331,7 @@ void* LISTEN_UDP(void* THREADABLE_ARG){ if( index != -1 ){ /* 2.1. On lance un thread pour le traitement de ce client */ - pthread_create(&UDPManagers[index], NULL, manageTerminal, (void*)(intptr_t) CLIENT_SOCKET); + pthread_create(&UDPManagers[index], NULL, arg->func, (void*)(intptr_t) CLIENT_SOCKET); if( DEBUGMOD&THR ) printf("{%s}{udp_com}(%d) démarré\n", entity, index); diff --git a/central-manager/central-manager.h b/central-manager/central-manager.h index 430177d..d0e9797 100644 --- a/central-manager/central-manager.h +++ b/central-manager/central-manager.h @@ -21,8 +21,8 @@ void* manageTerminal(void* THREADABLE_SOCKET); static pthread_t listenManagers[4]; // contiendra les THREADS d'écoute // 1. Multicast UDP (plane) // 2. ListenSock TCP (plane) -// 3. ListenSock UDP (vterm) -// 4. ListenSock UDP (cterm) +// 3. ListenSock UDP (vterm) Multicast +// 4. ListenSock UDP (cterm) Multicast // VARIABLES THREADS CONNECTION TCP static pthread_t TCPManagers[MAX_TCP_THR]; // contiendra les THREADS TCP @@ -34,8 +34,9 @@ static int activeUDPManagers[MAX_UDP_THR] = {0}; // contiendra les THREADS UDP a struct listn_thrd_arg{ - const char addr[16]; - const unsigned short port; + const char addr[16]; // socket address + const unsigned short port; // socket port + void* (*func)(); // management function }; diff --git a/central-manager/lib/header.h b/central-manager/lib/header.h index 729be92..a361a1d 100644 --- a/central-manager/lib/header.h +++ b/central-manager/lib/header.h @@ -41,4 +41,6 @@ #define maxHostLen 64 #define maxPortLen 6 + #define SOCK_TIMEOUT 2 // 2sec timeout + #endif \ No newline at end of file diff --git a/central-manager/lib/network/tcp/server.c b/central-manager/lib/network/tcp/server.c index f7f667d..89f6113 100644 --- a/central-manager/lib/network/tcp/server.c +++ b/central-manager/lib/network/tcp/server.c @@ -11,23 +11,32 @@ int DROP_TCP_SERVER(const int pPort, int* pListenSock){ /* [0] Initialisation des variables =========================================================*/ - // CREATION static struct sockaddr_in addr; // contiendra les infos du serveur int STATUS; // status + struct timeval timeout; - // INITIALISATION *pListenSock = -1; /* [1] Création de la socket =======================================================*/ + /* 1. Création socket */ *pListenSock = socket(AF_INET, SOCK_STREAM, 0); if( DEBUGMOD&SCK && DEBUGMOD&HDR ) printf(" * [drop_tcp_server] socket: %d\n", *pListenSock); - // si erreur + /* 2. Gestion erreur */ if( *pListenSock < 0 ) return -1; + /* 3. Timeout */ + timeout.tv_sec = SOCK_TIMEOUT; + timeout.tv_usec = 0; + + if( setsockopt(*pListenSock, SOL_SOCKET, SO_RCVTIMEO|SO_SNDTIMEO, (char*) &timeout, sizeof(struct timeval) ) < 0 ){ + close(*pListenSock); + return -1; + } + /* [2] On définit les infos de la socket =========================================================*/ @@ -42,24 +51,28 @@ int DROP_TCP_SERVER(const int pPort, int* pListenSock){ /* [3] On publie la SOCKET =======================================================*/ + /* 1. Publication socket */ STATUS = bind(*pListenSock, (struct sockaddr*) &addr, sizeof(addr)); if( DEBUGMOD&SCK && DEBUGMOD&HDR ) printf(" * [drop_tcp_server] bind: %d\n", STATUS); - // si erreur + /* 2. Gestion erreur */ if( STATUS < 0 ) return -1; + /* [4] On met la socket sur écoute =======================================================*/ + /* 1. Démarrage écoute */ STATUS = listen(*pListenSock, MAX_TCP_THR); if( DEBUGMOD&SCK && DEBUGMOD&HDR ) printf(" * [drop_tcp_server] listen: %d\n", STATUS); - // si erreur + /* 2. Gestion erreur */ if( STATUS < 0 ) return -1; + /* [n] Code succès =========================================================*/ return 0; diff --git a/central-manager/lib/network/udp/server.c b/central-manager/lib/network/udp/server.c index e18854a..56c5b09 100644 --- a/central-manager/lib/network/udp/server.c +++ b/central-manager/lib/network/udp/server.c @@ -12,6 +12,7 @@ int DROP_UDP_SERVER(const char* pAddr, const int pPort, int* pListenSock, struct uint yes = 1; struct ip_mreq mcastReq; *pListenSock = -1; + struct timeval timeout; /* [1] Création de la socket @@ -24,7 +25,16 @@ int DROP_UDP_SERVER(const char* pAddr, const int pPort, int* pListenSock, struct /* 2. Gestion erreur */ if( *pListenSock < 0 ) return -1; - /* 3. MULTICAST -> Autorisation plusieurs sockets sur port */ + /* 3. Timeout */ + timeout.tv_sec = SOCK_TIMEOUT; + timeout.tv_usec = 0; + + if( setsockopt(*pListenSock, SOL_SOCKET, SO_RCVTIMEO|SO_SNDTIMEO, (char*) &timeout, sizeof(struct timeval) ) < 0 ){ + close(*pListenSock); + return -1; + } + + /* 4. MULTICAST -> Autorisation plusieurs sockets sur port */ if( pMcast ){ if( DEBUGMOD&SCK && DEBUGMOD&HDR ) printf(" * [drop_udp_server] set port to be reused\n");