WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
NetworkStream.hpp
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <cstdlib>
8#include <functional>
9#include <memory>
10
11#include <uv.h>
12
13#include "wpi/net/uv/Stream.hpp"
14#include "wpi/util/Signal.h"
15
16namespace wpi::net::uv {
17
18class NetworkStream;
19
20/**
21 * Connection request.
22 */
23class ConnectReq : public RequestImpl<ConnectReq, uv_connect_t> {
24 public:
26
28 return *static_cast<NetworkStream*>(GetRaw()->handle->data);
29 }
30
31 /**
32 * Connection completed signal.
33 */
35};
36
37/**
38 * Network stream handle.
39 * This is an abstract type; there are two network stream implementations (Tcp
40 * and Pipe).
41 */
42class NetworkStream : public Stream {
43 public:
44 static constexpr int kDefaultBacklog = 128;
45
46 std::shared_ptr<NetworkStream> shared_from_this() {
47 return std::static_pointer_cast<NetworkStream>(Handle::shared_from_this());
48 }
49
50 std::shared_ptr<const NetworkStream> shared_from_this() const {
51 return std::static_pointer_cast<const NetworkStream>(
52 Handle::shared_from_this());
53 }
54
55 /**
56 * Start listening for incoming connections. When a new incoming connection
57 * is received the connection signal is generated.
58 * @param backlog the number of connections the kernel might queue, same as
59 * listen(2).
60 */
61 void Listen(int backlog = kDefaultBacklog);
62
63 /**
64 * Start listening for incoming connections. This is a convenience wrapper
65 * around `Listen(int)` that also connects a callback to the connection
66 * signal. When a new incoming connection is received the connection signal
67 * is generated (and the callback is called).
68 * @param callback the callback to call when a connection is received.
69 * `Accept()` should be called from this callback.
70 * @param backlog the number of connections the kernel might queue, same as
71 * listen(2).
72 */
73 void Listen(std::function<void()> callback, int backlog = kDefaultBacklog);
74
75 /**
76 * Accept incoming connection.
77 *
78 * This call is used in conjunction with `Listen()` to accept incoming
79 * connections. Call this function after receiving a ListenEvent event to
80 * accept the connection.
81 * An error signal will be emitted in case of errors.
82 *
83 * When the connection signal is emitted it is guaranteed that this
84 * function will complete successfully the first time. If you attempt to use
85 * it more than once, it may fail.
86 * It is suggested to only call this function once per connection signal.
87 *
88 * @return The stream handle for the accepted connection, or nullptr on error.
89 */
90 std::shared_ptr<NetworkStream> Accept() {
91 return DoAccept()->shared_from_this();
92 }
93
94 /**
95 * Accept incoming connection.
96 *
97 * This call is used in conjunction with `Listen()` to accept incoming
98 * connections. Call this function after receiving a connection signal to
99 * accept the connection.
100 * An error signal will be emitted in case of errors.
101 *
102 * When the connection signal is emitted it is guaranteed that this
103 * function will complete successfully the first time. If you attempt to use
104 * it more than once, it may fail.
105 * It is suggested to only call this function once per connection signal.
106 *
107 * @param client Client stream object.
108 * @return False on error.
109 */
110 bool Accept(const std::shared_ptr<NetworkStream>& client) {
111 return Invoke(&uv_accept, GetRawStream(), client->GetRawStream());
112 }
113
114 /**
115 * Signal generated when an incoming connection is received.
116 */
118
119 protected:
120 explicit NetworkStream(uv_stream_t* uv_stream) : Stream{uv_stream} {}
121
122 virtual NetworkStream* DoAccept() = 0;
123};
124
125template <typename T, typename U>
127 public:
128 std::shared_ptr<T> shared_from_this() {
129 return std::static_pointer_cast<T>(Handle::shared_from_this());
130 }
131
132 std::shared_ptr<const T> shared_from_this() const {
133 return std::static_pointer_cast<const T>(Handle::shared_from_this());
134 }
135
136 /**
137 * Get the underlying handle data structure.
138 *
139 * @return The underlying handle data structure.
140 */
141 U* GetRaw() const noexcept {
142 return reinterpret_cast<U*>(this->GetRawHandle());
143 }
144
145 protected:
147 : NetworkStream{static_cast<uv_stream_t*>(std::malloc(sizeof(U)))} {}
148};
149
150} // namespace wpi::net::uv
NetworkStream & GetStream() const
Definition NetworkStream.hpp:27
wpi::util::sig::Signal connected
Connection completed signal.
Definition NetworkStream.hpp:34
bool Invoke(F &&f, Args &&... args) const
Definition Handle.hpp:265
uv_handle_t * GetRawHandle() const noexcept
Get the underlying handle data structure.
Definition Handle.hpp:178
Network stream handle.
Definition NetworkStream.hpp:42
std::shared_ptr< const NetworkStream > shared_from_this() const
Definition NetworkStream.hpp:50
virtual NetworkStream * DoAccept()=0
std::shared_ptr< NetworkStream > shared_from_this()
Definition NetworkStream.hpp:46
wpi::util::sig::Signal connection
Signal generated when an incoming connection is received.
Definition NetworkStream.hpp:117
NetworkStream(uv_stream_t *uv_stream)
Definition NetworkStream.hpp:120
bool Accept(const std::shared_ptr< NetworkStream > &client)
Accept incoming connection.
Definition NetworkStream.hpp:110
void Listen(std::function< void()> callback, int backlog=kDefaultBacklog)
Start listening for incoming connections.
std::shared_ptr< NetworkStream > Accept()
Accept incoming connection.
Definition NetworkStream.hpp:90
static constexpr int kDefaultBacklog
Definition NetworkStream.hpp:44
void Listen(int backlog=kDefaultBacklog)
Start listening for incoming connections.
std::shared_ptr< const T > shared_from_this() const
Definition NetworkStream.hpp:132
U * GetRaw() const noexcept
Get the underlying handle data structure.
Definition NetworkStream.hpp:141
std::shared_ptr< T > shared_from_this()
Definition NetworkStream.hpp:128
NetworkStreamImpl()
Definition NetworkStream.hpp:146
uv_connect_t * GetRaw() noexcept
Definition Request.hpp:149
Stream(uv_stream_t *uv_stream)
Definition Stream.hpp:303
uv_stream_t * GetRawStream() const noexcept
Get the underlying stream data structure.
Definition Stream.hpp:288
Definition StringMap.hpp:773
Definition Prepare.hpp:14
SignalBase< detail::NullMutex, T... > Signal
Specialization of SignalBase to be used in single threaded contexts.
Definition Signal.h:809
uv_stream_t * handle
Definition uv.h:639
UV_EXTERN int uv_accept(uv_stream_t *server, uv_stream_t *client)
struct uv_stream_s uv_stream_t
Definition uv.h:218