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