Skip to content

Commit

Permalink
Improve logging in case of peer disconnection
Browse files Browse the repository at this point in the history
  • Loading branch information
arkq committed Nov 15, 2023
1 parent 84f88cf commit 1bbb0aa
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
19 changes: 14 additions & 5 deletions src/tcp2udp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,22 @@ void tcp2udp::tcp::peer::keepalive(unsigned int idle_time) {
m_socket.set_option(asio::socket_base::linger(true, 0));
}

void tcp2udp::tcp::peer::connected() {
// Save remote endpoint, so it could be used after the socket is disconnected
m_socket_ep_remote = m_socket.remote_endpoint();
}

void tcp2udp::do_accept_handler(tcp::peer::ptr peer, const boost::system::error_code & ec) {
if (ec) {
LOG(error) << "accept [" << utils::to_string(m_ep_tcp_acc) << "]: " << ec.message();
} else {
LOG(debug) << "accept [" << utils::to_string(m_ep_tcp_acc)
<< "]: New connection: peer=" << utils::to_string(peer->remote_endpoint());
LOG(debug) << "accept [" << utils::to_string(m_ep_tcp_acc) << "]: New connection: peer="
<< utils::to_string(peer->tcp().remote_endpoint());
if (m_tcp_keep_alive_idle_time > 0)
// Setup TCP keep-alive on the peer socket
peer->keepalive(m_tcp_keep_alive_idle_time);
// Mark peer as connected
peer->connected();
// Start handling TCP packets
do_send_init(peer);
}
Expand All @@ -87,12 +94,14 @@ void tcp2udp::do_send_handler(tcp::peer::ptr peer, const boost::system::error_co
utils::ip::tcp::buffer::ptr buffer, size_t length, bool ctrl) {

if (ec) {
LOG(error) << "send [" << to_string(peer) << "]: " << ec.message();
if (ec == asio::error::eof) {
if (ec == asio::error::eof || ec == asio::error::connection_reset) {
LOG(debug) << "send: Connection closed: peer="
<< utils::to_string(peer->remote_endpoint());
// Stop UDP receiver if there is no TCP peer
peer->udp().cancel();
return;
}
LOG(error) << "send [" << to_string(peer) << "]: " << ec.message();
// Try to recover from error
do_send_init(peer);
return;
Expand Down Expand Up @@ -135,9 +144,9 @@ void tcp2udp::do_recv_handler(tcp::peer::ptr peer, const boost::system::error_co
utils::ip::udp::buffer::ptr buffer, size_t length) {

if (ec) {
LOG(error) << "recv [" << to_string(peer) << "]: " << ec.message();
if (ec == asio::error::operation_aborted)
return;
LOG(error) << "recv [" << to_string(peer) << "]: " << ec.message();
// Try to recover from error
do_recv(peer);
return;
Expand Down
4 changes: 3 additions & 1 deletion src/tcp2udp.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,15 @@ class tcp2udp {

bool init() { return std::exchange(m_initialized, true); }
void keepalive(unsigned int idle_time);
void connected();

template <typename... A> auto send(A... args) { return m_socket.send(args...); }
auto local_endpoint() const { return m_socket.local_endpoint(); }
auto remote_endpoint() const { return m_socket.remote_endpoint(); }
auto remote_endpoint() const { return m_socket_ep_remote; }

private:
asio::ip::tcp::socket m_socket;
asio::ip::tcp::endpoint m_socket_ep_remote;
asio::ip::udp::socket m_socket_udp_dest;
bool m_initialized = false;
};
Expand Down
7 changes: 5 additions & 2 deletions src/udp2tcp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,14 @@ void udp2tcp::do_recv_handler(const boost::system::error_code & ec,
utils::ip::tcp::buffer::ptr buffer, size_t length, bool ctrl) {

if (ec) {
LOG(error) << "recv [" << to_string() << "]: " << ec.message();
if (ec == asio::error::eof) {
if (ec == asio::error::operation_aborted)
return;
if (ec == asio::error::eof || ec == asio::error::connection_reset) {
LOG(debug) << "recv: Connection closed: peer=" << utils::to_string(m_ep_tcp_dest);
m_socket_tcp_dest.close();
return;
}
LOG(error) << "recv [" << to_string() << "]: " << ec.message();
// Try to recover from error
do_recv_init();
return;
Expand Down

0 comments on commit 1bbb0aa

Please sign in to comment.