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; |
| 114 | uint32_t current_queue_index = 0; |
| 115 | size_t next_packet_number = 0; |
| 116 | }; |
| 117 | struct Message { |
| 118 | uint32_t index = 0xffffffff; |
Austin Schuh | 52e5e3a | 2021-04-24 22:30:02 -0700 | [diff] [blame^] | 119 | std::vector<std::shared_ptr<struct mbuf>> data; |
James Kuszmaul | 71a8193 | 2020-12-15 21:08:01 -0800 | [diff] [blame] | 120 | }; |
| 121 | |
Austin Schuh | 52e5e3a | 2021-04-24 22:30:02 -0700 | [diff] [blame^] | 122 | std::shared_ptr<struct mbuf> NextBuffer(ChannelInformation *channel); |
James Kuszmaul | 71a8193 | 2020-12-15 21:08:01 -0800 | [diff] [blame] | 123 | void SkipToLastMessage(ChannelInformation *channel); |
| 124 | |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 125 | std::unique_ptr<RawFetcher> fetcher_; |
| 126 | int channel_index_; |
James Kuszmaul | 71a8193 | 2020-12-15 21:08:01 -0800 | [diff] [blame] | 127 | int buffer_size_; |
| 128 | std::deque<Message> message_buffer_; |
Austin Schuh | 52e5e3a | 2021-04-24 22:30:02 -0700 | [diff] [blame^] | 129 | std::map<std::shared_ptr<ScopedDataChannel>, ChannelInformation> channels_; |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 130 | }; |
| 131 | |
Austin Schuh | 52e5e3a | 2021-04-24 22:30:02 -0700 | [diff] [blame^] | 132 | // Class to manage a WebRTC connection to a browser. |
| 133 | class ApplicationConnection { |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 134 | public: |
Austin Schuh | 52e5e3a | 2021-04-24 22:30:02 -0700 | [diff] [blame^] | 135 | ApplicationConnection( |
| 136 | ::seasocks::Server *server, ::seasocks::WebSocket *sock, |
| 137 | const std::vector<std::unique_ptr<Subscriber>> &subscribers, |
| 138 | const aos::FlatbufferDetachedBuffer<aos::Configuration> &config, |
| 139 | const EventLoop *event_loop); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 140 | |
Austin Schuh | 52e5e3a | 2021-04-24 22:30:02 -0700 | [diff] [blame^] | 141 | ~ApplicationConnection(); |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 142 | |
Austin Schuh | 52e5e3a | 2021-04-24 22:30:02 -0700 | [diff] [blame^] | 143 | // Handles a SDP sent through the negotiation channel. |
| 144 | void OnSdp(const char *sdp); |
| 145 | // Handles a ICE candidate sent through the negotiation channel. |
| 146 | void OnIce(const WebSocketIce *ice); |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 147 | |
| 148 | private: |
Austin Schuh | 52e5e3a | 2021-04-24 22:30:02 -0700 | [diff] [blame^] | 149 | void LocalCandidate( |
| 150 | struct rawrtc_peer_connection_ice_candidate *const candidate, |
| 151 | char const *const url); |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 152 | |
Austin Schuh | 52e5e3a | 2021-04-24 22:30:02 -0700 | [diff] [blame^] | 153 | // Handles a signaling channel being made. |
| 154 | void OnDataChannel(std::shared_ptr<ScopedDataChannel> channel); |
| 155 | |
| 156 | // Handles data coming in on the signaling channel requesting subscription. |
| 157 | void HandleSignallingData( |
| 158 | struct mbuf *const |
| 159 | buffer, // nullable (in case partial delivery has been requested) |
| 160 | const enum rawrtc_data_channel_message_flag /*flags*/); |
| 161 | |
| 162 | RawRTCConnection connection_; |
| 163 | |
| 164 | ::seasocks::Server *server_; |
| 165 | ::seasocks::WebSocket *sock_; |
| 166 | |
| 167 | struct ChannelState { |
| 168 | std::shared_ptr<ScopedDataChannel> data_channel; |
| 169 | bool requested = true; |
| 170 | }; |
| 171 | |
| 172 | std::map<int, ChannelState> channels_; |
| 173 | const std::vector<std::unique_ptr<Subscriber>> &subscribers_; |
| 174 | |
| 175 | const std::vector<FlatbufferDetachedBuffer<MessageHeader>> config_headers_; |
James Kuszmaul | 71a8193 | 2020-12-15 21:08:01 -0800 | [diff] [blame] | 176 | |
| 177 | const EventLoop *const event_loop_; |
Austin Schuh | 52e5e3a | 2021-04-24 22:30:02 -0700 | [diff] [blame^] | 178 | |
| 179 | std::shared_ptr<ScopedDataChannel> channel_; |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 180 | }; |
| 181 | |
| 182 | } // namespace web_proxy |
| 183 | } // namespace aos |
| 184 | |
| 185 | #endif // AOS_NETWORK_WEB_PROXY_H_ |