private async Task WriteMessage(MessageAndSerializerFunc message) { if (message.msg.Serialized == null) { message.msg.Serialized = message.serfunc(); } int length = message.msg.Serialized.Length; await connection.WriteBlock(BitConverter.GetBytes(length), 0, 4, cancel).ConfigureAwait(false); await connection.WriteBlock(message.msg.Serialized, 0, length, cancel).ConfigureAwait(false); Stats.MessagesSent++; Stats.BytesSent += length + 4; Stats.MessageDataSent += length; }
private void StartMessageWrite(bool immediateWrite) { MessageAndSerializerFunc holder = null; if (writingMessage || !headerWritten) { return; } lock ( outbox ) { if (outbox.Count > 0) { writingMessage = true; holder = outbox.Dequeue(); } if (outbox.Count < maxQueue) { queueFull = false; } } if (holder != null) { if (holder.msg.Serialized == null) { holder.msg.Serialized = holder.serfunc(); } byte[] outbuf = new byte[holder.msg.Serialized.Length + 4]; Array.Copy(holder.msg.Serialized, 0, outbuf, 4, holder.msg.Serialized.Length); Array.Copy(BitConverter.GetBytes(holder.msg.Serialized.Length), outbuf, 4); stats.messagesSent++; //ROS.Debug()("Message backlog = " + (triedtosend - stats.messages_sent)); stats.bytesSent += outbuf.Length; stats.messageDataSent += outbuf.Length; connection.write(outbuf, outbuf.Length, OnMessageWritten, immediateWrite); } }