Symbian platform (C++)
SDK native APIs, runtime and tooling
Loading...
Searching...
No Matches
byte_stream.h
Go to the documentation of this file.
1// Copyright 2026 The Symbian SDK Authors.
2// Licensed under the Apache License, Version 2.0.
3#ifndef SYMBIAN_NET_BYTE_STREAM_H_
4#define SYMBIAN_NET_BYTE_STREAM_H_
5#include <cstdint>
6#include <functional>
7#include <memory>
8#include <span>
9#include <utility>
10
11#include <absl/base/nullability.h>
12
13#include "absl/status/status.h"
14#include "absl/status/statusor.h"
15#include "absl/time/time.h"
16
17namespace symbian::net {
23class ByteStream final {
24 public:
25 ByteStream() = default;
26
27 template <typename Transport>
28 ByteStream(std::unique_ptr<Transport> transport)
29 : owner_(transport.release(), Destroy<Transport>),
30 read_(ReadTransport<Transport>),
31 write_(WriteTransport<Transport>),
32 close_(CloseTransport<Transport>) {}
33
34 ByteStream(ByteStream&&) noexcept = default;
35 ByteStream& operator=(ByteStream&&) noexcept = default;
36 ByteStream(const ByteStream&) = delete;
37 ByteStream& operator=(const ByteStream&) = delete;
38
39 explicit operator bool() const { return owner_ != nullptr; }
40
41 absl::StatusOr<std::size_t> Read(std::span<std::uint8_t> bytes,
42 absl::Time deadline) {
43 if (owner_ == nullptr) {
44 return absl::FailedPreconditionError("Byte stream is empty");
45 }
46 return read_(owner_.get(), bytes, deadline);
47 }
48
49 absl::Status Write(std::span<const std::uint8_t> bytes, absl::Time deadline) {
50 if (owner_ == nullptr) {
51 return absl::FailedPreconditionError("Byte stream is empty");
52 }
53 return write_(owner_.get(), bytes, deadline);
54 }
55
56 void Close() {
57 if (owner_ != nullptr) {
58 close_(owner_.get());
59 }
60 }
61
62 private:
63 template <typename Transport>
64 static void Destroy(void* absl_nonnull owner) {
65 delete static_cast<Transport*>(owner);
66 }
67
68 template <typename Transport>
69 static absl::StatusOr<std::size_t> ReadTransport(
70 void* absl_nonnull owner, std::span<std::uint8_t> bytes,
71 absl::Time deadline) {
72 return static_cast<Transport*>(owner)->Read(bytes, deadline);
73 }
74
75 template <typename Transport>
76 static absl::Status WriteTransport(void* absl_nonnull owner,
77 std::span<const std::uint8_t> bytes,
78 absl::Time deadline) {
79 return static_cast<Transport*>(owner)->Write(bytes, deadline);
80 }
81
82 template <typename Transport>
83 static void CloseTransport(void* absl_nonnull owner) {
84 static_cast<Transport*>(owner)->Close();
85 }
86
87 std::unique_ptr<void, std::function<void(void* absl_nonnull)>> owner_{
88 nullptr, [](void* absl_nullable) {}};
89 std::function<absl::StatusOr<std::size_t>(void* absl_nonnull,
90 std::span<std::uint8_t>,
91 absl::Time)> read_;
92 std::function<absl::Status(void* absl_nonnull,
93 std::span<const std::uint8_t>, absl::Time)>
94 write_;
95 std::function<void(void* absl_nonnull)> close_;
96};
97} // namespace symbian::net
98#endif
Move-only owner of one TCP, TLS, or application transport.
Definition byte_stream.h:23
void Close()
Definition byte_stream.h:56
ByteStream(std::unique_ptr< Transport > transport)
Definition byte_stream.h:28
absl::Status Write(std::span< const std::uint8_t > bytes, absl::Time deadline)
Definition byte_stream.h:49
absl::StatusOr< std::size_t > Read(std::span< std::uint8_t > bytes, absl::Time deadline)
Definition byte_stream.h:41
ByteStream(ByteStream &&) noexcept=default
Definition byte_stream.h:17
std::string_view bytes
Definition exports.cc:76