Files
progettoso/src/serverWorker.c

87 lines
1.7 KiB
C
Raw Normal View History

#include <stdlib.h>
#include <errno.h>
#include <ctype.h>
#include <assert.h>
#include <sys/select.h>
#include <conn.h>
#include <message.h>
2022-03-12 10:05:58 +01:00
#include <serverStatus.h>
// converte tutti i carattere minuscoli in maiuscoli
static void toup(char *str) {
char *p = str;
2022-03-12 10:05:58 +01:00
while(*p != '\0') {
*p = (islower(*p)?toupper(*p):*p);
++p;
2022-03-12 10:05:58 +01:00
}
}
// funzione eseguita dal Worker thread del pool
// gestisce una intera connessione di un client
//
void threadF(void *arg) {
assert(arg);
2022-03-12 10:05:58 +01:00
long* connfd = (long*)arg[0];
serverStatus* status = (serverStatus*)(arg[1]);
fd_set set, tmpset;
FD_ZERO(&set);
2022-03-12 10:05:58 +01:00
FD_SET(*connfd, &set);
do {
tmpset=set;
int r;
// ogni tanto controllo se devo terminare
struct timeval timeout={0, 100000}; // 100 milliseconds
if ((r=select(connfd+1, &tmpset, NULL, NULL, &timeout)) < 0) {
2022-03-12 10:05:58 +01:00
perror("Select");
break;
}
if (r==0) {
2022-03-12 10:05:58 +01:00
if ((status->exiting) != 0)
break;
continue;
}
2022-03-12 10:05:58 +01:00
// comunicate with the client
msg_t str;
2022-03-12 10:05:58 +01:00
long n;
if ((n=readn(connfd, &str.len, sizeof(long))) == -1) {
perror("read1");
break;
}
2022-03-12 10:05:58 +01:00
if (n==0)
break;
str.str = calloc(str.len, sizeof(char));
if (!str.str) {
perror("calloc");
fprintf(stderr, "Memoria esaurita....\n");
break;
2022-03-12 10:05:58 +01:00
}
if ((n=readn(connfd, str.str, str.len * sizeof(char))) == -1) {
perror("read2");
free(str.str);
break;
}
toup(str.str);
if ((n=writen(connfd, &str.len, sizeof(int))) == -1) {
perror("write1");
free(str.str);
break;
}
if ((n=writen(connfd, str.str, str.len*sizeof(char))) == -1) {
perror("write2");
free(str.str);
break;
}
free(str.str);
2022-03-12 10:05:58 +01:00
} while(status->exiting == 0);
close(connfd);
}