libos: Remove some shared pointers and change them to owned/live on the stack
All checks were successful
continuous-integration/drone/pr Build is passing

This commit is contained in:
apio 2023-08-08 11:40:22 +02:00
parent e5bb07874d
commit 563d4beb30
Signed by: apio
GPG Key ID: B8A7D06E42258954
5 changed files with 25 additions and 19 deletions

View File

@ -1,5 +1,5 @@
#pragma once
#include <luna/SharedPtr.h>
#include <luna/OwnedPtr.h>
#include <luna/StringView.h>
namespace os
@ -7,7 +7,7 @@ namespace os
/**
* @brief A client used to connect to a local server socket.
*/
class LocalClient : public Shareable
class LocalClient
{
public:
/**
@ -15,9 +15,9 @@ namespace os
*
* @param path The path of the server socket to connect to.
* @param blocking Whether the client should block if no data is available and recv() is called.
* @return Result<SharedPtr<LocalClient>> An error, or a new client object.
* @return Result<OwnedPtr<LocalClient>> An error, or a new client object.
*/
static Result<SharedPtr<LocalClient>> connect(StringView path, bool blocking);
static Result<OwnedPtr<LocalClient>> connect(StringView path, bool blocking);
/**
* @brief Return the underlying socket file descriptor used by this object.

View File

@ -1,6 +1,6 @@
#pragma once
#include <luna/OwnedPtr.h>
#include <luna/Result.h>
#include <luna/SharedPtr.h>
#include <luna/StringView.h>
namespace os
@ -8,7 +8,7 @@ namespace os
/**
* @brief A local domain server, used to communicate between processes on the same machine.
*/
class LocalServer : public Shareable
class LocalServer
{
public:
/**
@ -16,9 +16,9 @@ namespace os
*
* @param path The path to use for the server socket.
* @param blocking Whether the server should block if no connections are available when calling accept().
* @return Result<SharedPtr<LocalServer>> An error, or a new server object.
* @return Result<OwnedPtr<LocalServer>> An error, or a new server object.
*/
static Result<SharedPtr<LocalServer>> create(StringView path, bool blocking);
static Result<OwnedPtr<LocalServer>> create(StringView path, bool blocking);
/**
* @brief Activate the server and start listening for connections.
@ -41,7 +41,7 @@ namespace os
/**
* @brief An interface to communicate with clients connected to a local server.
*/
class Client : public Shareable
class Client
{
public:
/**
@ -107,6 +107,7 @@ namespace os
return m_fd;
}
Client(Client&& other);
Client(int fd);
~Client();
@ -119,9 +120,9 @@ namespace os
* accept() either blocks until there is one (if the object was created with blocking=true), or returns EAGAIN
* (if the object was created with blocking=false).
*
* @return Result<SharedPtr<Client>> An error, or a handle to the new connection.
* @return Result<Client> An error, or a handle to the new connection.
*/
Result<SharedPtr<Client>> accept();
Result<Client> accept();
~LocalServer();

View File

@ -7,9 +7,9 @@
namespace os
{
Result<SharedPtr<LocalClient>> LocalClient::connect(StringView path, bool blocking)
Result<OwnedPtr<LocalClient>> LocalClient::connect(StringView path, bool blocking)
{
auto client = TRY(make_shared<LocalClient>());
auto client = TRY(make_owned<LocalClient>());
int sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
if (sockfd < 0) return err(errno);

View File

@ -8,9 +8,9 @@
namespace os
{
Result<SharedPtr<LocalServer>> LocalServer::create(StringView path, bool blocking)
Result<OwnedPtr<LocalServer>> LocalServer::create(StringView path, bool blocking)
{
auto server = TRY(make_shared<LocalServer>());
auto server = TRY(make_owned<LocalServer>());
(void)os::FileSystem::remove(path); // We explicitly ignore any error here, either it doesn't exist (which is
// fine), or it cannot be removed, which will make bind() fail later.
@ -43,12 +43,12 @@ namespace os
return {};
}
Result<SharedPtr<LocalServer::Client>> LocalServer::accept()
Result<LocalServer::Client> LocalServer::accept()
{
int fd = ::accept(m_fd, nullptr, nullptr);
if (fd < 0) return err(errno);
if (!m_blocking) fcntl(fd, F_SETFL, O_NONBLOCK);
return make_shared<Client>(fd);
return Client { fd };
}
LocalServer::~LocalServer()
@ -56,6 +56,11 @@ namespace os
close(m_fd);
}
LocalServer::Client::Client(Client&& other) : m_fd(other.m_fd)
{
other.m_fd = -1;
}
LocalServer::Client::Client(int fd) : m_fd(fd)
{
}

View File

@ -83,7 +83,7 @@ Result<int> luna_main(int argc, char** argv)
TRY(make<Window>(ui::Rect { 100, 100, 300, 200 }, ui::RED, "Settings"_sv));
TRY(make<Window>(ui::Rect { 600, 130, 350, 250 }, ui::CYAN, "File Manager"_sv));
Vector<SharedPtr<os::LocalServer::Client>> clients;
Vector<os::LocalServer::Client> clients;
while (1)
{
@ -125,7 +125,7 @@ Result<int> luna_main(int argc, char** argv)
{
auto client = TRY(server->accept());
os::println("wind: New client connected!");
TRY(clients.try_append(client));
TRY(clients.try_append(move(client)));
}
}
}