1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 05:27:45 +00:00

ProtocolServer+LibProtocol: Introduce a server for handling downloads

This patch adds ProtocolServer, a server that handles network requests
on behalf of its clients. The first protocol implemented is HTTP.

The idea here is to use a plug-in architecture where any number of
protocols can be added and implemented without having to mess around
with each client program that wants to use the protocol.

A simple client API is provided through LibProtocol::Client. :^)
This commit is contained in:
Andreas Kling 2019-11-23 21:45:33 +01:00
parent 61f611bf3c
commit fd4349a9f2
21 changed files with 475 additions and 0 deletions

View file

@ -0,0 +1,45 @@
#include <LibProtocol/Client.h>
#include <SharedBuffer.h>
namespace LibProtocol {
Client::Client()
: ConnectionNG(*this, "/tmp/psportal")
{
}
void Client::handshake()
{
auto response = send_sync<ProtocolServer::Greet>(getpid());
set_server_pid(response->server_pid());
set_my_client_id(response->client_id());
}
bool Client::is_supported_protocol(const String& protocol)
{
return send_sync<ProtocolServer::IsSupportedProtocol>(protocol)->supported();
}
i32 Client::start_download(const String& url)
{
return send_sync<ProtocolServer::StartDownload>(url)->download_id();
}
bool Client::stop_download(i32 download_id)
{
return send_sync<ProtocolServer::StopDownload>(download_id)->success();
}
void Client::handle(const ProtocolClient::DownloadFinished& message)
{
if (on_download_finish)
on_download_finish(message.download_id(), message.success());
}
void Client::handle(const ProtocolClient::DownloadProgress& message)
{
if (on_download_progress)
on_download_progress(message.download_id(), message.total_size(), message.downloaded_size());
}
}

View file

@ -0,0 +1,29 @@
#pragma once
#include <LibCore/CoreIPCClient.h>
#include <ProtocolServer/ProtocolClientEndpoint.h>
#include <ProtocolServer/ProtocolServerEndpoint.h>
namespace LibProtocol {
class Client : public IPC::Client::ConnectionNG<ProtocolClientEndpoint, ProtocolServerEndpoint>
, public ProtocolClientEndpoint {
C_OBJECT(Client)
public:
Client();
virtual void handshake() override;
bool is_supported_protocol(const String&);
i32 start_download(const String& url);
bool stop_download(i32 download_id);
Function<void(i32 download_id, bool success)> on_download_finish;
Function<void(i32 download_id, u64 total_size, u64 downloaded_size)> on_download_progress;
private:
virtual void handle(const ProtocolClient::DownloadProgress&) override;
virtual void handle(const ProtocolClient::DownloadFinished&) override;
};
}

View file

@ -0,0 +1,20 @@
include ../../Makefile.common
OBJS = \
Client.o
LIBRARY = libprotocol.a
DEFINES += -DUSERLAND
all: $(LIBRARY)
$(LIBRARY): $(OBJS)
@echo "LIB $@"; $(AR) rcs $@ $(OBJS) $(LIBS)
.cpp.o:
@echo "CXX $<"; $(CXX) $(CXXFLAGS) -o $@ -c $<
-include $(OBJS:%.o=%.d)
clean:
@echo "CLEAN"; rm -f $(LIBRARY) $(OBJS) *.d