mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-26 01:20:25 +00:00
LibCore: Add CTCPServer
This is pretty much a find/replace copy of CLocalServer, and some modifications to CTCPSocket and CSocketAddress to support it.
This commit is contained in:
parent
79e22acb22
commit
ed66f1d6d4
Notes:
sideshowbarker
2024-07-19 12:52:42 +09:00
Author: https://github.com/deoxxa Commit: https://github.com/SerenityOS/serenity/commit/ed66f1d6d44 Pull-request: https://github.com/SerenityOS/serenity/pull/408
6 changed files with 118 additions and 1 deletions
|
@ -1,6 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/IPv4Address.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
|
||||
|
@ -19,6 +20,13 @@ public:
|
|||
{
|
||||
}
|
||||
|
||||
CSocketAddress(const IPv4Address& address, u16 port)
|
||||
: m_type(Type::IPv4)
|
||||
, m_ipv4_address(address)
|
||||
, m_port(port)
|
||||
{
|
||||
}
|
||||
|
||||
static CSocketAddress local(const String& address)
|
||||
{
|
||||
CSocketAddress addr;
|
||||
|
@ -30,12 +38,13 @@ public:
|
|||
Type type() const { return m_type; }
|
||||
bool is_valid() const { return m_type != Type::Invalid; }
|
||||
IPv4Address ipv4_address() const { return m_ipv4_address; }
|
||||
u16 port() const { return m_port; }
|
||||
|
||||
String to_string() const
|
||||
{
|
||||
switch (m_type) {
|
||||
case Type::IPv4:
|
||||
return m_ipv4_address.to_string();
|
||||
return String::format("%s:%d", m_ipv4_address.to_string().characters(), m_port);
|
||||
case Type::Local:
|
||||
return m_local_address;
|
||||
default:
|
||||
|
@ -53,9 +62,20 @@ public:
|
|||
return address;
|
||||
}
|
||||
|
||||
sockaddr_in to_sockaddr_in() const
|
||||
{
|
||||
ASSERT(type() == Type::IPv4);
|
||||
sockaddr_in address;
|
||||
address.sin_family = AF_INET;
|
||||
address.sin_addr.s_addr = m_ipv4_address.to_in_addr_t();
|
||||
address.sin_port = htons(m_port);
|
||||
return address;
|
||||
}
|
||||
|
||||
private:
|
||||
Type m_type { Type::Invalid };
|
||||
IPv4Address m_ipv4_address;
|
||||
u16 m_port;
|
||||
String m_local_address;
|
||||
};
|
||||
|
||||
|
|
55
Libraries/LibCore/CTCPServer.cpp
Normal file
55
Libraries/LibCore/CTCPServer.cpp
Normal file
|
@ -0,0 +1,55 @@
|
|||
#include <AK/IPv4Address.h>
|
||||
#include <AK/Types.h>
|
||||
#include <LibCore/CTCPServer.h>
|
||||
#include <LibCore/CTCPSocket.h>
|
||||
#include <LibCore/CNotifier.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/socket.h>
|
||||
|
||||
CTCPServer::CTCPServer(CObject* parent)
|
||||
: CObject(parent)
|
||||
{
|
||||
m_fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
|
||||
ASSERT(m_fd >= 0);
|
||||
}
|
||||
|
||||
CTCPServer::~CTCPServer()
|
||||
{
|
||||
}
|
||||
|
||||
bool CTCPServer::listen(const IPv4Address& address, u16 port)
|
||||
{
|
||||
if (m_listening)
|
||||
return false;
|
||||
|
||||
int rc;
|
||||
auto socket_address = CSocketAddress(address, port);
|
||||
auto in = socket_address.to_sockaddr_in();
|
||||
rc = ::bind(m_fd, (const sockaddr*)&in, sizeof(in));
|
||||
ASSERT(rc == 0);
|
||||
|
||||
rc = ::listen(m_fd, 5);
|
||||
ASSERT(rc == 0);
|
||||
m_listening = true;
|
||||
|
||||
m_notifier = make<CNotifier>(m_fd, CNotifier::Event::Read);
|
||||
m_notifier->on_ready_to_read = [this] {
|
||||
if (on_ready_to_accept)
|
||||
on_ready_to_accept();
|
||||
};
|
||||
return true;
|
||||
}
|
||||
|
||||
CTCPSocket* CTCPServer::accept()
|
||||
{
|
||||
ASSERT(m_listening);
|
||||
sockaddr_in in;
|
||||
socklen_t in_size = sizeof(in);
|
||||
int accepted_fd = ::accept(m_fd, (sockaddr*)&in, &in_size);
|
||||
if (accepted_fd < 0) {
|
||||
perror("accept");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return new CTCPSocket({}, accepted_fd);
|
||||
}
|
26
Libraries/LibCore/CTCPServer.h
Normal file
26
Libraries/LibCore/CTCPServer.h
Normal file
|
@ -0,0 +1,26 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/IPv4Address.h>
|
||||
#include <LibCore/CNotifier.h>
|
||||
#include <LibCore/CObject.h>
|
||||
|
||||
class CTCPSocket;
|
||||
|
||||
class CTCPServer : public CObject {
|
||||
C_OBJECT(CTCPServer)
|
||||
public:
|
||||
explicit CTCPServer(CObject* parent = nullptr);
|
||||
virtual ~CTCPServer() override;
|
||||
|
||||
bool is_listening() const { return m_listening; }
|
||||
bool listen(const IPv4Address& address, u16 port);
|
||||
|
||||
CTCPSocket* accept();
|
||||
|
||||
Function<void()> on_ready_to_accept;
|
||||
|
||||
private:
|
||||
int m_fd { -1 };
|
||||
bool m_listening { false };
|
||||
OwnPtr<CNotifier> m_notifier;
|
||||
};
|
|
@ -1,5 +1,14 @@
|
|||
#include <LibCore/CTCPSocket.h>
|
||||
#include <sys/socket.h>
|
||||
#include <errno.h>
|
||||
|
||||
CTCPSocket::CTCPSocket(Badge<CTCPServer>, int fd, CObject* parent)
|
||||
: CSocket(CSocket::Type::TCP, parent)
|
||||
{
|
||||
set_fd(fd);
|
||||
set_mode(CIODevice::ReadWrite);
|
||||
set_error(0);
|
||||
}
|
||||
|
||||
CTCPSocket::CTCPSocket(CObject* parent)
|
||||
: CSocket(CSocket::Type::TCP, parent)
|
||||
|
|
|
@ -1,8 +1,14 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/Badge.h>
|
||||
#include <LibCore/CSocket.h>
|
||||
|
||||
class CTCPServer;
|
||||
|
||||
class CTCPSocket final : public CSocket {
|
||||
C_OBJECT(CTCPSocket)
|
||||
public:
|
||||
explicit CTCPSocket(CObject* parent = nullptr);
|
||||
CTCPSocket(Badge<CTCPServer>, int fd, CObject* parent = nullptr);
|
||||
virtual ~CTCPSocket() override;
|
||||
};
|
||||
|
|
|
@ -9,6 +9,7 @@ OBJS = \
|
|||
CLocalSocket.o \
|
||||
CLocalServer.o \
|
||||
CTCPSocket.o \
|
||||
CTCPServer.o \
|
||||
CElapsedTimer.o \
|
||||
CNotifier.o \
|
||||
CHttpRequest.o \
|
||||
|
|
Loading…
Reference in a new issue