1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-10-29 02:02:35 +00:00
serenity/LibGUI/GSocket.h
Andreas Kling 6d5a54690e LibGUI: Make GSocket connection asynchronous.
Now connect() will return immediately. Later on, when the socket is actually
connected, it will call GSocket::on_connected from the event loop. :^)
2019-04-08 04:56:11 +02:00

73 lines
1.8 KiB
C++

#pragma once
#include <LibGUI/GIODevice.h>
#include <AK/AKString.h>
#include <Kernel/Net/IPv4.h>
class GSocketAddress {
public:
enum class Type { Invalid, IPv4, Local };
GSocketAddress() { }
GSocketAddress(const IPv4Address& address)
: m_type(Type::IPv4)
, m_ipv4_address(address)
{
}
Type type() const { return m_type; }
bool is_valid() const { return m_type != Type::Invalid; }
IPv4Address ipv4_address() const { return m_ipv4_address; }
String to_string() const
{
switch (m_type) {
case Type::IPv4: return m_ipv4_address.to_string();
default: return "[GSocketAddress]";
}
}
private:
Type m_type { Type::Invalid };
IPv4Address m_ipv4_address;
};
class GNotifier;
class GSocket : public GIODevice {
public:
enum class Type { Invalid, TCP, UDP };
virtual ~GSocket() override;
bool connect(const String& hostname, int port);
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; }
Function<void()> on_connected;
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 };
OwnPtr<GNotifier> m_notifier;
};