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