ladybird/Userland/Services/EchoServer/Client.cpp

39 lines
686 B
C++
Raw Normal View History

2020-12-15 23:42:01 -03:00
/*
* Copyright (c) 2020, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
2020-12-15 23:42:01 -03:00
*/
#include "Client.h"
Client::Client(int id, RefPtr<Core::TCPSocket> socket)
: m_id(id)
, m_socket(move(socket))
{
m_socket->on_ready_to_read = [this] { drain_socket(); };
}
void Client::drain_socket()
{
NonnullRefPtr<Client> protect(*this);
while (m_socket->can_read()) {
auto buf = m_socket->read(1024);
dbgln("Read {} bytes.", buf.size());
2020-12-15 23:42:01 -03:00
if (m_socket->eof()) {
quit();
break;
}
m_socket->write(buf);
}
}
void Client::quit()
{
m_socket->close();
if (on_exit)
on_exit();
}