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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
namespace saw {
}
#include <iostream>
namespace saw {
template <typename Incoming, typename Outgoing, typename TransportEncoding, typename ContentEncoding,
typename BufferT>
streaming_io_peer<Incoming, Outgoing, TransportEncoding, ContentEncoding,
BufferT>::
streaming_io_peer(
own<conveyor_feeder<data<Incoming, ContentEncoding>>> feed,
own<async_io_stream> str)
: streaming_io_peer{std::move(feed), std::move(str), {}} {}
template <typename Incoming, typename Outgoing, typename TransportEncoding, typename ContentEncoding, typename BufferT>
streaming_io_peer<Incoming, Outgoing, TransportEncoding, ContentEncoding,
BufferT>::
streaming_io_peer(
own<conveyor_feeder<data<Incoming, ContentEncoding>>> feed,
own<async_io_stream> stream, transport<TransportEncoding> in_codec)
: incoming_feeder_{std::move(feed)},
io_stream_{std::move(stream)},
in_codec_{std::move(in_codec)},
in_buffer_{},
out_buffer_{},
sink_read_{
io_stream_->read_done()
.then([this](size_t bytes) -> error_or<void> {
std::cout<<"Read done start: "<<bytes<<std::endl;
in_buffer_.write_advance(bytes);
std::cout<<"buff state: ";
std::cout<<in_buffer_.read_composite_length();
std::cout<<" ";
std::cout<<in_buffer_.read_segment_length();
std::cout<<" ";
std::cout<<in_buffer_.write_composite_length();
std::cout<<" ";
std::cout<<in_buffer_.write_segment_length();
std::cout<<std::endl;
std::cout<<"buff state: ";
std::cout<<out_buffer_.read_composite_length();
std::cout<<" ";
std::cout<<out_buffer_.read_segment_length();
std::cout<<" ";
std::cout<<out_buffer_.write_composite_length();
std::cout<<" ";
std::cout<<out_buffer_.write_segment_length();
std::cout<<std::endl;
if (in_buffer_.write_segment_length() == 0) {
return make_error<err::critical>("Message too long");
}
io_stream_->read(&in_buffer_.write(), 1,
in_buffer_.write_segment_length());
while (true) {
auto eov = in_codec_.view_slice(in_buffer_);
if(eov.is_error()){
auto& err = eov.get_error();
if(err.template is_type<err::buffer_exhausted>()){
break;
}
return std::move(err);
}
auto& view_val = eov.get_value();
/**
* Allocate buffer for the advertised length
*/
own<buffer> in_buff = heap<array_buffer>(view_val.read_composite_length());
auto eo_len = in_buff->write_from(view_val);
if(eo_len.is_error()){
return std::move(eo_len.get_error());
}
auto& len_val = eo_len.get_value();
in_buffer_.read_advance(len_val);
in_buff->write_advance(len_val);
std::cout<<"read buff state: ";
std::cout<<in_buffer_.read_composite_length();
std::cout<<" ";
std::cout<<in_buffer_.read_segment_length();
std::cout<<" ";
std::cout<<in_buffer_.write_composite_length();
std::cout<<" ";
std::cout<<in_buffer_.write_segment_length();
std::cout<<std::endl;
std::cout<<"write buff state: ";
std::cout<<out_buffer_.read_composite_length();
std::cout<<" ";
std::cout<<out_buffer_.read_segment_length();
std::cout<<" ";
std::cout<<out_buffer_.write_composite_length();
std::cout<<" ";
std::cout<<out_buffer_.write_segment_length();
std::cout<<std::endl;
data<Incoming, ContentEncoding> in_data{std::move(in_buff)};
incoming_feeder_->feed(std::move(in_data));
}
return void_t{};
})
.sink([this](error err) {
incoming_feeder_->fail(err.copy_error());
return err;
})},
sink_write_{io_stream_->write_done()
.then([this](size_t bytes) -> error_or<void> {
out_buffer_.read_advance(bytes);
if (out_buffer_.read_composite_length() > 0) {
io_stream_->write(
&out_buffer_.read(),
out_buffer_.read_segment_length());
}
return void_t{};
})
.sink()
},
conveyor_feeder_{
*this
}
{
io_stream_->read(&in_buffer_.write(), 1, in_buffer_.write_segment_length());
}
template <typename Incoming, typename Outgoing, typename TransportEncoding,
typename ContentEncoding, typename BufferT>
error_or<void> streaming_io_peer<Incoming, Outgoing, TransportEncoding, ContentEncoding,
BufferT>::send(data<Outgoing, ContentEncoding>
msg) {
std::cout<<"O: "<<out_buffer_.read_segment_length()<<std::endl;
bool restart_write = (out_buffer_.read_segment_length() == 0u);
std::cout<<"A: "<<out_buffer_.read_segment_length()<<std::endl;
auto& msg_buff = msg.get_buffer();
{
auto eov = in_codec_.wrap(out_buffer_, msg_buff);
if(eov.is_error()){
auto& err = eov.get_error();
return std::move(err);
}
// auto& len_val = eov.get_value();
}
std::cout<<"B"<<std::endl;
auto eov = out_buffer_.write_from(msg_buff);
if (eov.is_error()) {
auto& err = eov.get_error();
return std::move(err);
}
auto& len_val = eov.get_value();
out_buffer_.write_advance(len_val);
std::cout<<"C"<<std::endl;
if (restart_write && out_buffer_.read_segment_length() > 0u) {
std::cout<<"D"<<std::endl;
io_stream_->write(&out_buffer_.read(),
out_buffer_.read_segment_length());
}
std::cout<<"E"<<std::endl;
return void_t{};
}
template <typename Incoming, typename Outgoing, typename TransportEncoding, typename ContentEncoding,
typename BufferT>
conveyor<void>
streaming_io_peer<Incoming, Outgoing, TransportEncoding, ContentEncoding,
BufferT>::on_read_disconnected() {
return io_stream_->on_read_disconnected();
}
template <typename Incoming, typename Outgoing, typename TransportEncoding, typename ContentEncoding, typename BufferT>
std::pair<own<streaming_io_peer<Incoming, Outgoing, TransportEncoding, ContentEncoding, BufferT>>,
conveyor<data<Incoming,ContentEncoding>>>
new_streaming_io_peer(own<async_io_stream> stream) {
auto caf =
new_conveyor_and_feeder<data<Incoming, ContentEncoding>>();
return {heap<streaming_io_peer<Incoming, Outgoing, TransportEncoding, ContentEncoding,BufferT>>(
std::move(caf.feeder), std::move(stream)),
std::move(caf.conveyor)};
}
} // namespace saw
|