
Instead of trying to support both client and server in CLocalSocket, let's have a specialized server class. The basic usage is: CLocalServer server; server.listen("/tmp/name-of-portal"); server.on_ready_to_accept = [&] { CLocalSocket* client = server.accept(); ... }; This will make things a lot simpler, since an accepting socket doesn't need half of the stuff that a regular CIODevice provides. :^)
36 lines
820 B
C++
36 lines
820 B
C++
#include <LibCore/CLocalSocket.h>
|
|
#include <sys/socket.h>
|
|
#include <errno.h>
|
|
|
|
CLocalSocket::CLocalSocket(Badge<CLocalServer>, int fd, CObject* parent)
|
|
: CSocket(CSocket::Type::Local, parent)
|
|
{
|
|
set_fd(fd);
|
|
set_mode(CIODevice::ReadWrite);
|
|
set_error(0);
|
|
}
|
|
|
|
CLocalSocket::CLocalSocket(CObject* parent)
|
|
: CSocket(CSocket::Type::Local, parent)
|
|
{
|
|
int fd = socket(AF_LOCAL, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
|
|
if (fd < 0) {
|
|
set_error(fd);
|
|
} else {
|
|
set_fd(fd);
|
|
set_mode(CIODevice::ReadWrite);
|
|
set_error(0);
|
|
}
|
|
}
|
|
|
|
CLocalSocket::~CLocalSocket()
|
|
{
|
|
}
|
|
|
|
bool CLocalSocket::bind(const CSocketAddress& address)
|
|
{
|
|
auto un = address.to_sockaddr_un();
|
|
int rc = ::bind(fd(), (const sockaddr*)&un, sizeof(un));
|
|
set_error(errno);
|
|
return rc == 0;
|
|
}
|