summaryrefslogtreecommitdiff
path: root/Kernel
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2021-09-04 23:02:12 +0200
committerAndreas Kling <kling@serenityos.org>2021-09-04 23:11:04 +0200
commit648c768d8193763168d32837c1cd9f52c29b67dd (patch)
treea3d12e3c8edcaab15f17669508403c81bacd7ff9 /Kernel
parentac85fdeb1c4f64dd4ab845a84b20c3e845dedbac (diff)
downloadserenity-648c768d8193763168d32837c1cd9f52c29b67dd.zip
Kernel: Tidy up TCPSocket creation a bit
- Rename create() => try_create() - Use adopt_nonnull_ref_or_enomem()
Diffstat (limited to 'Kernel')
-rw-r--r--Kernel/Net/IPv4Socket.cpp8
-rw-r--r--Kernel/Net/TCPSocket.cpp13
-rw-r--r--Kernel/Net/TCPSocket.h2
3 files changed, 10 insertions, 13 deletions
diff --git a/Kernel/Net/IPv4Socket.cpp b/Kernel/Net/IPv4Socket.cpp
index 5943df7331..122ddfa4fc 100644
--- a/Kernel/Net/IPv4Socket.cpp
+++ b/Kernel/Net/IPv4Socket.cpp
@@ -47,10 +47,10 @@ KResultOr<NonnullRefPtr<Socket>> IPv4Socket::create(int type, int protocol)
return ENOMEM;
if (type == SOCK_STREAM) {
- auto tcp_socket = TCPSocket::create(protocol, receive_buffer.release_nonnull());
- if (tcp_socket.is_error())
- return tcp_socket.error();
- return tcp_socket.release_value();
+ auto tcp_socket_or_error = TCPSocket::try_create(protocol, receive_buffer.release_nonnull());
+ if (tcp_socket_or_error.is_error())
+ return tcp_socket_or_error.error();
+ return tcp_socket_or_error.release_value();
}
if (type == SOCK_DGRAM) {
auto udp_socket = UDPSocket::create(protocol, receive_buffer.release_nonnull());
diff --git a/Kernel/Net/TCPSocket.cpp b/Kernel/Net/TCPSocket.cpp
index 8a122c4658..dc50642fd1 100644
--- a/Kernel/Net/TCPSocket.cpp
+++ b/Kernel/Net/TCPSocket.cpp
@@ -100,11 +100,11 @@ RefPtr<TCPSocket> TCPSocket::create_client(const IPv4Address& new_local_address,
auto receive_buffer = create_receive_buffer();
if (!receive_buffer)
return {};
- auto result = TCPSocket::create(protocol(), receive_buffer.release_nonnull());
- if (result.is_error())
+ auto client_or_error = TCPSocket::try_create(protocol(), receive_buffer.release_nonnull());
+ if (client_or_error.is_error())
return {};
- auto client = result.release_value();
+ auto client = client_or_error.release_value();
client->set_setup_state(SetupState::InProgress);
client->set_local_address(new_local_address);
client->set_local_port(new_local_port);
@@ -152,17 +152,14 @@ TCPSocket::~TCPSocket()
dbgln_if(TCP_SOCKET_DEBUG, "~TCPSocket in state {}", to_string(state()));
}
-KResultOr<NonnullRefPtr<TCPSocket>> TCPSocket::create(int protocol, NonnullOwnPtr<DoubleBuffer> receive_buffer)
+KResultOr<NonnullRefPtr<TCPSocket>> TCPSocket::try_create(int protocol, NonnullOwnPtr<DoubleBuffer> receive_buffer)
{
// Note: Scratch buffer is only used for SOCK_STREAM sockets.
auto scratch_buffer = KBuffer::try_create_with_size(65536);
if (!scratch_buffer)
return ENOMEM;
- auto socket = adopt_ref_if_nonnull(new (nothrow) TCPSocket(protocol, move(receive_buffer), move(scratch_buffer)));
- if (socket)
- return socket.release_nonnull();
- return ENOMEM;
+ return adopt_nonnull_ref_or_enomem(new (nothrow) TCPSocket(protocol, move(receive_buffer), move(scratch_buffer)));
}
KResultOr<size_t> TCPSocket::protocol_receive(ReadonlyBytes raw_ipv4_packet, UserOrKernelBuffer& buffer, size_t buffer_size, [[maybe_unused]] int flags)
diff --git a/Kernel/Net/TCPSocket.h b/Kernel/Net/TCPSocket.h
index 66b66834ce..9a5427b65f 100644
--- a/Kernel/Net/TCPSocket.h
+++ b/Kernel/Net/TCPSocket.h
@@ -19,7 +19,7 @@ namespace Kernel {
class TCPSocket final : public IPv4Socket {
public:
static void for_each(Function<void(const TCPSocket&)>);
- static KResultOr<NonnullRefPtr<TCPSocket>> create(int protocol, NonnullOwnPtr<DoubleBuffer> receive_buffer);
+ static KResultOr<NonnullRefPtr<TCPSocket>> try_create(int protocol, NonnullOwnPtr<DoubleBuffer> receive_buffer);
virtual ~TCPSocket() override;
enum class Direction {