1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 15:48:12 +00:00

LibCore: Convert CTCPServer to ObjectPtr

Also get rid of the custom CNotifier::create() in favor of construct().
This commit is contained in:
Andreas Kling 2019-09-21 15:18:12 +02:00
parent bce58bbbca
commit 4ea229accd
12 changed files with 23 additions and 27 deletions

View file

@ -56,7 +56,7 @@ void IRCClient::set_server(const String& hostname, int port)
void IRCClient::on_socket_connected() void IRCClient::on_socket_connected()
{ {
m_notifier = CNotifier::create(m_socket->fd(), CNotifier::Read); m_notifier = CNotifier::construct(m_socket->fd(), CNotifier::Read);
m_notifier->on_ready_to_read = [this] { receive_from_server(); }; m_notifier->on_ready_to_read = [this] { receive_from_server(); };
send_user(); send_user();

View file

@ -22,7 +22,7 @@
TerminalWidget::TerminalWidget(int ptm_fd, RefPtr<CConfigFile> config) TerminalWidget::TerminalWidget(int ptm_fd, RefPtr<CConfigFile> config)
: m_terminal(*this) : m_terminal(*this)
, m_ptm_fd(ptm_fd) , m_ptm_fd(ptm_fd)
, m_notifier(CNotifier::create(ptm_fd, CNotifier::Read)) , m_notifier(CNotifier::construct(ptm_fd, CNotifier::Read))
, m_config(move(config)) , m_config(move(config))
{ {
m_cursor_blink_timer = CTimer::create(); m_cursor_blink_timer = CTimer::create();

View file

@ -31,7 +31,7 @@ bool CLocalServer::listen(const String& address)
ASSERT(rc == 0); ASSERT(rc == 0);
m_listening = true; m_listening = true;
m_notifier = CNotifier::create(m_fd, CNotifier::Event::Read, this); m_notifier = CNotifier::construct(m_fd, CNotifier::Event::Read, this);
m_notifier->on_ready_to_read = [this] { m_notifier->on_ready_to_read = [this] {
if (on_ready_to_accept) if (on_ready_to_accept)
on_ready_to_accept(); on_ready_to_accept();

View file

@ -14,11 +14,6 @@ public:
Exceptional = 4, Exceptional = 4,
}; };
static ObjectPtr<CNotifier> create(int fd, unsigned event_mask, CObject* parent = nullptr)
{
return new CNotifier(fd, event_mask, parent);
}
virtual ~CNotifier() override; virtual ~CNotifier() override;
void set_enabled(bool); void set_enabled(bool);
@ -33,7 +28,7 @@ public:
void event(CEvent&) override; void event(CEvent&) override;
private: private:
CNotifier(int fd, unsigned event_mask, CObject* parent); CNotifier(int fd, unsigned event_mask, CObject* parent = nullptr);
int m_fd { -1 }; int m_fd { -1 };
unsigned m_event_mask { 0 }; unsigned m_event_mask { 0 };

View file

@ -85,7 +85,7 @@ bool CSocket::common_connect(const struct sockaddr* addr, socklen_t addrlen)
if (rc < 0) { if (rc < 0) {
if (errno == EINPROGRESS) { if (errno == EINPROGRESS) {
dbg() << *this << " connection in progress (EINPROGRESS)"; dbg() << *this << " connection in progress (EINPROGRESS)";
m_notifier = CNotifier::create(fd(), CNotifier::Event::Write, this); m_notifier = CNotifier::construct(fd(), CNotifier::Event::Write, this);
m_notifier->on_ready_to_write = [this] { m_notifier->on_ready_to_write = [this] {
dbg() << *this << " connected!"; dbg() << *this << " connected!";
m_connected = true; m_connected = true;
@ -132,7 +132,7 @@ void CSocket::did_update_fd(int fd)
m_read_notifier = nullptr; m_read_notifier = nullptr;
return; return;
} }
m_read_notifier = CNotifier::create(fd, CNotifier::Event::Read, this); m_read_notifier = CNotifier::construct(fd, CNotifier::Event::Read, this);
m_read_notifier->on_ready_to_read = [this] { m_read_notifier->on_ready_to_read = [this] {
if (on_ready_to_read) if (on_ready_to_read)
on_ready_to_read(); on_ready_to_read();

View file

@ -32,7 +32,7 @@ bool CTCPServer::listen(const IPv4Address& address, u16 port)
ASSERT(rc == 0); ASSERT(rc == 0);
m_listening = true; m_listening = true;
m_notifier = CNotifier::create(m_fd, CNotifier::Event::Read); m_notifier = CNotifier::construct(m_fd, CNotifier::Event::Read);
m_notifier->on_ready_to_read = [this] { m_notifier->on_ready_to_read = [this] {
if (on_ready_to_accept) if (on_ready_to_accept)
on_ready_to_accept(); on_ready_to_accept();

View file

@ -9,7 +9,6 @@ class CTCPSocket;
class CTCPServer : public CObject { class CTCPServer : public CObject {
C_OBJECT(CTCPServer) C_OBJECT(CTCPServer)
public: public:
explicit CTCPServer(CObject* parent = nullptr);
virtual ~CTCPServer() override; virtual ~CTCPServer() override;
bool is_listening() const { return m_listening; } bool is_listening() const { return m_listening; }
@ -20,7 +19,9 @@ public:
Function<void()> on_ready_to_accept; Function<void()> on_ready_to_accept;
private: private:
explicit CTCPServer(CObject* parent = nullptr);
int m_fd { -1 }; int m_fd { -1 };
bool m_listening { false }; bool m_listening { false };
OwnPtr<CNotifier> m_notifier; ObjectPtr<CNotifier> m_notifier;
}; };

View file

@ -51,7 +51,7 @@ namespace Client {
public: public:
Connection(const StringView& address) Connection(const StringView& address)
: m_connection(CLocalSocket::construct(this)) : m_connection(CLocalSocket::construct(this))
, m_notifier(CNotifier::create(m_connection->fd(), CNotifier::Read, this)) , m_notifier(CNotifier::construct(m_connection->fd(), CNotifier::Read, this))
{ {
// We want to rate-limit our clients // We want to rate-limit our clients
m_connection->set_blocking(true); m_connection->set_blocking(true);
@ -241,7 +241,7 @@ namespace Client {
public: public:
ConnectionNG(const StringView& address) ConnectionNG(const StringView& address)
: m_connection(CLocalSocket::construct(this)) : m_connection(CLocalSocket::construct(this))
, m_notifier(CNotifier::create(m_connection->fd(), CNotifier::Read, this)) , m_notifier(CNotifier::construct(m_connection->fd(), CNotifier::Read, this))
{ {
// We want to rate-limit our clients // We want to rate-limit our clients
m_connection->set_blocking(true); m_connection->set_blocking(true);

View file

@ -344,7 +344,7 @@ void GDirectoryModel::open(const StringView& a_path)
perror("watch_file"); perror("watch_file");
ASSERT_NOT_REACHED(); ASSERT_NOT_REACHED();
} }
m_notifier = CNotifier::create(watch_fd, CNotifier::Event::Read); m_notifier = CNotifier::construct(watch_fd, CNotifier::Event::Read);
m_notifier->on_ready_to_read = [this] { m_notifier->on_ready_to_read = [this] {
update(); update();
char buffer[32]; char buffer[32];

View file

@ -14,7 +14,7 @@ Client::Client(int id, ObjectPtr<CTCPSocket> socket, int ptm_fd)
: m_id(id) : m_id(id)
, m_socket(move(socket)) , m_socket(move(socket))
, m_ptm_fd(ptm_fd) , m_ptm_fd(ptm_fd)
, m_ptm_notifier(CNotifier::create(ptm_fd, CNotifier::Read)) , m_ptm_notifier(CNotifier::construct(ptm_fd, CNotifier::Read))
{ {
m_socket->on_ready_to_read = [this] { drain_socket(); }; m_socket->on_ready_to_read = [this] { drain_socket(); };
m_ptm_notifier->on_ready_to_read = [this] { drain_pty(); }; m_ptm_notifier->on_ready_to_read = [this] { drain_pty(); };

View file

@ -1,3 +1,4 @@
#include "Client.h"
#include <AK/BufferStream.h> #include <AK/BufferStream.h>
#include <AK/ByteBuffer.h> #include <AK/ByteBuffer.h>
#include <AK/HashMap.h> #include <AK/HashMap.h>
@ -11,10 +12,9 @@
#include <fcntl.h> #include <fcntl.h>
#include <getopt.h> #include <getopt.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include "Client.h"
static void run_command(int ptm_fd, String command) static void run_command(int ptm_fd, String command)
{ {
pid_t pid = fork(); pid_t pid = fork();
@ -81,7 +81,7 @@ static void run_command(int ptm_fd, String command)
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
CEventLoop event_loop; CEventLoop event_loop;
CTCPServer server; auto server = CTCPServer::construct();
int opt; int opt;
u16 port = 23; u16 port = 23;
@ -96,7 +96,7 @@ int main(int argc, char** argv)
} }
} }
if (!server.listen({}, port)) { if (!server->listen({}, port)) {
perror("listen"); perror("listen");
exit(1); exit(1);
} }
@ -104,10 +104,10 @@ int main(int argc, char** argv)
HashMap<int, NonnullRefPtr<Client>> clients; HashMap<int, NonnullRefPtr<Client>> clients;
int next_id = 0; int next_id = 0;
server.on_ready_to_accept = [&next_id, &clients, &server] { server->on_ready_to_accept = [&next_id, &clients, &server] {
int id = next_id++; int id = next_id++;
auto client_socket = server.accept(); auto client_socket = server->accept();
if (!client_socket) { if (!client_socket) {
perror("accept"); perror("accept");
return; return;
@ -122,7 +122,7 @@ int main(int argc, char** argv)
run_command(ptm_fd, ""); run_command(ptm_fd, "");
auto client = Client::create(id, client_socket, ptm_fd); auto client = Client::create(id, move(client_socket), ptm_fd);
client->on_exit = [&clients, id] { clients.remove(id); }; client->on_exit = [&clients, id] { clients.remove(id); };
clients.set(id, client); clients.set(id, client);
}; };

View file

@ -44,10 +44,10 @@ WSEventLoop::WSEventLoop()
ASSERT(m_keyboard_fd >= 0); ASSERT(m_keyboard_fd >= 0);
ASSERT(m_mouse_fd >= 0); ASSERT(m_mouse_fd >= 0);
m_keyboard_notifier = CNotifier::create(m_keyboard_fd, CNotifier::Read); m_keyboard_notifier = CNotifier::construct(m_keyboard_fd, CNotifier::Read);
m_keyboard_notifier->on_ready_to_read = [this] { drain_keyboard(); }; m_keyboard_notifier->on_ready_to_read = [this] { drain_keyboard(); };
m_mouse_notifier = CNotifier::create(m_mouse_fd, CNotifier::Read); m_mouse_notifier = CNotifier::construct(m_mouse_fd, CNotifier::Read);
m_mouse_notifier->on_ready_to_read = [this] { drain_mouse(); }; m_mouse_notifier->on_ready_to_read = [this] { drain_mouse(); };
WSClipboard::the().on_content_change = [&] { WSClipboard::the().on_content_change = [&] {