我有一个学校作业,我将在其中实施一个基于客户端 - 服务器架构的游戏。 服务器端控制所有游戏,客户端只有一张正在发生的事情的图像。 服务器的一个功能是从游戏中踢出客户端,这就是问题所在。
我在客户端有一个线程接收来自服务器的消息,主线程接收来自用户的输入,但是当第一个线程收到shutdown命令时,主线程在执行关闭之前等待来自客户端的一个密钥。
我的问题是:有没有办法解除主线程上的wgetch,使其不等待一个键并执行关闭?
代码:
void *trata_fifo(void *dados) {
int i;
RESPONSE resp;
char namepipe[MAX];
sprintf(namepipe, CLIENT_FIFO, getpid());
mkfifo(namepipe, 0600);
fd_res = open(namepipe, O_RDWR);
do {
i = read(fd_res, &resp, sizeof (RESPONSE));
drawGame(resp);
if (resp.command == 3) {
wclear(info_win);
box(info_win, 0, 0);
wattron(info_win, A_REVERSE | A_BOLD);
mvwprintw(info_win, 2, 4, "Score");
mvwprintw(info_win, 4, 5, " %d ", resp.points);
mvwprintw(info_win, 6, 2, "Bombinhas");
mvwprintw(info_win, 8, 5, " %d ", resp.bombinha);
mvwprintw(info_win, 10, 2, "Megabombas");
mvwprintw(info_win, 12, 5, " %d ", resp.megaBomba);
wrefresh(info_win);
}
if (resp.command == 4) {//here
SHUTDOWN = true;
write(STDIN_FILENO,NULL,0);//i tried this , but its not working
}
if (resp.command == 5) {
SHUTDOWN = true;
}
}
} while (!SHUTDOWN);
close(fd_res);
pthread_exit(NULL);
}
int main() {
REQUEST client;
pthread_t tarefa;
int highlight = 1, choice = 0, opt;
int startx = 0, starty = 0;
signal(SIGINT, handleSignalCli);
signal(SIGUSR1, handleSignalCli);
signal(SIGALRM, handleSignalCli);
if (access(SERVER_FIFO, F_OK) != 0) //Verificar se servidor esta correndo
{
perror("[ERROR] Server ins't running...!/n");
exit(EXIT_FAILURE);
}
if (pthread_create(&tarefa, NULL, trata_fifo, NULL) != 0) {
fprintf(stderr, "[ERROR] Creating Thread\n");
exit(EXIT_FAILURE);
}
fd = open(SERVER_FIFO, O_WRONLY);
if (!fd) {
perror("[ERROR] Opening Server Fifo...!\n");
exit(EXIT_FAILURE);
}
/*...*/
while (!SHUTDOWN) {
cbreak();
keypad(game_win, TRUE);
noecho();
client.command = 3;
client.tecla = wgetch(game_win);
res = write(fd, &client, sizeof (client));
}
close(fd);
pthread_join(tarefa, NULL);
delwin(game_win);
delwin(main_win);
endwin();
exit(0);
}
PS:resp.comand = 4表示服务器正在踢出播放器/客户端。 抱歉英语不好:)