summaryrefslogtreecommitdiff
path: root/LibGUI/GSocket.h
blob: a4871f64807cc821b2880e4af8bf9b5c229065af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#pragma once

#include <LibGUI/GIODevice.h>
#include <AK/AKString.h>
#include <Kernel/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 GSocket : public GIODevice {
public:
    enum class Type { Invalid, TCP, UDP };
    virtual ~GSocket() override;

    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; }

    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 };

};