mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 19:27:35 +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
6 changed files with 118 additions and 1 deletions
|
@ -1,6 +1,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <AK/IPv4Address.h>
|
#include <AK/IPv4Address.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <sys/un.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)
|
static CSocketAddress local(const String& address)
|
||||||
{
|
{
|
||||||
CSocketAddress addr;
|
CSocketAddress addr;
|
||||||
|
@ -30,12 +38,13 @@ public:
|
||||||
Type type() const { return m_type; }
|
Type type() const { return m_type; }
|
||||||
bool is_valid() const { return m_type != Type::Invalid; }
|
bool is_valid() const { return m_type != Type::Invalid; }
|
||||||
IPv4Address ipv4_address() const { return m_ipv4_address; }
|
IPv4Address ipv4_address() const { return m_ipv4_address; }
|
||||||
|
u16 port() const { return m_port; }
|
||||||
|
|
||||||
String to_string() const
|
String to_string() const
|
||||||
{
|
{
|
||||||
switch (m_type) {
|
switch (m_type) {
|
||||||
case Type::IPv4:
|
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:
|
case Type::Local:
|
||||||
return m_local_address;
|
return m_local_address;
|
||||||
default:
|
default:
|
||||||
|
@ -53,9 +62,20 @@ public:
|
||||||
return address;
|
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:
|
private:
|
||||||
Type m_type { Type::Invalid };
|
Type m_type { Type::Invalid };
|
||||||
IPv4Address m_ipv4_address;
|
IPv4Address m_ipv4_address;
|
||||||
|
u16 m_port;
|
||||||
String m_local_address;
|
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 <LibCore/CTCPSocket.h>
|
||||||
#include <sys/socket.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)
|
CTCPSocket::CTCPSocket(CObject* parent)
|
||||||
: CSocket(CSocket::Type::TCP, parent)
|
: CSocket(CSocket::Type::TCP, parent)
|
||||||
|
|
|
@ -1,8 +1,14 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <AK/Badge.h>
|
||||||
#include <LibCore/CSocket.h>
|
#include <LibCore/CSocket.h>
|
||||||
|
|
||||||
|
class CTCPServer;
|
||||||
|
|
||||||
class CTCPSocket final : public CSocket {
|
class CTCPSocket final : public CSocket {
|
||||||
C_OBJECT(CTCPSocket)
|
C_OBJECT(CTCPSocket)
|
||||||
public:
|
public:
|
||||||
explicit CTCPSocket(CObject* parent = nullptr);
|
explicit CTCPSocket(CObject* parent = nullptr);
|
||||||
|
CTCPSocket(Badge<CTCPServer>, int fd, CObject* parent = nullptr);
|
||||||
virtual ~CTCPSocket() override;
|
virtual ~CTCPSocket() override;
|
||||||
};
|
};
|
||||||
|
|
|
@ -9,6 +9,7 @@ OBJS = \
|
||||||
CLocalSocket.o \
|
CLocalSocket.o \
|
||||||
CLocalServer.o \
|
CLocalServer.o \
|
||||||
CTCPSocket.o \
|
CTCPSocket.o \
|
||||||
|
CTCPServer.o \
|
||||||
CElapsedTimer.o \
|
CElapsedTimer.o \
|
||||||
CNotifier.o \
|
CNotifier.o \
|
||||||
CHttpRequest.o \
|
CHttpRequest.o \
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue