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-03-18 10:09:58 -03:00
|
|
|
#pragma once
|
|
|
|
|
|
2020-02-14 18:29:06 -03:00
|
|
|
#include <AK/Function.h>
|
2020-07-27 10:00:12 -03:00
|
|
|
#include <AK/Span.h>
|
2020-02-06 11:04:03 -03:00
|
|
|
#include <LibCore/IODevice.h>
|
|
|
|
|
#include <LibCore/SocketAddress.h>
|
2019-03-18 10:09:58 -03:00
|
|
|
|
2020-02-02 08:34:39 -03:00
|
|
|
namespace Core {
|
2019-04-07 23:53:45 -03:00
|
|
|
|
2020-02-02 08:34:39 -03:00
|
|
|
class Socket : public IODevice {
|
|
|
|
|
C_OBJECT(Socket)
|
2019-03-18 10:09:58 -03:00
|
|
|
public:
|
2019-06-07 12:13:23 -03:00
|
|
|
enum class Type {
|
2019-05-28 06:53:16 -03:00
|
|
|
Invalid,
|
|
|
|
|
TCP,
|
2019-07-13 14:42:03 -03:00
|
|
|
UDP,
|
|
|
|
|
Local,
|
2019-05-28 06:53:16 -03:00
|
|
|
};
|
2020-02-02 08:34:39 -03:00
|
|
|
virtual ~Socket() override;
|
2019-03-18 10:09:58 -03:00
|
|
|
|
2019-07-25 06:48:21 -03:00
|
|
|
Type type() const { return m_type; }
|
|
|
|
|
|
2020-04-19 05:27:13 -03:00
|
|
|
virtual bool connect(const String& hostname, int port);
|
2020-02-02 08:34:39 -03:00
|
|
|
bool connect(const SocketAddress&, int port);
|
|
|
|
|
bool connect(const SocketAddress&);
|
2019-03-18 10:09:58 -03:00
|
|
|
|
|
|
|
|
ByteBuffer receive(int max_size);
|
2020-07-27 10:00:12 -03:00
|
|
|
bool send(ReadonlyBytes);
|
2019-03-18 10:09:58 -03:00
|
|
|
|
|
|
|
|
bool is_connected() const { return m_connected; }
|
2019-07-16 13:00:08 -03:00
|
|
|
void set_blocking(bool blocking);
|
2019-03-18 10:09:58 -03:00
|
|
|
|
2020-02-02 08:34:39 -03:00
|
|
|
SocketAddress source_address() const { return m_source_address; }
|
2019-03-18 10:09:58 -03:00
|
|
|
int source_port() const { return m_source_port; }
|
|
|
|
|
|
2020-02-22 12:38:10 -03:00
|
|
|
SocketAddress destination_address() const { return m_destination_address; }
|
2019-03-18 10:09:58 -03:00
|
|
|
int destination_port() const { return m_destination_port; }
|
|
|
|
|
|
2019-04-07 23:53:45 -03:00
|
|
|
Function<void()> on_connected;
|
2021-08-12 12:28:19 -03:00
|
|
|
Function<void()> on_error;
|
2019-07-27 05:48:43 -03:00
|
|
|
Function<void()> on_ready_to_read;
|
2019-04-07 23:53:45 -03:00
|
|
|
|
2019-03-18 10:09:58 -03:00
|
|
|
protected:
|
2020-02-02 08:34:39 -03:00
|
|
|
Socket(Type, Object* parent);
|
2019-03-18 10:09:58 -03:00
|
|
|
|
2020-02-02 08:34:39 -03:00
|
|
|
SocketAddress m_source_address;
|
|
|
|
|
SocketAddress m_destination_address;
|
2019-03-18 10:09:58 -03:00
|
|
|
int m_source_port { -1 };
|
|
|
|
|
int m_destination_port { -1 };
|
|
|
|
|
bool m_connected { false };
|
|
|
|
|
|
2019-07-27 05:48:43 -03:00
|
|
|
virtual void did_update_fd(int) override;
|
2020-04-19 05:27:13 -03:00
|
|
|
virtual bool common_connect(const struct sockaddr*, socklen_t);
|
2019-07-27 05:48:43 -03:00
|
|
|
|
2019-03-18 10:09:58 -03:00
|
|
|
private:
|
2021-05-12 06:26:43 -03:00
|
|
|
virtual bool open(OpenMode) override { VERIFY_NOT_REACHED(); }
|
2019-09-22 16:46:46 -03:00
|
|
|
void ensure_read_notifier();
|
2019-09-11 14:44:15 -03:00
|
|
|
|
2019-03-18 10:09:58 -03:00
|
|
|
Type m_type { Type::Invalid };
|
2020-02-02 08:34:39 -03:00
|
|
|
RefPtr<Notifier> m_notifier;
|
|
|
|
|
RefPtr<Notifier> m_read_notifier;
|
2019-03-18 10:09:58 -03:00
|
|
|
};
|
2020-02-02 08:34:39 -03:00
|
|
|
|
|
|
|
|
}
|
2021-01-15 17:46:23 -03:00
|
|
|
|
|
|
|
|
template<>
|
|
|
|
|
struct AK::Formatter<Core::Socket> : Formatter<Core::Object> {
|
|
|
|
|
};
|