2020-01-18 05:38:21 -03:00
|
|
|
/*
|
2024-10-04 08:19:50 -03:00
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
|
2021-12-25 11:59:40 -03:00
|
|
|
* Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
|
2020-01-18 05:38:21 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2019-08-05 07:47:30 -03:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <AK/IPv4Address.h>
|
2023-08-06 13:09:39 -03:00
|
|
|
#include <LibCore/EventReceiver.h>
|
2020-02-06 11:04:03 -03:00
|
|
|
#include <LibCore/Notifier.h>
|
2019-08-05 07:47:30 -03:00
|
|
|
|
2020-02-02 08:34:39 -03:00
|
|
|
namespace Core {
|
2019-08-05 07:47:30 -03:00
|
|
|
|
2023-08-06 13:09:39 -03:00
|
|
|
class TCPServer : public EventReceiver {
|
2021-12-25 11:59:40 -03:00
|
|
|
C_OBJECT_ABSTRACT(TCPServer)
|
2019-08-05 07:47:30 -03:00
|
|
|
public:
|
2025-08-10 11:26:49 -03:00
|
|
|
static ErrorOr<NonnullRefPtr<TCPServer>> try_create();
|
2020-02-02 08:34:39 -03:00
|
|
|
virtual ~TCPServer() override;
|
2019-08-05 07:47:30 -03:00
|
|
|
|
2022-11-14 10:28:43 -03:00
|
|
|
enum class AllowAddressReuse {
|
|
|
|
|
Yes,
|
|
|
|
|
No,
|
|
|
|
|
};
|
|
|
|
|
|
2019-08-05 07:47:30 -03:00
|
|
|
bool is_listening() const { return m_listening; }
|
2022-11-14 10:28:43 -03:00
|
|
|
ErrorOr<void> listen(IPv4Address const& address, u16 port, AllowAddressReuse = AllowAddressReuse::No);
|
2021-12-25 11:59:40 -03:00
|
|
|
ErrorOr<void> set_blocking(bool blocking);
|
2019-08-05 07:47:30 -03:00
|
|
|
|
2023-02-08 19:05:44 -03:00
|
|
|
ErrorOr<NonnullOwnPtr<TCPSocket>> accept();
|
2019-08-05 07:47:30 -03:00
|
|
|
|
2019-11-04 09:07:41 -03:00
|
|
|
Optional<IPv4Address> local_address() const;
|
|
|
|
|
Optional<u16> local_port() const;
|
2019-10-07 05:07:24 -03:00
|
|
|
|
2019-08-05 07:47:30 -03:00
|
|
|
Function<void()> on_ready_to_accept;
|
|
|
|
|
|
|
|
|
|
private:
|
2025-08-10 11:26:49 -03:00
|
|
|
explicit TCPServer(int fd);
|
2019-09-21 10:18:12 -03:00
|
|
|
|
2019-08-05 07:47:30 -03:00
|
|
|
int m_fd { -1 };
|
|
|
|
|
bool m_listening { false };
|
2020-02-02 08:34:39 -03:00
|
|
|
RefPtr<Notifier> m_notifier;
|
2019-08-05 07:47:30 -03:00
|
|
|
};
|
2020-02-02 08:34:39 -03:00
|
|
|
|
|
|
|
|
}
|