mirror of
https://github.com/RGBCube/serenity
synced 2025-07-28 16:47:34 +00:00
LibCore+Userland: Convert TCPServer to use the Serenity Stream API
This is intended as a real-usecase test of the Serenity Stream API, and seemed like a good candidate due to its low amount of users.
This commit is contained in:
parent
2341b0159a
commit
dfdb52efa7
11 changed files with 263 additions and 124 deletions
|
@ -11,59 +11,100 @@
|
|||
#include <AK/StringBuilder.h>
|
||||
#include <AK/StringView.h>
|
||||
#include <AK/Types.h>
|
||||
#include <LibCore/EventLoop.h>
|
||||
#include <LibCore/Notifier.h>
|
||||
#include <LibCore/TCPSocket.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
Client::Client(int id, RefPtr<Core::TCPSocket> socket, int ptm_fd)
|
||||
Client::Client(int id, Core::Stream::TCPSocket socket, int ptm_fd)
|
||||
: m_id(id)
|
||||
, m_socket(move(socket))
|
||||
, m_ptm_fd(ptm_fd)
|
||||
, m_ptm_notifier(Core::Notifier::construct(ptm_fd, Core::Notifier::Read))
|
||||
{
|
||||
m_socket->on_ready_to_read = [this] { drain_socket(); };
|
||||
m_ptm_notifier->on_ready_to_read = [this] { drain_pty(); };
|
||||
m_parser.on_command = [this](const Command& command) { handle_command(command); };
|
||||
m_socket.on_ready_to_read = [this] {
|
||||
auto result = drain_socket();
|
||||
if (result.is_error()) {
|
||||
dbgln("Failed to drain the socket: {}", result.error());
|
||||
Core::deferred_invoke([this, strong_this = NonnullRefPtr(*this)] { quit(); });
|
||||
}
|
||||
};
|
||||
|
||||
m_ptm_notifier->on_ready_to_read = [this] {
|
||||
auto result = drain_pty();
|
||||
if (result.is_error()) {
|
||||
dbgln("Failed to drain the PTY: {}", result.error());
|
||||
Core::deferred_invoke([this, strong_this = NonnullRefPtr(*this)] { quit(); });
|
||||
}
|
||||
};
|
||||
|
||||
m_parser.on_command = [this](const Command& command) {
|
||||
auto result = handle_command(command);
|
||||
if (result.is_error()) {
|
||||
dbgln("Failed to handle the command: {}", result.error());
|
||||
Core::deferred_invoke([this, strong_this = NonnullRefPtr(*this)] { quit(); });
|
||||
}
|
||||
};
|
||||
|
||||
m_parser.on_data = [this](StringView data) { handle_data(data); };
|
||||
m_parser.on_error = [this]() { handle_error(); };
|
||||
send_commands({
|
||||
}
|
||||
|
||||
ErrorOr<NonnullRefPtr<Client>> Client::create(int id, Core::Stream::TCPSocket socket, int ptm_fd)
|
||||
{
|
||||
auto client = adopt_ref(*new Client(id, move(socket), ptm_fd));
|
||||
|
||||
auto result = client->send_commands({
|
||||
{ CMD_WILL, SUB_SUPPRESS_GO_AHEAD },
|
||||
{ CMD_WILL, SUB_ECHO },
|
||||
{ CMD_DO, SUB_SUPPRESS_GO_AHEAD },
|
||||
{ CMD_DONT, SUB_ECHO },
|
||||
});
|
||||
if (result.is_error()) {
|
||||
client->quit();
|
||||
return result.release_error();
|
||||
}
|
||||
|
||||
return client;
|
||||
}
|
||||
|
||||
void Client::drain_socket()
|
||||
ErrorOr<void> Client::drain_socket()
|
||||
{
|
||||
NonnullRefPtr<Client> protect(*this);
|
||||
while (m_socket->can_read()) {
|
||||
auto buf = m_socket->read(1024);
|
||||
|
||||
m_parser.write(buf);
|
||||
auto maybe_buffer = ByteBuffer::create_uninitialized(1024);
|
||||
if (!maybe_buffer.has_value())
|
||||
return ENOMEM;
|
||||
auto buffer = maybe_buffer.release_value();
|
||||
|
||||
if (m_socket->eof()) {
|
||||
quit();
|
||||
while (TRY(m_socket.can_read_without_blocking())) {
|
||||
auto nread = TRY(m_socket.read(buffer));
|
||||
|
||||
m_parser.write({ buffer.data(), nread });
|
||||
|
||||
if (m_socket.is_eof()) {
|
||||
Core::deferred_invoke([this, strong_this = NonnullRefPtr(*this)] { quit(); });
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
void Client::drain_pty()
|
||||
ErrorOr<void> Client::drain_pty()
|
||||
{
|
||||
u8 buffer[BUFSIZ];
|
||||
ssize_t nread = read(m_ptm_fd, buffer, sizeof(buffer));
|
||||
if (nread < 0) {
|
||||
perror("read(ptm)");
|
||||
quit();
|
||||
return;
|
||||
Core::deferred_invoke([this, strong_this = NonnullRefPtr(*this)] { quit(); });
|
||||
return static_cast<ErrnoCode>(errno);
|
||||
}
|
||||
if (nread == 0) {
|
||||
quit();
|
||||
return;
|
||||
Core::deferred_invoke([this, strong_this = NonnullRefPtr(*this)] { quit(); });
|
||||
return {};
|
||||
}
|
||||
send_data(StringView(buffer, (size_t)nread));
|
||||
|
||||
return send_data({ buffer, (size_t)nread });
|
||||
}
|
||||
|
||||
void Client::handle_data(StringView data)
|
||||
|
@ -71,7 +112,7 @@ void Client::handle_data(StringView data)
|
|||
write(m_ptm_fd, data.characters_without_null_termination(), data.length());
|
||||
}
|
||||
|
||||
void Client::handle_command(const Command& command)
|
||||
ErrorOr<void> Client::handle_command(const Command& command)
|
||||
{
|
||||
switch (command.command) {
|
||||
case CMD_DO:
|
||||
|
@ -87,10 +128,10 @@ void Client::handle_command(const Command& command)
|
|||
case SUB_ECHO:
|
||||
// we always want to be the ones in control of the output. tell
|
||||
// the client to disable local echo.
|
||||
send_command({ CMD_DONT, SUB_ECHO });
|
||||
TRY(send_command({ CMD_DONT, SUB_ECHO }));
|
||||
break;
|
||||
case SUB_SUPPRESS_GO_AHEAD:
|
||||
send_command({ CMD_DO, SUB_SUPPRESS_GO_AHEAD });
|
||||
TRY(send_command({ CMD_DO, SUB_SUPPRESS_GO_AHEAD }));
|
||||
break;
|
||||
default:
|
||||
// don't respond to unknown commands
|
||||
|
@ -102,14 +143,16 @@ void Client::handle_command(const Command& command)
|
|||
// won't do.
|
||||
break;
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
void Client::handle_error()
|
||||
{
|
||||
quit();
|
||||
Core::deferred_invoke([this, strong_this = NonnullRefPtr(*this)] { quit(); });
|
||||
}
|
||||
|
||||
void Client::send_data(StringView data)
|
||||
ErrorOr<void> Client::send_data(StringView data)
|
||||
{
|
||||
bool fast = true;
|
||||
for (size_t i = 0; i < data.length(); i++) {
|
||||
|
@ -119,8 +162,8 @@ void Client::send_data(StringView data)
|
|||
}
|
||||
|
||||
if (fast) {
|
||||
m_socket->write(data);
|
||||
return;
|
||||
TRY(m_socket.write({ data.characters_without_null_termination(), data.length() }));
|
||||
return {};
|
||||
}
|
||||
|
||||
StringBuilder builder;
|
||||
|
@ -140,31 +183,37 @@ void Client::send_data(StringView data)
|
|||
}
|
||||
}
|
||||
|
||||
m_socket->write(builder.to_string());
|
||||
auto builder_contents = builder.to_byte_buffer();
|
||||
TRY(m_socket.write(builder_contents));
|
||||
return {};
|
||||
}
|
||||
|
||||
void Client::send_command(Command command)
|
||||
ErrorOr<void> Client::send_command(Command command)
|
||||
{
|
||||
send_commands({ command });
|
||||
return send_commands({ command });
|
||||
}
|
||||
|
||||
void Client::send_commands(Vector<Command> commands)
|
||||
ErrorOr<void> Client::send_commands(Vector<Command> commands)
|
||||
{
|
||||
auto buffer = ByteBuffer::create_uninitialized(commands.size() * 3).release_value(); // FIXME: Handle possible OOM situation.
|
||||
auto maybe_buffer = ByteBuffer::create_uninitialized(commands.size() * 3);
|
||||
if (!maybe_buffer.has_value())
|
||||
return ENOMEM;
|
||||
auto buffer = maybe_buffer.release_value();
|
||||
OutputMemoryStream stream { buffer };
|
||||
|
||||
for (auto& command : commands)
|
||||
stream << (u8)IAC << command.command << command.subcommand;
|
||||
|
||||
VERIFY(stream.is_end());
|
||||
m_socket->write(buffer.data(), buffer.size());
|
||||
TRY(m_socket.write({ buffer.data(), buffer.size() }));
|
||||
return {};
|
||||
}
|
||||
|
||||
void Client::quit()
|
||||
{
|
||||
m_ptm_notifier->set_enabled(false);
|
||||
close(m_ptm_fd);
|
||||
m_socket->close();
|
||||
m_socket.close();
|
||||
if (on_exit)
|
||||
on_exit();
|
||||
}
|
||||
|
|
|
@ -10,38 +10,35 @@
|
|||
#include <AK/StringView.h>
|
||||
#include <AK/Types.h>
|
||||
#include <LibCore/Notifier.h>
|
||||
#include <LibCore/TCPSocket.h>
|
||||
#include <LibCore/Stream.h>
|
||||
|
||||
#include "Command.h"
|
||||
#include "Parser.h"
|
||||
|
||||
class Client : public RefCounted<Client> {
|
||||
public:
|
||||
static NonnullRefPtr<Client> create(int id, RefPtr<Core::TCPSocket> socket, int ptm_fd)
|
||||
{
|
||||
return adopt_ref(*new Client(id, move(socket), ptm_fd));
|
||||
}
|
||||
static ErrorOr<NonnullRefPtr<Client>> create(int id, Core::Stream::TCPSocket socket, int ptm_fd);
|
||||
|
||||
Function<void()> on_exit;
|
||||
|
||||
protected:
|
||||
Client(int id, RefPtr<Core::TCPSocket> socket, int ptm_fd);
|
||||
private:
|
||||
Client(int id, Core::Stream::TCPSocket socket, int ptm_fd);
|
||||
|
||||
ErrorOr<void> drain_socket();
|
||||
ErrorOr<void> drain_pty();
|
||||
ErrorOr<void> handle_command(Command const& command);
|
||||
ErrorOr<void> send_data(StringView str);
|
||||
ErrorOr<void> send_command(Command command);
|
||||
ErrorOr<void> send_commands(Vector<Command> commands);
|
||||
|
||||
void drain_socket();
|
||||
void drain_pty();
|
||||
void handle_data(StringView);
|
||||
void handle_command(const Command& command);
|
||||
void handle_error();
|
||||
void send_data(StringView str);
|
||||
void send_command(Command command);
|
||||
void send_commands(Vector<Command> commands);
|
||||
void quit();
|
||||
|
||||
private:
|
||||
// client id
|
||||
int m_id { 0 };
|
||||
// client resources
|
||||
RefPtr<Core::TCPSocket> m_socket;
|
||||
Core::Stream::TCPSocket m_socket;
|
||||
Parser m_parser;
|
||||
// pty resources
|
||||
int m_ptm_fd { -1 };
|
||||
|
|
|
@ -109,32 +109,39 @@ int main(int argc, char** argv)
|
|||
server->on_ready_to_accept = [&next_id, &clients, &server, command] {
|
||||
int id = next_id++;
|
||||
|
||||
auto client_socket = server->accept();
|
||||
if (!client_socket) {
|
||||
perror("accept");
|
||||
ErrorOr<Core::Stream::TCPSocket> maybe_client_socket = server->accept();
|
||||
if (maybe_client_socket.is_error()) {
|
||||
warnln("accept: {}", maybe_client_socket.error());
|
||||
return;
|
||||
}
|
||||
auto client_socket = maybe_client_socket.release_value();
|
||||
|
||||
int ptm_fd = posix_openpt(O_RDWR);
|
||||
if (ptm_fd < 0) {
|
||||
perror("posix_openpt");
|
||||
client_socket->close();
|
||||
client_socket.close();
|
||||
return;
|
||||
}
|
||||
if (grantpt(ptm_fd) < 0) {
|
||||
perror("grantpt");
|
||||
client_socket->close();
|
||||
client_socket.close();
|
||||
return;
|
||||
}
|
||||
if (unlockpt(ptm_fd) < 0) {
|
||||
perror("unlockpt");
|
||||
client_socket->close();
|
||||
client_socket.close();
|
||||
return;
|
||||
}
|
||||
|
||||
run_command(ptm_fd, command);
|
||||
|
||||
auto client = Client::create(id, move(client_socket), ptm_fd);
|
||||
auto maybe_client = Client::create(id, move(client_socket), ptm_fd);
|
||||
if (maybe_client.is_error()) {
|
||||
warnln("Failed to create the client: {}", maybe_client.error());
|
||||
return;
|
||||
}
|
||||
|
||||
auto client = maybe_client.release_value();
|
||||
client->on_exit = [&clients, id] {
|
||||
Core::deferred_invoke([&clients, id] { clients.remove(id); });
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue