Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
ZLibTransformStreams.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "AsyncStreams.h"
5#include "Internal/ZLibStream.h"
6
7namespace SC
8{
10{
12 ZLibStream stream;
13
14 private:
15 size_t consumedInputBytes = 0;
16
17 virtual Result asyncWrite(AsyncBufferView::ID bufferID, Function<void(AsyncBufferView::ID)> cb) override;
18 virtual bool canEndWritable() override;
19};
20
21template <typename T_AsyncEventLoop>
23{
25 {
26 asyncWork.work.template bind<AsyncZLibTransformStreamT, &AsyncZLibTransformStreamT::work>(*this);
27 asyncWork.callback.template bind<AsyncZLibTransformStreamT, &AsyncZLibTransformStreamT::afterWork>(*this);
28 }
29
30 ZLibStream stream;
31
32 typename T_AsyncEventLoop::LoopWork asyncWork;
33
34 void setEventLoop(T_AsyncEventLoop& loop) { eventLoop = &loop; }
35
36 private:
37 T_AsyncEventLoop* eventLoop = nullptr;
38
39 virtual Result onProcess(Span<const char> input, Span<char> output) override
40 {
41 SC_ASYNC_STREAMS_ASSERT_RELEASE(not finalizing);
42 savedInput = input;
43 savedOutput = output;
44 finalizing = false;
45 SC_TRY_MSG(eventLoop != nullptr, "AsyncZLibTransformStreamT::setEventLoop not called");
46 return asyncWork.start(*eventLoop);
47 }
48
49 virtual Result onFinalize(Span<char> output) override
50 {
51 // Intentionally not resetting savedInput, that can contain leftover data to process
52 savedOutput = output;
53 finalizing = true;
54 SC_TRY_MSG(eventLoop != nullptr, "AsyncZLibTransformStreamT::setEventLoop not called");
55 return asyncWork.start(*eventLoop);
56 }
57
58 void afterWork(typename T_AsyncEventLoop::LoopWork::Result&)
59 {
60 if (finalizing)
61 {
62 AsyncTransformStream::afterFinalize(savedOutput, streamEnded);
63 }
64 else
65 {
66 AsyncTransformStream::afterProcess(savedInput, savedOutput);
67 }
68 }
69
70 Result work()
71 {
72 return finalizing ? stream.finalize(savedOutput, streamEnded) : stream.process(savedInput, savedOutput);
73 }
74
75 bool finalizing = false;
76 bool streamEnded = false;
77
78 Span<const char> savedInput;
79 Span<char> savedOutput;
80};
81
82} // namespace SC
Definition AsyncStreams.h:66
A stream that can both produce and consume buffers.
Definition AsyncStreams.h:492
A duplex stream that produces new buffers transforming received buffers.
Definition AsyncStreams.h:503
Definition ZLibTransformStreams.h:23
Definition ZLibTransformStreams.h:10