2019-02-23 04:15:54 -05:00
|
|
|
#pragma once
|
|
|
|
|
2021-08-13 02:08:56 -07:00
|
|
|
#include <sys/uio.h>
|
|
|
|
#include <cstdio>
|
2019-02-23 04:15:54 -05:00
|
|
|
#include <memory>
|
|
|
|
|
2020-03-09 01:50:30 -07:00
|
|
|
#include "../files.hpp"
|
2019-11-23 04:57:52 -05:00
|
|
|
|
2024-02-28 11:19:56 -08:00
|
|
|
#define ENABLE_IOV 0
|
|
|
|
|
2023-05-19 04:19:43 -07:00
|
|
|
struct out_stream {
|
|
|
|
virtual bool write(const void *buf, size_t len) = 0;
|
2024-02-28 11:19:56 -08:00
|
|
|
#if ENABLE_IOV
|
2021-08-13 02:08:56 -07:00
|
|
|
virtual ssize_t writev(const iovec *iov, int iovcnt);
|
2024-02-28 11:19:56 -08:00
|
|
|
#endif
|
2023-05-19 04:19:43 -07:00
|
|
|
virtual ~out_stream() = default;
|
2019-11-20 21:48:49 -05:00
|
|
|
};
|
|
|
|
|
2023-05-19 04:19:43 -07:00
|
|
|
using out_strm_ptr = std::unique_ptr<out_stream>;
|
2019-12-13 00:37:06 -05:00
|
|
|
|
|
|
|
// Delegates all operations to base stream
|
2023-05-19 04:19:43 -07:00
|
|
|
class filter_out_stream : public out_stream {
|
2019-11-20 21:48:49 -05:00
|
|
|
public:
|
2023-05-19 04:19:43 -07:00
|
|
|
filter_out_stream(out_strm_ptr &&base) : base(std::move(base)) {}
|
2021-11-21 04:38:22 -08:00
|
|
|
bool write(const void *buf, size_t len) override;
|
2019-11-20 21:48:49 -05:00
|
|
|
protected:
|
2023-05-19 04:19:43 -07:00
|
|
|
out_strm_ptr base;
|
2019-11-20 21:48:49 -05:00
|
|
|
};
|
|
|
|
|
2021-11-21 02:27:52 -08:00
|
|
|
// Buffered output stream, writing in chunks
|
2023-05-19 04:19:43 -07:00
|
|
|
class chunk_out_stream : public filter_out_stream {
|
2021-11-21 02:27:52 -08:00
|
|
|
public:
|
2023-05-19 04:19:43 -07:00
|
|
|
chunk_out_stream(out_strm_ptr &&base, size_t buf_sz, size_t chunk_sz)
|
2023-05-20 14:19:40 -07:00
|
|
|
: filter_out_stream(std::move(base)), chunk_sz(chunk_sz), data(buf_sz) {}
|
2021-11-21 02:27:52 -08:00
|
|
|
|
2023-05-19 04:19:43 -07:00
|
|
|
chunk_out_stream(out_strm_ptr &&base, size_t buf_sz = 4096)
|
2021-11-21 02:27:52 -08:00
|
|
|
: chunk_out_stream(std::move(base), buf_sz, buf_sz) {}
|
|
|
|
|
2021-11-21 04:38:22 -08:00
|
|
|
bool write(const void *buf, size_t len) final;
|
2021-11-21 02:27:52 -08:00
|
|
|
|
|
|
|
protected:
|
2021-11-23 16:50:08 -08:00
|
|
|
// Classes inheriting this class has to call finalize() in its destructor
|
|
|
|
void finalize();
|
2023-05-20 14:19:40 -07:00
|
|
|
virtual bool write_chunk(const void *buf, size_t len, bool final);
|
2021-11-21 02:27:52 -08:00
|
|
|
|
|
|
|
size_t chunk_sz;
|
|
|
|
|
|
|
|
private:
|
|
|
|
size_t buf_off = 0;
|
2023-05-20 14:19:40 -07:00
|
|
|
heap_data data;
|
2021-11-21 02:27:52 -08:00
|
|
|
};
|
|
|
|
|
2023-05-19 04:19:43 -07:00
|
|
|
struct in_stream {
|
|
|
|
virtual ssize_t read(void *buf, size_t len) = 0;
|
2024-02-28 11:19:56 -08:00
|
|
|
ssize_t readFully(void *buf, size_t len);
|
|
|
|
#if ENABLE_IOV
|
2023-05-19 04:19:43 -07:00
|
|
|
virtual ssize_t readv(const iovec *iov, int iovcnt);
|
2024-02-28 11:19:56 -08:00
|
|
|
#endif
|
2023-05-19 04:19:43 -07:00
|
|
|
virtual ~in_stream() = default;
|
|
|
|
};
|
|
|
|
|
2024-02-28 11:07:53 -08:00
|
|
|
// A stream is something that is writable and readable
|
2024-02-28 15:52:03 -08:00
|
|
|
struct stream : public out_stream, public in_stream {};
|
2023-05-19 04:19:43 -07:00
|
|
|
|
2024-02-28 11:07:53 -08:00
|
|
|
using stream_ptr = std::unique_ptr<stream>;
|
2023-05-19 04:19:43 -07:00
|
|
|
|
2024-02-28 11:07:53 -08:00
|
|
|
// Byte stream that dynamically allocates memory
|
|
|
|
class byte_stream : public stream {
|
2019-11-20 21:48:49 -05:00
|
|
|
public:
|
2024-02-28 11:07:53 -08:00
|
|
|
byte_stream(heap_data &data) : _data(data) {}
|
2021-08-13 02:08:56 -07:00
|
|
|
|
|
|
|
ssize_t read(void *buf, size_t len) override;
|
2021-11-21 04:38:22 -08:00
|
|
|
bool write(const void *buf, size_t len) override;
|
2019-11-20 21:48:49 -05:00
|
|
|
|
|
|
|
private:
|
2023-05-20 14:19:40 -07:00
|
|
|
heap_data &_data;
|
2020-12-30 22:11:24 -08:00
|
|
|
size_t _pos = 0;
|
|
|
|
size_t _cap = 0;
|
2019-11-20 21:48:49 -05:00
|
|
|
|
2023-05-20 14:19:40 -07:00
|
|
|
void resize(size_t new_sz, bool zero = false);
|
2019-11-20 21:48:49 -05:00
|
|
|
};
|
|
|
|
|
2024-02-28 11:07:53 -08:00
|
|
|
class rust_vec_stream : public stream {
|
2023-12-08 23:30:55 +08:00
|
|
|
public:
|
2024-02-28 11:07:53 -08:00
|
|
|
rust_vec_stream(rust::Vec<uint8_t> &data) : _data(data) {}
|
2023-12-08 23:30:55 +08:00
|
|
|
|
|
|
|
ssize_t read(void *buf, size_t len) override;
|
|
|
|
bool write(const void *buf, size_t len) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
rust::Vec<uint8_t> &_data;
|
|
|
|
size_t _pos = 0;
|
|
|
|
|
2023-12-09 01:17:19 +08:00
|
|
|
void ensure_size(size_t sz, bool zero = false);
|
2023-12-08 23:30:55 +08:00
|
|
|
};
|
|
|
|
|
2024-02-28 11:07:53 -08:00
|
|
|
class file_stream : public stream {
|
2021-11-21 04:38:22 -08:00
|
|
|
public:
|
|
|
|
bool write(const void *buf, size_t len) final;
|
|
|
|
protected:
|
|
|
|
virtual ssize_t do_write(const void *buf, size_t len) = 0;
|
|
|
|
};
|
|
|
|
|
2024-02-28 11:07:53 -08:00
|
|
|
// File stream but does not close the file descriptor at any time
|
|
|
|
class fd_stream : public file_stream {
|
2019-11-20 21:48:49 -05:00
|
|
|
public:
|
2024-02-28 11:07:53 -08:00
|
|
|
fd_stream(int fd) : fd(fd) {}
|
2021-08-13 02:08:56 -07:00
|
|
|
ssize_t read(void *buf, size_t len) override;
|
2024-02-28 11:19:56 -08:00
|
|
|
#if ENABLE_IOV
|
2021-08-13 02:08:56 -07:00
|
|
|
ssize_t readv(const iovec *iov, int iovcnt) override;
|
|
|
|
ssize_t writev(const iovec *iov, int iovcnt) override;
|
2024-02-28 11:19:56 -08:00
|
|
|
#endif
|
2021-11-21 04:38:22 -08:00
|
|
|
protected:
|
|
|
|
ssize_t do_write(const void *buf, size_t len) override;
|
2019-11-20 21:48:49 -05:00
|
|
|
private:
|
2020-12-30 22:11:24 -08:00
|
|
|
int fd;
|
2019-11-20 21:48:49 -05:00
|
|
|
};
|
2019-12-13 00:37:06 -05:00
|
|
|
|
|
|
|
/* ****************************************
|
2024-02-28 11:07:53 -08:00
|
|
|
* Bridge between stream class and C stdio
|
2019-12-13 00:37:06 -05:00
|
|
|
* ****************************************/
|
|
|
|
|
2024-02-28 11:07:53 -08:00
|
|
|
// stream_ptr -> sFILE
|
|
|
|
sFILE make_stream_fp(stream_ptr &&strm);
|
2019-12-13 00:37:06 -05:00
|
|
|
|
|
|
|
template <class T, class... Args>
|
2024-02-28 11:07:53 -08:00
|
|
|
sFILE make_stream_fp(Args &&... args) {
|
|
|
|
return make_stream_fp(stream_ptr(new T(std::forward<Args>(args)...)));
|
2019-12-13 00:37:06 -05:00
|
|
|
}
|