2020-01-18 05:38:21 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
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>
|
2020-02-06 11:04:03 -03:00
|
|
|
#include <LibCore/Notifier.h>
|
|
|
|
|
#include <LibCore/Object.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
|
|
|
|
2020-02-02 08:34:39 -03:00
|
|
|
class TCPServer : public Object {
|
|
|
|
|
C_OBJECT(TCPServer)
|
2019-08-05 07:47:30 -03:00
|
|
|
public:
|
2020-02-02 08:34:39 -03:00
|
|
|
virtual ~TCPServer() override;
|
2019-08-05 07:47:30 -03:00
|
|
|
|
|
|
|
|
bool is_listening() const { return m_listening; }
|
|
|
|
|
bool listen(const IPv4Address& address, u16 port);
|
2021-05-25 16:40:56 -03:00
|
|
|
void set_blocking(bool blocking);
|
2019-08-05 07:47:30 -03:00
|
|
|
|
2020-02-02 08:34:39 -03:00
|
|
|
RefPtr<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:
|
2020-02-02 08:34:39 -03:00
|
|
|
explicit TCPServer(Object* parent = nullptr);
|
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
|
|
|
|
|
|
|
|
}
|