mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-01-04 09:14:21 +03:00
LibCore: Move GIODevice hierarchy from LibGUI to LibCore.
This commit is contained in:
parent
fc1d3074de
commit
cfd6e6cc36
Notes:
sideshowbarker
2024-07-19 14:46:07 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/cfd6e6cc368
@ -32,7 +32,7 @@ IRCClient::IRCClient()
|
||||
, m_client_window_list_model(IRCWindowListModel::create(*this))
|
||||
, m_log(IRCLogBuffer::create())
|
||||
{
|
||||
m_socket = new GTCPSocket(this);
|
||||
m_socket = new CTCPSocket(this);
|
||||
}
|
||||
|
||||
IRCClient::~IRCClient()
|
||||
|
@ -4,7 +4,7 @@
|
||||
#include <AK/HashMap.h>
|
||||
#include <AK/CircularQueue.h>
|
||||
#include <AK/Function.h>
|
||||
#include <LibGUI/GTCPSocket.h>
|
||||
#include <LibCore/CTCPSocket.h>
|
||||
#include "IRCLogBuffer.h"
|
||||
#include "IRCWindow.h"
|
||||
|
||||
@ -114,7 +114,7 @@ private:
|
||||
String m_hostname;
|
||||
int m_port { 6667 };
|
||||
|
||||
GTCPSocket* m_socket { nullptr };
|
||||
CTCPSocket* m_socket { nullptr };
|
||||
|
||||
String m_nickname;
|
||||
Vector<char> m_line_buffer;
|
||||
|
@ -1,5 +1,5 @@
|
||||
#include "ProcessModel.h"
|
||||
#include <LibGUI/GFile.h>
|
||||
#include <LibCore/CFile.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <pwd.h>
|
||||
@ -125,8 +125,8 @@ GVariant ProcessModel::data(const GModelIndex& index, Role role) const
|
||||
|
||||
void ProcessModel::update()
|
||||
{
|
||||
GFile file("/proc/all");
|
||||
if (!file.open(GIODevice::ReadOnly)) {
|
||||
CFile file("/proc/all");
|
||||
if (!file.open(CIODevice::ReadOnly)) {
|
||||
fprintf(stderr, "ProcessManager: Failed to open /proc/all: %s\n", file.error_string());
|
||||
exit(1);
|
||||
return;
|
||||
|
@ -8,7 +8,7 @@
|
||||
#include <LibGUI/GTextEditor.h>
|
||||
#include <LibGUI/GAction.h>
|
||||
#include <LibGUI/GFontDatabase.h>
|
||||
#include <LibGUI/GFile.h>
|
||||
#include <LibCore/CFile.h>
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
@ -35,9 +35,9 @@ int main(int argc, char** argv)
|
||||
String path = "/tmp/TextEditor.save.txt";
|
||||
if (argc >= 2) {
|
||||
path = argv[1];
|
||||
GFile file(path);
|
||||
CFile file(path);
|
||||
|
||||
if (!file.open(GIODevice::ReadOnly)) {
|
||||
if (!file.open(CIODevice::ReadOnly)) {
|
||||
fprintf(stderr, "Opening %s: %s\n", path.characters(), file.error_string());
|
||||
return 1;
|
||||
}
|
||||
|
@ -1,34 +1,34 @@
|
||||
#include <LibGUI/GFile.h>
|
||||
#include <LibCore/CFile.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
|
||||
GFile::GFile(const String& filename)
|
||||
CFile::CFile(const String& filename)
|
||||
: m_filename(filename)
|
||||
{
|
||||
}
|
||||
|
||||
GFile::~GFile()
|
||||
CFile::~CFile()
|
||||
{
|
||||
if (mode() != NotOpen)
|
||||
close();
|
||||
}
|
||||
|
||||
bool GFile::open(GIODevice::OpenMode mode)
|
||||
bool CFile::open(CIODevice::OpenMode mode)
|
||||
{
|
||||
int flags = 0;
|
||||
if ((mode & GIODevice::ReadWrite) == GIODevice::ReadWrite) {
|
||||
if ((mode & CIODevice::ReadWrite) == CIODevice::ReadWrite) {
|
||||
flags |= O_RDWR | O_CREAT;
|
||||
} else if (mode & GIODevice::ReadOnly) {
|
||||
} else if (mode & CIODevice::ReadOnly) {
|
||||
flags |= O_RDONLY;
|
||||
} else if (mode & GIODevice::WriteOnly) {
|
||||
} else if (mode & CIODevice::WriteOnly) {
|
||||
flags |= O_WRONLY | O_CREAT;
|
||||
}
|
||||
if (mode & GIODevice::Append)
|
||||
if (mode & CIODevice::Append)
|
||||
flags |= O_APPEND;
|
||||
if (mode & GIODevice::Truncate)
|
||||
if (mode & CIODevice::Truncate)
|
||||
flags |= O_TRUNC;
|
||||
if (mode & GIODevice::MustBeNew)
|
||||
if (mode & CIODevice::MustBeNew)
|
||||
flags |= O_EXCL;
|
||||
int fd = ::open(m_filename.characters(), flags, 0666);
|
||||
if (fd < 0) {
|
21
LibCore/CFile.h
Normal file
21
LibCore/CFile.h
Normal file
@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include <LibCore/CIODevice.h>
|
||||
#include <AK/AKString.h>
|
||||
|
||||
class CFile final : public CIODevice {
|
||||
public:
|
||||
CFile() { }
|
||||
explicit CFile(const String&);
|
||||
virtual ~CFile() override;
|
||||
|
||||
String filename() const { return m_filename; }
|
||||
void set_filename(const String& filename) { m_filename = filename; }
|
||||
|
||||
virtual bool open(CIODevice::OpenMode) override;
|
||||
|
||||
virtual const char* class_name() const override { return "CFile"; }
|
||||
|
||||
private:
|
||||
String m_filename;
|
||||
};
|
@ -1,23 +1,23 @@
|
||||
#include <LibGUI/GIODevice.h>
|
||||
#include <LibCore/CIODevice.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/select.h>
|
||||
#include <stdio.h>
|
||||
|
||||
GIODevice::GIODevice(CObject* parent)
|
||||
CIODevice::CIODevice(CObject* parent)
|
||||
: CObject(parent)
|
||||
{
|
||||
}
|
||||
|
||||
GIODevice::~GIODevice()
|
||||
CIODevice::~CIODevice()
|
||||
{
|
||||
}
|
||||
|
||||
const char* GIODevice::error_string() const
|
||||
const char* CIODevice::error_string() const
|
||||
{
|
||||
return strerror(m_error);
|
||||
}
|
||||
|
||||
ByteBuffer GIODevice::read(int max_size)
|
||||
ByteBuffer CIODevice::read(int max_size)
|
||||
{
|
||||
if (m_fd < 0)
|
||||
return { };
|
||||
@ -50,9 +50,9 @@ ByteBuffer GIODevice::read(int max_size)
|
||||
return buffer;
|
||||
}
|
||||
|
||||
bool GIODevice::can_read_from_fd() const
|
||||
bool CIODevice::can_read_from_fd() const
|
||||
{
|
||||
// FIXME: Can we somehow remove this once GSocket is implemented using non-blocking sockets?
|
||||
// FIXME: Can we somehow remove this once CSocket is implemented using non-blocking sockets?
|
||||
fd_set rfds;
|
||||
FD_ZERO(&rfds);
|
||||
FD_SET(m_fd, &rfds);
|
||||
@ -60,13 +60,13 @@ bool GIODevice::can_read_from_fd() const
|
||||
int rc = select(m_fd + 1, &rfds, nullptr, nullptr, &timeout);
|
||||
if (rc < 0) {
|
||||
// NOTE: We don't set m_error here.
|
||||
perror("GIODevice::can_read: select");
|
||||
perror("CIODevice::can_read: select");
|
||||
return false;
|
||||
}
|
||||
return FD_ISSET(m_fd, &rfds);
|
||||
}
|
||||
|
||||
bool GIODevice::can_read_line()
|
||||
bool CIODevice::can_read_line()
|
||||
{
|
||||
if (m_eof && !m_buffered_data.is_empty())
|
||||
return true;
|
||||
@ -78,12 +78,12 @@ bool GIODevice::can_read_line()
|
||||
return m_buffered_data.contains_slow('\n');
|
||||
}
|
||||
|
||||
bool GIODevice::can_read() const
|
||||
bool CIODevice::can_read() const
|
||||
{
|
||||
return !m_buffered_data.is_empty() || can_read_from_fd();
|
||||
}
|
||||
|
||||
ByteBuffer GIODevice::read_all()
|
||||
ByteBuffer CIODevice::read_all()
|
||||
{
|
||||
ByteBuffer buffer;
|
||||
if (!m_buffered_data.is_empty()) {
|
||||
@ -107,7 +107,7 @@ ByteBuffer GIODevice::read_all()
|
||||
return buffer;
|
||||
}
|
||||
|
||||
ByteBuffer GIODevice::read_line(int max_size)
|
||||
ByteBuffer CIODevice::read_line(int max_size)
|
||||
{
|
||||
if (m_fd < 0)
|
||||
return { };
|
||||
@ -117,7 +117,7 @@ ByteBuffer GIODevice::read_line(int max_size)
|
||||
return { };
|
||||
if (m_eof) {
|
||||
if (m_buffered_data.size() > max_size) {
|
||||
printf("GIODevice::read_line: At EOF but there's more than max_size(%d) buffered\n", max_size);
|
||||
printf("CIODevice::read_line: At EOF but there's more than max_size(%d) buffered\n", max_size);
|
||||
return { };
|
||||
}
|
||||
auto buffer = ByteBuffer::copy(m_buffered_data.data(), m_buffered_data.size());
|
||||
@ -141,7 +141,7 @@ ByteBuffer GIODevice::read_line(int max_size)
|
||||
return { };
|
||||
}
|
||||
|
||||
bool GIODevice::populate_read_buffer()
|
||||
bool CIODevice::populate_read_buffer()
|
||||
{
|
||||
if (m_fd < 0)
|
||||
return false;
|
||||
@ -160,7 +160,7 @@ bool GIODevice::populate_read_buffer()
|
||||
return true;
|
||||
}
|
||||
|
||||
bool GIODevice::close()
|
||||
bool CIODevice::close()
|
||||
{
|
||||
if (fd() < 0 || mode() == NotOpen)
|
||||
return false;
|
||||
@ -170,6 +170,6 @@ bool GIODevice::close()
|
||||
return false;
|
||||
}
|
||||
set_fd(-1);
|
||||
set_mode(GIODevice::NotOpen);
|
||||
set_mode(CIODevice::NotOpen);
|
||||
return true;
|
||||
}
|
@ -3,7 +3,7 @@
|
||||
#include <LibCore/CObject.h>
|
||||
#include <AK/ByteBuffer.h>
|
||||
|
||||
class GIODevice : public CObject {
|
||||
class CIODevice : public CObject {
|
||||
public:
|
||||
enum OpenMode {
|
||||
NotOpen = 0,
|
||||
@ -15,7 +15,7 @@ public:
|
||||
MustBeNew = 16,
|
||||
};
|
||||
|
||||
virtual ~GIODevice() override;
|
||||
virtual ~CIODevice() override;
|
||||
|
||||
int fd() const { return m_fd; }
|
||||
unsigned mode() const { return m_mode; }
|
||||
@ -35,13 +35,13 @@ public:
|
||||
|
||||
bool can_read() const;
|
||||
|
||||
virtual bool open(GIODevice::OpenMode) = 0;
|
||||
virtual bool open(CIODevice::OpenMode) = 0;
|
||||
virtual bool close();
|
||||
|
||||
virtual const char* class_name() const override { return "GIODevice"; }
|
||||
virtual const char* class_name() const override { return "CIODevice"; }
|
||||
|
||||
protected:
|
||||
explicit GIODevice(CObject* parent = nullptr);
|
||||
explicit CIODevice(CObject* parent = nullptr);
|
||||
|
||||
void set_fd(int fd) { m_fd = fd; }
|
||||
void set_mode(OpenMode mode) { m_mode = mode; }
|
@ -1,4 +1,4 @@
|
||||
#include <LibGUI/GSocket.h>
|
||||
#include <LibCore/CSocket.h>
|
||||
#include <LibCore/CNotifier.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
@ -8,33 +8,33 @@
|
||||
#include <netdb.h>
|
||||
#include <errno.h>
|
||||
|
||||
GSocket::GSocket(Type type, CObject* parent)
|
||||
: GIODevice(parent)
|
||||
CSocket::CSocket(Type type, CObject* parent)
|
||||
: CIODevice(parent)
|
||||
, m_type(type)
|
||||
{
|
||||
}
|
||||
|
||||
GSocket::~GSocket()
|
||||
CSocket::~CSocket()
|
||||
{
|
||||
}
|
||||
|
||||
bool GSocket::connect(const String& hostname, int port)
|
||||
bool CSocket::connect(const String& hostname, int port)
|
||||
{
|
||||
auto* hostent = gethostbyname(hostname.characters());
|
||||
if (!hostent) {
|
||||
dbgprintf("GSocket::connect: Unable to resolve '%s'\n", hostname.characters());
|
||||
dbgprintf("CSocket::connect: Unable to resolve '%s'\n", hostname.characters());
|
||||
return false;
|
||||
}
|
||||
|
||||
IPv4Address host_address((const byte*)hostent->h_addr_list[0]);
|
||||
dbgprintf("GSocket::connect: Resolved '%s' to %s\n", hostname.characters(), host_address.to_string().characters());
|
||||
dbgprintf("CSocket::connect: Resolved '%s' to %s\n", hostname.characters(), host_address.to_string().characters());
|
||||
return connect(host_address, port);
|
||||
}
|
||||
|
||||
bool GSocket::connect(const GSocketAddress& address, int port)
|
||||
bool CSocket::connect(const CSocketAddress& address, int port)
|
||||
{
|
||||
ASSERT(!is_connected());
|
||||
ASSERT(address.type() == GSocketAddress::Type::IPv4);
|
||||
ASSERT(address.type() == CSocketAddress::Type::IPv4);
|
||||
ASSERT(port > 0 && port <= 65535);
|
||||
|
||||
struct sockaddr_in addr;
|
||||
@ -71,17 +71,17 @@ bool GSocket::connect(const GSocketAddress& address, int port)
|
||||
return true;
|
||||
}
|
||||
|
||||
ByteBuffer GSocket::receive(int max_size)
|
||||
ByteBuffer CSocket::receive(int max_size)
|
||||
{
|
||||
auto buffer = read(max_size);
|
||||
if (eof()) {
|
||||
dbgprintf("GSocket{%p}: Connection appears to have closed in receive().\n", this);
|
||||
dbgprintf("CSocket{%p}: Connection appears to have closed in receive().\n", this);
|
||||
m_connected = false;
|
||||
}
|
||||
return buffer;
|
||||
}
|
||||
|
||||
bool GSocket::send(const ByteBuffer& data)
|
||||
bool CSocket::send(const ByteBuffer& data)
|
||||
{
|
||||
int nsent = ::send(fd(), data.pointer(), data.size(), 0);
|
||||
if (nsent < 0) {
|
@ -1,44 +1,44 @@
|
||||
#pragma once
|
||||
|
||||
#include <LibGUI/GIODevice.h>
|
||||
#include <LibGUI/GSocketAddress.h>
|
||||
#include <LibCore/CIODevice.h>
|
||||
#include <LibCore/CSocketAddress.h>
|
||||
|
||||
class CNotifier;
|
||||
|
||||
class GSocket : public GIODevice {
|
||||
class CSocket : public CIODevice {
|
||||
public:
|
||||
enum class Type { Invalid, TCP, UDP };
|
||||
virtual ~GSocket() override;
|
||||
virtual ~CSocket() override;
|
||||
|
||||
bool connect(const String& hostname, int port);
|
||||
bool connect(const GSocketAddress&, int port);
|
||||
bool connect(const CSocketAddress&, int port);
|
||||
|
||||
ByteBuffer receive(int max_size);
|
||||
bool send(const ByteBuffer&);
|
||||
|
||||
bool is_connected() const { return m_connected; }
|
||||
|
||||
GSocketAddress source_address() const { return m_source_address; }
|
||||
CSocketAddress source_address() const { return m_source_address; }
|
||||
int source_port() const { return m_source_port; }
|
||||
|
||||
GSocketAddress destination_address() const { return m_source_address; }
|
||||
CSocketAddress destination_address() const { return m_source_address; }
|
||||
int destination_port() const { return m_destination_port; }
|
||||
|
||||
Function<void()> on_connected;
|
||||
|
||||
virtual const char* class_name() const override { return "GSocket"; }
|
||||
virtual const char* class_name() const override { return "CSocket"; }
|
||||
|
||||
protected:
|
||||
GSocket(Type, CObject* parent);
|
||||
CSocket(Type, CObject* parent);
|
||||
|
||||
GSocketAddress m_source_address;
|
||||
GSocketAddress m_destination_address;
|
||||
CSocketAddress m_source_address;
|
||||
CSocketAddress m_destination_address;
|
||||
int m_source_port { -1 };
|
||||
int m_destination_port { -1 };
|
||||
bool m_connected { false };
|
||||
|
||||
private:
|
||||
virtual bool open(GIODevice::OpenMode) override { ASSERT_NOT_REACHED(); }
|
||||
virtual bool open(CIODevice::OpenMode) override { ASSERT_NOT_REACHED(); }
|
||||
Type m_type { Type::Invalid };
|
||||
OwnPtr<CNotifier> m_notifier;
|
||||
};
|
@ -2,12 +2,12 @@
|
||||
|
||||
#include <Kernel/Net/IPv4.h>
|
||||
|
||||
class GSocketAddress {
|
||||
class CSocketAddress {
|
||||
public:
|
||||
enum class Type { Invalid, IPv4, Local };
|
||||
|
||||
GSocketAddress() { }
|
||||
GSocketAddress(const IPv4Address& address)
|
||||
CSocketAddress() { }
|
||||
CSocketAddress(const IPv4Address& address)
|
||||
: m_type(Type::IPv4)
|
||||
, m_ipv4_address(address)
|
||||
{
|
||||
@ -21,7 +21,7 @@ public:
|
||||
{
|
||||
switch (m_type) {
|
||||
case Type::IPv4: return m_ipv4_address.to_string();
|
||||
default: return "[GSocketAddress]";
|
||||
default: return "[CSocketAddress]";
|
||||
}
|
||||
}
|
||||
|
@ -1,19 +1,19 @@
|
||||
#include <LibGUI/GTCPSocket.h>
|
||||
#include <LibCore/CTCPSocket.h>
|
||||
#include <sys/socket.h>
|
||||
|
||||
GTCPSocket::GTCPSocket(CObject* parent)
|
||||
: GSocket(GSocket::Type::TCP, parent)
|
||||
CTCPSocket::CTCPSocket(CObject* parent)
|
||||
: CSocket(CSocket::Type::TCP, parent)
|
||||
{
|
||||
int fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
|
||||
if (fd < 0) {
|
||||
set_error(fd);
|
||||
} else {
|
||||
set_fd(fd);
|
||||
set_mode(GIODevice::ReadWrite);
|
||||
set_mode(CIODevice::ReadWrite);
|
||||
set_error(0);
|
||||
}
|
||||
}
|
||||
|
||||
GTCPSocket::~GTCPSocket()
|
||||
CTCPSocket::~CTCPSocket()
|
||||
{
|
||||
}
|
10
LibCore/CTCPSocket.h
Normal file
10
LibCore/CTCPSocket.h
Normal file
@ -0,0 +1,10 @@
|
||||
#include <LibCore/CSocket.h>
|
||||
|
||||
class CTCPSocket final : public CSocket {
|
||||
public:
|
||||
explicit CTCPSocket(CObject* parent = nullptr);
|
||||
virtual ~CTCPSocket() override;
|
||||
|
||||
private:
|
||||
};
|
||||
|
@ -1,4 +1,8 @@
|
||||
OBJS = \
|
||||
CIODevice.o \
|
||||
CFile.o \
|
||||
CSocket.o \
|
||||
CTCPSocket.o \
|
||||
CElapsedTimer.o \
|
||||
CNotifier.o \
|
||||
CObject.o \
|
||||
|
@ -1,21 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <LibGUI/GIODevice.h>
|
||||
#include <AK/AKString.h>
|
||||
|
||||
class GFile final : public GIODevice {
|
||||
public:
|
||||
GFile() { }
|
||||
explicit GFile(const String&);
|
||||
virtual ~GFile() override;
|
||||
|
||||
String filename() const { return m_filename; }
|
||||
void set_filename(const String& filename) { m_filename = filename; }
|
||||
|
||||
virtual bool open(GIODevice::OpenMode) override;
|
||||
|
||||
virtual const char* class_name() const override { return "GFile"; }
|
||||
|
||||
private:
|
||||
String m_filename;
|
||||
};
|
@ -1,6 +1,6 @@
|
||||
#include <LibGUI/GHttpJob.h>
|
||||
#include <LibGUI/GHttpResponse.h>
|
||||
#include <LibGUI/GTCPSocket.h>
|
||||
#include <LibCore/CTCPSocket.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
@ -98,7 +98,7 @@ void GHttpJob::on_socket_connected()
|
||||
void GHttpJob::start()
|
||||
{
|
||||
ASSERT(!m_socket);
|
||||
m_socket = new GTCPSocket(this);
|
||||
m_socket = new CTCPSocket(this);
|
||||
m_socket->on_connected = [this] {
|
||||
printf("Socket on_connected callback\n");
|
||||
on_socket_connected();
|
||||
|
@ -4,7 +4,7 @@
|
||||
#include <LibGUI/GHttpRequest.h>
|
||||
#include <AK/HashMap.h>
|
||||
|
||||
class GTCPSocket;
|
||||
class CTCPSocket;
|
||||
|
||||
class GHttpJob final : public GNetworkJob {
|
||||
public:
|
||||
@ -26,7 +26,7 @@ private:
|
||||
};
|
||||
|
||||
GHttpRequest m_request;
|
||||
GTCPSocket* m_socket { nullptr };
|
||||
CTCPSocket* m_socket { nullptr };
|
||||
State m_state { State::InStatus };
|
||||
int m_code { -1 };
|
||||
HashMap<String, String> m_headers;
|
||||
|
@ -1,10 +0,0 @@
|
||||
#include <LibGUI/GSocket.h>
|
||||
|
||||
class GTCPSocket final : public GSocket {
|
||||
public:
|
||||
explicit GTCPSocket(CObject* parent);
|
||||
virtual ~GTCPSocket() override;
|
||||
|
||||
private:
|
||||
};
|
||||
|
@ -10,8 +10,6 @@ SHAREDGRAPHICS_OBJS = \
|
||||
|
||||
LIBGUI_OBJS = \
|
||||
GPainter.o \
|
||||
GIODevice.o \
|
||||
GFile.o \
|
||||
GButton.o \
|
||||
GCheckBox.o \
|
||||
GEventLoop.o \
|
||||
@ -39,8 +37,6 @@ LIBGUI_OBJS = \
|
||||
GSortingProxyModel.o \
|
||||
GStackWidget.o \
|
||||
GScrollableWidget.o \
|
||||
GSocket.o \
|
||||
GTCPSocket.o \
|
||||
GMessageBox.o \
|
||||
GInputBox.o \
|
||||
GDialog.o \
|
||||
|
Loading…
Reference in New Issue
Block a user