jatpat / lidgren-network-gen3

Automatically exported from code.google.com/p/lidgren-network-gen3
0 stars 0 forks source link

Loopback like mechanism #86

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I think it would be useful to have some kind of loopback like mechanism so 
NetPeer could send messages to itself.

It benefits situations where for simplicity of processing, messages are sent 
not only to remote peers but to local one too. Imagine a game, where peer A 
sends a message to build something to peers B and C. With current 
implementation you have to double the logic for building: invoke building code 
in peer A, and send messages to B and C. If A could send a message to itself, 
it could use the same message processing code used in B and C, simplifying 
things a lot.

Several ways it could be implemented:
1) Something like NetPeer.SendMessageToLoopback(), that would queue the message 
localy.
2) Allow creation of NetIncommingMessage with a constructor parameter of 
NetOutgoingMessage, which would copy outgoing message content to incoming.
3) A LoopbackNetConnection class inheriting NetConnection that could be passed 
to NetPeer.SendMessage();. Could be exposed by a static property 
NetConnection.Loopback, that returns a NetConnection behaving like loopback. 
NetPeer.ConnectionsWithLoopback should be added in this case, to return the 
list with a loopback connection to simplify sending.

Original issue reported on code.google.com by smiledi...@joystickninjas.com on 22 Aug 2011 at 1:37

GoogleCodeExporter commented 9 years ago
it took me about 10 minutes to modify the xna sample to have loopback:

run the server code in it's own thread (execute in XnaClient.Program.Main 
before invoking the Game.Run, and do a thread.Join() after the Game is finished 
exiting)

and then for the client and server,  set config.LocalAddress = 
IPAddress.Loopback;

this is probably better than netpeer auto-loopback because it uses the existing 
workflows for normal client/server stuff

Original comment by jas...@novaleaf.com on 4 Sep 2011 at 10:43

GoogleCodeExporter commented 9 years ago
In my particular case I'm not using server-client topology, but rather 
peer-to-peer. And there's no such thing as a server in the game, all clients 
simulate the game on their own, only commands are sent through network.

Anyway, for my case, I've added this simple method to NetPeer, and it worked 
perfectly so far:

public void SendMessageToLoopback(NetOutgoingMessage msg) {
    if (msg == null)
        throw new ArgumentNullException("msg");

    NetIncomingMessage im = CreateIncomingMessage(NetIncomingMessageType.Data, msg.LengthBytes);
    im.Write(msg.PeekDataBuffer(), 0, msg.LengthBytes);
    ReleaseMessage(im);
}

Original comment by smiledi...@joystickninjas.com on 8 Sep 2011 at 2:36

GoogleCodeExporter commented 9 years ago
The solution in comment 1 is probably preferable in many situations; but I've 
added a SendUnconnectedToSelf() in case you really need to reduce overhead and 
latency for self sends. Revision 270.

Original comment by lidg...@gmail.com on 2 Nov 2011 at 9:48