WPILibC++  2018.4.1-1235-g11e5faf
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
NetworkStream.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_NETWORKSTREAM_H_
9 #define WPIUTIL_WPI_UV_NETWORKSTREAM_H_
10 
11 #include <uv.h>
12 
13 #include <functional>
14 #include <memory>
15 
16 #include "wpi/Signal.h"
17 #include "wpi/uv/Stream.h"
18 
19 namespace wpi {
20 namespace uv {
21 
22 class NetworkStream;
23 
27 class ConnectReq : public RequestImpl<ConnectReq, uv_connect_t> {
28  public:
29  ConnectReq();
30 
31  NetworkStream& GetStream() const {
32  return *static_cast<NetworkStream*>(GetRaw()->handle->data);
33  }
34 
39 };
40 
46 class NetworkStream : public Stream {
47  public:
48  static constexpr int kDefaultBacklog = 128;
49 
50  std::shared_ptr<NetworkStream> shared_from_this() {
51  return std::static_pointer_cast<NetworkStream>(Handle::shared_from_this());
52  }
53 
54  std::shared_ptr<const NetworkStream> shared_from_this() const {
55  return std::static_pointer_cast<const NetworkStream>(
56  Handle::shared_from_this());
57  }
58 
65  void Listen(int backlog = kDefaultBacklog);
66 
77  void Listen(std::function<void()> callback, int backlog = kDefaultBacklog);
78 
94  std::shared_ptr<NetworkStream> Accept() {
95  return DoAccept()->shared_from_this();
96  }
97 
114  bool Accept(const std::shared_ptr<NetworkStream>& client) {
115  return Invoke(&uv_accept, GetRawStream(), client->GetRawStream());
116  }
117 
122 
123  protected:
124  explicit NetworkStream(uv_stream_t* uv_stream) : Stream{uv_stream} {}
125 
126  virtual NetworkStream* DoAccept() = 0;
127 };
128 
129 template <typename T, typename U>
131  public:
132  std::shared_ptr<T> shared_from_this() {
133  return std::static_pointer_cast<T>(Handle::shared_from_this());
134  }
135 
136  std::shared_ptr<const T> shared_from_this() const {
137  return std::static_pointer_cast<const T>(Handle::shared_from_this());
138  }
139 
145  U* GetRaw() const noexcept {
146  return reinterpret_cast<U*>(this->GetRawHandle());
147  }
148 
149  protected:
150  NetworkStreamImpl() : NetworkStream{reinterpret_cast<uv_stream_t*>(new U)} {}
151 };
152 
153 } // namespace uv
154 } // namespace wpi
155 
156 #endif // WPIUTIL_WPI_UV_NETWORKSTREAM_H_
Definition: NetworkStream.h:17
std::shared_ptr< NetworkStream > Accept()
Accept incoming connection.
Definition: NetworkStream.h:94
bool Accept(const std::shared_ptr< NetworkStream > &client)
Accept incoming connection.
Definition: NetworkStream.h:114
U * GetRaw() const noexcept
Get the underlying handle data structure.
Definition: NetworkStream.h:145
Definition: uv.h:466
WPILib C++ utilities (wpiutil) namespace.
Definition: SmallString.h:21
Stream handle.
Definition: Stream.h:68
uv_connect_t * GetRaw() noexcept
Get the underlying request data structure.
Definition: Request.h:149
Definition: NetworkStream.h:130
sig::Signal connection
Signal generated when an incoming connection is received.
Definition: NetworkStream.h:121
Network stream handle.
Definition: NetworkStream.h:46
uv_stream_t * GetRawStream() const noexcept
Get the underlying stream data structure.
Definition: Stream.h:205
void Listen(int backlog=kDefaultBacklog)
Start listening for incoming connections.
Connection request.
Definition: NetworkStream.h:27
sig::Signal connected
Connection completed signal.
Definition: NetworkStream.h:38
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
uv_handle_t * GetRawHandle() const noexcept
Get the underlying handle data structure.
Definition: Handle.h:153