mirror of
https://github.com/fumiama/simple-kanban.git
synced 2026-06-20 09:30:29 +08:00
修复recv返回-1时线程崩溃
This commit is contained in:
15
server.c
15
server.c
@@ -269,10 +269,10 @@ void accept_timer(void *p) {
|
|||||||
THREADTIMER *timer = timerPointerOf(p);
|
THREADTIMER *timer = timerPointerOf(p);
|
||||||
signal(SIGQUIT, handle_pipe);
|
signal(SIGQUIT, handle_pipe);
|
||||||
signal(SIGPIPE, handle_pipe);
|
signal(SIGPIPE, handle_pipe);
|
||||||
while(!pthread_kill(*timer->thread, 0)) {
|
while(!pthread_kill(*(timer->thread), 0)) {
|
||||||
sleep(MAXWAITSEC);
|
sleep(MAXWAITSEC);
|
||||||
puts("Check accept status");
|
puts("Check accept status");
|
||||||
if(!*timer->thread) {
|
if(!*(timer->thread)) {
|
||||||
free(timer);
|
free(timer);
|
||||||
break;
|
break;
|
||||||
} else if(time(NULL) - timer->touch > MAXWAITSEC) {
|
} else if(time(NULL) - timer->touch > MAXWAITSEC) {
|
||||||
@@ -284,7 +284,7 @@ void accept_timer(void *p) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void kill_thread(THREADTIMER* timer) {
|
void kill_thread(THREADTIMER* timer) {
|
||||||
pthread_kill(*timer->thread, SIGQUIT);
|
pthread_kill(*(timer->thread), SIGQUIT);
|
||||||
close(timer->accept_fd);
|
close(timer->accept_fd);
|
||||||
if(timer->data) {
|
if(timer->data) {
|
||||||
free(timer->data);
|
free(timer->data);
|
||||||
@@ -296,12 +296,12 @@ void kill_thread(THREADTIMER* timer) {
|
|||||||
timer->is_open = 0;
|
timer->is_open = 0;
|
||||||
puts("Close file.");
|
puts("Close file.");
|
||||||
}
|
}
|
||||||
*timer->thread = 0;
|
*(timer->thread) = 0;
|
||||||
puts("Kill thread.");
|
puts("Kill thread.");
|
||||||
}
|
}
|
||||||
|
|
||||||
void handle_pipe(int signo) {
|
void handle_pipe(int signo) {
|
||||||
puts("Pipe error");
|
printf("Pipe error: %d", signo);
|
||||||
}
|
}
|
||||||
|
|
||||||
void handle_accept(void *p) {
|
void handle_accept(void *p) {
|
||||||
@@ -319,7 +319,7 @@ void handle_accept(void *p) {
|
|||||||
char *buff = calloc(BUFSIZ, sizeof(char));
|
char *buff = calloc(BUFSIZ, sizeof(char));
|
||||||
if(buff) {
|
if(buff) {
|
||||||
timerPointerOf(p)->data = buff;
|
timerPointerOf(p)->data = buff;
|
||||||
while(*timerPointerOf(p)->thread && (timerPointerOf(p)->numbytes = recv(accept_fd, buff, BUFSIZ, 0)) > 0) {
|
while(*(timerPointerOf(p)->thread) && (timerPointerOf(p)->numbytes = recv(accept_fd, buff, BUFSIZ, 0)) > 0) {
|
||||||
touchTimer(p);
|
touchTimer(p);
|
||||||
buff[timerPointerOf(p)->numbytes] = 0;
|
buff[timerPointerOf(p)->numbytes] = 0;
|
||||||
printf("Get %zd bytes: %s\n", timerPointerOf(p)->numbytes, buff);
|
printf("Get %zd bytes: %s\n", timerPointerOf(p)->numbytes, buff);
|
||||||
@@ -327,9 +327,8 @@ void handle_accept(void *p) {
|
|||||||
if(!check_buffer(timerPointerOf(p))) break;
|
if(!check_buffer(timerPointerOf(p))) break;
|
||||||
}
|
}
|
||||||
printf("Break: recv %zd bytes\n", timerPointerOf(p)->numbytes);
|
printf("Break: recv %zd bytes\n", timerPointerOf(p)->numbytes);
|
||||||
kill_thread(timerPointerOf(p));
|
|
||||||
} else puts("Error allocating buffer");
|
} else puts("Error allocating buffer");
|
||||||
close(accept_fd);
|
kill_thread(timerPointerOf(p));
|
||||||
} else puts("Error accepting client");
|
} else puts("Error accepting client");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user