2019-03-18 14:09:58 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <LibGUI/GIODevice.h>
|
2019-04-08 17:19:35 +02:00
|
|
|
#include <LibGUI/GSocketAddress.h>
|
2019-03-18 14:09:58 +01:00
|
|
|
|
2019-04-10 17:35:43 +02:00
|
|
|
class CNotifier;
|
2019-04-08 04:53:45 +02:00
|
|
|
|
2019-03-18 14:09:58 +01:00
|
|
|
class GSocket : public GIODevice {
|
|
|
|
|
public:
|
|
|
|
|
enum class Type { Invalid, TCP, UDP };
|
|
|
|
|
virtual ~GSocket() override;
|
|
|
|
|
|
2019-04-02 20:40:10 +02:00
|
|
|
bool connect(const String& hostname, int port);
|
2019-03-18 14:09:58 +01: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-08 04:53:45 +02:00
|
|
|
Function<void()> on_connected;
|
|
|
|
|
|
2019-03-18 14:09:58 +01:00
|
|
|
virtual const char* class_name() const override { return "GSocket"; }
|
|
|
|
|
|
|
|
|
|
protected:
|
2019-04-10 17:01:54 +02:00
|
|
|
GSocket(Type, CObject* parent);
|
2019-03-18 14:09:58 +01:00
|
|
|
|
|
|
|
|
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-10 17:35:43 +02:00
|
|
|
OwnPtr<CNotifier> m_notifier;
|
2019-03-18 14:09:58 +01:00
|
|
|
};
|