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