lidgren / lidgren-network-gen3

Lidgren Network Library
https://groups.google.com/forum/#!forum/lidgren-network-gen3
MIT License
1.19k stars 331 forks source link

How to efficiently turn a NetIncomingMessage into a NetOutgoingMessage? #110

Open chrisnobrega opened 5 years ago

chrisnobrega commented 5 years ago

I have very little server-side logic in my application. The server just relays incoming messages to each client. Each message contains a byte and some messages may contain an additional array of bytes.

I am currently parsing the data out of each incoming message into a new outgoing message (with the code I hacked together below). Is there a built-in and/or more efficient way to do this?

NetOutgoingMessage outgoingMessage = this.netServer.CreateMessage ();

outgoingMessage.Write (messageType);

if (netIncomingMessage.PositionInBytes != netIncomingMessage.LengthBytes)
{
    outgoingMessage.WritePadBits ();
    outgoingMessage.Write (netIncomingMessage.ReadBytes (netIncomingMessage.LengthBytes - netIncomingMessage.PositionInBytes));
}               

this.netServer.SendToAll (outgoingMessage, netIncomingMessage.SenderConnection, netIncomingMessage.DeliveryMethod, netIncomingMessage.SequenceChannel);

Thanks for any help!