Fixed poll() after discard of initial message

This commit is contained in:
Daniele Lacamera 2024-11-29 14:45:34 +00:00
parent 7aace4e904
commit d3c0fbf8aa

View file

@ -51,6 +51,7 @@ void clear_screen(int client_fd) {
/* Execute a git-slides command */
void execute_git_slides(const char *repo_path, const char *slide_command) {
char command[CMD_SIZE];
memset(command, 0, CMD_SIZE);
snprintf(command, sizeof(command), "cd %s && git-slides %s", repo_path, slide_command);
system(command);
}
@ -109,8 +110,8 @@ void send_welcome_message(struct Client *client) {
const char *welcome_msg =
"Welcome to the git-slides presentation!\n"
"Use SPACE (forward), BACKSPACE (back), or Q (quit).\n\n";
write(client->fd, welcome_msg, strlen(welcome_msg));
write(client->fd, client->current_slide, strlen(client->current_slide));
write(client->fd, welcome_msg, strlen(welcome_msg));
}
@ -151,13 +152,13 @@ int main() {
clients[i].fd = -1;
}
fds[0].fd = server_fd;
fds[0].events = POLLIN;
printf("Server running on port %d\n", PORT);
while (1) {
int num_fds = 1;
fds[0].fd = server_fd;
fds[0].events = POLLIN;
/* Prepare the pollfd array */
for (i = 0; i < MAX_CLIENTS; i++) {
@ -210,16 +211,17 @@ int main() {
load_slide_content(client->slides_path, client->current_slide);
send_telnet_negotiation(client_fd);
send_welcome_message(client);
printf("Client connected: %d\n", client_fd);
/* Discard any pending input */
fds[0].fd = client_fd;
fds[0].events = POLLIN;
while (poll(fds, 1, 20) > 0) {
while (poll(fds, 1, 500) > 0) {
char command;
read(client_fd, &command, 1);
}
printf("Go!\n");
send_welcome_message(client);
continue;
}
}