Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 1 | #ifndef AOS_NETWORK_WEB_PROXY_H_ |
| 2 | #define AOS_NETWORK_WEB_PROXY_H_ |
Austin Schuh | 52e5e3a | 2021-04-24 22:30:02 -0700 | [diff] [blame] | 3 | |
| 4 | #include <deque> |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 5 | #include <map> |
| 6 | #include <set> |
Austin Schuh | 52e5e3a | 2021-04-24 22:30:02 -0700 | [diff] [blame] | 7 | |
James Kuszmaul | 7ad9152 | 2020-09-01 19:15:35 -0700 | [diff] [blame] | 8 | #include "aos/events/event_loop.h" |
James Kuszmaul | 71a8193 | 2020-12-15 21:08:01 -0800 | [diff] [blame] | 9 | #include "aos/events/shm_event_loop.h" |
James Kuszmaul | 8d928d0 | 2020-12-25 17:47:49 -0800 | [diff] [blame] | 10 | #include "aos/mutex/mutex.h" |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 11 | #include "aos/network/connect_generated.h" |
Austin Schuh | 52e5e3a | 2021-04-24 22:30:02 -0700 | [diff] [blame] | 12 | #include "aos/network/rawrtc.h" |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 13 | #include "aos/network/web_proxy_generated.h" |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 14 | #include "aos/seasocks/seasocks_logger.h" |
| 15 | #include "flatbuffers/flatbuffers.h" |
| 16 | #include "seasocks/Server.h" |
| 17 | #include "seasocks/StringUtil.h" |
| 18 | #include "seasocks/WebSocket.h" |
| 19 | |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 20 | namespace aos { |
| 21 | namespace web_proxy { |
| 22 | |
| 23 | class Connection; |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 24 | class Subscriber; |
Austin Schuh | 52e5e3a | 2021-04-24 22:30:02 -0700 | [diff] [blame] | 25 | class ApplicationConnection; |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 26 | |
| 27 | // Basic class that handles receiving new websocket connections. Creates a new |
| 28 | // Connection to manage the rest of the negotiation and data passing. When the |
| 29 | // websocket closes, it deletes the Connection. |
| 30 | class WebsocketHandler : public ::seasocks::WebSocket::Handler { |
| 31 | public: |
James Kuszmaul | 71a8193 | 2020-12-15 21:08:01 -0800 | [diff] [blame] | 32 | WebsocketHandler(::seasocks::Server *server, aos::EventLoop *event_loop, |
| 33 | int buffer_size); |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 34 | void onConnect(::seasocks::WebSocket *sock) override; |
| 35 | void onData(::seasocks::WebSocket *sock, const uint8_t *data, |
| 36 | size_t size) override; |
| 37 | void onDisconnect(::seasocks::WebSocket *sock) override; |
| 38 | |
| 39 | private: |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 40 | ::seasocks::Server *server_; |
James Kuszmaul | 7ad9152 | 2020-09-01 19:15:35 -0700 | [diff] [blame] | 41 | std::vector<std::unique_ptr<Subscriber>> subscribers_; |
| 42 | const aos::FlatbufferDetachedBuffer<aos::Configuration> config_; |
James Kuszmaul | 71a8193 | 2020-12-15 21:08:01 -0800 | [diff] [blame] | 43 | |
Austin Schuh | 52e5e3a | 2021-04-24 22:30:02 -0700 | [diff] [blame] | 44 | std::map<::seasocks::WebSocket *, std::unique_ptr<ApplicationConnection>> |
| 45 | connections_; |
| 46 | |
| 47 | EventLoop *const event_loop_; |
James Kuszmaul | 71a8193 | 2020-12-15 21:08:01 -0800 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | // Wrapper class that manages the seasocks server and WebsocketHandler. |
| 51 | class WebProxy { |
| 52 | public: |
| 53 | WebProxy(aos::EventLoop *event_loop, int buffer_size); |
| 54 | WebProxy(aos::ShmEventLoop *event_loop, int buffer_size); |
| 55 | ~WebProxy(); |
| 56 | |
| 57 | void SetDataPath(const char *path) { server_.setStaticPath(path); } |
| 58 | |
| 59 | private: |
| 60 | WebProxy(aos::EventLoop *event_loop, aos::internal::EPoll *epoll, |
| 61 | int buffer_size); |
| 62 | |
| 63 | aos::internal::EPoll internal_epoll_; |
| 64 | aos::internal::EPoll *const epoll_; |
| 65 | ::seasocks::Server server_; |
| 66 | std::shared_ptr<WebsocketHandler> websocket_handler_; |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | // Seasocks requires that sends happen on the correct thread. This class takes a |
| 70 | // detached buffer to send on a specific websocket connection and sends it when |
| 71 | // seasocks is ready. |
| 72 | class UpdateData : public ::seasocks::Server::Runnable { |
| 73 | public: |
| 74 | UpdateData(::seasocks::WebSocket *websocket, |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 75 | flatbuffers::DetachedBuffer &&buffer) |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 76 | : sock_(websocket), buffer_(std::move(buffer)) {} |
| 77 | ~UpdateData() override = default; |
| 78 | UpdateData(const UpdateData &) = delete; |
| 79 | UpdateData &operator=(const UpdateData &) = delete; |
| 80 | |
| 81 | void run() override { sock_->send(buffer_.data(), buffer_.size()); } |
| 82 | |
| 83 | private: |
| 84 | ::seasocks::WebSocket *sock_; |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 85 | const flatbuffers::DetachedBuffer buffer_; |
| 86 | }; |
| 87 | |
| 88 | // Represents a fetcher and all the Connections that care about it. |
| 89 | // Handles building the message and telling each connection to send it. |
| 90 | // indexed by location of the channel it handles in the config. |
James Kuszmaul | 71a8193 | 2020-12-15 21:08:01 -0800 | [diff] [blame] | 91 | // Subscriber also uses an internal buffer to store past messages. This is |
| 92 | // primarily meant for use in offline log replay/simulation where we want to be |
| 93 | // able to store infinite buffers. In the future, we will probably want to be |
| 94 | // able to specify *which* channels to store buffers for so that we aren't just |
| 95 | // loading the entire logfile into memory. |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 96 | class Subscriber { |
| 97 | public: |
James Kuszmaul | 71a8193 | 2020-12-15 21:08:01 -0800 | [diff] [blame] | 98 | Subscriber(std::unique_ptr<RawFetcher> fetcher, int channel_index, |
| 99 | int buffer_size) |
Austin Schuh | 52e5e3a | 2021-04-24 22:30:02 -0700 | [diff] [blame] | 100 | : fetcher_(std::move(fetcher)), |
James Kuszmaul | 71a8193 | 2020-12-15 21:08:01 -0800 | [diff] [blame] | 101 | channel_index_(channel_index), |
| 102 | buffer_size_(buffer_size) {} |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 103 | |
| 104 | void RunIteration(); |
| 105 | |
Austin Schuh | 52e5e3a | 2021-04-24 22:30:02 -0700 | [diff] [blame] | 106 | void AddListener(std::shared_ptr<ScopedDataChannel> data_channel, |
| 107 | TransferMethod transfer_method); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 108 | |
Austin Schuh | 52e5e3a | 2021-04-24 22:30:02 -0700 | [diff] [blame] | 109 | void RemoveListener(std::shared_ptr<ScopedDataChannel> data_channel); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 110 | |
| 111 | private: |
James Kuszmaul | 71a8193 | 2020-12-15 21:08:01 -0800 | [diff] [blame] | 112 | struct ChannelInformation { |
| 113 | TransferMethod transfer_method; |
James Kuszmaul | a582268 | 2021-12-23 18:39:28 -0800 | [diff] [blame^] | 114 | // Queue index (same as the queue index within the AOS channel) of the |
| 115 | // message that we are currently sending or, if we are between messages, |
| 116 | // the next message we will send. |
James Kuszmaul | 71a8193 | 2020-12-15 21:08:01 -0800 | [diff] [blame] | 117 | uint32_t current_queue_index = 0; |
James Kuszmaul | a582268 | 2021-12-23 18:39:28 -0800 | [diff] [blame^] | 118 | // Index of the next packet to send within current_queue_index (large |
| 119 | // messages are broken into multiple packets, as we have encountered |
| 120 | // issues with how some WebRTC implementations handle large packets). |
James Kuszmaul | 71a8193 | 2020-12-15 21:08:01 -0800 | [diff] [blame] | 121 | size_t next_packet_number = 0; |
James Kuszmaul | a582268 | 2021-12-23 18:39:28 -0800 | [diff] [blame^] | 122 | // The last queue/packet index reported by the client. |
| 123 | uint32_t reported_queue_index = 0; |
| 124 | size_t reported_packet_index = 0; |
James Kuszmaul | 71a8193 | 2020-12-15 21:08:01 -0800 | [diff] [blame] | 125 | }; |
| 126 | struct Message { |
| 127 | uint32_t index = 0xffffffff; |
Austin Schuh | 52e5e3a | 2021-04-24 22:30:02 -0700 | [diff] [blame] | 128 | std::vector<std::shared_ptr<struct mbuf>> data; |
James Kuszmaul | 71a8193 | 2020-12-15 21:08:01 -0800 | [diff] [blame] | 129 | }; |
| 130 | |
Austin Schuh | 52e5e3a | 2021-04-24 22:30:02 -0700 | [diff] [blame] | 131 | std::shared_ptr<struct mbuf> NextBuffer(ChannelInformation *channel); |
James Kuszmaul | 71a8193 | 2020-12-15 21:08:01 -0800 | [diff] [blame] | 132 | void SkipToLastMessage(ChannelInformation *channel); |
| 133 | |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 134 | std::unique_ptr<RawFetcher> fetcher_; |
| 135 | int channel_index_; |
James Kuszmaul | 71a8193 | 2020-12-15 21:08:01 -0800 | [diff] [blame] | 136 | int buffer_size_; |
| 137 | std::deque<Message> message_buffer_; |
James Kuszmaul | a582268 | 2021-12-23 18:39:28 -0800 | [diff] [blame^] | 138 | // The ScopedDataChannel that we use for actually sending data over WebRTC |
| 139 | // is stored using a weak_ptr because: |
| 140 | // (a) There are some dangers of accidentally creating circular dependencies |
| 141 | // that prevent a ScopedDataChannel from ever being destroyed. |
| 142 | // (b) The inter-dependencies involved are complicated enough that we want |
| 143 | // to be able to check whether someone has destroyed the ScopedDataChannel |
| 144 | // before using it (if it has been destroyed and the Subscriber still |
| 145 | // wants to use it, that is a bug, but checking for bugs is useful). |
| 146 | // This particular location *may* be able to get away with a shared_ptr, but |
| 147 | // because the ScopedDataChannel effectively destroys itself (see |
| 148 | // ScopedDataChannel::StaticDataChannelCloseHandler) while also potentially |
| 149 | // holding references to other objects (e.g., through the various handlers |
| 150 | // that can be registered), creating unnecessary shared_ptr's is dubious. |
| 151 | std::vector<std::pair<std::weak_ptr<ScopedDataChannel>, ChannelInformation>> |
| 152 | channels_; |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 153 | }; |
| 154 | |
Austin Schuh | 52e5e3a | 2021-04-24 22:30:02 -0700 | [diff] [blame] | 155 | // Class to manage a WebRTC connection to a browser. |
| 156 | class ApplicationConnection { |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 157 | public: |
Austin Schuh | 52e5e3a | 2021-04-24 22:30:02 -0700 | [diff] [blame] | 158 | ApplicationConnection( |
| 159 | ::seasocks::Server *server, ::seasocks::WebSocket *sock, |
| 160 | const std::vector<std::unique_ptr<Subscriber>> &subscribers, |
| 161 | const aos::FlatbufferDetachedBuffer<aos::Configuration> &config, |
| 162 | const EventLoop *event_loop); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 163 | |
Austin Schuh | 52e5e3a | 2021-04-24 22:30:02 -0700 | [diff] [blame] | 164 | ~ApplicationConnection(); |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 165 | |
Austin Schuh | 52e5e3a | 2021-04-24 22:30:02 -0700 | [diff] [blame] | 166 | // Handles a SDP sent through the negotiation channel. |
| 167 | void OnSdp(const char *sdp); |
| 168 | // Handles a ICE candidate sent through the negotiation channel. |
| 169 | void OnIce(const WebSocketIce *ice); |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 170 | |
| 171 | private: |
Austin Schuh | 52e5e3a | 2021-04-24 22:30:02 -0700 | [diff] [blame] | 172 | void LocalCandidate( |
| 173 | struct rawrtc_peer_connection_ice_candidate *const candidate, |
| 174 | char const *const url); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 175 | |
Austin Schuh | 52e5e3a | 2021-04-24 22:30:02 -0700 | [diff] [blame] | 176 | // Handles a signaling channel being made. |
| 177 | void OnDataChannel(std::shared_ptr<ScopedDataChannel> channel); |
| 178 | |
| 179 | // Handles data coming in on the signaling channel requesting subscription. |
| 180 | void HandleSignallingData( |
| 181 | struct mbuf *const |
| 182 | buffer, // nullable (in case partial delivery has been requested) |
| 183 | const enum rawrtc_data_channel_message_flag /*flags*/); |
| 184 | |
| 185 | RawRTCConnection connection_; |
| 186 | |
| 187 | ::seasocks::Server *server_; |
| 188 | ::seasocks::WebSocket *sock_; |
| 189 | |
| 190 | struct ChannelState { |
| 191 | std::shared_ptr<ScopedDataChannel> data_channel; |
| 192 | bool requested = true; |
| 193 | }; |
| 194 | |
| 195 | std::map<int, ChannelState> channels_; |
| 196 | const std::vector<std::unique_ptr<Subscriber>> &subscribers_; |
| 197 | |
| 198 | const std::vector<FlatbufferDetachedBuffer<MessageHeader>> config_headers_; |
James Kuszmaul | 71a8193 | 2020-12-15 21:08:01 -0800 | [diff] [blame] | 199 | |
| 200 | const EventLoop *const event_loop_; |
Austin Schuh | 52e5e3a | 2021-04-24 22:30:02 -0700 | [diff] [blame] | 201 | |
| 202 | std::shared_ptr<ScopedDataChannel> channel_; |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 203 | }; |
| 204 | |
| 205 | } // namespace web_proxy |
| 206 | } // namespace aos |
| 207 | |
| 208 | #endif // AOS_NETWORK_WEB_PROXY_H_ |