Skip to content

Commit

Permalink
tcp graceful close
Browse files Browse the repository at this point in the history
  • Loading branch information
cnbatch committed Dec 23, 2023
1 parent 3b4a7bb commit a7d79e2
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ int main(int argc, char *argv[])
if (argc <= 1)
{
char app_name[] = "punchnat";
printf("%s version 20231112\n", app_name);
printf("%s version 20231223\n", app_name);
printf("Usage: %s config1.conf\n", app_name);
printf(" %s config1.conf config2.conf...\n", app_name);
return 0;
Expand Down
9 changes: 5 additions & 4 deletions src/networks/connections.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ bool tcp_session::is_open()

void tcp_session::async_read_data()
{
if (!stopped.load() && connection_socket.is_open())
if (connection_socket.is_open())
{
std::unique_ptr<uint8_t[]> buffer_cache = std::make_unique<uint8_t[]>(BUFFER_SIZE);
auto asio_buffer = asio::buffer(buffer_cache.get(), BUFFER_SIZE);
Expand Down Expand Up @@ -250,11 +250,12 @@ void tcp_session::after_write_completed(const asio::error_code &error, size_t by

void tcp_session::after_read_completed(std::unique_ptr<uint8_t[]> buffer_cache, const asio::error_code &error, size_t bytes_transferred)
{
if (stopped.load())
return;

if (error)
{
callback(std::move(buffer_cache), bytes_transferred, this);
if (stopped.load())
return;

std::shared_lock locker{ callback_mutex };
auto callback_before_disconnect = callback_for_disconnect;
locker.unlock();
Expand Down
3 changes: 2 additions & 1 deletion src/networks/tcp_mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ void tcp_mode::local_disconnect(tcp_session *incoming_session, tcp_session *outc
incoming_session->when_disconnect(empty_tcp_disconnect);
if (tcp_sessions.find(outcoming_session) != tcp_sessions.end())
{
outcoming_session->socket().close();
asio::error_code ec;
outcoming_session->socket().shutdown(asio::socket_base::shutdown_both, ec);
}
incoming_session->replace_callback(empty_tcp_callback);
incoming_session->stop();
Expand Down

0 comments on commit a7d79e2

Please sign in to comment.