blob: 749e8fcef4ddc3b02182b7fb9f60b1fd8252df97 [file] [log] [blame]
Alex Perryb3b50792020-01-18 16:13:45 -08001#ifndef AOS_NETWORK_WEB_PROXY_H_
2#define AOS_NETWORK_WEB_PROXY_H_
3#include <map>
4#include <set>
James Kuszmaul7ad91522020-09-01 19:15:35 -07005#include "aos/events/event_loop.h"
Alex Perry5f474f22020-02-01 12:14:24 -08006#include "aos/network/connect_generated.h"
7#include "aos/network/web_proxy_generated.h"
Alex Perryb3b50792020-01-18 16:13:45 -08008#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
16namespace aos {
17namespace web_proxy {
18
19class Connection;
Alex Perry5f474f22020-02-01 12:14:24 -080020class Subscriber;
Alex Perryb3b50792020-01-18 16:13:45 -080021
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.
25class WebsocketHandler : public ::seasocks::WebSocket::Handler {
26 public:
James Kuszmaul7ad91522020-09-01 19:15:35 -070027 WebsocketHandler(::seasocks::Server *server, aos::EventLoop *event_loop);
Alex Perryb3b50792020-01-18 16:13:45 -080028 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 Kuszmaul7ad91522020-09-01 19:15:35 -070036 std::vector<std::unique_ptr<Subscriber>> subscribers_;
37 const aos::FlatbufferDetachedBuffer<aos::Configuration> config_;
Alex Perryb3b50792020-01-18 16:13:45 -080038};
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.
43class UpdateData : public ::seasocks::Server::Runnable {
44 public:
45 UpdateData(::seasocks::WebSocket *websocket,
Alex Perry5f474f22020-02-01 12:14:24 -080046 flatbuffers::DetachedBuffer &&buffer)
Alex Perryb3b50792020-01-18 16:13:45 -080047 : 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 Perry5f474f22020-02-01 12:14:24 -080056 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 Kuszmaul7ad91522020-09-01 19:15:35 -070062// TODO(james): Make it so that Subscriber can optionally maintain an infinite
63// backlog of messages.
Alex Perry5f474f22020-02-01 12:14:24 -080064class 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 Perryb3b50792020-01-18 16:13:45 -080092};
93
94// Represents a single connection to a browser for the entire lifetime of the
95// connection.
96class Connection : public webrtc::PeerConnectionObserver,
97 public webrtc::CreateSessionDescriptionObserver,
98 public webrtc::DataChannelObserver {
99 public:
Alex Perry5f474f22020-02-01 12:14:24 -0800100 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 Kuszmaul1ec74432020-07-30 20:26:45 -0700107 if (data_channel_) {
108 data_channel_->UnregisterObserver();
109 }
Alex Perry5f474f22020-02-01 12:14:24 -0800110 }
Alex Perryb3b50792020-01-18 16:13:45 -0800111
112 void HandleWebSocketData(const uint8_t *data, size_t size);
113
Alex Perry5f474f22020-02-01 12:14:24 -0800114 void Send(const flatbuffers::DetachedBuffer &buffer) const;
115
Alex Perryb3b50792020-01-18 16:13:45 -0800116 // 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 Perry5f474f22020-02-01 12:14:24 -0800144 void OnStateChange() override;
Alex Perryb3b50792020-01-18 16:13:45 -0800145 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 Perry5f474f22020-02-01 12:14:24 -0800151 const std::vector<std::unique_ptr<Subscriber>> &subscribers_;
James Kuszmaul1ec74432020-07-30 20:26:45 -0700152 const std::vector<FlatbufferDetachedBuffer<MessageHeader>> config_headers_;
Alex Perry5f474f22020-02-01 12:14:24 -0800153 std::map<int, rtc::scoped_refptr<webrtc::DataChannelInterface>> channels_;
154
Alex Perryb3b50792020-01-18 16:13:45 -0800155 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_