Nexus HTTP/3
A QUIC and HTTP/3 library
stream.hpp
1 #pragma once
2 
3 #include <nexus/error_code.hpp>
4 #include <nexus/quic/stream_id.hpp>
5 #include <nexus/quic/detail/stream_impl.hpp>
6 
7 namespace nexus::quic {
8 
9 namespace detail {
10 
11 struct connection_impl;
12 
13 template <typename Stream> struct stream_factory;
14 
15 } // namespace detail
16 
17 class connection;
18 
21 class stream {
22  protected:
23  friend class connection;
24  friend class detail::connection_impl;
25  detail::stream_impl impl;
26  explicit stream(detail::connection_impl& impl);
27  public:
29  explicit stream(connection& conn);
30 
33 
34  stream(const stream&) = delete;
35  stream& operator=(const stream&) = delete;
36  stream(stream&&) = delete;
37  stream& operator=(stream&&) = delete;
38 
40  using executor_type = detail::stream_impl::executor_type;
41 
44 
46  bool is_open() const;
47 
50  stream_id id(error_code& ec) const;
52  stream_id id() const;
53 
55  template <typename MutableBufferSequence,
56  typename CompletionToken> // void(error_code, size_t)
57  decltype(auto) async_read_some(const MutableBufferSequence& buffers,
58  CompletionToken&& token) {
59  return impl.async_read_some(buffers, std::forward<CompletionToken>(token));
60  }
61 
63  template <typename MutableBufferSequence>
64  size_t read_some(const MutableBufferSequence& buffers, error_code& ec) {
65  return impl.read_some(buffers, ec);
66  }
68  template <typename MutableBufferSequence>
69  size_t read_some(const MutableBufferSequence& buffers) {
70  error_code ec;
71  const size_t bytes = impl.read_some(buffers, ec);
72  if (ec) {
73  throw system_error(ec);
74  }
75  return bytes;
76  }
77 
80  template <typename ConstBufferSequence,
81  typename CompletionToken> // void(error_code, size_t)
82  decltype(auto) async_write_some(const ConstBufferSequence& buffers,
83  CompletionToken&& token) {
84  return impl.async_write_some(buffers, std::forward<CompletionToken>(token));
85  }
86 
89  template <typename ConstBufferSequence>
90  size_t write_some(const ConstBufferSequence& buffers, error_code& ec) {
91  return impl.write_some(buffers, ec);
92  }
94  template <typename ConstBufferSequence>
95  size_t write_some(const ConstBufferSequence& buffers) {
96  error_code ec;
97  const size_t bytes = impl.write_some(buffers, ec);
98  if (ec) {
99  throw system_error(ec);
100  }
101  return bytes;
102  }
103 
106  void flush(error_code& ec);
108  void flush();
109 
114  void shutdown(int how, error_code& ec);
116  void shutdown(int how);
117 
121  template <typename CompletionToken> // void(error_code)
122  decltype(auto) async_close(CompletionToken&& token) {
123  return impl.async_close(std::forward<CompletionToken>(token));
124  }
126  void close(error_code& ec);
128  void close();
129 
132  void reset();
133 };
134 
135 } // namespace nexus::quic
a generic QUIC connection that can initiate outgoing streams and accept incoming streams
Definition: connection.hpp:14
a generic bidirectional QUIC stream that meets the type requirements of asio's AsyncRead/WriteStream ...
Definition: stream.hpp:21
stream_id id() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
void flush()
This is an overloaded member function, provided for convenience. It differs from the above function o...
void close()
This is an overloaded member function, provided for convenience. It differs from the above function o...
executor_type get_executor() const
return the associated io executor
stream_id id(error_code &ec) const
return the stream identifier if open. for streams initiated locally, an identifier may not be assigne...
size_t write_some(const ConstBufferSequence &buffers, error_code &ec)
write some bytes from the given buffer sequence. written bytes may be buffered until they fill an out...
Definition: stream.hpp:90
size_t write_some(const ConstBufferSequence &buffers)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: stream.hpp:95
~stream()
reset the stream on destruction
decltype(auto) async_close(CompletionToken &&token)
close the stream gracefully, blocking until all written data is acknowledged by the peer....
Definition: stream.hpp:122
size_t read_some(const MutableBufferSequence &buffers)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: stream.hpp:69
bool is_open() const
determine whether the stream is open
void flush(error_code &ec)
flush any bytes that were buffered by write_some()/async_write_some() but not yet delivered
decltype(auto) async_write_some(const ConstBufferSequence &buffers, CompletionToken &&token)
write some bytes from the given buffer sequence. written bytes may be buffered until they fill an out...
Definition: stream.hpp:82
void reset()
reset the stream immediately in both directions, canceling any pending operations and discarding any ...
void close(error_code &ec)
This is an overloaded member function, provided for convenience. It differs from the above function o...
size_t read_some(const MutableBufferSequence &buffers, error_code &ec)
read some bytes into the given buffer sequence
Definition: stream.hpp:64
void shutdown(int how)
This is an overloaded member function, provided for convenience. It differs from the above function o...
detail::stream_impl::executor_type executor_type
the polymorphic executor type, boost::asio::any_io_executor
Definition: stream.hpp:40
stream(connection &conn)
construct a stream associated with the given connection
void shutdown(int how, error_code &ec)
shut down a stream for reads (0), writes (1), or both (2). shutting down the read side will cancel an...
decltype(auto) async_read_some(const MutableBufferSequence &buffers, CompletionToken &&token)
read some bytes into the given buffer sequence
Definition: stream.hpp:57
Generic QUIC library.
Definition: client.hpp:8
uint64_t stream_id
stream identifier that is unique to a connection
Definition: stream_id.hpp:8