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_ |
| 3 | #include <map> |
| 4 | #include <set> |
James Kuszmaul | 7ad9152 | 2020-09-01 19:15:35 -0700 | [diff] [blame^] | 5 | #include "aos/events/event_loop.h" |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 6 | #include "aos/network/connect_generated.h" |
| 7 | #include "aos/network/web_proxy_generated.h" |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 8 | #include "aos/seasocks/seasocks_logger.h" |
| 9 | #include "flatbuffers/flatbuffers.h" |
| 10 | #include "seasocks/Server.h" |
| 11 | #include "seasocks/StringUtil.h" |
| 12 | #include "seasocks/WebSocket.h" |
| 13 | |
| 14 | #include "api/peer_connection_interface.h" |
| 15 | |
| 16 | namespace aos { |
| 17 | namespace web_proxy { |
| 18 | |
| 19 | class Connection; |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 20 | class Subscriber; |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 21 | |
| 22 | // Basic class that handles receiving new websocket connections. Creates a new |
| 23 | // Connection to manage the rest of the negotiation and data passing. When the |
| 24 | // websocket closes, it deletes the Connection. |
| 25 | class WebsocketHandler : public ::seasocks::WebSocket::Handler { |
| 26 | public: |
James Kuszmaul | 7ad9152 | 2020-09-01 19:15:35 -0700 | [diff] [blame^] | 27 | WebsocketHandler(::seasocks::Server *server, aos::EventLoop *event_loop); |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 28 | void onConnect(::seasocks::WebSocket *sock) override; |
| 29 | void onData(::seasocks::WebSocket *sock, const uint8_t *data, |
| 30 | size_t size) override; |
| 31 | void onDisconnect(::seasocks::WebSocket *sock) override; |
| 32 | |
| 33 | private: |
| 34 | std::map<::seasocks::WebSocket *, std::unique_ptr<Connection>> connections_; |
| 35 | ::seasocks::Server *server_; |
James Kuszmaul | 7ad9152 | 2020-09-01 19:15:35 -0700 | [diff] [blame^] | 36 | std::vector<std::unique_ptr<Subscriber>> subscribers_; |
| 37 | const aos::FlatbufferDetachedBuffer<aos::Configuration> config_; |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | // Seasocks requires that sends happen on the correct thread. This class takes a |
| 41 | // detached buffer to send on a specific websocket connection and sends it when |
| 42 | // seasocks is ready. |
| 43 | class UpdateData : public ::seasocks::Server::Runnable { |
| 44 | public: |
| 45 | UpdateData(::seasocks::WebSocket *websocket, |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 46 | flatbuffers::DetachedBuffer &&buffer) |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 47 | : sock_(websocket), buffer_(std::move(buffer)) {} |
| 48 | ~UpdateData() override = default; |
| 49 | UpdateData(const UpdateData &) = delete; |
| 50 | UpdateData &operator=(const UpdateData &) = delete; |
| 51 | |
| 52 | void run() override { sock_->send(buffer_.data(), buffer_.size()); } |
| 53 | |
| 54 | private: |
| 55 | ::seasocks::WebSocket *sock_; |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 56 | const flatbuffers::DetachedBuffer buffer_; |
| 57 | }; |
| 58 | |
| 59 | // Represents a fetcher and all the Connections that care about it. |
| 60 | // Handles building the message and telling each connection to send it. |
| 61 | // indexed by location of the channel it handles in the config. |
James Kuszmaul | 7ad9152 | 2020-09-01 19:15:35 -0700 | [diff] [blame^] | 62 | // TODO(james): Make it so that Subscriber can optionally maintain an infinite |
| 63 | // backlog of messages. |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 64 | class Subscriber { |
| 65 | public: |
| 66 | Subscriber(std::unique_ptr<RawFetcher> fetcher, int channel_index) |
| 67 | : fbb_(1024), |
| 68 | fetcher_(std::move(fetcher)), |
| 69 | channel_index_(channel_index) {} |
| 70 | |
| 71 | void RunIteration(); |
| 72 | |
| 73 | void AddListener(rtc::scoped_refptr<webrtc::DataChannelInterface> channel) { |
| 74 | channels_.insert(channel); |
| 75 | } |
| 76 | |
| 77 | void RemoveListener( |
| 78 | rtc::scoped_refptr<webrtc::DataChannelInterface> channel) { |
| 79 | channels_.erase(channel); |
| 80 | } |
| 81 | |
| 82 | // Check if the Channel passed matches the channel this fetchs. |
| 83 | bool Compare(const Channel *channel) const; |
| 84 | |
| 85 | int index() const { return channel_index_; } |
| 86 | |
| 87 | private: |
| 88 | flatbuffers::FlatBufferBuilder fbb_; |
| 89 | std::unique_ptr<RawFetcher> fetcher_; |
| 90 | int channel_index_; |
| 91 | std::set<rtc::scoped_refptr<webrtc::DataChannelInterface>> channels_; |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | // Represents a single connection to a browser for the entire lifetime of the |
| 95 | // connection. |
| 96 | class Connection : public webrtc::PeerConnectionObserver, |
| 97 | public webrtc::CreateSessionDescriptionObserver, |
| 98 | public webrtc::DataChannelObserver { |
| 99 | public: |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 100 | Connection(::seasocks::WebSocket *sock, ::seasocks::Server *server, |
| 101 | const std::vector<std::unique_ptr<Subscriber>> &subscribers, |
| 102 | const aos::FlatbufferDetachedBuffer<aos::Configuration> &config); |
| 103 | |
| 104 | ~Connection() { |
| 105 | // DataChannel may call OnStateChange after this is destroyed, so make sure |
| 106 | // it doesn't. |
James Kuszmaul | 1ec7443 | 2020-07-30 20:26:45 -0700 | [diff] [blame] | 107 | if (data_channel_) { |
| 108 | data_channel_->UnregisterObserver(); |
| 109 | } |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 110 | } |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 111 | |
| 112 | void HandleWebSocketData(const uint8_t *data, size_t size); |
| 113 | |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 114 | void Send(const flatbuffers::DetachedBuffer &buffer) const; |
| 115 | |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 116 | // PeerConnectionObserver implementation |
| 117 | void OnSignalingChange( |
| 118 | webrtc::PeerConnectionInterface::SignalingState) override {} |
| 119 | void OnAddStream(rtc::scoped_refptr<webrtc::MediaStreamInterface>) override {} |
| 120 | void OnRemoveStream( |
| 121 | rtc::scoped_refptr<webrtc::MediaStreamInterface>) override {} |
| 122 | void OnDataChannel( |
| 123 | rtc::scoped_refptr<webrtc::DataChannelInterface> channel) override; |
| 124 | void OnRenegotiationNeeded() override {} |
| 125 | void OnIceConnectionChange( |
| 126 | webrtc::PeerConnectionInterface::IceConnectionState state) override {} |
| 127 | void OnIceGatheringChange( |
| 128 | webrtc::PeerConnectionInterface::IceGatheringState) override {} |
| 129 | void OnIceCandidate(const webrtc::IceCandidateInterface *candidate) override; |
| 130 | void OnIceConnectionReceivingChange(bool) override {} |
| 131 | |
| 132 | // CreateSessionDescriptionObserver implementation |
| 133 | void OnSuccess(webrtc::SessionDescriptionInterface *desc) override; |
| 134 | void OnFailure(webrtc::RTCError error) override {} |
| 135 | // CreateSessionDescriptionObserver is a refcounted object |
| 136 | void AddRef() const override {} |
| 137 | // We handle ownership with a unique_ptr so don't worry about actually |
| 138 | // refcounting. We will delete when we are done. |
| 139 | rtc::RefCountReleaseStatus Release() const override { |
| 140 | return rtc::RefCountReleaseStatus::kOtherRefsRemained; |
| 141 | } |
| 142 | |
| 143 | // DataChannelObserver implementation |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 144 | void OnStateChange() override; |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 145 | void OnMessage(const webrtc::DataBuffer &buffer) override; |
| 146 | void OnBufferedAmountChange(uint64_t sent_data_size) override {} |
| 147 | |
| 148 | private: |
| 149 | ::seasocks::WebSocket *sock_; |
| 150 | ::seasocks::Server *server_; |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 151 | const std::vector<std::unique_ptr<Subscriber>> &subscribers_; |
James Kuszmaul | 1ec7443 | 2020-07-30 20:26:45 -0700 | [diff] [blame] | 152 | const std::vector<FlatbufferDetachedBuffer<MessageHeader>> config_headers_; |
Alex Perry | 5f474f2 | 2020-02-01 12:14:24 -0800 | [diff] [blame] | 153 | std::map<int, rtc::scoped_refptr<webrtc::DataChannelInterface>> channels_; |
| 154 | |
Alex Perry | b3b5079 | 2020-01-18 16:13:45 -0800 | [diff] [blame] | 155 | rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_; |
| 156 | rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel_; |
| 157 | }; |
| 158 | |
| 159 | } // namespace web_proxy |
| 160 | } // namespace aos |
| 161 | |
| 162 | #endif // AOS_NETWORK_WEB_PROXY_H_ |