1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 21:07:34 +00:00

Kernel: Rename LocalSocket factory to try_create() & tighten return type

Also tighten the return type to KResultOr<NonnullRefPtr<LocalSocket>>
since it cannot return any other socket type.
This commit is contained in:
Andreas Kling 2021-08-29 01:22:34 +02:00
parent 244ede561b
commit 242063866f
3 changed files with 10 additions and 6 deletions

View file

@ -34,7 +34,7 @@ void LocalSocket::for_each(Function<void(const LocalSocket&)> callback)
}); });
} }
KResultOr<NonnullRefPtr<Socket>> LocalSocket::create(int type) KResultOr<NonnullRefPtr<LocalSocket>> LocalSocket::try_create(int type)
{ {
auto client_buffer = DoubleBuffer::try_create(); auto client_buffer = DoubleBuffer::try_create();
if (!client_buffer) if (!client_buffer)
@ -50,11 +50,11 @@ KResultOr<NonnullRefPtr<Socket>> LocalSocket::create(int type)
KResultOr<SocketPair> LocalSocket::create_connected_pair(int type) KResultOr<SocketPair> LocalSocket::create_connected_pair(int type)
{ {
auto socket_or_error = LocalSocket::create(type); auto socket_or_error = LocalSocket::try_create(type);
if (socket_or_error.is_error()) if (socket_or_error.is_error())
return socket_or_error.error(); return socket_or_error.error();
auto socket = static_ptr_cast<LocalSocket>(socket_or_error.release_value()); auto socket = socket_or_error.release_value();
auto description1_result = FileDescription::try_create(*socket); auto description1_result = FileDescription::try_create(*socket);
if (description1_result.is_error()) if (description1_result.is_error())
return description1_result.error(); return description1_result.error();

View file

@ -22,7 +22,7 @@ struct SocketPair {
class LocalSocket final : public Socket { class LocalSocket final : public Socket {
public: public:
static KResultOr<NonnullRefPtr<Socket>> create(int type); static KResultOr<NonnullRefPtr<LocalSocket>> try_create(int type);
static KResultOr<SocketPair> create_connected_pair(int type); static KResultOr<SocketPair> create_connected_pair(int type);
virtual ~LocalSocket() override; virtual ~LocalSocket() override;

View file

@ -20,8 +20,12 @@ namespace Kernel {
KResultOr<NonnullRefPtr<Socket>> Socket::create(int domain, int type, int protocol) KResultOr<NonnullRefPtr<Socket>> Socket::create(int domain, int type, int protocol)
{ {
switch (domain) { switch (domain) {
case AF_LOCAL: case AF_LOCAL: {
return LocalSocket::create(type & SOCK_TYPE_MASK); auto socket_or_error = LocalSocket::try_create(type & SOCK_TYPE_MASK);
if (socket_or_error.is_error())
return socket_or_error.error();
return socket_or_error.release_value();
}
case AF_INET: case AF_INET:
return IPv4Socket::create(type & SOCK_TYPE_MASK, protocol); return IPv4Socket::create(type & SOCK_TYPE_MASK, protocol);
default: default: