mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 23:27:43 +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
|
@ -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");
|
||||
|
|
|
@ -109,6 +109,8 @@ private:
|
|||
void handle(const Message&, const String& verbatim);
|
||||
void handle_user_command(const String&);
|
||||
|
||||
void on_socket_connected();
|
||||
|
||||
String m_hostname;
|
||||
int m_port { 6667 };
|
||||
|
||||
|
|
|
@ -12,7 +12,7 @@ OBJS = \
|
|||
|
||||
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
|
||||
FLAVOR_FLAGS = -fno-exceptions -fno-rtti
|
||||
OPTIMIZATION_FLAGS = -Os
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue