blob: 409e61d455b4e35441495b3beb13949556e88cbd [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>
Alex Perry5f474f22020-02-01 12:14:24 -08005#include "aos/events/shm_event_loop.h"
6#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:
Alex Perry5f474f22020-02-01 12:14:24 -080027 WebsocketHandler(
28 ::seasocks::Server *server,
29 const std::vector<std::unique_ptr<Subscriber>> &subscribers,
30 const aos::FlatbufferDetachedBuffer<aos::Configuration> &config);
Alex Perryb3b50792020-01-18 16:13:45 -080031 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 Perry5f474f22020-02-01 12:14:24 -080039 const std::vector<std::unique_ptr<Subscriber>> &subscribers_;
40 const aos::FlatbufferDetachedBuffer<aos::Configuration> &config_;
Alex Perryb3b50792020-01-18 16:13:45 -080041};
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.
46class UpdateData : public ::seasocks::Server::Runnable {
47 public:
48 UpdateData(::seasocks::WebSocket *websocket,
Alex Perry5f474f22020-02-01 12:14:24 -080049 flatbuffers::DetachedBuffer &&buffer)
Alex Perryb3b50792020-01-18 16:13:45 -080050 : 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 Perry5f474f22020-02-01 12:14:24 -080059 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.
65class 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 Perryb3b50792020-01-18 16:13:45 -080093};
94
95// Represents a single connection to a browser for the entire lifetime of the
96// connection.
97class Connection : public webrtc::PeerConnectionObserver,
98 public webrtc::CreateSessionDescriptionObserver,
99 public webrtc::DataChannelObserver {
100 public:
Alex Perry5f474f22020-02-01 12:14:24 -0800101 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 Perryb3b50792020-01-18 16:13:45 -0800110
111 void HandleWebSocketData(const uint8_t *data, size_t size);
112
Alex Perry5f474f22020-02-01 12:14:24 -0800113 void Send(const flatbuffers::DetachedBuffer &buffer) const;
114
Alex Perryb3b50792020-01-18 16:13:45 -0800115 // 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 Perry5f474f22020-02-01 12:14:24 -0800143 void OnStateChange() override;
Alex Perryb3b50792020-01-18 16:13:45 -0800144 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 Perry5f474f22020-02-01 12:14:24 -0800150 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 Perryb3b50792020-01-18 16:13:45 -0800154 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_