2019-03-18 10:09:58 -03:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <LibGUI/GIODevice.h>
|
2019-04-08 12:19:35 -03:00
|
|
|
#include <LibGUI/GSocketAddress.h>
|
2019-03-18 10:09:58 -03:00
|
|
|
|
2019-04-07 23:53:45 -03:00
|
|
|
class GNotifier;
|
|
|
|
|
|
2019-03-18 10:09:58 -03:00
|
|
|
class GSocket : public GIODevice {
|
|
|
|
|
public:
|
|
|
|
|
enum class Type { Invalid, TCP, UDP };
|
|
|
|
|
virtual ~GSocket() override;
|
|
|
|
|
|
2019-04-02 15:40:10 -03:00
|
|
|
bool connect(const String& hostname, int port);
|
2019-03-18 10:09:58 -03:00
|
|
|
bool connect(const GSocketAddress&, int port);
|
|
|
|
|
|
|
|
|
|
ByteBuffer receive(int max_size);
|
|
|
|
|
bool send(const ByteBuffer&);
|
|
|
|
|
|
|
|
|
|
bool is_connected() const { return m_connected; }
|
|
|
|
|
|
|
|
|
|
GSocketAddress source_address() const { return m_source_address; }
|
|
|
|
|
int source_port() const { return m_source_port; }
|
|
|
|
|
|
|
|
|
|
GSocketAddress destination_address() const { return m_source_address; }
|
|
|
|
|
int destination_port() const { return m_destination_port; }
|
|
|
|
|
|
2019-04-07 23:53:45 -03:00
|
|
|
Function<void()> on_connected;
|
|
|
|
|
|
2019-03-18 10:09:58 -03:00
|
|
|
virtual const char* class_name() const override { return "GSocket"; }
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
GSocket(Type, GObject* parent);
|
|
|
|
|
|
|
|
|
|
GSocketAddress m_source_address;
|
|
|
|
|
GSocketAddress m_destination_address;
|
|
|
|
|
int m_source_port { -1 };
|
|
|
|
|
int m_destination_port { -1 };
|
|
|
|
|
bool m_connected { false };
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
virtual bool open(GIODevice::OpenMode) override { ASSERT_NOT_REACHED(); }
|
|
|
|
|
Type m_type { Type::Invalid };
|
2019-04-07 23:53:45 -03:00
|
|
|
OwnPtr<GNotifier> m_notifier;
|
2019-03-18 10:09:58 -03:00
|
|
|
};
|