Skip to content

Commit

Permalink
LibCore: Move Stream-based sockets into the Core namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
timschumi authored and linusg committed Feb 13, 2023
1 parent d43a7ea commit a96339b
Show file tree
Hide file tree
Showing 123 changed files with 1,157 additions and 1,100 deletions.
2 changes: 1 addition & 1 deletion Ladybird/WebContent/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)

auto webcontent_socket = TRY(Core::take_over_socket_from_system_server("WebContent"sv));
auto webcontent_client = TRY(WebContent::ConnectionFromClient::try_create(move(webcontent_socket)));
webcontent_client->set_fd_passing_socket(TRY(Core::Stream::LocalSocket::adopt_fd(webcontent_fd_passing_socket)));
webcontent_client->set_fd_passing_socket(TRY(Core::LocalSocket::adopt_fd(webcontent_fd_passing_socket)));

QSocketNotifier webcontent_notifier(QSocketNotifier::Type::Read);
proxy_socket_through_notifier(*webcontent_client, webcontent_notifier);
Expand Down
4 changes: 2 additions & 2 deletions Ladybird/WebContentView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -600,11 +600,11 @@ void WebContentView::create_client()
MUST(Core::System::close(wc_fd_passing_fd));
MUST(Core::System::close(wc_fd));

auto socket = MUST(Core::Stream::LocalSocket::adopt_fd(ui_fd));
auto socket = MUST(Core::LocalSocket::adopt_fd(ui_fd));
MUST(socket->set_blocking(true));

auto new_client = MUST(adopt_nonnull_ref_or_enomem(new (nothrow) WebView::WebContentClient(std::move(socket), *this)));
new_client->set_fd_passing_socket(MUST(Core::Stream::LocalSocket::adopt_fd(ui_fd_passing_fd)));
new_client->set_fd_passing_socket(MUST(Core::LocalSocket::adopt_fd(ui_fd_passing_fd)));

m_web_content_notifier.setSocket(new_client->socket().fd().value());
m_web_content_notifier.setEnabled(true);
Expand Down
2 changes: 1 addition & 1 deletion Ladybird/WebDriver/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
return;
}

auto maybe_buffered_socket = Core::Stream::BufferedTCPSocket::create(maybe_client_socket.release_value());
auto maybe_buffered_socket = Core::BufferedTCPSocket::create(maybe_client_socket.release_value());
if (maybe_buffered_socket.is_error()) {
warnln("Could not obtain a buffered socket for the client: {}", maybe_buffered_socket.error());
return;
Expand Down
4 changes: 2 additions & 2 deletions Meta/Lagom/Tools/CodeGenerators/IPCCompiler/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ public:)~~~");
static i32 static_message_id() { return (int)MessageID::@message.pascal_name@; }
virtual const char* message_name() const override { return "@endpoint.name@::@message.pascal_name@"; }
static ErrorOr<NonnullOwnPtr<@message.pascal_name@>> decode(AK::Stream& stream, Core::Stream::LocalSocket& socket)
static ErrorOr<NonnullOwnPtr<@message.pascal_name@>> decode(AK::Stream& stream, Core::LocalSocket& socket)
{
IPC::Decoder decoder { stream, socket };)~~~");

Expand Down Expand Up @@ -584,7 +584,7 @@ class @endpoint.name@Endpoint {
static u32 static_magic() { return @endpoint.magic@; }
static ErrorOr<NonnullOwnPtr<IPC::Message>> decode_message(ReadonlyBytes buffer, [[maybe_unused]] Core::Stream::LocalSocket& socket)
static ErrorOr<NonnullOwnPtr<IPC::Message>> decode_message(ReadonlyBytes buffer, [[maybe_unused]] Core::LocalSocket& socket)
{
FixedMemoryStream stream { buffer };
auto message_endpoint_magic = TRY(stream.read_value<u32>());)~~~");
Expand Down
25 changes: 13 additions & 12 deletions Tests/LibCore/TestLibCoreStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <AK/String.h>
#include <LibCore/EventLoop.h>
#include <LibCore/LocalServer.h>
#include <LibCore/Socket.h>
#include <LibCore/Stream.h>
#include <LibCore/TCPServer.h>
#include <LibCore/Timer.h>
Expand Down Expand Up @@ -151,11 +152,11 @@ TEST_CASE(file_truncate)

TEST_CASE(should_error_when_connection_fails)
{
// NOTE: This is required here because Core::Stream::TCPSocket requires
// NOTE: This is required here because Core::TCPSocket requires
// Core::EventLoop through Core::Notifier.
Core::EventLoop event_loop;

auto maybe_tcp_socket = Core::Stream::TCPSocket::connect({ { 127, 0, 0, 1 }, 1234 });
auto maybe_tcp_socket = Core::TCPSocket::connect({ { 127, 0, 0, 1 }, 1234 });
EXPECT(maybe_tcp_socket.is_error());
EXPECT(maybe_tcp_socket.error().is_syscall());
EXPECT(maybe_tcp_socket.error().code() == ECONNREFUSED);
Expand All @@ -175,7 +176,7 @@ TEST_CASE(tcp_socket_read)
EXPECT(!tcp_server->listen({ 127, 0, 0, 1 }, 9090).is_error());
EXPECT(!tcp_server->set_blocking(true).is_error());

auto maybe_client_socket = Core::Stream::TCPSocket::connect({ { 127, 0, 0, 1 }, 9090 });
auto maybe_client_socket = Core::TCPSocket::connect({ { 127, 0, 0, 1 }, 9090 });
EXPECT(!maybe_client_socket.is_error());
auto client_socket = maybe_client_socket.release_value();

Expand Down Expand Up @@ -211,7 +212,7 @@ TEST_CASE(tcp_socket_write)
EXPECT(!tcp_server->listen({ 127, 0, 0, 1 }, 9090).is_error());
EXPECT(!tcp_server->set_blocking(true).is_error());

auto maybe_client_socket = Core::Stream::TCPSocket::connect({ { 127, 0, 0, 1 }, 9090 });
auto maybe_client_socket = Core::TCPSocket::connect({ { 127, 0, 0, 1 }, 9090 });
EXPECT(!maybe_client_socket.is_error());
auto client_socket = maybe_client_socket.release_value();

Expand Down Expand Up @@ -244,7 +245,7 @@ TEST_CASE(tcp_socket_eof)
EXPECT(!tcp_server->listen({ 127, 0, 0, 1 }, 9090).is_error());
EXPECT(!tcp_server->set_blocking(true).is_error());

auto maybe_client_socket = Core::Stream::TCPSocket::connect({ { 127, 0, 0, 1 }, 9090 });
auto maybe_client_socket = Core::TCPSocket::connect({ { 127, 0, 0, 1 }, 9090 });
EXPECT(!maybe_client_socket.is_error());
auto client_socket = maybe_client_socket.release_value();

Expand Down Expand Up @@ -279,7 +280,7 @@ TEST_CASE(udp_socket_read_write)
auto udp_server = Core::UDPServer::construct();
EXPECT(udp_server->bind({ 127, 0, 0, 1 }, 9090));

auto maybe_client_socket = Core::Stream::UDPSocket::connect({ { 127, 0, 0, 1 }, 9090 });
auto maybe_client_socket = Core::UDPSocket::connect({ { 127, 0, 0, 1 }, 9090 });
EXPECT(!maybe_client_socket.is_error());
auto client_socket = maybe_client_socket.release_value();

Expand Down Expand Up @@ -328,7 +329,7 @@ TEST_CASE(local_socket_read)
auto local_server = Core::LocalServer::construct();
EXPECT(local_server->listen("/tmp/test-socket"));

local_server->on_accept = [&](NonnullOwnPtr<Core::Stream::LocalSocket> server_socket) {
local_server->on_accept = [&](NonnullOwnPtr<Core::LocalSocket> server_socket) {
EXPECT(!server_socket->write(sent_data.bytes()).is_error());

event_loop.quit(0);
Expand All @@ -343,7 +344,7 @@ TEST_CASE(local_socket_read)
[](auto&) {
Core::EventLoop event_loop;

auto maybe_client_socket = Core::Stream::LocalSocket::connect("/tmp/test-socket");
auto maybe_client_socket = Core::LocalSocket::connect("/tmp/test-socket");
EXPECT(!maybe_client_socket.is_error());
auto client_socket = maybe_client_socket.release_value();

Expand Down Expand Up @@ -377,7 +378,7 @@ TEST_CASE(local_socket_write)
auto local_server = Core::LocalServer::construct();
EXPECT(local_server->listen("/tmp/test-socket"));

local_server->on_accept = [&](NonnullOwnPtr<Core::Stream::LocalSocket> server_socket) {
local_server->on_accept = [&](NonnullOwnPtr<Core::LocalSocket> server_socket) {
// NOTE: For some reason LocalServer gives us a nonblocking socket..?
MUST(server_socket->set_blocking(true));

Expand All @@ -400,7 +401,7 @@ TEST_CASE(local_socket_write)
// NOTE: Same reason as in the local_socket_read test.
auto background_action = Threading::BackgroundAction<int>::construct(
[](auto&) {
auto maybe_client_socket = Core::Stream::LocalSocket::connect("/tmp/test-socket");
auto maybe_client_socket = Core::LocalSocket::connect("/tmp/test-socket");
EXPECT(!maybe_client_socket.is_error());
auto client_socket = maybe_client_socket.release_value();

Expand Down Expand Up @@ -566,9 +567,9 @@ TEST_CASE(buffered_tcp_socket_read)
EXPECT(!tcp_server->listen({ 127, 0, 0, 1 }, 9090).is_error());
EXPECT(!tcp_server->set_blocking(true).is_error());

auto maybe_client_socket = Core::Stream::TCPSocket::connect({ { 127, 0, 0, 1 }, 9090 });
auto maybe_client_socket = Core::TCPSocket::connect({ { 127, 0, 0, 1 }, 9090 });
EXPECT(!maybe_client_socket.is_error());
auto maybe_buffered_socket = Core::Stream::BufferedTCPSocket::create(maybe_client_socket.release_value());
auto maybe_buffered_socket = Core::BufferedTCPSocket::create(maybe_client_socket.release_value());
EXPECT(!maybe_buffered_socket.is_error());
auto client_socket = maybe_buffered_socket.release_value();

Expand Down
2 changes: 1 addition & 1 deletion Userland/DevTools/HackStudio/LanguageClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ConnectionToServer
friend class ConnectionToServerWrapper;

public:
ConnectionToServer(NonnullOwnPtr<Core::Stream::LocalSocket> socket, DeprecatedString const& project_path)
ConnectionToServer(NonnullOwnPtr<Core::LocalSocket> socket, DeprecatedString const& project_path)
: IPC::ConnectionToServer<LanguageClientEndpoint, LanguageServerEndpoint>(*this, move(socket))
{
m_project_path = project_path;
Expand Down
32 changes: 16 additions & 16 deletions Userland/DevTools/HackStudio/LanguageClients/ConnectionsToServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@
#include <DevTools/HackStudio/LanguageServers/LanguageServerEndpoint.h>
#include <LibIPC/ConnectionToServer.h>

#define LANGUAGE_CLIENT(language_name_, socket_name) \
namespace language_name_ { \
class ConnectionToServer final : public HackStudio::ConnectionToServer { \
IPC_CLIENT_CONNECTION(ConnectionToServer, "/tmp/session/%sid/portal/language/" socket_name) \
public: \
static char const* language_name() \
{ \
return #language_name_; \
} \
\
private: \
ConnectionToServer(NonnullOwnPtr<Core::Stream::LocalSocket> socket, DeprecatedString const& project_path) \
: HackStudio::ConnectionToServer(move(socket), project_path) \
{ \
} \
}; \
#define LANGUAGE_CLIENT(language_name_, socket_name) \
namespace language_name_ { \
class ConnectionToServer final : public HackStudio::ConnectionToServer { \
IPC_CLIENT_CONNECTION(ConnectionToServer, "/tmp/session/%sid/portal/language/" socket_name) \
public: \
static char const* language_name() \
{ \
return #language_name_; \
} \
\
private: \
ConnectionToServer(NonnullOwnPtr<Core::LocalSocket> socket, DeprecatedString const& project_path) \
: HackStudio::ConnectionToServer(move(socket), project_path) \
{ \
} \
}; \
}

namespace LanguageClients {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace LanguageServers {

static HashMap<int, RefPtr<ConnectionFromClient>> s_connections;

ConnectionFromClient::ConnectionFromClient(NonnullOwnPtr<Core::Stream::LocalSocket> socket)
ConnectionFromClient::ConnectionFromClient(NonnullOwnPtr<Core::LocalSocket> socket)
: IPC::ConnectionFromClient<LanguageClientEndpoint, LanguageServerEndpoint>(*this, move(socket), 1)
{
s_connections.set(1, *this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace LanguageServers {

class ConnectionFromClient : public IPC::ConnectionFromClient<LanguageClientEndpoint, LanguageServerEndpoint> {
public:
explicit ConnectionFromClient(NonnullOwnPtr<Core::Stream::LocalSocket>);
explicit ConnectionFromClient(NonnullOwnPtr<Core::LocalSocket>);
~ConnectionFromClient() override = default;

virtual void die() override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ConnectionFromClient final : public LanguageServers::ConnectionFromClient
C_OBJECT(ConnectionFromClient);

private:
ConnectionFromClient(NonnullOwnPtr<Core::Stream::LocalSocket> socket)
ConnectionFromClient(NonnullOwnPtr<Core::LocalSocket> socket)
: LanguageServers::ConnectionFromClient(move(socket))
{
m_autocomplete_engine = adopt_own(*new CodeComprehension::Cpp::CppComprehensionEngine(m_filedb));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ConnectionFromClient final : public LanguageServers::ConnectionFromClient
C_OBJECT(ConnectionFromClient);

private:
ConnectionFromClient(NonnullOwnPtr<Core::Stream::LocalSocket> socket)
ConnectionFromClient(NonnullOwnPtr<Core::LocalSocket> socket)
: LanguageServers::ConnectionFromClient(move(socket))
{
m_autocomplete_engine = make<CodeComprehension::Shell::ShellComprehensionEngine>(m_filedb);
Expand Down
2 changes: 1 addition & 1 deletion Userland/DevTools/Inspector/InspectorServerClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class InspectorServerClient final
virtual ~InspectorServerClient() override = default;

private:
InspectorServerClient(NonnullOwnPtr<Core::Stream::LocalSocket> socket)
InspectorServerClient(NonnullOwnPtr<Core::LocalSocket> socket)
: IPC::ConnectionToServer<InspectorClientEndpoint, InspectorServerEndpoint>(*this, move(socket))
{
}
Expand Down
2 changes: 1 addition & 1 deletion Userland/Libraries/LibAudio/ConnectionToServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

namespace Audio {

ConnectionToServer::ConnectionToServer(NonnullOwnPtr<Core::Stream::LocalSocket> socket)
ConnectionToServer::ConnectionToServer(NonnullOwnPtr<Core::LocalSocket> socket)
: IPC::ConnectionToServer<AudioClientEndpoint, AudioServerEndpoint>(*this, move(socket))
, m_buffer(make<AudioQueue>(MUST(AudioQueue::create())))
, m_user_queue(make<UserSampleQueue>())
Expand Down
2 changes: 1 addition & 1 deletion Userland/Libraries/LibAudio/ConnectionToServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class ConnectionToServer final
Function<void(double volume)> on_client_volume_change;

private:
ConnectionToServer(NonnullOwnPtr<Core::Stream::LocalSocket>);
ConnectionToServer(NonnullOwnPtr<Core::LocalSocket>);

virtual void main_mix_muted_state_changed(bool) override;
virtual void main_mix_volume_changed(double) override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ConnectionFromClient final : public LanguageServers::ConnectionFromClient
C_OBJECT(ConnectionFromClient);

private:
ConnectionFromClient(NonnullOwnPtr<Core::Stream::LocalSocket> socket)
ConnectionFromClient(NonnullOwnPtr<Core::LocalSocket> socket)
: LanguageServers::ConnectionFromClient(move(socket))
{
m_autocomplete_engine = make<CodeComprehension::Cpp::CppComprehensionEngine>(m_filedb);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ConnectionFromClient final : public LanguageServers::ConnectionFromClient
C_OBJECT(ConnectionFromClient);

private:
ConnectionFromClient(NonnullOwnPtr<Core::Stream::LocalSocket> socket)
ConnectionFromClient(NonnullOwnPtr<Core::LocalSocket> socket)
: LanguageServers::ConnectionFromClient(move(socket))
{
m_autocomplete_engine = make<CodeComprehension::Shell::ShellComprehensionEngine>(m_filedb);
Expand Down
2 changes: 1 addition & 1 deletion Userland/Libraries/LibConfig/Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Client final
static Client& the();

private:
explicit Client(NonnullOwnPtr<Core::Stream::LocalSocket> socket)
explicit Client(NonnullOwnPtr<Core::LocalSocket> socket)
: IPC::ConnectionToServer<ConfigClientEndpoint, ConfigServerEndpoint>(*this, move(socket))
{
}
Expand Down
1 change: 1 addition & 0 deletions Userland/Libraries/LibCore/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ set(SOURCES
Property.cpp
SecretString.cpp
SessionManagement.cpp
Socket.cpp
SOCKSProxyClient.cpp
StandardPaths.cpp
Stream.cpp
Expand Down
7 changes: 4 additions & 3 deletions Userland/Libraries/LibCore/EventLoop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <LibCore/Notifier.h>
#include <LibCore/Object.h>
#include <LibCore/SessionManagement.h>
#include <LibCore/Socket.h>
#include <LibThreading/Mutex.h>
#include <LibThreading/MutexProtected.h>
#include <errno.h>
Expand Down Expand Up @@ -156,7 +157,7 @@ pid_t EventLoop::s_pid;
class InspectorServerConnection : public Object {
C_OBJECT(InspectorServerConnection)
private:
explicit InspectorServerConnection(NonnullOwnPtr<Stream::LocalSocket> socket)
explicit InspectorServerConnection(NonnullOwnPtr<LocalSocket> socket)
: m_socket(move(socket))
, m_client_id(s_id_allocator.with_locked([](auto& allocator) {
return allocator->allocate();
Expand Down Expand Up @@ -305,7 +306,7 @@ class InspectorServerConnection : public Object {
}

private:
NonnullOwnPtr<Stream::LocalSocket> m_socket;
NonnullOwnPtr<LocalSocket> m_socket;
WeakPtr<Object> m_inspected_object;
int m_client_id { -1 };
};
Expand Down Expand Up @@ -370,7 +371,7 @@ bool connect_to_inspector_server()
return false;
}
auto inspector_server_path = maybe_path.value();
auto maybe_socket = Stream::LocalSocket::connect(inspector_server_path, Stream::PreventSIGPIPE::Yes);
auto maybe_socket = LocalSocket::connect(inspector_server_path, Socket::PreventSIGPIPE::Yes);
if (maybe_socket.is_error()) {
dbgln("connect_to_inspector_server: Failed to connect: {}", maybe_socket.error());
return false;
Expand Down
7 changes: 5 additions & 2 deletions Userland/Libraries/LibCore/Forward.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Core {

class AnonymousBuffer;
class ArgsParser;
class BufferedSocketBase;
class ChildEvent;
class ConfigFile;
class CustomEvent;
Expand All @@ -22,25 +23,27 @@ class Event;
class EventLoop;
class IODevice;
class LocalServer;
class LocalSocket;
class MimeData;
class NetworkJob;
class NetworkResponse;
class Notifier;
class Object;
class ObjectClassRegistration;
class ProcessStatisticsReader;
class Socket;
class SocketAddress;
class TCPServer;
class TCPSocket;
class Timer;
class TimerEvent;
class UDPServer;
class UDPSocket;

enum class TimerShouldFireWhenNotVisible;

namespace Stream {
class File;
class Socket;
class BufferedSocketBase;
}

}
Loading

0 comments on commit a96339b

Please sign in to comment.