1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 02:57:42 +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:
Andreas Kling 2019-04-08 04:53:45 +02:00
parent 65d6318c33
commit 6d5a54690e
11 changed files with 65 additions and 29 deletions

View file

@ -45,15 +45,8 @@ void IRCClient::set_server(const String &hostname, int 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->on_ready_to_read = [this] (GNotifier&) { receive_from_server(); };
@ -62,13 +55,24 @@ bool IRCClient::connect()
if (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;
}
void IRCClient::receive_from_server()
{
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 (!m_socket->is_connected()) {
printf("IRCClient: Connection closed!\n");