My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
ReceivingMessages  
Three different way to consume incoming messages
Featured, Phase-Implementation
Updated May 18, 2011 by lidg...@gmail.com

There are three different ways of consuming messages received by the library.

1. Polling

If you already have an application loop for your game this would be the most suitable to use; just call ReadMessage() until it returns null.

NetIncomingMessage msg;
while ((msg = peer.ReadMessage()) != null)
{
   // process message here
}

2. Callback

If your application is completely event/UI-driven and does not have an application loop per se, you can register a callback to fire when a message arrives. The registration must happen from the correct SynchronizationContext (ie. call RegisterReceivedCallback from the same thread you want to receive the message notification on)

peer.RegisterReceivedCallback(new SendOrPostCallback(GotMessage)); 

public static void GotMessage(object peer)
{
	var msg = peer.ReadMessage();
	// process message here
}	

3. EventWaitHandle

If your incoming message processing is run on a separate thread you can use the MessageReceivedEvent property to have your thread block until an incoming message arrives.

// in your separate thread
peer.MessageReceivedEvent.WaitOne(); // this will block until a message arrives

var msg = peer.ReadMessage();
// process message here
Comment by jandoerr...@gmail.com, Dec 2, 2011

Having a blocking call so that you can work on your own thread (EventWaitHandle?) is very useful. But is there a way to run a peer/server completely synchronously? I mean, once I call peer.Start(), a thread is spawned, right? So what is the advantage of being on my own thread, if a network thread is running in the background anyway?


Sign in to add a comment
Powered by Google Project Hosting