mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 19:07:35 +00:00
LibGUI: Make GSocket connection asynchronous.
Now connect() will return immediately. Later on, when the socket is actually connected, it will call GSocket::on_connected from the event loop. :^)
This commit is contained in:
parent
65d6318c33
commit
6d5a54690e
11 changed files with 65 additions and 29 deletions
|
@ -19,9 +19,9 @@ int main(int argc, char** argv)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto& response = static_cast<const GHttpResponse&>(*job->response());
|
auto& response = static_cast<const GHttpResponse&>(*job->response());
|
||||||
printf("on_receive: code=%d\n", response.code());
|
printf("%s{%p}: on_receive: code=%d\n", job->class_name(), job, response.code());
|
||||||
printf("payload:\n");
|
//printf("payload:\n");
|
||||||
printf("%s", response.payload().pointer());
|
//printf("%s", response.payload().pointer());
|
||||||
printf("payload was %d bytes\n", response.payload().size());
|
printf("payload was %d bytes\n", response.payload().size());
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -45,15 +45,8 @@ void IRCClient::set_server(const String &hostname, int port)
|
||||||
m_port = port;
|
m_port = port;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool IRCClient::connect()
|
void IRCClient::on_socket_connected()
|
||||||
{
|
{
|
||||||
if (m_socket->is_connected())
|
|
||||||
ASSERT_NOT_REACHED();
|
|
||||||
|
|
||||||
bool success = m_socket->connect(m_hostname, m_port);
|
|
||||||
if (!success)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
m_notifier = make<GNotifier>(m_socket->fd(), GNotifier::Read);
|
m_notifier = make<GNotifier>(m_socket->fd(), GNotifier::Read);
|
||||||
m_notifier->on_ready_to_read = [this] (GNotifier&) { receive_from_server(); };
|
m_notifier->on_ready_to_read = [this] (GNotifier&) { receive_from_server(); };
|
||||||
|
|
||||||
|
@ -62,13 +55,24 @@ bool IRCClient::connect()
|
||||||
|
|
||||||
if (on_connect)
|
if (on_connect)
|
||||||
on_connect();
|
on_connect();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IRCClient::connect()
|
||||||
|
{
|
||||||
|
if (m_socket->is_connected())
|
||||||
|
ASSERT_NOT_REACHED();
|
||||||
|
|
||||||
|
m_socket->on_connected = [this] { on_socket_connected(); };
|
||||||
|
bool success = m_socket->connect(m_hostname, m_port);
|
||||||
|
if (!success)
|
||||||
|
return false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void IRCClient::receive_from_server()
|
void IRCClient::receive_from_server()
|
||||||
{
|
{
|
||||||
while (m_socket->can_read_line()) {
|
while (m_socket->can_read_line()) {
|
||||||
auto line = m_socket->read_line(4096);
|
auto line = m_socket->read_line(PAGE_SIZE);
|
||||||
if (line.is_null()) {
|
if (line.is_null()) {
|
||||||
if (!m_socket->is_connected()) {
|
if (!m_socket->is_connected()) {
|
||||||
printf("IRCClient: Connection closed!\n");
|
printf("IRCClient: Connection closed!\n");
|
||||||
|
|
|
@ -109,6 +109,8 @@ private:
|
||||||
void handle(const Message&, const String& verbatim);
|
void handle(const Message&, const String& verbatim);
|
||||||
void handle_user_command(const String&);
|
void handle_user_command(const String&);
|
||||||
|
|
||||||
|
void on_socket_connected();
|
||||||
|
|
||||||
String m_hostname;
|
String m_hostname;
|
||||||
int m_port { 6667 };
|
int m_port { 6667 };
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,7 @@ OBJS = \
|
||||||
|
|
||||||
APP = IRCClient
|
APP = IRCClient
|
||||||
|
|
||||||
STANDARD_FLAGS = -std=c++17
|
STANDARD_FLAGS = -std=c++17 -Wno-sized-deallocation
|
||||||
WARNING_FLAGS = -Wextra -Wall -Wundef -Wcast-qual -Wwrite-strings -Wimplicit-fallthrough
|
WARNING_FLAGS = -Wextra -Wall -Wundef -Wcast-qual -Wwrite-strings -Wimplicit-fallthrough
|
||||||
FLAVOR_FLAGS = -fno-exceptions -fno-rtti
|
FLAVOR_FLAGS = -fno-exceptions -fno-rtti
|
||||||
OPTIMIZATION_FLAGS = -Os
|
OPTIMIZATION_FLAGS = -Os
|
||||||
|
|
|
@ -13,19 +13,14 @@ GHttpJob::~GHttpJob()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void GHttpJob::start()
|
void GHttpJob::on_socket_connected()
|
||||||
{
|
{
|
||||||
ASSERT(!m_socket);
|
|
||||||
m_socket = new GTCPSocket(this);
|
|
||||||
int success = m_socket->connect(m_request.hostname(), m_request.port());
|
|
||||||
if (!success)
|
|
||||||
return did_fail(GNetworkJob::Error::ConnectionFailed);
|
|
||||||
|
|
||||||
auto raw_request = m_request.to_raw_request();
|
auto raw_request = m_request.to_raw_request();
|
||||||
|
#if 0
|
||||||
|
printf("raw_request:\n%s\n", String::from_byte_buffer(raw_request).characters());
|
||||||
|
#endif
|
||||||
|
|
||||||
printf("raw_request:\n%s\n", raw_request.pointer());
|
bool success = m_socket->send(raw_request);
|
||||||
|
|
||||||
success = m_socket->send(raw_request);
|
|
||||||
if (!success)
|
if (!success)
|
||||||
return deferred_invoke([this](auto&){ did_fail(GNetworkJob::Error::TransmissionFailed); });
|
return deferred_invoke([this](auto&){ did_fail(GNetworkJob::Error::TransmissionFailed); });
|
||||||
|
|
||||||
|
@ -99,3 +94,16 @@ void GHttpJob::start()
|
||||||
did_finish(move(response));
|
did_finish(move(response));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void GHttpJob::start()
|
||||||
|
{
|
||||||
|
ASSERT(!m_socket);
|
||||||
|
m_socket = new GTCPSocket(this);
|
||||||
|
m_socket->on_connected = [this] {
|
||||||
|
printf("Socket on_connected callback\n");
|
||||||
|
on_socket_connected();
|
||||||
|
};
|
||||||
|
bool success = m_socket->connect(m_request.hostname(), m_request.port());
|
||||||
|
if (!success)
|
||||||
|
return did_fail(GNetworkJob::Error::ConnectionFailed);
|
||||||
|
}
|
||||||
|
|
|
@ -16,6 +16,8 @@ public:
|
||||||
virtual const char* class_name() const override { return "GHttpJob"; }
|
virtual const char* class_name() const override { return "GHttpJob"; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void on_socket_connected();
|
||||||
|
|
||||||
enum class State {
|
enum class State {
|
||||||
InStatus,
|
InStatus,
|
||||||
InHeaders,
|
InHeaders,
|
||||||
|
|
|
@ -145,7 +145,7 @@ bool GIODevice::populate_read_buffer()
|
||||||
{
|
{
|
||||||
if (m_fd < 0)
|
if (m_fd < 0)
|
||||||
return false;
|
return false;
|
||||||
auto buffer = ByteBuffer::create_uninitialized(1024);
|
auto buffer = ByteBuffer::create_uninitialized(PAGE_SIZE);
|
||||||
int nread = ::read(m_fd, buffer.pointer(), buffer.size());
|
int nread = ::read(m_fd, buffer.pointer(), buffer.size());
|
||||||
if (nread < 0) {
|
if (nread < 0) {
|
||||||
set_error(errno);
|
set_error(errno);
|
||||||
|
|
|
@ -18,6 +18,7 @@ public:
|
||||||
|
|
||||||
int fd() const { return m_fd; }
|
int fd() const { return m_fd; }
|
||||||
unsigned event_mask() const { return m_event_mask; }
|
unsigned event_mask() const { return m_event_mask; }
|
||||||
|
void set_event_mask(unsigned event_mask) { m_event_mask = event_mask; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int m_fd { -1 };
|
int m_fd { -1 };
|
||||||
|
|
|
@ -1,9 +1,12 @@
|
||||||
#include <LibGUI/GSocket.h>
|
#include <LibGUI/GSocket.h>
|
||||||
|
#include <LibGUI/GNotifier.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
GSocket::GSocket(Type type, GObject* parent)
|
GSocket::GSocket(Type type, GObject* parent)
|
||||||
: GIODevice(parent)
|
: GIODevice(parent)
|
||||||
|
@ -41,17 +44,29 @@ bool GSocket::connect(const GSocketAddress& address, int port)
|
||||||
addr.sin_family = AF_INET;
|
addr.sin_family = AF_INET;
|
||||||
addr.sin_port = htons(port);
|
addr.sin_port = htons(port);
|
||||||
|
|
||||||
|
m_destination_address = address;
|
||||||
|
m_destination_port = port;
|
||||||
|
|
||||||
printf("Connecting to %s...", address.to_string().characters());
|
printf("Connecting to %s...", address.to_string().characters());
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
int rc = ::connect(fd(), (struct sockaddr*)&addr, sizeof(addr));
|
int rc = ::connect(fd(), (struct sockaddr*)&addr, sizeof(addr));
|
||||||
if (rc < 0) {
|
if (rc < 0) {
|
||||||
|
if (errno == EINPROGRESS) {
|
||||||
|
printf("in progress.\n");
|
||||||
|
m_notifier = make<GNotifier>(fd(), GNotifier::Event::Write);
|
||||||
|
m_notifier->on_ready_to_write = [this] (GNotifier&) {
|
||||||
|
printf("%s{%p} connected!\n", class_name(), this);
|
||||||
|
m_connected = true;
|
||||||
|
m_notifier->set_event_mask(GNotifier::Event::None);
|
||||||
|
if (on_connected)
|
||||||
|
on_connected();
|
||||||
|
};
|
||||||
|
return true;
|
||||||
|
}
|
||||||
perror("connect");
|
perror("connect");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
printf("ok!\n");
|
printf("ok!\n");
|
||||||
|
|
||||||
m_destination_address = address;
|
|
||||||
m_destination_port = port;
|
|
||||||
m_connected = true;
|
m_connected = true;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,6 +32,8 @@ private:
|
||||||
IPv4Address m_ipv4_address;
|
IPv4Address m_ipv4_address;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class GNotifier;
|
||||||
|
|
||||||
class GSocket : public GIODevice {
|
class GSocket : public GIODevice {
|
||||||
public:
|
public:
|
||||||
enum class Type { Invalid, TCP, UDP };
|
enum class Type { Invalid, TCP, UDP };
|
||||||
|
@ -51,6 +53,8 @@ public:
|
||||||
GSocketAddress destination_address() const { return m_source_address; }
|
GSocketAddress 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;
|
||||||
|
|
||||||
virtual const char* class_name() const override { return "GSocket"; }
|
virtual const char* class_name() const override { return "GSocket"; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
@ -65,5 +69,5 @@ protected:
|
||||||
private:
|
private:
|
||||||
virtual bool open(GIODevice::OpenMode) override { ASSERT_NOT_REACHED(); }
|
virtual bool open(GIODevice::OpenMode) override { ASSERT_NOT_REACHED(); }
|
||||||
Type m_type { Type::Invalid };
|
Type m_type { Type::Invalid };
|
||||||
|
OwnPtr<GNotifier> m_notifier;
|
||||||
};
|
};
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
GTCPSocket::GTCPSocket(GObject* parent)
|
GTCPSocket::GTCPSocket(GObject* parent)
|
||||||
: GSocket(GSocket::Type::TCP, parent)
|
: GSocket(GSocket::Type::TCP, parent)
|
||||||
{
|
{
|
||||||
int fd = socket(AF_INET, SOCK_STREAM, 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 {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue