/// <summary> /// Sends a message to the user over the network. /// /// In general, the msg buffer does not need to live longer than the /// duration of the call. It's contents will be copied. /// </summary> /// <param name="msg">the buffer of the message</param> /// <param name="size">the size of the message, uses the size of the buffer if negative.</param> /// <param name="reliability">does the message need to be reliably sent</param> public void SendMessage(ReadOnlySpan <byte> msg, Reliability reliability = Reliability.Reliable) { if (MessageProcessor == null) { Lobby.SendNetworkMessage(Id, msg, reliability: reliability); _stats.BytesSent += (ulong)msg.Length; } else { int outputSize = msg.Length + 1; while (true) { Span <byte> output = stackalloc byte[outputSize]; if (!MessageProcessor.Apply(msg, ref output)) { outputSize *= 2; continue; } Lobby.SendNetworkMessage(Id, output, reliability: reliability); _stats.BytesSent += (ulong)msg.Length; break; } } _stats.PacketsSent++; }
/// <summary> /// Sends a message to the user over the network. /// /// In general, the msg buffer does not need to live longer than the /// duration of the call. It's contents will be copied. /// </summary> /// <param name="msg">the buffer of the message</param> /// <param name="size">the size of the message, uses the size of the buffer if negative.</param> /// <param name="reliability">does the message need to be reliably sent</param> public unsafe void SendMessage(FixedBuffer msg, Reliability reliability = Reliability.Reliable) { if (MessageProcessor == null) { Lobby.SendNetworkMessage(Id, msg, reliability: reliability); } else { var buffer = msg.ToArray(); var size = (int)msg.Size; MessageProcessor.Apply(ref buffer, ref size); fixed(byte *ptr = buffer) { msg = new FixedBuffer(ptr, size); Lobby.SendNetworkMessage(Id, msg, reliability: reliability); } } _stats.PacketsSent++; _stats.BytesSent += (ulong)msg.Size; }