Skip to content

Commit

Permalink
Improved on uxr_close_tcp_platform() to avoid potentially losing byte…
Browse files Browse the repository at this point in the history
…s in flight.
  • Loading branch information
sp193 committed Nov 13, 2019
1 parent 0a3d113 commit ff4aa80
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
17 changes: 16 additions & 1 deletion src/c/profile/transport/tcp/tcp_transport_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,22 @@ bool uxr_init_tcp_platform(struct uxrTCPPlatform* platform, const char* ip, uint

bool uxr_close_tcp_platform(struct uxrTCPPlatform* platform)
{
return (-1 == platform->poll_fd.fd) ? true : (0 == close(platform->poll_fd.fd));
bool rv = true;

if (-1 != platform->poll_fd.fd)
{
/* Synchronize the stream with the agent, to avoid losing bytes currently in flight. */
shutdown(platform->poll_fd.fd, SHUT_WR);
int poll_rv = poll(&platform->poll_fd, 1, 10000);
if (0 < poll_rv)
{
char dummy;
while (recv(platform->poll_fd.fd, &dummy, sizeof(dummy), 0) > 0) {};
}

rv = (0 == close(platform->poll_fd.fd));
}
return rv;
}

size_t uxr_write_tcp_data_platform(struct uxrTCPPlatform* platform,
Expand Down
20 changes: 18 additions & 2 deletions src/c/profile/transport/tcp/tcp_transport_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,24 @@ bool uxr_init_tcp_platform(struct uxrTCPPlatform* platform, const char* ip, uint

bool uxr_close_tcp_platform(struct uxrTCPPlatform* platform)
{
bool rv = (INVALID_SOCKET == platform->poll_fd.fd) ? true : (0 == closesocket(platform->poll_fd.fd));
return (0 == WSACleanup()) && rv;
bool rv = true;

if (INVALID_SOCKET != platform->poll_fd.fd)
{
/* Synchronize the stream with the agent, to avoid losing bytes currently in flight. */
shutdown(platform->poll_fd.fd, SD_SEND);
int poll_rv = WSAPoll(&platform->poll_fd, 1, 10000);
if (0 < poll_rv)
{
char dummy;
while (recv(platform->poll_fd.fd, &dummy, sizeof(dummy), 0) > 0) {};
}
if (0 == closesocket(platform->poll_fd.fd))
{
rv = (0 == WSACleanup());
}
}
return rv;
}

size_t uxr_write_tcp_data_platform(struct uxrTCPPlatform* platform,
Expand Down

0 comments on commit ff4aa80

Please sign in to comment.