-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforge_memory_stream_example.cpp
More file actions
125 lines (112 loc) · 4.33 KB
/
Copy pathforge_memory_stream_example.cpp
File metadata and controls
125 lines (112 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// MIT License
//
// Copyright (c) 2026 Forge Project
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include <forge/io/memory_stream.hpp>
#include "example_support.hpp"
#include <array>
#include <cstddef>
#include <iostream>
#include <span>
#include <string>
#include <string_view>
#include <system_error>
#include <utility>
#include <vector>
namespace {
auto to_bytes(std::string_view text) -> std::vector<std::byte> {
std::vector<std::byte> bytes;
bytes.reserve(text.size());
for (char ch : text) {
bytes.push_back(std::byte{static_cast<unsigned char>(ch)});
}
return bytes;
}
template<class Stream>
auto read_length_prefixed_packet(Stream& stream)
-> forge::io::io_result<std::string> {
std::array<std::byte, 1> length_storage{};
auto length_result = stream.read_some(
forge::io::mutable_buffer{std::span{length_storage}});
auto [length_error, length_count] = length_result;
if (length_error) {
return forge::io::io_result<std::string>::failure(
length_error,
std::string{});
}
if (length_result.eof()) {
return forge::io::io_result<std::string>::end_of_file(std::string{});
}
if (length_count != 1) {
return forge::io::io_result<std::string>::failure(
std::make_error_code(std::errc::io_error),
std::string{});
}
const auto expected =
static_cast<std::size_t>(std::to_integer<unsigned char>(
length_storage[0]));
std::string payload(expected, '\0');
std::size_t offset = 0;
while (offset < expected) {
auto read_result = stream.read_some(
forge::io::mutable_buffer{
payload.data() + offset,
payload.size() - offset});
auto [read_error, read_count] = read_result;
offset += read_count;
if (read_error) {
payload.resize(offset);
return forge::io::io_result<std::string>::failure(
read_error,
std::move(payload));
}
if (read_result.eof()) {
payload.resize(offset);
return forge::io::io_result<std::string>::end_of_file(
std::move(payload));
}
if (read_count == 0) {
payload.resize(offset);
return forge::io::io_result<std::string>::failure(
std::make_error_code(std::errc::io_error),
std::move(payload));
}
}
return forge::io::io_result<std::string>::success(std::move(payload));
}
} // namespace
int main() {
auto first = to_bytes("he");
first.insert(first.begin(), std::byte{5});
forge::io::scripted_read_stream stream{
forge::io::scripted_read_step::bytes(
forge::io::const_buffer{first.data(), first.size()}),
forge::io::scripted_read_step::bytes("llo"),
forge::io::scripted_read_step::eof()};
auto [read_error, payload] = read_length_prefixed_packet(stream);
forge_example::require(!read_error);
forge_example::require(payload == "hello");
forge::io::memory_write_stream output;
auto [write_error, written] = output.write_some(
forge::io::const_buffer{payload.data(), payload.size()});
forge_example::require(!write_error);
forge_example::require(written == payload.size());
std::cout << "memory stream packet: " << payload << '\n';
}