1
1
Fork 0
mirror of https://github.com/oxen-io/lokinet synced 2023-12-14 06:53:00 +01:00

change how tcp connection closes are handled

this still feels incorrect, but more correct than before
This commit is contained in:
Thomas Winget 2023-04-25 10:09:05 -04:00
parent 5514a27a59
commit 767935ede8
3 changed files with 26 additions and 3 deletions

View file

@ -326,7 +326,7 @@ namespace llarp::quic
if (is_shutdown)
log::debug(logcat, "Stream is already shutting down");
else if (error_code)
else if (error_code && *error_code != 0)
{
is_closing = is_shutdown = true;
ngtcp2_conn_shutdown_stream(conn, stream_id.id, *error_code);

View file

@ -201,6 +201,20 @@ namespace llarp::quic
return is_closing;
}
// Informs the stream that TCP EOF has been received on this end
void
set_eof()
{
tcp_eof = true;
}
// Returns true of the TCP connection on this end gave us EOF
bool
has_eof()
{
return tcp_eof;
}
// Callback invoked when data is received
using data_callback_t = std::function<void(Stream&, bstring_view)>;
@ -357,6 +371,7 @@ namespace llarp::quic
bool is_closing{false};
bool sent_fin{false};
bool is_shutdown{false};
bool tcp_eof{false};
// Async trigger we use to schedule when_available callbacks (so that we can make them happen in
// batches rather than after each and every packet ack).

View file

@ -120,7 +120,11 @@ namespace llarp::quic
// otherwise
if (auto locked_conn = weak_conn.lock())
{
stream->close(-1);
// If this end of the stream closed due to an abrupt close to the local TCP
// connection rather than an EOF, send an error code along so the other end
// knows this end is dead. Otherwise, the streams on either end should die
// gracefully when both ends of the TCP connection are properly closed.
stream->close(stream->has_eof() ? 0 : tunnel::ERROR_TCP);
}
stream->data(nullptr);
}
@ -129,6 +133,10 @@ namespace llarp::quic
tcp.on<uvw::EndEvent>([](auto&, uvw::TCPHandle& c) {
// This fires on eof, most likely because the other side of the TCP connection closed it.
log::info(logcat, "EOF on connection to {}:{}", c.peer().ip, c.peer().port);
if (auto stream = c.data<Stream>())
{
stream->set_eof(); // CloseEvent will send graceful shutdown to other end
}
c.close();
});
tcp.on<uvw::ErrorEvent>([](const uvw::ErrorEvent& e, uvw::TCPHandle& tcp) {
@ -145,7 +153,7 @@ namespace llarp::quic
stream->data(nullptr);
tcp.data(nullptr);
}
// tcp.closeReset();
tcp.close();
});
tcp.on<uvw::DataEvent>(on_outgoing_data);
stream.data_callback = on_incoming_data;