Skip to content

Commit

Permalink
Refactor close_outbound_socket().
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakio815 committed Dec 21, 2024
1 parent de2fbde commit 9ea9f1d
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions core/federated/federate.c
Original file line number Diff line number Diff line change
Expand Up @@ -814,22 +814,22 @@ static void* listen_to_federates(void* _args) {
* if _lf_normal_termination is true and otherwise proceeds without the lock.
* @param fed_id The ID of the peer federate receiving messages from this
* federate, or -1 if the RTI (centralized coordination).
* @param flag 0 if the socket has received EOF, 1 if not, -1 if abnormal termination.
*/
static void close_outbound_socket(int fed_id, int flag) {
static void close_outbound_socket(int fed_id) {
assert(fed_id >= 0 && fed_id < NUMBER_OF_FEDERATES);
// Close outbound connections, in case they have not closed themselves.
// This will result in EOF being sent to the remote federate, except for
// abnormal termination, in which case it will just close the socket.
if (_lf_normal_termination) {
LF_MUTEX_LOCK(&lf_outbound_socket_mutex);
}
if (_fed.sockets_for_outbound_p2p_connections[fed_id] >= 0) {
// Close the socket by sending a FIN packet indicating that no further writes
// are expected. Then read until we get an EOF indication.
if (flag > 0) {
if (_fed.sockets_for_outbound_p2p_connections[fed_id] >= 0) {
// Close the socket by sending a FIN packet indicating that no further writes
// are expected. Then read until we get an EOF indication.
shutdown_socket(&_fed.sockets_for_outbound_p2p_connections[fed_id], true);
}
}
if (_lf_normal_termination) {
LF_MUTEX_UNLOCK(&lf_outbound_socket_mutex);
} else {
shutdown_socket(&_fed.sockets_for_outbound_p2p_connections[fed_id], false);
}
}

Expand Down Expand Up @@ -1643,8 +1643,7 @@ void lf_terminate_execution(environment_t* env) {
// Close outbound connections, in case they have not closed themselves.
// This will result in EOF being sent to the remote federate, except for
// abnormal termination, in which case it will just close the socket.
int flag = _lf_normal_termination ? 1 : -1;
close_outbound_socket(i, flag);
close_outbound_socket(i);
}

LF_PRINT_DEBUG("Waiting for inbound p2p socket listener threads.");
Expand Down

0 comments on commit 9ea9f1d

Please sign in to comment.