summaryrefslogtreecommitdiff
path: root/modules/io_codec/c++/io_peer.tmpl.hpp
blob: d9dfe0469b4e407fafef173d702861019f36c462 (plain)
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
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, Encoding>>> feed,
		own<async_io_stream> str)
	: streaming_io_peer{std::move(feed), std::move(str), {}, {}, {}, {}} {}

template <typename Incoming, typename Outgoing, typename TransportEncoding, ContentEncoding, typename BufferT>
streaming_io_peer<Incoming, Outgoing, TransportEncoding, ContentEncoding,
				  BufferT>::
	streaming_io_peer(
		own<conveyor_feeder<data<Incoming, Encoding>>> feed,
		own<async_io_stream> stream, codec<Incoming, Encoding> in_codec, codec<Outgoing, Encoding> out_codec, BufferT in, BufferT out)
	: incoming_feeder_{std::move(feed)},
	  io_stream_{std::move(stream)}, 
		in_codec_{std::move(in_codec)},
		out_codec_{std::move(out_codec)},
	  in_buffer_{std::move(in)}, out_buffer_{std::move(out)},
	  sink_read_{
		  io_stream_->read_done()
			  .then([this](size_t bytes) -> error_or<void> {
				  in_buffer_.write_advance(bytes);

				  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) {
						buffer_view in_view{in_buffer_};
					  auto in_data = data<Incoming, Encoding>{in_view};

						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()} {
	io_stream_->read(&in_buffer_.write(), 1, in_buffer_.write_segment_length());
}

template <typename Incoming, typename Outgoing,
		  typename Encoding, typename BufferT>
error_or<void> streaming_io_peer<Incoming, Outgoing, Encoding,
						BufferT>::send(data<Outgoing>
										   msg) {
	bool restart_write = out_buffer_.read_segment_length() == 0;

	data<Outgoing, Encoding> enc;

	auto eov =
		out_codec_.encode(msg, enc);//msg.read(), out_buffer_);
	if (eov.is_error()) {
		return eov;
	}

	if (restart_write) {
		io_stream_->write(&out_buffer_.read(),
						  out_buffer_.read_segment_length());
	}

	return void_t{};
}

template <typename Incoming, typename Outgoing, typename Encoding,
		  typename BufferT>
conveyor<void>
streaming_io_peer<Incoming, Outgoing, Encoding,
				  BufferT>::on_read_disconnected() {
	return io_stream_->on_read_disconnected();
}

template <typename Incoming, typename Outgoing, typename Encoding, typename BufferT>
std::pair<own<streaming_io_peer<Incoming, Outgoing, Encoding, BufferT>>,
		  conveyor<data<Incoming,Encoding>>>
new_streaming_io_peer(own<async_io_stream> stream) {
	auto caf =
		new_conveyor_and_feeder<data<Incoming, Encoding>>();

	return {heap<streaming_io_peer<Incoming, Outgoing, Encoding,BufferT>>(
				std::move(caf.feeder), std::move(stream)),
			std::move(caf.conveyor)};
}

} // namespace saw