Skip to content

Commit

Permalink
demo/uring/UringNetCat: use ShutdownListener
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKellermann committed Feb 4, 2025
1 parent 6fb5cfc commit 7215426
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion demo/uring/UringNetCat.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "event/net/BufferedSocket.hxx"
#include "event/net/ConnectSocket.hxx"
#include "event/Loop.hxx"
#include "event/ShutdownListener.hxx"
#include "system/Error.hxx"
#include "util/PrintException.hxx"

Expand All @@ -18,6 +19,7 @@
#include <stdlib.h>

class NetCat final : ConnectSocketHandler, BufferedSocketHandler {
ShutdownListener shutdown_listener;
ConnectSocket connect_socket;
BufferedSocket socket;

Expand All @@ -26,8 +28,10 @@ class NetCat final : ConnectSocketHandler, BufferedSocketHandler {
public:
[[nodiscard]]
explicit NetCat(EventLoop &event_loop) noexcept
:connect_socket(event_loop, *this), socket(event_loop)
:shutdown_listener(event_loop, BIND_THIS_METHOD(OnShutdown)),
connect_socket(event_loop, *this), socket(event_loop)
{
shutdown_listener.Enable();
}

auto &GetEventLoop() const noexcept {
Expand All @@ -44,13 +48,22 @@ class NetCat final : ConnectSocketHandler, BufferedSocketHandler {
}

private:
void OnShutdown() noexcept {
if (connect_socket.IsPending())
connect_socket.Cancel();
else
socket.Close();
GetEventLoop().SetVolatile();
}

/* virtual methods from class ConnectSocketHandler */
void OnSocketConnectSuccess(UniqueSocketDescriptor fd) noexcept override {
socket.Init(fd.Release(), FdType::FD_TCP, std::chrono::minutes{1}, *this);
socket.EnableUring(*GetEventLoop().GetUring());
}

void OnSocketConnectError(std::exception_ptr e) noexcept override {
shutdown_listener.Disable();
GetEventLoop().SetVolatile();
error = std::move(e);
}
Expand All @@ -69,6 +82,7 @@ class NetCat final : ConnectSocketHandler, BufferedSocketHandler {
}

bool OnBufferedEnd() override {
shutdown_listener.Disable();
GetEventLoop().SetVolatile();
return true;
}
Expand All @@ -78,6 +92,7 @@ class NetCat final : ConnectSocketHandler, BufferedSocketHandler {
}

void OnBufferedError(std::exception_ptr e) noexcept override {
shutdown_listener.Disable();
GetEventLoop().SetVolatile();
error = std::move(e);
}
Expand Down

0 comments on commit 7215426

Please sign in to comment.