diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-11-23 22:11:44 +0100 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-11-23 22:11:44 +0100 |
commit | eb85103271329d269b5854c0131889d87adceeec (patch) | |
tree | 2a739e8f954640fb5b3cd5f83406175e7108ecf0 /Servers | |
parent | 88c5126fa72b8eedcf38b4768697709b43d7876f (diff) | |
download | serenity-eb85103271329d269b5854c0131889d87adceeec.zip |
ProtocolServer: Send the download payload to clients as a shared buffer
The DownloadFinished message from the server now includes a buffer ID
that can be mapped into the client program.
To avoid prematurely destroying the buffer, the server will hang on to
it until the client lets it know that they're all good. That's what the
ProtocolServer::DisownSharedBuffer message is about.
In the future it would be nice if the kernel had a mechanism to allow
passing ownership of a shared buffer along with an IPC message somehow.
Diffstat (limited to 'Servers')
-rw-r--r-- | Servers/ProtocolServer/Download.cpp | 6 | ||||
-rw-r--r-- | Servers/ProtocolServer/Download.h | 3 | ||||
-rw-r--r-- | Servers/ProtocolServer/HttpDownload.cpp | 2 | ||||
-rw-r--r-- | Servers/ProtocolServer/PSClientConnection.cpp | 17 | ||||
-rw-r--r-- | Servers/ProtocolServer/PSClientConnection.h | 4 | ||||
-rw-r--r-- | Servers/ProtocolServer/ProtocolClient.ipc | 2 | ||||
-rw-r--r-- | Servers/ProtocolServer/ProtocolServer.ipc | 3 |
7 files changed, 35 insertions, 2 deletions
diff --git a/Servers/ProtocolServer/Download.cpp b/Servers/ProtocolServer/Download.cpp index d6fe75bef4..a2d6904ce0 100644 --- a/Servers/ProtocolServer/Download.cpp +++ b/Servers/ProtocolServer/Download.cpp @@ -31,6 +31,12 @@ void Download::stop() all_downloads().remove(m_id); } +void Download::set_payload(const ByteBuffer& payload) +{ + m_payload = payload; + m_total_size = payload.size(); +} + void Download::did_finish(bool success) { if (!m_client) { diff --git a/Servers/ProtocolServer/Download.h b/Servers/ProtocolServer/Download.h index d12a6b7e52..eb2e1ac529 100644 --- a/Servers/ProtocolServer/Download.h +++ b/Servers/ProtocolServer/Download.h @@ -17,6 +17,7 @@ public: size_t total_size() const { return m_total_size; } size_t downloaded_size() const { return m_downloaded_size; } + const ByteBuffer& payload() const { return m_payload; } void stop(); @@ -25,11 +26,13 @@ protected: void did_finish(bool success); void did_progress(size_t total_size, size_t downloaded_size); + void set_payload(const ByteBuffer&); private: i32 m_id; URL m_url; size_t m_total_size { 0 }; size_t m_downloaded_size { 0 }; + ByteBuffer m_payload; WeakPtr<PSClientConnection> m_client; }; diff --git a/Servers/ProtocolServer/HttpDownload.cpp b/Servers/ProtocolServer/HttpDownload.cpp index 76dba63491..b20c553a58 100644 --- a/Servers/ProtocolServer/HttpDownload.cpp +++ b/Servers/ProtocolServer/HttpDownload.cpp @@ -1,4 +1,5 @@ #include <LibCore/CHttpJob.h> +#include <LibCore/CHttpResponse.h> #include <ProtocolServer/HttpDownload.h> HttpDownload::HttpDownload(PSClientConnection& client, NonnullRefPtr<CHttpJob>&& job) @@ -6,6 +7,7 @@ HttpDownload::HttpDownload(PSClientConnection& client, NonnullRefPtr<CHttpJob>&& , m_job(job) { m_job->on_finish = [this](bool success) { + set_payload(m_job->response()->payload()); did_finish(success); }; } diff --git a/Servers/ProtocolServer/PSClientConnection.cpp b/Servers/ProtocolServer/PSClientConnection.cpp index fb93268efe..be0e67129d 100644 --- a/Servers/ProtocolServer/PSClientConnection.cpp +++ b/Servers/ProtocolServer/PSClientConnection.cpp @@ -2,6 +2,7 @@ #include <ProtocolServer/PSClientConnection.h> #include <ProtocolServer/Protocol.h> #include <ProtocolServer/ProtocolClientEndpoint.h> +#include <LibC/SharedBuffer.h> static HashMap<int, RefPtr<PSClientConnection>> s_connections; @@ -48,7 +49,15 @@ OwnPtr<ProtocolServer::StopDownloadResponse> PSClientConnection::handle(const Pr void PSClientConnection::did_finish_download(Badge<Download>, Download& download, bool success) { - post_message(ProtocolClient::DownloadFinished(download.id(), success)); + RefPtr<SharedBuffer> buffer; + if (success && !download.payload().is_null()) { + buffer = SharedBuffer::create_with_size(download.payload().size()); + memcpy(buffer->data(), download.payload().data(), download.payload().size()); + buffer->seal(); + buffer->share_with(client_pid()); + m_shared_buffers.set(buffer->shared_buffer_id(), buffer); + } + post_message(ProtocolClient::DownloadFinished(download.id(), success, download.total_size(), buffer ? buffer->shared_buffer_id() : -1)); } void PSClientConnection::did_progress_download(Badge<Download>, Download& download) @@ -61,3 +70,9 @@ OwnPtr<ProtocolServer::GreetResponse> PSClientConnection::handle(const ProtocolS set_client_pid(message.client_pid()); return make<ProtocolServer::GreetResponse>(getpid(), client_id()); } + +OwnPtr<ProtocolServer::DisownSharedBufferResponse> PSClientConnection::handle(const ProtocolServer::DisownSharedBuffer& message) +{ + m_shared_buffers.remove(message.shared_buffer_id()); + return make<ProtocolServer::DisownSharedBufferResponse>(); +} diff --git a/Servers/ProtocolServer/PSClientConnection.h b/Servers/ProtocolServer/PSClientConnection.h index 190e9bd062..fd6b71f195 100644 --- a/Servers/ProtocolServer/PSClientConnection.h +++ b/Servers/ProtocolServer/PSClientConnection.h @@ -5,6 +5,7 @@ #include <ProtocolServer/ProtocolServerEndpoint.h> class Download; +class SharedBuffer; class PSClientConnection final : public IPC::Server::ConnectionNG<ProtocolServerEndpoint> , public ProtocolServerEndpoint { @@ -23,4 +24,7 @@ private: virtual OwnPtr<ProtocolServer::IsSupportedProtocolResponse> handle(const ProtocolServer::IsSupportedProtocol&) override; virtual OwnPtr<ProtocolServer::StartDownloadResponse> handle(const ProtocolServer::StartDownload&) override; virtual OwnPtr<ProtocolServer::StopDownloadResponse> handle(const ProtocolServer::StopDownload&) override; + virtual OwnPtr<ProtocolServer::DisownSharedBufferResponse> handle(const ProtocolServer::DisownSharedBuffer&) override; + + HashMap<i32, RefPtr<SharedBuffer>> m_shared_buffers; }; diff --git a/Servers/ProtocolServer/ProtocolClient.ipc b/Servers/ProtocolServer/ProtocolClient.ipc index df88714f50..58a408add9 100644 --- a/Servers/ProtocolServer/ProtocolClient.ipc +++ b/Servers/ProtocolServer/ProtocolClient.ipc @@ -2,5 +2,5 @@ endpoint ProtocolClient = 13 { // Download notifications DownloadProgress(i32 download_id, u32 total_size, u32 downloaded_size) =| - DownloadFinished(i32 download_id, bool success) =| + DownloadFinished(i32 download_id, bool success, u32 total_size, i32 shared_buffer_id) =| } diff --git a/Servers/ProtocolServer/ProtocolServer.ipc b/Servers/ProtocolServer/ProtocolServer.ipc index 90af2d33ec..6ed9eba623 100644 --- a/Servers/ProtocolServer/ProtocolServer.ipc +++ b/Servers/ProtocolServer/ProtocolServer.ipc @@ -3,6 +3,9 @@ endpoint ProtocolServer = 9 // Basic protocol Greet(i32 client_pid) => (i32 server_pid, i32 client_id) + // FIXME: It would be nice if the kernel provided a way to avoid this + DisownSharedBuffer(i32 shared_buffer_id) => () + // Test if a specific protocol is supported, e.g "http" IsSupportedProtocol(String protocol) => (bool supported) |