Fix sending large messages to all nodes

We were setting wmem to hold the largest message being sent.  This was
breaking when a large message needed to be broadcast to all nodes, since
it was being written N times in rapid succession, causing the TX buffer
to immediately fill.

Instead, account for the number of destinations as well when computing
wmem.

Change-Id: I3f130d550fea5e4668971c3cadada3b9484a1003
Signed-off-by: Austin Schuh <austin.schuh@bluerivertech.com>
diff --git a/aos/network/message_bridge_server_lib.cc b/aos/network/message_bridge_server_lib.cc
index 2ca8e48..e8e0261 100644
--- a/aos/network/message_bridge_server_lib.cc
+++ b/aos/network/message_bridge_server_lib.cc
@@ -257,7 +257,7 @@
       }) {
   CHECK(event_loop_->node() != nullptr) << ": No nodes configured.";
 
-  int32_t max_size = 0;
+  size_t max_size = 0;
 
   // Seed up all the per-node connection state.
   // We are making the assumption here that every connection is bidirectional
@@ -269,14 +269,14 @@
     // Find the largest connection message so we can size our buffers big enough
     // to receive a connection message.  The connect message comes from the
     // client to the server, so swap the node arguments.
-    const int32_t connect_size = static_cast<int32_t>(
+    const size_t connect_size =
         MakeConnectMessage(event_loop->configuration(),
                            configuration::GetNode(event_loop->configuration(),
                                                   destination_node_name),
                            event_loop->node()->name()->string_view(),
                            UUID::Zero())
             .span()
-            .size());
+            .size();
     VLOG(1) << "Connection to " << destination_node_name << " has size "
             << connect_size;
     max_size = std::max(max_size, connect_size);
@@ -310,7 +310,10 @@
           any_reliable = true;
         }
       }
-      max_size = std::max(channel->max_size(), max_size);
+      max_size =
+          std::max(static_cast<size_t>(channel->max_size() *
+                                       channel->destination_nodes()->size()),
+                   max_size);
       std::unique_ptr<ChannelState> state(new ChannelState{
           channel, channel_index,
           any_reliable ? event_loop_->MakeRawFetcher(channel) : nullptr});
@@ -371,7 +374,7 @@
 
   // Buffer up the max size a bit so everything fits nicely.
   LOG(INFO) << "Max message size for all clients is " << max_size;
-  server_.SetMaxSize(max_size + 100);
+  server_.SetMaxSize(max_size + 100u);
 }
 
 void MessageBridgeServer::NodeConnected(sctp_assoc_t assoc_id) {
diff --git a/aos/network/sctp_lib.cc b/aos/network/sctp_lib.cc
index 33d3352..5bc1b37 100644
--- a/aos/network/sctp_lib.cc
+++ b/aos/network/sctp_lib.cc
@@ -319,6 +319,9 @@
   if (size == -1) {
     if (errno == EPIPE || errno == EAGAIN || errno == ESHUTDOWN ||
         errno == EINTR) {
+      if (VLOG_IS_ON(1)) {
+        PLOG(WARNING) << "sendmsg on sctp socket failed";
+      }
       return false;
     }
     PLOG(FATAL) << "sendmsg on sctp socket failed";