Add backpressure to web_proxy

Implement things such that the client will send back what it has
currently processed on a given channel. The server will then avoid
sending more than 10000 messages ahead of that point.

Also, fix up some memory management to ensure that data channels
actually get closed/destroyed at the end of a browser session.

Change-Id: Id1795d7496f410332407624a559d6a16a1698702
Signed-off-by: James Kuszmaul <jabukuszmaul@gmail.com>
diff --git a/aos/network/rawrtc.cc b/aos/network/rawrtc.cc
index 89141db..98f2448 100644
--- a/aos/network/rawrtc.cc
+++ b/aos/network/rawrtc.cc
@@ -27,6 +27,12 @@
 
 ScopedDataChannel::ScopedDataChannel() {}
 
+std::shared_ptr<ScopedDataChannel> ScopedDataChannel::MakeDataChannel() {
+  std::shared_ptr<ScopedDataChannel> channel(new ScopedDataChannel());
+  channel->self_ = channel;
+  return channel;
+}
+
 void ScopedDataChannel::Open(struct rawrtc_peer_connection *connection,
                              const std::string &label) {
   label_ = label;
@@ -89,7 +95,7 @@
 
 ScopedDataChannel::~ScopedDataChannel() {
   CHECK(opened_);
-  CHECK(closed_);
+  CHECK(closed_) << ": Never closed " << label();
   CHECK(data_channel_ == nullptr)
       << ": Destroying open data channel " << this << ".";
 }
@@ -129,6 +135,7 @@
     on_close();
   }
   mem_deref(data_channel);
+  client->self_.reset();
 }
 
 void ScopedDataChannel::StaticDataChannelMessageHandler(
@@ -285,7 +292,7 @@
   RawRTCConnection *const client = reinterpret_cast<RawRTCConnection *>(arg);
   if (client->on_data_channel_) {
     std::shared_ptr<ScopedDataChannel> new_channel =
-        std::make_shared<ScopedDataChannel>();
+        ScopedDataChannel::MakeDataChannel();
     new_channel->Open(channel);
     client->on_data_channel_(std::move(new_channel));
   }