WPILibC++  2020.3.2
Stream.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2018-2019 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #ifndef WPIUTIL_WPI_UV_STREAM_H_
9 #define WPIUTIL_WPI_UV_STREAM_H_
10 
11 #include <uv.h>
12 
13 #include <functional>
14 #include <initializer_list>
15 #include <memory>
16 
17 #include "wpi/ArrayRef.h"
18 #include "wpi/Signal.h"
19 #include "wpi/uv/Buffer.h"
20 #include "wpi/uv/Handle.h"
21 #include "wpi/uv/Request.h"
22 
23 namespace wpi {
24 namespace uv {
25 
26 class Stream;
27 
31 class ShutdownReq : public RequestImpl<ShutdownReq, uv_shutdown_t> {
32  public:
33  ShutdownReq();
34 
35  Stream& GetStream() const {
36  return *static_cast<Stream*>(GetRaw()->handle->data);
37  }
38 
43 };
44 
48 class WriteReq : public RequestImpl<WriteReq, uv_write_t> {
49  public:
50  WriteReq();
51 
52  Stream& GetStream() const {
53  return *static_cast<Stream*>(GetRaw()->handle->data);
54  }
55 
61 };
62 
69 class Stream : public Handle {
70  public:
71  std::shared_ptr<Stream> shared_from_this() {
72  return std::static_pointer_cast<Stream>(Handle::shared_from_this());
73  }
74 
75  std::shared_ptr<const Stream> shared_from_this() const {
76  return std::static_pointer_cast<const Stream>(Handle::shared_from_this());
77  }
78 
86  void Shutdown(const std::shared_ptr<ShutdownReq>& req);
87 
96  void Shutdown(std::function<void()> callback = nullptr);
97 
107  void StartRead();
108 
114  void StopRead() { Invoke(&uv_read_stop, GetRawStream()); }
115 
132  void Write(ArrayRef<Buffer> bufs, const std::shared_ptr<WriteReq>& req);
133 
150  void Write(std::initializer_list<Buffer> bufs,
151  const std::shared_ptr<WriteReq>& req) {
152  Write(makeArrayRef(bufs.begin(), bufs.end()), req);
153  }
154 
168  void Write(ArrayRef<Buffer> bufs,
169  std::function<void(MutableArrayRef<Buffer>, Error)> callback);
170 
184  void Write(std::initializer_list<Buffer> bufs,
185  std::function<void(MutableArrayRef<Buffer>, Error)> callback) {
186  Write(makeArrayRef(bufs.begin(), bufs.end()), callback);
187  }
188 
199  int TryWrite(ArrayRef<Buffer> bufs);
200 
211  int TryWrite(std::initializer_list<Buffer> bufs) {
212  return TryWrite(makeArrayRef(bufs.begin(), bufs.end()));
213  }
214 
219  bool IsReadable() const noexcept {
220  return uv_is_readable(GetRawStream()) == 1;
221  }
222 
227  bool IsWritable() const noexcept {
228  return uv_is_writable(GetRawStream()) == 1;
229  }
230 
242  bool SetBlocking(bool enable) noexcept {
243  return uv_stream_set_blocking(GetRawStream(), enable) == 0;
244  }
245 
250  size_t GetWriteQueueSize() const noexcept {
251  return GetRawStream()->write_queue_size;
252  }
253 
259  uv_stream_t* GetRawStream() const noexcept {
260  return reinterpret_cast<uv_stream_t*>(GetRawHandle());
261  }
262 
267 
272 
273  protected:
274  explicit Stream(uv_stream_t* uv_stream)
275  : Handle{reinterpret_cast<uv_handle_t*>(uv_stream)} {}
276 };
277 
278 template <typename T, typename U>
279 class StreamImpl : public Stream {
280  public:
281  std::shared_ptr<T> shared_from_this() {
282  return std::static_pointer_cast<T>(Handle::shared_from_this());
283  }
284 
285  std::shared_ptr<const T> shared_from_this() const {
286  return std::static_pointer_cast<const T>(Handle::shared_from_this());
287  }
288 
294  U* GetRaw() const noexcept {
295  return reinterpret_cast<U*>(this->GetRawHandle());
296  }
297 
298  protected:
299  StreamImpl() : Stream{reinterpret_cast<uv_stream_t*>(new U)} {}
300 };
301 
302 } // namespace uv
303 } // namespace wpi
304 
305 #endif // WPIUTIL_WPI_UV_STREAM_H_
wpi::uv::Stream::TryWrite
int TryWrite(std::initializer_list< Buffer > bufs)
Queue a write request if it can be completed immediately.
Definition: Stream.h:211
wpi::uv::Stream
Stream handle.
Definition: Stream.h:69
wpi::uv::Stream::Write
void Write(std::initializer_list< Buffer > bufs, std::function< void(MutableArrayRef< Buffer >, Error)> callback)
Write data to the stream.
Definition: Stream.h:184
wpi::uv::Stream::IsWritable
bool IsWritable() const noexcept
Checks if the stream is writable.
Definition: Stream.h:227
wpi::uv::ShutdownReq::complete
sig::Signal complete
Shutdown completed signal.
Definition: Stream.h:42
wpi::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:42
wpi::uv::Stream::IsReadable
bool IsReadable() const noexcept
Check if the stream is readable.
Definition: Stream.h:219
wpi::uv::Stream::Write
void Write(std::initializer_list< Buffer > bufs, const std::shared_ptr< WriteReq > &req)
Write data to the stream.
Definition: Stream.h:150
wpi::uv::Stream::Write
void Write(ArrayRef< Buffer > bufs, const std::shared_ptr< WriteReq > &req)
Write data to the stream.
wpi::uv::Handle::GetRawHandle
uv_handle_t * GetRawHandle() const noexcept
Get the underlying handle data structure.
Definition: Handle.h:178
wpi::uv::StreamImpl
Definition: Stream.h:279
wpi::uv::WriteReq
Write request.
Definition: Stream.h:48
wpi::uv::Stream::StopRead
void StopRead()
Stop reading data from the stream.
Definition: Stream.h:114
wpi::makeArrayRef
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:447
wpi::uv::Handle
Handle.
Definition: Handle.h:32
wpi::uv::ShutdownReq
Shutdown request.
Definition: Stream.h:31
wpi::uv::RequestImpl< ShutdownReq, uv_shutdown_t >::GetRaw
uv_shutdown_t * GetRaw() noexcept
Get the underlying request data structure.
Definition: Request.h:149
wpi
WPILib C++ utilities (wpiutil) namespace.
Definition: EventLoopRunner.h:17
wpi::uv::WriteReq::finish
sig::Signal< Error > finish
Write completed signal.
Definition: Stream.h:60
wpi::uv::StreamImpl::GetRaw
U * GetRaw() const noexcept
Get the underlying handle data structure.
Definition: Stream.h:294
wpi::uv::Stream::GetRawStream
uv_stream_t * GetRawStream() const noexcept
Get the underlying stream data structure.
Definition: Stream.h:259
wpi::uv::Stream::GetWriteQueueSize
size_t GetWriteQueueSize() const noexcept
Gets the amount of queued bytes waiting to be sent.
Definition: Stream.h:250
wpi::MutableArrayRef
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:287
wpi::uv::RequestImpl
Request.
Definition: Request.h:134
wpi::uv::Stream::Shutdown
void Shutdown(const std::shared_ptr< ShutdownReq > &req)
Shutdown the outgoing (write) side of a duplex stream.
wpi::uv::Stream::data
sig::Signal< Buffer &, size_t > data
Signal generated when data was read on a stream.
Definition: Stream.h:266
wpi::uv::Stream::TryWrite
int TryWrite(ArrayRef< Buffer > bufs)
Queue a write request if it can be completed immediately.
wpi::uv::Stream::StartRead
void StartRead()
Start reading data from an incoming stream.
wpi::uv::Error
Error code.
Definition: Error.h:19
wpi::uv::Stream::end
sig::Signal end
Signal generated when no more read data is available.
Definition: Stream.h:271
wpi::sig::SignalBase
SignalBase is an implementation of the observer pattern, through the use of an emitting object and sl...
Definition: Signal.h:495
wpi::uv::Stream::SetBlocking
bool SetBlocking(bool enable) noexcept
Enable or disable blocking mode for a stream.
Definition: Stream.h:242