mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 21:27:44 +00:00
LibCore: Convert CTCPSocket to ObjectPtr, add construct() helper
The C_OBJECT macro now also inserts a static construct(...) helper into the class. Now we can make the constructor(s) private and instead call: auto socket = CTCPSocket::construct(arguments); construct() returns an ObjectPtr<T>, which we'll later switch to being a NonnullRefPtr<T>, once everything else in in place for ref-counting.
This commit is contained in:
parent
1f5a9762a2
commit
4298ba25c3
15 changed files with 30 additions and 25 deletions
|
@ -35,7 +35,7 @@ IRCClient::IRCClient()
|
||||||
, m_log(IRCLogBuffer::create())
|
, m_log(IRCLogBuffer::create())
|
||||||
, m_config(CConfigFile::get_for_app("IRCClient"))
|
, m_config(CConfigFile::get_for_app("IRCClient"))
|
||||||
{
|
{
|
||||||
m_socket = new CTCPSocket(this);
|
m_socket = CTCPSocket::construct(this);
|
||||||
m_nickname = m_config->read_entry("User", "Nickname", "seren1ty");
|
m_nickname = m_config->read_entry("User", "Nickname", "seren1ty");
|
||||||
m_hostname = m_config->read_entry("Connection", "Server", "");
|
m_hostname = m_config->read_entry("Connection", "Server", "");
|
||||||
m_port = m_config->read_num_entry("Connection", "Port", 6667);
|
m_port = m_config->read_num_entry("Connection", "Port", 6667);
|
||||||
|
|
|
@ -137,7 +137,7 @@ private:
|
||||||
String m_hostname;
|
String m_hostname;
|
||||||
int m_port { 6667 };
|
int m_port { 6667 };
|
||||||
|
|
||||||
CTCPSocket* m_socket { nullptr };
|
ObjectPtr<CTCPSocket> m_socket;
|
||||||
|
|
||||||
String m_nickname;
|
String m_nickname;
|
||||||
ObjectPtr<CNotifier> m_notifier;
|
ObjectPtr<CNotifier> m_notifier;
|
||||||
|
|
|
@ -116,7 +116,7 @@ void CHttpJob::finish_up()
|
||||||
void CHttpJob::start()
|
void CHttpJob::start()
|
||||||
{
|
{
|
||||||
ASSERT(!m_socket);
|
ASSERT(!m_socket);
|
||||||
m_socket = new CTCPSocket(this);
|
m_socket = CTCPSocket::construct(this);
|
||||||
m_socket->on_connected = [this] {
|
m_socket->on_connected = [this] {
|
||||||
dbg() << "CHttpJob: on_connected callback";
|
dbg() << "CHttpJob: on_connected callback";
|
||||||
on_socket_connected();
|
on_socket_connected();
|
||||||
|
|
|
@ -26,7 +26,7 @@ private:
|
||||||
};
|
};
|
||||||
|
|
||||||
CHttpRequest m_request;
|
CHttpRequest m_request;
|
||||||
CTCPSocket* m_socket { nullptr };
|
ObjectPtr<CTCPSocket> m_socket;
|
||||||
State m_state { State::InStatus };
|
State m_state { State::InStatus };
|
||||||
int m_code { -1 };
|
int m_code { -1 };
|
||||||
HashMap<String, String> m_headers;
|
HashMap<String, String> m_headers;
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <AK/String.h>
|
|
||||||
#include <AK/Function.h>
|
#include <AK/Function.h>
|
||||||
#include <AK/IntrusiveList.h>
|
#include <AK/IntrusiveList.h>
|
||||||
#include <AK/StdLibExtras.h>
|
#include <AK/StdLibExtras.h>
|
||||||
|
#include <AK/String.h>
|
||||||
#include <AK/Vector.h>
|
#include <AK/Vector.h>
|
||||||
#include <AK/Weakable.h>
|
#include <AK/Weakable.h>
|
||||||
|
#include <LibCore/ObjectPtr.h>
|
||||||
|
|
||||||
namespace AK {
|
namespace AK {
|
||||||
class JsonObject;
|
class JsonObject;
|
||||||
|
@ -16,9 +17,14 @@ class CChildEvent;
|
||||||
class CCustomEvent;
|
class CCustomEvent;
|
||||||
class CTimerEvent;
|
class CTimerEvent;
|
||||||
|
|
||||||
#define C_OBJECT(klass) \
|
#define C_OBJECT(klass) \
|
||||||
public: \
|
public: \
|
||||||
virtual const char* class_name() const override { return #klass; }
|
virtual const char* class_name() const override { return #klass; } \
|
||||||
|
template<class... Args> \
|
||||||
|
static inline ObjectPtr<klass> construct(Args&&... args) \
|
||||||
|
{ \
|
||||||
|
return ObjectPtr<klass>(new klass(forward<Args>(args)...)); \
|
||||||
|
}
|
||||||
|
|
||||||
class CObject : public Weakable<CObject> {
|
class CObject : public Weakable<CObject> {
|
||||||
// NOTE: No C_OBJECT macro for CObject itself.
|
// NOTE: No C_OBJECT macro for CObject itself.
|
||||||
|
|
|
@ -40,7 +40,7 @@ bool CTCPServer::listen(const IPv4Address& address, u16 port)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
CTCPSocket* CTCPServer::accept()
|
ObjectPtr<CTCPSocket> CTCPServer::accept()
|
||||||
{
|
{
|
||||||
ASSERT(m_listening);
|
ASSERT(m_listening);
|
||||||
sockaddr_in in;
|
sockaddr_in in;
|
||||||
|
@ -51,5 +51,5 @@ CTCPSocket* CTCPServer::accept()
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
return new CTCPSocket({}, accepted_fd);
|
return CTCPSocket::construct(accepted_fd);
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,7 +15,7 @@ public:
|
||||||
bool is_listening() const { return m_listening; }
|
bool is_listening() const { return m_listening; }
|
||||||
bool listen(const IPv4Address& address, u16 port);
|
bool listen(const IPv4Address& address, u16 port);
|
||||||
|
|
||||||
CTCPSocket* accept();
|
ObjectPtr<CTCPSocket> accept();
|
||||||
|
|
||||||
Function<void()> on_ready_to_accept;
|
Function<void()> on_ready_to_accept;
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
CTCPSocket::CTCPSocket(Badge<CTCPServer>, int fd, CObject* parent)
|
CTCPSocket::CTCPSocket(int fd, CObject* parent)
|
||||||
: CSocket(CSocket::Type::TCP, parent)
|
: CSocket(CSocket::Type::TCP, parent)
|
||||||
{
|
{
|
||||||
set_fd(fd);
|
set_fd(fd);
|
||||||
|
|
|
@ -8,7 +8,9 @@ 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);
|
|
||||||
CTCPSocket(Badge<CTCPServer>, int fd, CObject* parent = nullptr);
|
|
||||||
virtual ~CTCPSocket() override;
|
virtual ~CTCPSocket() override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
CTCPSocket(int fd, CObject* parent = nullptr);
|
||||||
|
explicit CTCPSocket(CObject* parent = nullptr);
|
||||||
};
|
};
|
||||||
|
|
|
@ -47,7 +47,6 @@ namespace Client {
|
||||||
|
|
||||||
template<typename ServerMessage, typename ClientMessage>
|
template<typename ServerMessage, typename ClientMessage>
|
||||||
class Connection : public CObject {
|
class Connection : public CObject {
|
||||||
C_OBJECT(Connection)
|
|
||||||
public:
|
public:
|
||||||
Connection(const StringView& address)
|
Connection(const StringView& address)
|
||||||
: m_connection(this)
|
: m_connection(this)
|
||||||
|
@ -238,7 +237,6 @@ namespace Client {
|
||||||
|
|
||||||
template<typename Endpoint>
|
template<typename Endpoint>
|
||||||
class ConnectionNG : public CObject {
|
class ConnectionNG : public CObject {
|
||||||
C_OBJECT(Connection)
|
|
||||||
public:
|
public:
|
||||||
ConnectionNG(const StringView& address)
|
ConnectionNG(const StringView& address)
|
||||||
: m_connection(this)
|
: m_connection(this)
|
||||||
|
|
|
@ -64,7 +64,6 @@ namespace Server {
|
||||||
|
|
||||||
template<typename ServerMessage, typename ClientMessage>
|
template<typename ServerMessage, typename ClientMessage>
|
||||||
class Connection : public CObject {
|
class Connection : public CObject {
|
||||||
C_OBJECT(Connection)
|
|
||||||
public:
|
public:
|
||||||
Connection(CLocalSocket& socket, int client_id)
|
Connection(CLocalSocket& socket, int client_id)
|
||||||
: m_socket(socket)
|
: m_socket(socket)
|
||||||
|
@ -206,7 +205,6 @@ namespace Server {
|
||||||
|
|
||||||
template<typename Endpoint>
|
template<typename Endpoint>
|
||||||
class ConnectionNG : public CObject {
|
class ConnectionNG : public CObject {
|
||||||
C_OBJECT(Connection)
|
|
||||||
public:
|
public:
|
||||||
ConnectionNG(Endpoint& endpoint, CLocalSocket& socket, int client_id)
|
ConnectionNG(Endpoint& endpoint, CLocalSocket& socket, int client_id)
|
||||||
: m_endpoint(endpoint)
|
: m_endpoint(endpoint)
|
||||||
|
|
|
@ -11,6 +11,7 @@ class CNotifier;
|
||||||
class GWindow;
|
class GWindow;
|
||||||
|
|
||||||
class GWindowServerConnection : public IPC::Client::Connection<WSAPI_ServerMessage, WSAPI_ClientMessage> {
|
class GWindowServerConnection : public IPC::Client::Connection<WSAPI_ServerMessage, WSAPI_ClientMessage> {
|
||||||
|
C_OBJECT(GWindowServerConnection)
|
||||||
public:
|
public:
|
||||||
GWindowServerConnection()
|
GWindowServerConnection()
|
||||||
: Connection("/tmp/wsportal")
|
: Connection("/tmp/wsportal")
|
||||||
|
|
|
@ -10,9 +10,9 @@
|
||||||
|
|
||||||
#include "Client.h"
|
#include "Client.h"
|
||||||
|
|
||||||
Client::Client(int id, CTCPSocket* socket, int ptm_fd)
|
Client::Client(int id, ObjectPtr<CTCPSocket> socket, int ptm_fd)
|
||||||
: m_id(id)
|
: m_id(id)
|
||||||
, m_socket(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::create(ptm_fd, CNotifier::Read))
|
||||||
{
|
{
|
||||||
|
|
|
@ -11,15 +11,15 @@
|
||||||
|
|
||||||
class Client : public RefCounted<Client> {
|
class Client : public RefCounted<Client> {
|
||||||
public:
|
public:
|
||||||
static NonnullRefPtr<Client> create(int id, CTCPSocket* socket, int ptm_fd)
|
static NonnullRefPtr<Client> create(int id, ObjectPtr<CTCPSocket> socket, int ptm_fd)
|
||||||
{
|
{
|
||||||
return adopt(*new Client(id, socket, ptm_fd));
|
return adopt(*new Client(id, move(socket), ptm_fd));
|
||||||
}
|
}
|
||||||
|
|
||||||
Function<void()> on_exit;
|
Function<void()> on_exit;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Client(int id, CTCPSocket* socket, int ptm_fd);
|
Client(int id, ObjectPtr<CTCPSocket> socket, int ptm_fd);
|
||||||
|
|
||||||
void drain_socket();
|
void drain_socket();
|
||||||
void drain_pty();
|
void drain_pty();
|
||||||
|
@ -35,7 +35,7 @@ private:
|
||||||
// client id
|
// client id
|
||||||
int m_id { 0 };
|
int m_id { 0 };
|
||||||
// client resources
|
// client resources
|
||||||
CTCPSocket* m_socket { nullptr };
|
ObjectPtr<CTCPSocket> m_socket;
|
||||||
Parser m_parser;
|
Parser m_parser;
|
||||||
// pty resources
|
// pty resources
|
||||||
int m_ptm_fd { -1 };
|
int m_ptm_fd { -1 };
|
||||||
|
|
|
@ -107,7 +107,7 @@ int main(int argc, char** argv)
|
||||||
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;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue