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-07-25 17:49:28 +00:00
|
|
|
C_OBJECT(CSocket)
|
2019-03-18 13:09:58 +00:00
|
|
|
public:
|
2019-06-07 15:13:23 +00:00
|
|
|
enum class Type {
|
2019-05-28 09:53:16 +00:00
|
|
|
Invalid,
|
|
|
|
TCP,
|
2019-07-13 17:42:03 +00:00
|
|
|
UDP,
|
|
|
|
Local,
|
2019-05-28 09:53:16 +00:00
|
|
|
};
|
2019-04-10 18:22:23 +00:00
|
|
|
virtual ~CSocket() override;
|
2019-03-18 13:09:58 +00:00
|
|
|
|
2019-07-25 09:48:21 +00:00
|
|
|
Type type() const { return m_type; }
|
|
|
|
|
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-07-13 17:42:03 +00:00
|
|
|
bool connect(const CSocketAddress&);
|
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-07-16 16:00:08 +00:00
|
|
|
void set_blocking(bool blocking);
|
2019-03-18 13:09:58 +00:00
|
|
|
|
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-07-27 08:48:43 +00:00
|
|
|
Function<void()> on_ready_to_read;
|
2019-04-08 02:53:45 +00:00
|
|
|
|
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 };
|
|
|
|
|
2019-07-27 08:48:43 +00:00
|
|
|
virtual void did_update_fd(int) override;
|
|
|
|
|
2019-03-18 13:09:58 +00:00
|
|
|
private:
|
2019-04-10 18:22:23 +00:00
|
|
|
virtual bool open(CIODevice::OpenMode) override { ASSERT_NOT_REACHED(); }
|
2019-09-11 17:44:15 +00:00
|
|
|
bool common_connect(const struct sockaddr*, socklen_t);
|
2019-09-22 19:46:46 +00:00
|
|
|
void ensure_read_notifier();
|
2019-09-11 17:44:15 +00:00
|
|
|
|
2019-03-18 13:09:58 +00:00
|
|
|
Type m_type { Type::Invalid };
|
2019-09-21 22:31:54 +00:00
|
|
|
RefPtr<CNotifier> m_notifier;
|
|
|
|
RefPtr<CNotifier> m_read_notifier;
|
2019-03-18 13:09:58 +00:00
|
|
|
};
|