ladybird/Kernel/Socket.h
Andreas Kling bf58241c11 Port the WindowServer and LibGUI to communicate through local sockets.
This is really cool! :^)

Apps currently refuse to start if the WindowServer isn't listening on the
socket in /wsportal. This makes sense, but I guess it would also be nice
to have some sort of "wait for server on startup" mode.

This has performance issues, and I'll work on those, but this stuff seems
to actually work and I'm very happy with that.
2019-02-14 17:18:35 +01:00

56 lines
1.7 KiB
C++

#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>
enum class SocketRole { None, Accepted, Connected };
class Socket : public Retainable<Socket> {
public:
static RetainPtr<Socket> create(int domain, int type, int protocol, int& error);
virtual ~Socket();
bool is_listening() const { return m_listening; }
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; }
bool listen(int backlog, int& error);
virtual bool bind(const sockaddr*, socklen_t, int& error) = 0;
virtual bool connect(const sockaddr*, socklen_t, int& error) = 0;
virtual bool get_address(sockaddr*, socklen_t*) = 0;
virtual bool is_local() const { return false; }
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;
pid_t origin_pid() const { return m_origin_pid; }
protected:
Socket(int domain, int type, int protocol);
bool queue_connection_from(Socket&, int& error);
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_listening { false };
bool m_connected { false };
Vector<RetainPtr<Socket>> m_pending;
Vector<RetainPtr<Socket>> m_clients;
};