WPILibC++  2018.4.1-20180927020231-1209-ga732854
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
Stream.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2018 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 <memory>
15 
16 #include "wpi/ArrayRef.h"
17 #include "wpi/Signal.h"
18 #include "wpi/uv/Buffer.h"
19 #include "wpi/uv/Handle.h"
20 #include "wpi/uv/Request.h"
21 
22 namespace wpi {
23 namespace uv {
24 
25 class Stream;
26 
30 class ShutdownReq : public RequestImpl<ShutdownReq, uv_shutdown_t> {
31  public:
32  ShutdownReq();
33 
34  Stream& GetStream() const {
35  return *static_cast<Stream*>(GetRaw()->handle->data);
36  }
37 
42 };
43 
47 class WriteReq : public RequestImpl<WriteReq, uv_write_t> {
48  public:
49  WriteReq();
50 
51  Stream& GetStream() const {
52  return *static_cast<Stream*>(GetRaw()->handle->data);
53  }
54 
60 };
61 
68 class Stream : public Handle {
69  public:
70  std::shared_ptr<Stream> shared_from_this() {
71  return std::static_pointer_cast<Stream>(Handle::shared_from_this());
72  }
73 
74  std::shared_ptr<const Stream> shared_from_this() const {
75  return std::static_pointer_cast<const Stream>(Handle::shared_from_this());
76  }
77 
85  void Shutdown(const std::shared_ptr<ShutdownReq>& req);
86 
95  void Shutdown(std::function<void()> callback = nullptr);
96 
106  void StartRead();
107 
113  void StopRead() { Invoke(&uv_read_stop, GetRawStream()); }
114 
131  void Write(ArrayRef<Buffer> bufs, const std::shared_ptr<WriteReq>& req);
132 
146  void Write(ArrayRef<Buffer> bufs,
147  std::function<void(MutableArrayRef<Buffer>, Error)> callback);
148 
159  int TryWrite(ArrayRef<Buffer> bufs);
160 
165  bool IsReadable() const noexcept {
166  return uv_is_readable(GetRawStream()) == 1;
167  }
168 
173  bool IsWritable() const noexcept {
174  return uv_is_writable(GetRawStream()) == 1;
175  }
176 
188  bool SetBlocking(bool enable) noexcept {
189  return uv_stream_set_blocking(GetRawStream(), enable) == 0;
190  }
191 
196  size_t GetWriteQueueSize() const noexcept {
197  return GetRawStream()->write_queue_size;
198  }
199 
205  uv_stream_t* GetRawStream() const noexcept {
206  return reinterpret_cast<uv_stream_t*>(GetRawHandle());
207  }
208 
213 
218 
219  protected:
220  explicit Stream(uv_stream_t* uv_stream)
221  : Handle{reinterpret_cast<uv_handle_t*>(uv_stream)} {}
222 };
223 
224 template <typename T, typename U>
225 class StreamImpl : public Stream {
226  public:
227  std::shared_ptr<T> shared_from_this() {
228  return std::static_pointer_cast<T>(Handle::shared_from_this());
229  }
230 
231  std::shared_ptr<const T> shared_from_this() const {
232  return std::static_pointer_cast<const T>(Handle::shared_from_this());
233  }
234 
240  U* GetRaw() const noexcept {
241  return reinterpret_cast<U*>(this->GetRawHandle());
242  }
243 
244  protected:
245  StreamImpl() : Stream{reinterpret_cast<uv_stream_t*>(new U)} {}
246 };
247 
248 } // namespace uv
249 } // namespace wpi
250 
251 #endif // WPIUTIL_WPI_UV_STREAM_H_
void Write(ArrayRef< Buffer > bufs, const std::shared_ptr< WriteReq > &req)
Write data to the stream.
int TryWrite(ArrayRef< Buffer > bufs)
Queue a write request if it can be completed immediately.
void StopRead()
Stop reading data from the stream.
Definition: Stream.h:113
void StartRead()
Start reading data from an incoming stream.
sig::Signal complete
Shutdown completed signal.
Definition: Stream.h:41
size_t GetWriteQueueSize() const noexcept
Gets the amount of queued bytes waiting to be sent.
Definition: Stream.h:196
sig::Signal end
Signal generated when no more read data is available.
Definition: Stream.h:217
sig::Signal< Error > finish
Write completed signal.
Definition: Stream.h:59
Definition: uv.h:466
Write request.
Definition: Stream.h:47
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:41
WPILib C++ utilities (wpiutil) namespace.
Definition: SmallString.h:21
Stream handle.
Definition: Stream.h:68
uv_shutdown_t * GetRaw() noexcept
Get the underlying request data structure.
Definition: Request.h:149
sig::Signal< Buffer &, size_t > data
Signal generated when data was read on a stream.
Definition: Stream.h:212
U * GetRaw() const noexcept
Get the underlying handle data structure.
Definition: Stream.h:240
bool SetBlocking(bool enable) noexcept
Enable or disable blocking mode for a stream.
Definition: Stream.h:188
uv_stream_t * GetRawStream() const noexcept
Get the underlying stream data structure.
Definition: Stream.h:205
Shutdown request.
Definition: Stream.h:30
Definition: uv.h:416
bool IsWritable() const noexcept
Checks if the stream is writable.
Definition: Stream.h:173
Error code.
Definition: Error.h:19
SignalBase is an implementation of the observer pattern, through the use of an emitting object and sl...
Definition: Signal.h:495
Request.
Definition: Request.h:134
Handle.
Definition: Handle.h:32
uv_handle_t * GetRawHandle() const noexcept
Get the underlying handle data structure.
Definition: Handle.h:153
void Shutdown(const std::shared_ptr< ShutdownReq > &req)
Shutdown the outgoing (write) side of a duplex stream.
bool IsReadable() const noexcept
Check if the stream is readable.
Definition: Stream.h:165
Definition: Stream.h:225
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:291