2019-03-18 10:09:58 -03:00
|
|
|
#pragma once
|
|
|
|
|
|
2019-04-10 15:22:23 -03:00
|
|
|
#include <LibCore/CIODevice.h>
|
|
|
|
|
#include <LibCore/CSocketAddress.h>
|
2019-03-18 10:09:58 -03:00
|
|
|
|
2019-04-10 12:35:43 -03:00
|
|
|
class CNotifier;
|
2019-04-07 23:53:45 -03:00
|
|
|
|
2019-04-10 15:22:23 -03:00
|
|
|
class CSocket : public CIODevice {
|
2019-07-25 14:49:28 -03:00
|
|
|
C_OBJECT(CSocket)
|
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
|
|
|
};
|
2019-04-10 15:22:23 -03:00
|
|
|
virtual ~CSocket() override;
|
2019-03-18 10:09:58 -03:00
|
|
|
|
2019-07-25 06:48:21 -03:00
|
|
|
Type type() const { return m_type; }
|
|
|
|
|
|
2019-04-02 15:40:10 -03:00
|
|
|
bool connect(const String& hostname, int port);
|
2019-04-10 15:22:23 -03:00
|
|
|
bool connect(const CSocketAddress&, int port);
|
2019-07-13 14:42:03 -03:00
|
|
|
bool connect(const CSocketAddress&);
|
2019-03-18 10:09:58 -03:00
|
|
|
|
|
|
|
|
ByteBuffer receive(int max_size);
|
|
|
|
|
bool send(const ByteBuffer&);
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2019-04-10 15:22:23 -03:00
|
|
|
CSocketAddress source_address() const { return m_source_address; }
|
2019-03-18 10:09:58 -03:00
|
|
|
int source_port() const { return m_source_port; }
|
|
|
|
|
|
2019-04-10 15:22:23 -03:00
|
|
|
CSocketAddress destination_address() const { return m_source_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;
|
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:
|
2019-04-10 15:22:23 -03:00
|
|
|
CSocket(Type, CObject* parent);
|
2019-03-18 10:09:58 -03:00
|
|
|
|
2019-04-10 15:22:23 -03:00
|
|
|
CSocketAddress m_source_address;
|
|
|
|
|
CSocketAddress 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;
|
|
|
|
|
|
2019-03-18 10:09:58 -03:00
|
|
|
private:
|
2019-04-10 15:22:23 -03:00
|
|
|
virtual bool open(CIODevice::OpenMode) override { ASSERT_NOT_REACHED(); }
|
2019-09-11 14:44:15 -03:00
|
|
|
bool common_connect(const struct sockaddr*, socklen_t);
|
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 };
|
2019-09-21 19:31:54 -03:00
|
|
|
RefPtr<CNotifier> m_notifier;
|
|
|
|
|
RefPtr<CNotifier> m_read_notifier;
|
2019-03-18 10:09:58 -03:00
|
|
|
};
|