Skip to content

Commit

Permalink
Kernel: Rename LocalSocket factory to try_create() & tighten return type
Browse files Browse the repository at this point in the history
Also tighten the return type to KResultOr<NonnullRefPtr<LocalSocket>>
since it cannot return any other socket type.
  • Loading branch information
awesomekling committed Aug 28, 2021
1 parent 244ede5 commit 2420638
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
6 changes: 3 additions & 3 deletions Kernel/Net/LocalSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
if (!client_buffer)
Expand All @@ -50,11 +50,11 @@ KResultOr<NonnullRefPtr<Socket>> LocalSocket::create(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())
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);
if (description1_result.is_error())
return description1_result.error();
Expand Down
2 changes: 1 addition & 1 deletion Kernel/Net/LocalSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct SocketPair {
class LocalSocket final : public Socket {

public:
static KResultOr<NonnullRefPtr<Socket>> create(int type);
static KResultOr<NonnullRefPtr<LocalSocket>> try_create(int type);
static KResultOr<SocketPair> create_connected_pair(int type);
virtual ~LocalSocket() override;

Expand Down
8 changes: 6 additions & 2 deletions Kernel/Net/Socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ namespace Kernel {
KResultOr<NonnullRefPtr<Socket>> Socket::create(int domain, int type, int protocol)
{
switch (domain) {
case AF_LOCAL:
return LocalSocket::create(type & SOCK_TYPE_MASK);
case AF_LOCAL: {
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:
return IPv4Socket::create(type & SOCK_TYPE_MASK, protocol);
default:
Expand Down

0 comments on commit 2420638

Please sign in to comment.