2020-01-26 14:44:24 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
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>
|
|
|
|
#include <LibCore/Forward.h>
|
2020-02-06 15:04:03 +01:00
|
|
|
#include <LibCore/Object.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
|
|
|
|
2020-03-15 11:41:10 +13:00
|
|
|
class UDPServer : public Object {
|
|
|
|
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
|
|
|
|
2020-04-04 14:29:33 +04:30
|
|
|
bool bind(const IPv4Address& address, u16 port);
|
|
|
|
ByteBuffer receive(size_t size, sockaddr_in& from);
|
|
|
|
ByteBuffer receive(size_t size)
|
|
|
|
{
|
|
|
|
struct sockaddr_in saddr;
|
|
|
|
return receive(size, saddr);
|
|
|
|
};
|
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:
|
2020-03-15 11:41:10 +13:00
|
|
|
explicit UDPServer(Object* parent = nullptr);
|
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
|
|
|
|
|
|
|
}
|