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
|
#pragma once
#include <AK/Lock.h>
#include <AK/Retainable.h>
#include <AK/RetainPtr.h>
#include <AK/HashTable.h>
#include <AK/Vector.h>
#include <Kernel/UnixTypes.h>
#include <Kernel/KResult.h>
enum class SocketRole { None, Listener, Accepted, Connected };
class Socket : public Retainable<Socket> {
public:
static KResultOr<Retained<Socket>> create(int domain, int type, int protocol);
virtual ~Socket();
int domain() const { return m_domain; }
int type() const { return m_type; }
int protocol() const { return m_protocol; }
bool can_accept() const { return !m_pending.is_empty(); }
RetainPtr<Socket> accept();
bool is_connected() const { return m_connected; }
KResult listen(int backlog);
virtual KResult bind(const sockaddr*, socklen_t) = 0;
virtual KResult connect(const sockaddr*, socklen_t) = 0;
virtual bool get_address(sockaddr*, socklen_t*) = 0;
virtual bool is_local() const { return false; }
virtual void attach_fd(SocketRole) = 0;
virtual void detach_fd(SocketRole) = 0;
virtual bool can_read(SocketRole) const = 0;
virtual ssize_t read(SocketRole, byte*, ssize_t) = 0;
virtual ssize_t write(SocketRole, const byte*, ssize_t) = 0;
virtual bool can_write(SocketRole) const = 0;
pid_t origin_pid() const { return m_origin_pid; }
protected:
Socket(int domain, int type, int protocol);
KResult queue_connection_from(Socket&);
private:
Lock m_lock;
pid_t m_origin_pid { 0 };
int m_domain { 0 };
int m_type { 0 };
int m_protocol { 0 };
int m_backlog { 0 };
bool m_connected { false };
Vector<RetainPtr<Socket>> m_pending;
Vector<RetainPtr<Socket>> m_clients;
};
|