diff --git a/Kernel/Net/TCPSocket.cpp b/Kernel/Net/TCPSocket.cpp index fa7eb8cc10d..cf4c6f71ff0 100644 --- a/Kernel/Net/TCPSocket.cpp +++ b/Kernel/Net/TCPSocket.cpp @@ -29,6 +29,21 @@ void TCPSocket::for_each(Function callback) }); } +bool TCPSocket::unref() const +{ + bool did_hit_zero = sockets_by_tuple().with_exclusive([&](auto& table) { + if (deref_base()) + return false; + table.remove(tuple()); + return true; + }); + if (did_hit_zero) { + const_cast(*this).will_be_destroyed(); + delete this; + } + return did_hit_zero; +} + void TCPSocket::set_state(State new_state) { dbgln_if(TCP_SOCKET_DEBUG, "TCPSocket({}) state moving from {} to {}", this, to_string(m_state), to_string(new_state)); diff --git a/Kernel/Net/TCPSocket.h b/Kernel/Net/TCPSocket.h index 4cf0f322962..85f4c9ec983 100644 --- a/Kernel/Net/TCPSocket.h +++ b/Kernel/Net/TCPSocket.h @@ -22,6 +22,8 @@ public: static ErrorOr> try_create(int protocol, NonnullOwnPtr receive_buffer); virtual ~TCPSocket() override; + virtual bool unref() const override; + enum class Direction { Unspecified, Outgoing,