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