mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 18:17:44 +00:00
Kernel: Tidy up TCPSocket creation a bit
- Rename create() => try_create() - Use adopt_nonnull_ref_or_enomem()
This commit is contained in:
parent
ac85fdeb1c
commit
648c768d81
3 changed files with 10 additions and 13 deletions
|
@ -47,10 +47,10 @@ KResultOr<NonnullRefPtr<Socket>> IPv4Socket::create(int type, int protocol)
|
||||||
return ENOMEM;
|
return ENOMEM;
|
||||||
|
|
||||||
if (type == SOCK_STREAM) {
|
if (type == SOCK_STREAM) {
|
||||||
auto tcp_socket = TCPSocket::create(protocol, receive_buffer.release_nonnull());
|
auto tcp_socket_or_error = TCPSocket::try_create(protocol, receive_buffer.release_nonnull());
|
||||||
if (tcp_socket.is_error())
|
if (tcp_socket_or_error.is_error())
|
||||||
return tcp_socket.error();
|
return tcp_socket_or_error.error();
|
||||||
return tcp_socket.release_value();
|
return tcp_socket_or_error.release_value();
|
||||||
}
|
}
|
||||||
if (type == SOCK_DGRAM) {
|
if (type == SOCK_DGRAM) {
|
||||||
auto udp_socket = UDPSocket::create(protocol, receive_buffer.release_nonnull());
|
auto udp_socket = UDPSocket::create(protocol, receive_buffer.release_nonnull());
|
||||||
|
|
|
@ -100,11 +100,11 @@ RefPtr<TCPSocket> TCPSocket::create_client(const IPv4Address& new_local_address,
|
||||||
auto receive_buffer = create_receive_buffer();
|
auto receive_buffer = create_receive_buffer();
|
||||||
if (!receive_buffer)
|
if (!receive_buffer)
|
||||||
return {};
|
return {};
|
||||||
auto result = TCPSocket::create(protocol(), receive_buffer.release_nonnull());
|
auto client_or_error = TCPSocket::try_create(protocol(), receive_buffer.release_nonnull());
|
||||||
if (result.is_error())
|
if (client_or_error.is_error())
|
||||||
return {};
|
return {};
|
||||||
|
|
||||||
auto client = result.release_value();
|
auto client = client_or_error.release_value();
|
||||||
client->set_setup_state(SetupState::InProgress);
|
client->set_setup_state(SetupState::InProgress);
|
||||||
client->set_local_address(new_local_address);
|
client->set_local_address(new_local_address);
|
||||||
client->set_local_port(new_local_port);
|
client->set_local_port(new_local_port);
|
||||||
|
@ -152,17 +152,14 @@ TCPSocket::~TCPSocket()
|
||||||
dbgln_if(TCP_SOCKET_DEBUG, "~TCPSocket in state {}", to_string(state()));
|
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.
|
// Note: Scratch buffer is only used for SOCK_STREAM sockets.
|
||||||
auto scratch_buffer = KBuffer::try_create_with_size(65536);
|
auto scratch_buffer = KBuffer::try_create_with_size(65536);
|
||||||
if (!scratch_buffer)
|
if (!scratch_buffer)
|
||||||
return ENOMEM;
|
return ENOMEM;
|
||||||
|
|
||||||
auto socket = adopt_ref_if_nonnull(new (nothrow) TCPSocket(protocol, move(receive_buffer), move(scratch_buffer)));
|
return adopt_nonnull_ref_or_enomem(new (nothrow) TCPSocket(protocol, move(receive_buffer), move(scratch_buffer)));
|
||||||
if (socket)
|
|
||||||
return socket.release_nonnull();
|
|
||||||
return ENOMEM;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
KResultOr<size_t> TCPSocket::protocol_receive(ReadonlyBytes raw_ipv4_packet, UserOrKernelBuffer& buffer, size_t buffer_size, [[maybe_unused]] int flags)
|
KResultOr<size_t> TCPSocket::protocol_receive(ReadonlyBytes raw_ipv4_packet, UserOrKernelBuffer& buffer, size_t buffer_size, [[maybe_unused]] int flags)
|
||||||
|
|
|
@ -19,7 +19,7 @@ namespace Kernel {
|
||||||
class TCPSocket final : public IPv4Socket {
|
class TCPSocket final : public IPv4Socket {
|
||||||
public:
|
public:
|
||||||
static void for_each(Function<void(const TCPSocket&)>);
|
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;
|
virtual ~TCPSocket() override;
|
||||||
|
|
||||||
enum class Direction {
|
enum class Direction {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue