client now disconnects

This commit is contained in:
elvis
2022-05-01 00:10:28 +02:00
parent 85231d073f
commit e095f4306f
4 changed files with 55 additions and 16 deletions

View File

@ -309,22 +309,32 @@ int main(int argc, char *argv[]) {
continue;
}
if (i == request_pipe[0]) { // un worker ha finito di servire un client
int pdr; // ottengo il descrittore della pipe
if (readn(request_pipe[0], &pdr, sizeof(int)) == -1) {
long pdr; // ottengo il descrittore della pipe
if (readn(request_pipe[0], &pdr, sizeof(long)) == -1) {
perror("readn");
free(connfd);
break;
}
switch (pdr) {
case -1: // client disconnected
--numberOfConnections;
n = snprintf(buf, sizeof(buf), "Client %ld disconnected.\n", pdr);
if( n<0 ) {
perror("snprintf");
free(connfd);
goto _cleanup;
}
if( taglia_log(taglia, buf) < 0) {
free(connfd);
goto _cleanup;
}
if (stopNewConnections && numberOfConnections <= 0) {
quit = 1;
// termino il signalThread
pthread_cancel(sighandler_thread);
break;
}
free(connfd);
continue;
default: // client served but not disconnected
FD_SET(pdr, &set);
@ -399,7 +409,6 @@ int main(int argc, char *argv[]) {
// aggiungo al threadpool
int r = addToThreadPool(pool, threadF, args);
if (r == 0) {
numberOfConnections++;
free(connfd);
continue; // aggiunto con successo
}

View File

@ -66,8 +66,11 @@ void threadF(void *arg) {
goto _cleanup;
}
if (n==0)
if (n==-1)
goto _cleanup;
if (n==0)
goto closeConnection;
str.str = calloc(str.len+1, sizeof(char));
if (!str.str) {
perror("calloc");
@ -82,12 +85,13 @@ void threadF(void *arg) {
}
str.str[str.len] = '\0';
if(strncmp(str.str, "quit", 5) == 0) { // il client vuole chiudere la connessione
closeConnection:
if(n==0 || strncmp(str.str, "quit", 5) == 0) { // il client vuole chiudere la connessione
close(connfd);
int close = -1;
long close = -1;
// comunico al manager che ho chiuso la connessione
if (writen(request_pipe, &close, sizeof(int)) == -1) {
if (writen(request_pipe, &close, sizeof(long)) == -1) {
perror("writen");
goto _cleanup;
}
@ -111,7 +115,7 @@ void threadF(void *arg) {
// str.str non è più valido perchè parser fa free
// comunico al manager che è stata servita la richiesta
if (writen(request_pipe, &connfd, sizeof(int)) == -1) {
if (writen(request_pipe, &connfd, sizeof(long)) == -1) {
perror("writen");
}
// log