1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 19:57:35 +00:00

Kernel: Remove unnecessary SocketHandle<T> class

This was used to return a pre-locked UDPSocket in one place, but there
was really no need for that mechanism in the first place since the
caller ends up locking the socket anyway.
This commit is contained in:
Andreas Kling 2021-12-25 11:23:57 +01:00
parent e923cf6624
commit 9965e59ad8
3 changed files with 4 additions and 45 deletions

View file

@ -183,44 +183,6 @@ private:
NonnullRefPtrVector<Socket> m_pending; NonnullRefPtrVector<Socket> m_pending;
}; };
template<typename SocketType>
class SocketHandle {
public:
SocketHandle() = default;
SocketHandle(NonnullRefPtr<SocketType>&& socket)
: m_socket(move(socket))
{
if (m_socket)
m_socket->mutex().lock();
}
SocketHandle(SocketHandle&& other)
: m_socket(move(other.m_socket))
{
}
~SocketHandle()
{
if (m_socket)
m_socket->mutex().unlock();
}
SocketHandle(const SocketHandle&) = delete;
SocketHandle& operator=(const SocketHandle&) = delete;
operator bool() const { return m_socket; }
SocketType* operator->() { return &socket(); }
const SocketType* operator->() const { return &socket(); }
SocketType& socket() { return *m_socket; }
const SocketType& socket() const { return *m_socket; }
private:
RefPtr<SocketType> m_socket;
};
// This is a special variant of TRY() that also updates the socket's SO_ERROR field on error. // This is a special variant of TRY() that also updates the socket's SO_ERROR field on error.
#define SOCKET_TRY(expression) \ #define SOCKET_TRY(expression) \
({ \ ({ \

View file

@ -29,16 +29,13 @@ MutexProtected<HashMap<u16, UDPSocket*>>& UDPSocket::sockets_by_port()
return *s_map; return *s_map;
} }
SocketHandle<UDPSocket> UDPSocket::from_port(u16 port) RefPtr<UDPSocket> UDPSocket::from_port(u16 port)
{ {
return sockets_by_port().with_shared([&](const auto& table) -> SocketHandle<UDPSocket> { return sockets_by_port().with_shared([&](const auto& table) -> RefPtr<UDPSocket> {
RefPtr<UDPSocket> socket;
auto it = table.find(port); auto it = table.find(port);
if (it == table.end()) if (it == table.end())
return {}; return {};
socket = (*it).value; return (*it).value;
VERIFY(socket);
return { *socket };
}); });
} }

View file

@ -17,7 +17,7 @@ public:
static ErrorOr<NonnullRefPtr<UDPSocket>> try_create(int protocol, NonnullOwnPtr<DoubleBuffer> receive_buffer); static ErrorOr<NonnullRefPtr<UDPSocket>> try_create(int protocol, NonnullOwnPtr<DoubleBuffer> receive_buffer);
virtual ~UDPSocket() override; virtual ~UDPSocket() override;
static SocketHandle<UDPSocket> from_port(u16); static RefPtr<UDPSocket> from_port(u16);
static void for_each(Function<void(const UDPSocket&)>); static void for_each(Function<void(const UDPSocket&)>);
private: private: