Skip to content
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

Fixed various TCP-related deadlocks and improved disconnection mechanism #102

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 91 additions & 56 deletions src/cpp/transport/tcp/TCPServerLinux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,81 +78,93 @@ bool TCPv4Agent::init()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rebase it over develop branch to solve conflicts. Thanks!

if (-1 != listener_poll_.fd)
{
/* IP and Port setup. */
struct sockaddr_in address;
address.sin_family = AF_INET;
address.sin_port = htons(transport_address_.medium_locator().port());
address.sin_addr.s_addr = INADDR_ANY;
memset(address.sin_zero, '\0', sizeof(address.sin_zero));
if (-1 != bind(listener_poll_.fd, (struct sockaddr*)&address, sizeof(address)))
/* Set reuse port. */
int reuse = 1;
if (-1 != setsockopt(listener_poll_.fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)))
{
/* Log. */
UXR_AGENT_LOG_DEBUG(
UXR_DECORATE_GREEN("port opened"),
"port: {}",
transport_address_.medium_locator().port());
/* IP and Port setup. */
struct sockaddr_in address;
address.sin_family = AF_INET;
address.sin_port = htons(transport_address_.medium_locator().port());
address.sin_addr.s_addr = INADDR_ANY;
memset(address.sin_zero, '\0', sizeof(address.sin_zero));
if (-1 != bind(listener_poll_.fd, (struct sockaddr*)&address, sizeof(address)))
{
/* Log. */
UXR_AGENT_LOG_DEBUG(
UXR_DECORATE_GREEN("port opened"),
"port: {}",
transport_address_.medium_locator().port());

/* Setup listener poll. */
listener_poll_.events = POLLIN;
/* Setup listener poll. */
listener_poll_.events = POLLIN;

/* Setup connections. */
for (size_t i = 0; i < poll_fds_.size(); ++i)
{
poll_fds_[i].fd = -1;
poll_fds_[i].events = POLLIN;
connections_[i].poll_fd = &poll_fds_[i];
connections_[i].id = uint32_t(i);
connections_[i].active = false;
init_input_buffer(connections_[i].input_buffer);
free_connections_.push_back(connections_[i].id);
}
/* Setup connections. */
for (size_t i = 0; i < poll_fds_.size(); ++i)
{
poll_fds_[i].fd = -1;
poll_fds_[i].events = POLLIN;
connections_[i].poll_fd = &poll_fds_[i];
connections_[i].id = uint32_t(i);
connections_[i].active = false;
init_input_buffer(connections_[i].input_buffer);
free_connections_.push_back(connections_[i].id);
}

/* Init listener. */
if (-1 != listen(listener_poll_.fd, TCP_MAX_BACKLOG_CONNECTIONS))
{
running_cond_ = true;
listener_thread_ = std::thread(&TCPv4Agent::listener_loop, this);

/* Get local address. */
int fd = socket(PF_INET, SOCK_DGRAM, 0);
struct sockaddr_in temp_addr;
temp_addr.sin_family = AF_INET;
temp_addr.sin_port = htons(80);
temp_addr.sin_addr.s_addr = inet_addr("1.2.3.4");
int connected = connect(fd, (struct sockaddr *)&temp_addr, sizeof(temp_addr));
if (0 == connected)
/* Init listener. */
if (-1 != listen(listener_poll_.fd, TCP_MAX_BACKLOG_CONNECTIONS))
{
struct sockaddr local_addr;
socklen_t local_addr_len = sizeof(local_addr);
if (-1 != getsockname(fd, &local_addr, &local_addr_len))
running_cond_ = true;
listener_thread_ = std::thread(&TCPv4Agent::listener_loop, this);

/* Get local address. */
int fd = socket(PF_INET, SOCK_DGRAM, 0);
struct sockaddr_in temp_addr;
temp_addr.sin_family = AF_INET;
temp_addr.sin_port = htons(80);
temp_addr.sin_addr.s_addr = inet_addr("1.2.3.4");
int connected = connect(fd, (struct sockaddr *)&temp_addr, sizeof(temp_addr));
if (0 == connected)
{
transport_address_.medium_locator().address({uint8_t(local_addr.sa_data[2]),
uint8_t(local_addr.sa_data[3]),
uint8_t(local_addr.sa_data[4]),
uint8_t(local_addr.sa_data[5])});
rv = true;
UXR_AGENT_LOG_INFO(
UXR_DECORATE_GREEN("running..."),
"port: {}",
transport_address_.medium_locator().port());
struct sockaddr local_addr;
socklen_t local_addr_len = sizeof(local_addr);
if (-1 != getsockname(fd, &local_addr, &local_addr_len))
{
transport_address_.medium_locator().address({uint8_t(local_addr.sa_data[2]),
uint8_t(local_addr.sa_data[3]),
uint8_t(local_addr.sa_data[4]),
uint8_t(local_addr.sa_data[5])});
rv = true;
UXR_AGENT_LOG_INFO(
UXR_DECORATE_GREEN("running..."),
"port: {}",
transport_address_.medium_locator().port());
}
::close(fd);
}
::close(fd);
}
else
{
UXR_AGENT_LOG_ERROR(
UXR_DECORATE_RED("listen error"),
"port: {}",
transport_address_.medium_locator().port());
}
}
else
{
UXR_AGENT_LOG_ERROR(
UXR_DECORATE_RED("listen error"),
UXR_DECORATE_RED("bind error"),
"port: {}",
transport_address_.medium_locator().port());
}
}
else
{
UXR_AGENT_LOG_ERROR(
UXR_DECORATE_RED("bind error"),
"port: {}",
transport_address_.medium_locator().port());
UXR_DECORATE_RED("socket opt error"),
"Port: {}",
listener_poll_.fd);
}
}
else
Expand Down Expand Up @@ -391,6 +403,16 @@ bool TCPv4Agent::close_connection(TCPConnection& connection)
lock.unlock();
/* Add lock for close. */
std::unique_lock<std::mutex> conn_lock(connection.mtx);

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

if (0 == ::close(connection_platform.poll_fd->fd))
{
connection_platform.poll_fd->fd = -1;
Expand Down Expand Up @@ -445,6 +467,11 @@ bool TCPv4Agent::read_message(int timeout)
rv = true;
}
}
else if (0 < ((POLLERR | POLLHUP) & conn.poll_fd->revents))
{
/* Disconnect failed connections. */
close_connection(conn);
}
}
}
else
Expand Down Expand Up @@ -519,6 +546,10 @@ size_t TCPv4Agent::recv_locking(
errcode = (0 == poll_rv) ? 0 : 1;
}
}
else
{
errcode = 1;
}
return rv;
}

Expand All @@ -544,6 +575,10 @@ size_t TCPv4Agent::send_locking(
errcode = 1;
}
}
else
{
errcode = 1;
}
return rv;
}

Expand Down
Loading