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