From b2a5af8206f7a973d04eba8661d8978889fd2ed9 Mon Sep 17 00:00:00 2001 From: Dongha Kim Date: Fri, 27 Dec 2024 07:37:22 +0900 Subject: [PATCH] Make create_server to one integrated function of TCP and UDP. --- core/federated/RTI/rti_remote.c | 6 +++--- core/federated/RTI/rti_remote.h | 3 +-- core/federated/federate.c | 2 +- core/federated/network/socket_common.c | 17 ++++------------- include/core/federated/network/socket_common.h | 16 ++++------------ 5 files changed, 13 insertions(+), 31 deletions(-) diff --git a/core/federated/RTI/rti_remote.c b/core/federated/RTI/rti_remote.c index 2ca3ad139..4d38d416f 100644 --- a/core/federated/RTI/rti_remote.c +++ b/core/federated/RTI/rti_remote.c @@ -1508,15 +1508,15 @@ void initialize_federate(federate_info_t* fed, uint16_t id) { int32_t start_rti_server(uint16_t port) { _lf_initialize_clock(); // Create the TCP socket server - if (create_TCP_server(port, &rti_remote->socket_descriptor_TCP, &rti_remote->final_port_TCP, true)) { + if (create_server(port, &rti_remote->socket_descriptor_TCP, &rti_remote->final_port_TCP, TCP, true)) { lf_print_error_system_failure("RTI failed to create TCP server: %s.", strerror(errno)); }; lf_print("RTI: Listening for federates."); // Create the UDP socket server // Try to get the rti_remote->final_port_TCP + 1 port if (rti_remote->clock_sync_global_status >= clock_sync_on) { - if (create_UDP_server(rti_remote->final_port_TCP + 1, &rti_remote->socket_descriptor_UDP, - &rti_remote->final_port_UDP, true)) { + if (create_server(rti_remote->final_port_TCP + 1, &rti_remote->socket_descriptor_UDP, + &rti_remote->final_port_UDP, UDP, true)) { lf_print_error_system_failure("RTI failed to create UDP server: %s.", strerror(errno)); } } diff --git a/core/federated/RTI/rti_remote.h b/core/federated/RTI/rti_remote.h index 000fabcac..de6b144aa 100644 --- a/core/federated/RTI/rti_remote.h +++ b/core/federated/RTI/rti_remote.h @@ -31,6 +31,7 @@ #include "lf_types.h" #include "pqueue_tag.h" +#include "socket_common.h" /** Time allowed for federates to reply to stop request. */ #define MAX_TIME_FOR_REPLY_TO_STOP_REQUEST SEC(30) @@ -38,8 +39,6 @@ ///////////////////////////////////////////// //// Data structures -typedef enum socket_type_t { TCP, UDP } socket_type_t; - /** * Information about a federate known to the RTI, including its runtime state, * mode of execution, and connectivity with other federates. diff --git a/core/federated/federate.c b/core/federated/federate.c index f8f13d740..fc2f86911 100644 --- a/core/federated/federate.c +++ b/core/federated/federate.c @@ -1942,7 +1942,7 @@ void lf_connect_to_rti(const char* hostname, int port) { void lf_create_server(int specified_port) { assert(specified_port <= UINT16_MAX && specified_port >= 0); - if (create_TCP_server(specified_port, &_fed.server_socket, (uint16_t*)&_fed.server_port, false)) { + if (create_server(specified_port, &_fed.server_socket, (uint16_t*)&_fed.server_port, TCP, false)) { lf_print_error_system_failure("RTI failed to create TCP server: %s.", strerror(errno)); }; LF_PRINT_LOG("Server for communicating with other federates started using port %d.", _fed.server_port); diff --git a/core/federated/network/socket_common.c b/core/federated/network/socket_common.c index 20fff0585..1d6945346 100644 --- a/core/federated/network/socket_common.c +++ b/core/federated/network/socket_common.c @@ -133,11 +133,10 @@ static int set_socket_bind_option(int socket_descriptor, uint16_t specified_port return used_port; } -static int create_server(uint16_t port, int* final_socket, uint16_t* final_port, int sock_type, - bool increment_port_on_retry) { +int create_server(uint16_t port, int* final_socket, uint16_t* final_port, socket_type_t sock_type, bool increment_port_on_retry) { int socket_descriptor; struct timeval timeout_time; - if (sock_type == 0) { + if (sock_type == TCP) { // Create an IPv4 socket for TCP. socket_descriptor = create_real_time_tcp_socket_errexit(); // Set the timeout time for the communications of the server @@ -149,14 +148,14 @@ static int create_server(uint16_t port, int* final_socket, uint16_t* final_port, timeout_time = (struct timeval){.tv_sec = UDP_TIMEOUT_TIME / BILLION, .tv_usec = (UDP_TIMEOUT_TIME % BILLION) / 1000}; } - char* type = (sock_type == 0) ? "TCP" : "UDP"; + char* type = (sock_type == TCP) ? "TCP" : "UDP"; if (socket_descriptor < 0) { lf_print_error("Failed to create %s socket.", type); return -1; } set_socket_timeout_option(socket_descriptor, &timeout_time); int used_port = set_socket_bind_option(socket_descriptor, port, increment_port_on_retry); - if (sock_type == 0) { + if (sock_type == TCP) { // Enable listening for socket connections. // The second argument is the maximum number of queued socket requests, // which according to the Mac man page is limited to 128. @@ -170,14 +169,6 @@ static int create_server(uint16_t port, int* final_socket, uint16_t* final_port, return 0; } -int create_TCP_server(uint16_t port, int* final_socket, uint16_t* final_port, bool increment_port_on_retry) { - return create_server(port, final_socket, final_port, 0, increment_port_on_retry); -} - -int create_UDP_server(uint16_t port, int* final_socket, uint16_t* final_port, bool increment_port_on_retry) { - return create_server(port, final_socket, final_port, 1, increment_port_on_retry); -} - /** * Return true if either the socket to the RTI is broken or the socket is * alive and the first unread byte on the socket's queue is MSG_TYPE_FAILED. diff --git a/include/core/federated/network/socket_common.h b/include/core/federated/network/socket_common.h index 71570c125..a9e744791 100644 --- a/include/core/federated/network/socket_common.h +++ b/include/core/federated/network/socket_common.h @@ -63,6 +63,8 @@ */ #define MSG_TYPE_FAILED 25 +typedef enum socket_type_t { TCP, UDP } socket_type_t; + /** * Mutex protecting socket close operations. */ @@ -93,21 +95,11 @@ int create_real_time_tcp_socket_errexit(); * @param port The port number to use or 0 to let the OS pick or 1 to start trying at DEFAULT_PORT. * @param final_socket Pointer to the returned socket descriptor on which accepting connections will occur. * @param final_port Pointer to the final port the server will use. + * @param sock_type Type of the socket wheter TCP or UDP. * @param increment_port_on_retry Boolean to retry port increment. * @return 0 for success, -1 for failure. */ -int create_TCP_server(uint16_t port, int* final_socket, uint16_t* final_port, bool increment_port_on_retry); -/** - * @brief Create a UDP server that listens for socket connections. - * - * This function is just like create_TCP_server(), except that it creates a UDP server. - * - * @param port The port number to use or 0 to let the OS pick or 1 to start trying at DEFAULT_PORT. - * @param final_socket Pointer to the returned socket descriptor on which accepting connections will occur. - * @param final_port Pointer to the final port the server will use. - * @return 0 for success, -1 for failure. - */ -int create_UDP_server(uint16_t port, int* final_socket, uint16_t* final_port, bool increment_port_on_retry); +int create_server(uint16_t port, int* final_socket, uint16_t* final_port, socket_type_t sock_type, bool increment_port_on_retry); /** * These two functions waits for an incoming connection request on the specified server socket.