1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 10:38:11 +00:00

LibCore: Move GIODevice hierarchy from LibGUI to LibCore.

This commit is contained in:
Andreas Kling 2019-04-10 20:22:23 +02:00
parent fc1d3074de
commit cfd6e6cc36
19 changed files with 112 additions and 112 deletions

View file

@ -32,7 +32,7 @@ IRCClient::IRCClient()
, m_client_window_list_model(IRCWindowListModel::create(*this)) , m_client_window_list_model(IRCWindowListModel::create(*this))
, m_log(IRCLogBuffer::create()) , m_log(IRCLogBuffer::create())
{ {
m_socket = new GTCPSocket(this); m_socket = new CTCPSocket(this);
} }
IRCClient::~IRCClient() IRCClient::~IRCClient()

View file

@ -4,7 +4,7 @@
#include <AK/HashMap.h> #include <AK/HashMap.h>
#include <AK/CircularQueue.h> #include <AK/CircularQueue.h>
#include <AK/Function.h> #include <AK/Function.h>
#include <LibGUI/GTCPSocket.h> #include <LibCore/CTCPSocket.h>
#include "IRCLogBuffer.h" #include "IRCLogBuffer.h"
#include "IRCWindow.h" #include "IRCWindow.h"
@ -114,7 +114,7 @@ private:
String m_hostname; String m_hostname;
int m_port { 6667 }; int m_port { 6667 };
GTCPSocket* m_socket { nullptr }; CTCPSocket* m_socket { nullptr };
String m_nickname; String m_nickname;
Vector<char> m_line_buffer; Vector<char> m_line_buffer;

View file

@ -1,5 +1,5 @@
#include "ProcessModel.h" #include "ProcessModel.h"
#include <LibGUI/GFile.h> #include <LibCore/CFile.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h> #include <stdio.h>
#include <pwd.h> #include <pwd.h>
@ -125,8 +125,8 @@ GVariant ProcessModel::data(const GModelIndex& index, Role role) const
void ProcessModel::update() void ProcessModel::update()
{ {
GFile file("/proc/all"); CFile file("/proc/all");
if (!file.open(GIODevice::ReadOnly)) { if (!file.open(CIODevice::ReadOnly)) {
fprintf(stderr, "ProcessManager: Failed to open /proc/all: %s\n", file.error_string()); fprintf(stderr, "ProcessManager: Failed to open /proc/all: %s\n", file.error_string());
exit(1); exit(1);
return; return;

View file

@ -8,7 +8,7 @@
#include <LibGUI/GTextEditor.h> #include <LibGUI/GTextEditor.h>
#include <LibGUI/GAction.h> #include <LibGUI/GAction.h>
#include <LibGUI/GFontDatabase.h> #include <LibGUI/GFontDatabase.h>
#include <LibGUI/GFile.h> #include <LibCore/CFile.h>
#include <AK/StringBuilder.h> #include <AK/StringBuilder.h>
#include <unistd.h> #include <unistd.h>
#include <stdio.h> #include <stdio.h>
@ -35,9 +35,9 @@ int main(int argc, char** argv)
String path = "/tmp/TextEditor.save.txt"; String path = "/tmp/TextEditor.save.txt";
if (argc >= 2) { if (argc >= 2) {
path = argv[1]; 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()); fprintf(stderr, "Opening %s: %s\n", path.characters(), file.error_string());
return 1; return 1;
} }

View file

@ -1,34 +1,34 @@
#include <LibGUI/GFile.h> #include <LibCore/CFile.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h> #include <stdio.h>
GFile::GFile(const String& filename) CFile::CFile(const String& filename)
: m_filename(filename) : m_filename(filename)
{ {
} }
GFile::~GFile() CFile::~CFile()
{ {
if (mode() != NotOpen) if (mode() != NotOpen)
close(); close();
} }
bool GFile::open(GIODevice::OpenMode mode) bool CFile::open(CIODevice::OpenMode mode)
{ {
int flags = 0; int flags = 0;
if ((mode & GIODevice::ReadWrite) == GIODevice::ReadWrite) { if ((mode & CIODevice::ReadWrite) == CIODevice::ReadWrite) {
flags |= O_RDWR | O_CREAT; flags |= O_RDWR | O_CREAT;
} else if (mode & GIODevice::ReadOnly) { } else if (mode & CIODevice::ReadOnly) {
flags |= O_RDONLY; flags |= O_RDONLY;
} else if (mode & GIODevice::WriteOnly) { } else if (mode & CIODevice::WriteOnly) {
flags |= O_WRONLY | O_CREAT; flags |= O_WRONLY | O_CREAT;
} }
if (mode & GIODevice::Append) if (mode & CIODevice::Append)
flags |= O_APPEND; flags |= O_APPEND;
if (mode & GIODevice::Truncate) if (mode & CIODevice::Truncate)
flags |= O_TRUNC; flags |= O_TRUNC;
if (mode & GIODevice::MustBeNew) if (mode & CIODevice::MustBeNew)
flags |= O_EXCL; flags |= O_EXCL;
int fd = ::open(m_filename.characters(), flags, 0666); int fd = ::open(m_filename.characters(), flags, 0666);
if (fd < 0) { if (fd < 0) {

21
LibCore/CFile.h Normal file
View 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;
};

View file

@ -1,23 +1,23 @@
#include <LibGUI/GIODevice.h> #include <LibCore/CIODevice.h>
#include <unistd.h> #include <unistd.h>
#include <sys/select.h> #include <sys/select.h>
#include <stdio.h> #include <stdio.h>
GIODevice::GIODevice(CObject* parent) CIODevice::CIODevice(CObject* parent)
: CObject(parent) : CObject(parent)
{ {
} }
GIODevice::~GIODevice() CIODevice::~CIODevice()
{ {
} }
const char* GIODevice::error_string() const const char* CIODevice::error_string() const
{ {
return strerror(m_error); return strerror(m_error);
} }
ByteBuffer GIODevice::read(int max_size) ByteBuffer CIODevice::read(int max_size)
{ {
if (m_fd < 0) if (m_fd < 0)
return { }; return { };
@ -50,9 +50,9 @@ ByteBuffer GIODevice::read(int max_size)
return buffer; 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_set rfds;
FD_ZERO(&rfds); FD_ZERO(&rfds);
FD_SET(m_fd, &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); int rc = select(m_fd + 1, &rfds, nullptr, nullptr, &timeout);
if (rc < 0) { if (rc < 0) {
// NOTE: We don't set m_error here. // NOTE: We don't set m_error here.
perror("GIODevice::can_read: select"); perror("CIODevice::can_read: select");
return false; return false;
} }
return FD_ISSET(m_fd, &rfds); return FD_ISSET(m_fd, &rfds);
} }
bool GIODevice::can_read_line() bool CIODevice::can_read_line()
{ {
if (m_eof && !m_buffered_data.is_empty()) if (m_eof && !m_buffered_data.is_empty())
return true; return true;
@ -78,12 +78,12 @@ bool GIODevice::can_read_line()
return m_buffered_data.contains_slow('\n'); 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(); return !m_buffered_data.is_empty() || can_read_from_fd();
} }
ByteBuffer GIODevice::read_all() ByteBuffer CIODevice::read_all()
{ {
ByteBuffer buffer; ByteBuffer buffer;
if (!m_buffered_data.is_empty()) { if (!m_buffered_data.is_empty()) {
@ -107,7 +107,7 @@ ByteBuffer GIODevice::read_all()
return buffer; return buffer;
} }
ByteBuffer GIODevice::read_line(int max_size) ByteBuffer CIODevice::read_line(int max_size)
{ {
if (m_fd < 0) if (m_fd < 0)
return { }; return { };
@ -117,7 +117,7 @@ ByteBuffer GIODevice::read_line(int max_size)
return { }; return { };
if (m_eof) { if (m_eof) {
if (m_buffered_data.size() > max_size) { 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 { }; return { };
} }
auto buffer = ByteBuffer::copy(m_buffered_data.data(), m_buffered_data.size()); auto buffer = ByteBuffer::copy(m_buffered_data.data(), m_buffered_data.size());
@ -141,7 +141,7 @@ ByteBuffer GIODevice::read_line(int max_size)
return { }; return { };
} }
bool GIODevice::populate_read_buffer() bool CIODevice::populate_read_buffer()
{ {
if (m_fd < 0) if (m_fd < 0)
return false; return false;
@ -160,7 +160,7 @@ bool GIODevice::populate_read_buffer()
return true; return true;
} }
bool GIODevice::close() bool CIODevice::close()
{ {
if (fd() < 0 || mode() == NotOpen) if (fd() < 0 || mode() == NotOpen)
return false; return false;
@ -170,6 +170,6 @@ bool GIODevice::close()
return false; return false;
} }
set_fd(-1); set_fd(-1);
set_mode(GIODevice::NotOpen); set_mode(CIODevice::NotOpen);
return true; return true;
} }

View file

@ -3,7 +3,7 @@
#include <LibCore/CObject.h> #include <LibCore/CObject.h>
#include <AK/ByteBuffer.h> #include <AK/ByteBuffer.h>
class GIODevice : public CObject { class CIODevice : public CObject {
public: public:
enum OpenMode { enum OpenMode {
NotOpen = 0, NotOpen = 0,
@ -15,7 +15,7 @@ public:
MustBeNew = 16, MustBeNew = 16,
}; };
virtual ~GIODevice() override; virtual ~CIODevice() override;
int fd() const { return m_fd; } int fd() const { return m_fd; }
unsigned mode() const { return m_mode; } unsigned mode() const { return m_mode; }
@ -35,13 +35,13 @@ public:
bool can_read() const; bool can_read() const;
virtual bool open(GIODevice::OpenMode) = 0; virtual bool open(CIODevice::OpenMode) = 0;
virtual bool close(); virtual bool close();
virtual const char* class_name() const override { return "GIODevice"; } virtual const char* class_name() const override { return "CIODevice"; }
protected: protected:
explicit GIODevice(CObject* parent = nullptr); explicit CIODevice(CObject* parent = nullptr);
void set_fd(int fd) { m_fd = fd; } void set_fd(int fd) { m_fd = fd; }
void set_mode(OpenMode mode) { m_mode = mode; } void set_mode(OpenMode mode) { m_mode = mode; }

View file

@ -1,4 +1,4 @@
#include <LibGUI/GSocket.h> #include <LibCore/CSocket.h>
#include <LibCore/CNotifier.h> #include <LibCore/CNotifier.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
@ -8,33 +8,33 @@
#include <netdb.h> #include <netdb.h>
#include <errno.h> #include <errno.h>
GSocket::GSocket(Type type, CObject* parent) CSocket::CSocket(Type type, CObject* parent)
: GIODevice(parent) : CIODevice(parent)
, m_type(type) , 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()); auto* hostent = gethostbyname(hostname.characters());
if (!hostent) { if (!hostent) {
dbgprintf("GSocket::connect: Unable to resolve '%s'\n", hostname.characters()); dbgprintf("CSocket::connect: Unable to resolve '%s'\n", hostname.characters());
return false; return false;
} }
IPv4Address host_address((const byte*)hostent->h_addr_list[0]); 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); 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(!is_connected());
ASSERT(address.type() == GSocketAddress::Type::IPv4); ASSERT(address.type() == CSocketAddress::Type::IPv4);
ASSERT(port > 0 && port <= 65535); ASSERT(port > 0 && port <= 65535);
struct sockaddr_in addr; struct sockaddr_in addr;
@ -71,17 +71,17 @@ bool GSocket::connect(const GSocketAddress& address, int port)
return true; return true;
} }
ByteBuffer GSocket::receive(int max_size) ByteBuffer CSocket::receive(int max_size)
{ {
auto buffer = read(max_size); auto buffer = read(max_size);
if (eof()) { 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; m_connected = false;
} }
return buffer; return buffer;
} }
bool GSocket::send(const ByteBuffer& data) bool CSocket::send(const ByteBuffer& data)
{ {
int nsent = ::send(fd(), data.pointer(), data.size(), 0); int nsent = ::send(fd(), data.pointer(), data.size(), 0);
if (nsent < 0) { if (nsent < 0) {

View file

@ -1,44 +1,44 @@
#pragma once #pragma once
#include <LibGUI/GIODevice.h> #include <LibCore/CIODevice.h>
#include <LibGUI/GSocketAddress.h> #include <LibCore/CSocketAddress.h>
class CNotifier; class CNotifier;
class GSocket : public GIODevice { class CSocket : public CIODevice {
public: public:
enum class Type { Invalid, TCP, UDP }; enum class Type { Invalid, TCP, UDP };
virtual ~GSocket() override; virtual ~CSocket() override;
bool connect(const String& hostname, int port); bool connect(const String& hostname, int port);
bool connect(const GSocketAddress&, int port); bool connect(const CSocketAddress&, int port);
ByteBuffer receive(int max_size); ByteBuffer receive(int max_size);
bool send(const ByteBuffer&); bool send(const ByteBuffer&);
bool is_connected() const { return m_connected; } 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; } 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; } int destination_port() const { return m_destination_port; }
Function<void()> on_connected; Function<void()> on_connected;
virtual const char* class_name() const override { return "GSocket"; } virtual const char* class_name() const override { return "CSocket"; }
protected: protected:
GSocket(Type, CObject* parent); CSocket(Type, CObject* parent);
GSocketAddress m_source_address; CSocketAddress m_source_address;
GSocketAddress m_destination_address; CSocketAddress m_destination_address;
int m_source_port { -1 }; int m_source_port { -1 };
int m_destination_port { -1 }; int m_destination_port { -1 };
bool m_connected { false }; bool m_connected { false };
private: private:
virtual bool open(GIODevice::OpenMode) override { ASSERT_NOT_REACHED(); } virtual bool open(CIODevice::OpenMode) override { ASSERT_NOT_REACHED(); }
Type m_type { Type::Invalid }; Type m_type { Type::Invalid };
OwnPtr<CNotifier> m_notifier; OwnPtr<CNotifier> m_notifier;
}; };

View file

@ -2,12 +2,12 @@
#include <Kernel/Net/IPv4.h> #include <Kernel/Net/IPv4.h>
class GSocketAddress { class CSocketAddress {
public: public:
enum class Type { Invalid, IPv4, Local }; enum class Type { Invalid, IPv4, Local };
GSocketAddress() { } CSocketAddress() { }
GSocketAddress(const IPv4Address& address) CSocketAddress(const IPv4Address& address)
: m_type(Type::IPv4) : m_type(Type::IPv4)
, m_ipv4_address(address) , m_ipv4_address(address)
{ {
@ -21,7 +21,7 @@ public:
{ {
switch (m_type) { switch (m_type) {
case Type::IPv4: return m_ipv4_address.to_string(); case Type::IPv4: return m_ipv4_address.to_string();
default: return "[GSocketAddress]"; default: return "[CSocketAddress]";
} }
} }

View file

@ -1,19 +1,19 @@
#include <LibGUI/GTCPSocket.h> #include <LibCore/CTCPSocket.h>
#include <sys/socket.h> #include <sys/socket.h>
GTCPSocket::GTCPSocket(CObject* parent) CTCPSocket::CTCPSocket(CObject* parent)
: GSocket(GSocket::Type::TCP, parent) : CSocket(CSocket::Type::TCP, parent)
{ {
int fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0); int fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
if (fd < 0) { if (fd < 0) {
set_error(fd); set_error(fd);
} else { } else {
set_fd(fd); set_fd(fd);
set_mode(GIODevice::ReadWrite); set_mode(CIODevice::ReadWrite);
set_error(0); set_error(0);
} }
} }
GTCPSocket::~GTCPSocket() CTCPSocket::~CTCPSocket()
{ {
} }

10
LibCore/CTCPSocket.h Normal file
View file

@ -0,0 +1,10 @@
#include <LibCore/CSocket.h>
class CTCPSocket final : public CSocket {
public:
explicit CTCPSocket(CObject* parent = nullptr);
virtual ~CTCPSocket() override;
private:
};

View file

@ -1,4 +1,8 @@
OBJS = \ OBJS = \
CIODevice.o \
CFile.o \
CSocket.o \
CTCPSocket.o \
CElapsedTimer.o \ CElapsedTimer.o \
CNotifier.o \ CNotifier.o \
CObject.o \ CObject.o \

View file

@ -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;
};

View file

@ -1,6 +1,6 @@
#include <LibGUI/GHttpJob.h> #include <LibGUI/GHttpJob.h>
#include <LibGUI/GHttpResponse.h> #include <LibGUI/GHttpResponse.h>
#include <LibGUI/GTCPSocket.h> #include <LibCore/CTCPSocket.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
@ -98,7 +98,7 @@ void GHttpJob::on_socket_connected()
void GHttpJob::start() void GHttpJob::start()
{ {
ASSERT(!m_socket); ASSERT(!m_socket);
m_socket = new GTCPSocket(this); m_socket = new CTCPSocket(this);
m_socket->on_connected = [this] { m_socket->on_connected = [this] {
printf("Socket on_connected callback\n"); printf("Socket on_connected callback\n");
on_socket_connected(); on_socket_connected();

View file

@ -4,7 +4,7 @@
#include <LibGUI/GHttpRequest.h> #include <LibGUI/GHttpRequest.h>
#include <AK/HashMap.h> #include <AK/HashMap.h>
class GTCPSocket; class CTCPSocket;
class GHttpJob final : public GNetworkJob { class GHttpJob final : public GNetworkJob {
public: public:
@ -26,7 +26,7 @@ private:
}; };
GHttpRequest m_request; GHttpRequest m_request;
GTCPSocket* m_socket { nullptr }; CTCPSocket* m_socket { nullptr };
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;

View file

@ -1,10 +0,0 @@
#include <LibGUI/GSocket.h>
class GTCPSocket final : public GSocket {
public:
explicit GTCPSocket(CObject* parent);
virtual ~GTCPSocket() override;
private:
};

View file

@ -10,8 +10,6 @@ SHAREDGRAPHICS_OBJS = \
LIBGUI_OBJS = \ LIBGUI_OBJS = \
GPainter.o \ GPainter.o \
GIODevice.o \
GFile.o \
GButton.o \ GButton.o \
GCheckBox.o \ GCheckBox.o \
GEventLoop.o \ GEventLoop.o \
@ -39,8 +37,6 @@ LIBGUI_OBJS = \
GSortingProxyModel.o \ GSortingProxyModel.o \
GStackWidget.o \ GStackWidget.o \
GScrollableWidget.o \ GScrollableWidget.o \
GSocket.o \
GTCPSocket.o \
GMessageBox.o \ GMessageBox.o \
GInputBox.o \ GInputBox.o \
GDialog.o \ GDialog.o \