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

Kernel: Add SocketHandle helper class that wraps locked sockets.

This allows us to have a comfy IPv4Socket::from_tcp_port() API that returns
a socket that's locked and safe to access. No need to worry about locking
at the client site.
This commit is contained in:
Andreas Kling 2019-03-14 09:19:24 +01:00
parent 3d5296a901
commit 54e7df0586
4 changed files with 106 additions and 22 deletions

View file

@ -76,3 +76,40 @@ private:
Vector<RetainPtr<Socket>> m_pending;
Vector<RetainPtr<Socket>> m_clients;
};
class SocketHandle {
public:
SocketHandle() { }
SocketHandle(RetainPtr<Socket>&& socket)
: m_socket(move(socket))
{
if (m_socket)
m_socket->lock().lock();
}
SocketHandle(SocketHandle&& other)
: m_socket(move(other.m_socket))
{
}
~SocketHandle()
{
if (m_socket)
m_socket->lock().unlock();
}
SocketHandle(const SocketHandle&) = delete;
SocketHandle& operator=(const SocketHandle&) = delete;
operator bool() const { return m_socket; }
Socket* operator->() { return &socket(); }
const Socket* operator->() const { return &socket(); }
Socket& socket() { return *m_socket; }
const Socket& socket() const { return *m_socket; }
private:
RetainPtr<Socket> m_socket;
};