Build the mbuf directly instead of copying in web_proxy

No sense doing a malloc followed by a memcpy for each packet.  Instead,
we know how big the message will be, so allocate that much memory and
build directly into it.

Change-Id: Ie2361ced5403ff481dd10b30e8d9f106071c506b
diff --git a/aos/network/web_proxy_utils.cc b/aos/network/web_proxy_utils.cc
index deb4a38..d6148d4 100644
--- a/aos/network/web_proxy_utils.cc
+++ b/aos/network/web_proxy_utils.cc
@@ -9,6 +9,10 @@
 // the middle which seems to work.
 constexpr size_t kPacketSize = 125000;
 
+// Max header size we have seen is 72 bytes, followed by 48 bytes of scratch
+// space.
+constexpr size_t kMaxHeaderSize = 72 + 48;
+
 int GetPacketCountFromSize(const int packet_size) {
   return packet_size / kPacketSize + 1;
 }
@@ -35,6 +39,19 @@
   return GetPacketCountFromSize(context.size);
 }
 
+size_t PackedMessageSize(const Context &context, int packet_index) {
+  // Make sure the final size is aligned because flatbuffers will align it up
+  // otherwise.
+  constexpr size_t kAlignment = 8;
+  if (kPacketSize * (packet_index + 1) < context.size) {
+    return (kPacketSize + kMaxHeaderSize + kAlignment - 1) & ~(kAlignment - 1);
+  } else {
+    const int prefix_size = kPacketSize * packet_index;
+    return (context.size - prefix_size + kMaxHeaderSize + kAlignment - 1) &
+           ~(kAlignment - 1);
+  }
+}
+
 flatbuffers::Offset<MessageHeader> PackMessage(
     flatbuffers::FlatBufferBuilder *fbb, const Context &context,
     int channel_index, int packet_index) {