From dba38fc917b55b5ba4c729a656842706d273227c Mon Sep 17 00:00:00 2001 From: Valerio De Benedetto Date: Wed, 7 Feb 2024 15:53:54 +0100 Subject: [PATCH] Fixes to linux TCP server example --- examples/linux/platform.h | 20 +++++--------------- examples/linux/server-tcp.c | 25 ++++++++++++++++++------- 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/examples/linux/platform.h b/examples/linux/platform.h index 6b5e71d..20f037b 100644 --- a/examples/linux/platform.h +++ b/examples/linux/platform.h @@ -59,10 +59,11 @@ int server_fd = -1; int client_read_fd = -1; fd_set client_connections; -void close_server_on_exit(int sig) { - UNUSED_PARAM(sig); - if (server_fd != -1) +void close_tcp_server() { + if (server_fd != -1) { close(server_fd); + printf("Server closed\n"); + } } @@ -110,12 +111,6 @@ int create_tcp_server(const char* address, const char* port) { return errno; } - signal(SIGINT, close_server_on_exit); - signal(SIGTERM, close_server_on_exit); - signal(SIGQUIT, close_server_on_exit); - signal(SIGSTOP, close_server_on_exit); - signal(SIGHUP, close_server_on_exit); - server_fd = fd; FD_ZERO(&client_connections); @@ -163,17 +158,12 @@ void* server_poll(void) { void disconnect(void* conn) { int fd = *(int*) conn; + FD_CLR(fd, &client_connections); close(fd); printf("Closed connection %d\n", fd); } -void close_server(void) { - close(server_fd); - printf("Server closed\n"); -} - - // Read/write/sleep platform functions int32_t read_fd_linux(uint8_t* buf, uint16_t count, int32_t timeout_ms, void* arg) { diff --git a/examples/linux/server-tcp.c b/examples/linux/server-tcp.c index 9c422cc..a391e95 100644 --- a/examples/linux/server-tcp.c +++ b/examples/linux/server-tcp.c @@ -25,10 +25,17 @@ #define FILE_SIZE_MAX 32 // A single nmbs_bitfield variable can keep 2000 coils +bool terminate = false; nmbs_bitfield server_coils = {0}; uint16_t server_registers[REGS_ADDR_MAX] = {0}; uint16_t server_file[FILE_SIZE_MAX]; + +void sighandler(int s) { + UNUSED_PARAM(s); + terminate = true; +} + nmbs_error handle_read_coils(uint16_t address, uint16_t quantity, nmbs_bitfield coils_out, uint8_t unit_id, void* arg) { UNUSED_PARAM(arg); UNUSED_PARAM(unit_id); @@ -130,6 +137,11 @@ nmbs_error handle_write_file_record(uint16_t file_number, uint16_t record_number int main(int argc, char* argv[]) { + signal(SIGTERM, sighandler); + signal(SIGSTOP, sighandler); + signal(SIGINT, sighandler); + signal(SIGQUIT, sighandler); + if (argc < 3) { fprintf(stderr, "Usage: server-tcp [address] [port]\n"); return 1; @@ -170,14 +182,13 @@ int main(int argc, char* argv[]) { printf("Modbus TCP server started\n"); // Our server supports requests from more than one client - while (true) { + while (!terminate) { // Our server_poll() function will return the next client TCP connection to read from void* conn = server_poll(); - if (!conn) - break; - - // Set the next connection handler used by the read/write platform functions - nmbs_set_platform_arg(&nmbs, conn); + if (conn) { + // Set the next connection handler used by the read/write platform functions + nmbs_set_platform_arg(&nmbs, conn); + } err = nmbs_server_poll(&nmbs); if (err != NMBS_ERROR_NONE) { @@ -187,7 +198,7 @@ int main(int argc, char* argv[]) { } // Close the TCP server - close_server(); + close_tcp_server(); // No need to destroy the nmbs instance, bye bye return 0;