2020-01-26 08:44:24 -05:00
|
|
|
/*
|
2024-10-04 07:19:50 -04:00
|
|
|
* Copyright (c) 2020, Andreas Kling <andreas@ladybird.org>
|
2022-12-17 16:15:54 -05:00
|
|
|
* Copyright (c) 2022, Alexander Narsudinov <a.narsudinov@gmail.com>
|
2020-01-26 08:44:24 -05:00
|
|
|
*
|
2021-04-22 04:24:48 -04:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-26 08:44:24 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-04-04 05:59:33 -04:00
|
|
|
#include <AK/ByteBuffer.h>
|
2020-02-14 16:29:06 -05:00
|
|
|
#include <AK/Forward.h>
|
|
|
|
#include <AK/Function.h>
|
2023-08-06 12:09:39 -04:00
|
|
|
#include <LibCore/EventReceiver.h>
|
2020-02-14 16:29:06 -05:00
|
|
|
#include <LibCore/Forward.h>
|
2020-04-04 05:59:33 -04:00
|
|
|
#include <LibCore/SocketAddress.h>
|
2020-01-26 08:44:24 -05:00
|
|
|
|
2020-02-02 06:34:39 -05:00
|
|
|
namespace Core {
|
2020-01-26 08:44:24 -05:00
|
|
|
|
2023-08-06 12:09:39 -04:00
|
|
|
class UDPServer : public EventReceiver {
|
2020-03-14 18:41:10 -04:00
|
|
|
C_OBJECT(UDPServer)
|
2020-01-26 08:44:24 -05:00
|
|
|
public:
|
2020-03-14 18:41:10 -04:00
|
|
|
virtual ~UDPServer() override;
|
2020-01-26 08:44:24 -05:00
|
|
|
|
2020-04-04 05:59:33 -04:00
|
|
|
bool is_bound() const { return m_bound; }
|
2020-01-26 08:44:24 -05:00
|
|
|
|
2022-04-01 13:58:27 -04:00
|
|
|
bool bind(IPv4Address const& address, u16 port);
|
2022-12-17 16:15:54 -05:00
|
|
|
ErrorOr<ByteBuffer> receive(size_t size, sockaddr_in& from);
|
|
|
|
ErrorOr<ByteBuffer> receive(size_t size)
|
2020-04-04 05:59:33 -04:00
|
|
|
{
|
|
|
|
struct sockaddr_in saddr;
|
|
|
|
return receive(size, saddr);
|
2023-07-07 22:48:11 -04:00
|
|
|
}
|
2020-01-26 08:44:24 -05:00
|
|
|
|
2021-09-11 16:11:30 -04:00
|
|
|
ErrorOr<size_t> send(ReadonlyBytes, sockaddr_in const& to);
|
|
|
|
|
2020-01-26 08:44:24 -05:00
|
|
|
Optional<IPv4Address> local_address() const;
|
|
|
|
Optional<u16> local_port() const;
|
|
|
|
|
2021-02-14 09:10:00 -05:00
|
|
|
int fd() const { return m_fd; }
|
|
|
|
|
2020-04-04 05:59:33 -04:00
|
|
|
Function<void()> on_ready_to_receive;
|
2020-01-26 08:44:24 -05:00
|
|
|
|
2021-02-14 09:10:00 -05:00
|
|
|
protected:
|
2023-08-06 12:09:39 -04:00
|
|
|
explicit UDPServer(EventReceiver* parent = nullptr);
|
2020-01-26 08:44:24 -05:00
|
|
|
|
2021-02-14 09:10:00 -05:00
|
|
|
private:
|
2020-01-26 08:44:24 -05:00
|
|
|
int m_fd { -1 };
|
2020-04-04 05:59:33 -04:00
|
|
|
bool m_bound { false };
|
2020-02-02 06:34:39 -05:00
|
|
|
RefPtr<Notifier> m_notifier;
|
2020-01-26 08:44:24 -05:00
|
|
|
};
|
2020-02-02 06:34:39 -05:00
|
|
|
|
|
|
|
}
|