1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 21:47:46 +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:
Conrad Pankoff 2019-08-05 20:47:30 +10:00 committed by Andreas Kling
parent 79e22acb22
commit ed66f1d6d4
6 changed files with 118 additions and 1 deletions

View file

@ -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;
};