Fixes to linux TCP server example
This commit is contained in:
parent
dc6a9ee949
commit
dba38fc917
@ -59,10 +59,11 @@ int server_fd = -1;
|
|||||||
int client_read_fd = -1;
|
int client_read_fd = -1;
|
||||||
fd_set client_connections;
|
fd_set client_connections;
|
||||||
|
|
||||||
void close_server_on_exit(int sig) {
|
void close_tcp_server() {
|
||||||
UNUSED_PARAM(sig);
|
if (server_fd != -1) {
|
||||||
if (server_fd != -1)
|
|
||||||
close(server_fd);
|
close(server_fd);
|
||||||
|
printf("Server closed\n");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -110,12 +111,6 @@ int create_tcp_server(const char* address, const char* port) {
|
|||||||
return errno;
|
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;
|
server_fd = fd;
|
||||||
FD_ZERO(&client_connections);
|
FD_ZERO(&client_connections);
|
||||||
|
|
||||||
@ -163,17 +158,12 @@ void* server_poll(void) {
|
|||||||
|
|
||||||
void disconnect(void* conn) {
|
void disconnect(void* conn) {
|
||||||
int fd = *(int*) conn;
|
int fd = *(int*) conn;
|
||||||
|
FD_CLR(fd, &client_connections);
|
||||||
close(fd);
|
close(fd);
|
||||||
printf("Closed connection %d\n", fd);
|
printf("Closed connection %d\n", fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void close_server(void) {
|
|
||||||
close(server_fd);
|
|
||||||
printf("Server closed\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Read/write/sleep platform functions
|
// Read/write/sleep platform functions
|
||||||
|
|
||||||
int32_t read_fd_linux(uint8_t* buf, uint16_t count, int32_t timeout_ms, void* arg) {
|
int32_t read_fd_linux(uint8_t* buf, uint16_t count, int32_t timeout_ms, void* arg) {
|
||||||
|
|||||||
@ -25,10 +25,17 @@
|
|||||||
#define FILE_SIZE_MAX 32
|
#define FILE_SIZE_MAX 32
|
||||||
|
|
||||||
// A single nmbs_bitfield variable can keep 2000 coils
|
// A single nmbs_bitfield variable can keep 2000 coils
|
||||||
|
bool terminate = false;
|
||||||
nmbs_bitfield server_coils = {0};
|
nmbs_bitfield server_coils = {0};
|
||||||
uint16_t server_registers[REGS_ADDR_MAX] = {0};
|
uint16_t server_registers[REGS_ADDR_MAX] = {0};
|
||||||
uint16_t server_file[FILE_SIZE_MAX];
|
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) {
|
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(arg);
|
||||||
UNUSED_PARAM(unit_id);
|
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[]) {
|
int main(int argc, char* argv[]) {
|
||||||
|
signal(SIGTERM, sighandler);
|
||||||
|
signal(SIGSTOP, sighandler);
|
||||||
|
signal(SIGINT, sighandler);
|
||||||
|
signal(SIGQUIT, sighandler);
|
||||||
|
|
||||||
if (argc < 3) {
|
if (argc < 3) {
|
||||||
fprintf(stderr, "Usage: server-tcp [address] [port]\n");
|
fprintf(stderr, "Usage: server-tcp [address] [port]\n");
|
||||||
return 1;
|
return 1;
|
||||||
@ -170,14 +182,13 @@ int main(int argc, char* argv[]) {
|
|||||||
printf("Modbus TCP server started\n");
|
printf("Modbus TCP server started\n");
|
||||||
|
|
||||||
// Our server supports requests from more than one client
|
// 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
|
// Our server_poll() function will return the next client TCP connection to read from
|
||||||
void* conn = server_poll();
|
void* conn = server_poll();
|
||||||
if (!conn)
|
if (conn) {
|
||||||
break;
|
// Set the next connection handler used by the read/write platform functions
|
||||||
|
nmbs_set_platform_arg(&nmbs, conn);
|
||||||
// Set the next connection handler used by the read/write platform functions
|
}
|
||||||
nmbs_set_platform_arg(&nmbs, conn);
|
|
||||||
|
|
||||||
err = nmbs_server_poll(&nmbs);
|
err = nmbs_server_poll(&nmbs);
|
||||||
if (err != NMBS_ERROR_NONE) {
|
if (err != NMBS_ERROR_NONE) {
|
||||||
@ -187,7 +198,7 @@ int main(int argc, char* argv[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Close the TCP server
|
// Close the TCP server
|
||||||
close_server();
|
close_tcp_server();
|
||||||
|
|
||||||
// No need to destroy the nmbs instance, bye bye
|
// No need to destroy the nmbs instance, bye bye
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user