-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add shutdown_socket()
function for follow up of #505
#506
base: main
Are you sure you want to change the base?
Changes from 5 commits
0af50a5
e5129c5
1b00ed3
de2fbde
9ea9f1d
d9a67a2
996896c
e3b0ea1
30a9446
276d7b4
93adf2e
d27c743
ef3345b
311205d
c0d59db
96b146a
e93f266
0f89b4c
aa6fe33
0c7be7f
62354ab
6b8bd91
1edd1e3
8334783
048a5b3
5d34858
7e7d912
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -871,14 +871,7 @@ static void handle_federate_failed(federate_info_t* my_fed) { | |
// Indicate that there will no further events from this federate. | ||
my_fed->enclave.next_event = FOREVER_TAG; | ||
|
||
// According to this: https://stackoverflow.com/questions/4160347/close-vs-shutdown-socket, | ||
// the close should happen when receiving a 0 length message from the other end. | ||
// Here, we just signal the other side that no further writes to the socket are | ||
// forthcoming, which should result in the other end getting a zero-length reception. | ||
shutdown(my_fed->socket, SHUT_RDWR); | ||
|
||
// We can now safely close the socket. | ||
close(my_fed->socket); // from unistd.h | ||
shutdown_socket(&my_fed->socket, false); | ||
|
||
// Check downstream federates to see whether they should now be granted a TAG. | ||
// To handle cycles, need to create a boolean array to keep | ||
|
@@ -917,21 +910,7 @@ static void handle_federate_resign(federate_info_t* my_fed) { | |
// Indicate that there will no further events from this federate. | ||
my_fed->enclave.next_event = FOREVER_TAG; | ||
|
||
// According to this: https://stackoverflow.com/questions/4160347/close-vs-shutdown-socket, | ||
// the close should happen when receiving a 0 length message from the other end. | ||
// Here, we just signal the other side that no further writes to the socket are | ||
// forthcoming, which should result in the other end getting a zero-length reception. | ||
shutdown(my_fed->socket, SHUT_WR); | ||
|
||
// Wait for the federate to send an EOF or a socket error to occur. | ||
// Discard any incoming bytes. Normally, this read should return 0 because | ||
// the federate is resigning and should itself invoke shutdown. | ||
unsigned char buffer[10]; | ||
while (read(my_fed->socket, buffer, 10) > 0) | ||
; | ||
|
||
// We can now safely close the socket. | ||
close(my_fed->socket); // from unistd.h | ||
shutdown_socket(&my_fed->socket, true); | ||
|
||
// Check downstream federates to see whether they should now be granted a TAG. | ||
// To handle cycles, need to create a boolean array to keep | ||
|
@@ -1030,9 +1009,7 @@ void send_reject(int* socket_id, unsigned char error_code) { | |
lf_print_warning("RTI failed to write MSG_TYPE_REJECT message on the socket."); | ||
} | ||
// Close the socket. | ||
shutdown(*socket_id, SHUT_RDWR); | ||
close(*socket_id); | ||
*socket_id = -1; | ||
shutdown_socket(socket_id, false); | ||
LF_MUTEX_UNLOCK(&rti_mutex); | ||
} | ||
|
||
|
@@ -1420,9 +1397,7 @@ void lf_connect_to_federates(int socket_descriptor) { | |
if (!authenticate_federate(&socket_id)) { | ||
lf_print_warning("RTI failed to authenticate the incoming federate."); | ||
// Close the socket. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you, I fixed it as suggested. |
||
shutdown(socket_id, SHUT_RDWR); | ||
close(socket_id); | ||
socket_id = -1; | ||
shutdown_socket(&socket_id, false); | ||
// Ignore the federate that failed authentication. | ||
i--; | ||
continue; | ||
|
@@ -1490,8 +1465,7 @@ void* respond_to_erroneous_connections(void* nothing) { | |
lf_print_warning("RTI failed to write FEDERATION_ID_DOES_NOT_MATCH to erroneous incoming connection."); | ||
} | ||
// Close the socket. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here as well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you, I fixed it as suggested. |
||
shutdown(socket_id, SHUT_RDWR); | ||
close(socket_id); | ||
shutdown_socket(&socket_id, false); | ||
} | ||
return NULL; | ||
} | ||
|
@@ -1554,21 +1528,10 @@ void wait_for_federates(int socket_descriptor) { | |
// Shutdown and close the socket that is listening for incoming connections | ||
// so that the accept() call in respond_to_erroneous_connections returns. | ||
// That thread should then check rti->all_federates_exited and it should exit. | ||
if (shutdown(socket_descriptor, SHUT_RDWR)) { | ||
LF_PRINT_LOG("On shut down TCP socket, received reply: %s", strerror(errno)); | ||
} | ||
// NOTE: In all common TCP/IP stacks, there is a time period, | ||
// typically between 30 and 120 seconds, called the TIME_WAIT period, | ||
// before the port is released after this close. This is because | ||
// the OS is preventing another program from accidentally receiving | ||
// duplicated packets intended for this program. | ||
close(socket_descriptor); | ||
shutdown_socket(&socket_descriptor, false); | ||
|
||
if (rti_remote->socket_descriptor_UDP > 0) { | ||
if (shutdown(rti_remote->socket_descriptor_UDP, SHUT_RDWR)) { | ||
LF_PRINT_LOG("On shut down UDP socket, received reply: %s", strerror(errno)); | ||
} | ||
close(rti_remote->socket_descriptor_UDP); | ||
shutdown_socket(&rti_remote->socket_descriptor_UDP, false); | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor: how about adding more information for this call? For example, "Close the socket without reading until EOF."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, I fixed it as suggested.