Skip to content

Commit

Permalink
Fixes to linux TCP server example
Browse files Browse the repository at this point in the history
  • Loading branch information
debevv committed Feb 7, 2024
1 parent dc6a9ee commit dba38fc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
20 changes: 5 additions & 15 deletions examples/linux/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}


Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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) {
Expand Down
25 changes: 18 additions & 7 deletions examples/linux/server-tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand Down

0 comments on commit dba38fc

Please sign in to comment.