2019-02-14 14:17:38 +01:00
|
|
|
#pragma once
|
|
|
|
|
2019-02-14 15:17:30 +01:00
|
|
|
#include <AK/Lock.h>
|
2019-02-14 14:17:38 +01:00
|
|
|
#include <AK/Retainable.h>
|
|
|
|
#include <AK/RetainPtr.h>
|
2019-02-14 15:17:30 +01:00
|
|
|
#include <AK/HashTable.h>
|
|
|
|
#include <AK/Vector.h>
|
2019-02-14 14:38:30 +01:00
|
|
|
#include <Kernel/UnixTypes.h>
|
2019-02-14 14:17:38 +01:00
|
|
|
|
2019-02-17 09:40:52 +01:00
|
|
|
enum class SocketRole { None, Listener, Accepted, Connected };
|
2019-02-14 16:01:08 +01:00
|
|
|
|
2019-02-14 14:17:38 +01:00
|
|
|
class Socket : public Retainable<Socket> {
|
|
|
|
public:
|
|
|
|
static RetainPtr<Socket> create(int domain, int type, int protocol, int& error);
|
|
|
|
virtual ~Socket();
|
|
|
|
|
2019-02-14 15:17:30 +01:00
|
|
|
bool is_listening() const { return m_listening; }
|
2019-02-14 14:17:38 +01:00
|
|
|
int domain() const { return m_domain; }
|
|
|
|
int type() const { return m_type; }
|
|
|
|
int protocol() const { return m_protocol; }
|
|
|
|
|
2019-02-14 17:18:35 +01:00
|
|
|
bool can_accept() const { return !m_pending.is_empty(); }
|
2019-02-14 15:17:30 +01:00
|
|
|
RetainPtr<Socket> accept();
|
2019-02-14 17:18:35 +01:00
|
|
|
bool is_connected() const { return m_connected; }
|
2019-02-14 15:17:30 +01:00
|
|
|
bool listen(int backlog, int& error);
|
|
|
|
|
2019-02-14 14:38:30 +01:00
|
|
|
virtual bool bind(const sockaddr*, socklen_t, int& error) = 0;
|
2019-02-14 17:18:35 +01:00
|
|
|
virtual bool connect(const sockaddr*, socklen_t, int& error) = 0;
|
2019-02-14 15:17:30 +01:00
|
|
|
virtual bool get_address(sockaddr*, socklen_t*) = 0;
|
2019-02-14 15:55:19 +01:00
|
|
|
virtual bool is_local() const { return false; }
|
2019-02-17 00:13:47 +01:00
|
|
|
virtual void close(SocketRole) = 0;
|
2019-02-14 16:01:08 +01:00
|
|
|
virtual bool can_read(SocketRole) const = 0;
|
|
|
|
virtual ssize_t read(SocketRole, byte*, size_t) = 0;
|
|
|
|
virtual ssize_t write(SocketRole, const byte*, size_t) = 0;
|
|
|
|
virtual bool can_write(SocketRole) const = 0;
|
|
|
|
|
2019-02-14 17:18:35 +01:00
|
|
|
pid_t origin_pid() const { return m_origin_pid; }
|
|
|
|
|
2019-02-14 14:17:38 +01:00
|
|
|
protected:
|
|
|
|
Socket(int domain, int type, int protocol);
|
|
|
|
|
2019-02-14 17:18:35 +01:00
|
|
|
bool queue_connection_from(Socket&, int& error);
|
|
|
|
|
2019-02-14 14:17:38 +01:00
|
|
|
private:
|
2019-02-14 15:17:30 +01:00
|
|
|
Lock m_lock;
|
2019-02-14 17:18:35 +01:00
|
|
|
pid_t m_origin_pid { 0 };
|
2019-02-14 14:17:38 +01:00
|
|
|
int m_domain { 0 };
|
|
|
|
int m_type { 0 };
|
|
|
|
int m_protocol { 0 };
|
2019-02-14 15:17:30 +01:00
|
|
|
int m_backlog { 0 };
|
|
|
|
bool m_listening { false };
|
2019-02-14 17:18:35 +01:00
|
|
|
bool m_connected { false };
|
2019-02-14 14:17:38 +01:00
|
|
|
|
2019-02-14 15:17:30 +01:00
|
|
|
Vector<RetainPtr<Socket>> m_pending;
|
|
|
|
Vector<RetainPtr<Socket>> m_clients;
|
|
|
|
};
|