Synchronous socket performance

Started by
2 comments, last by hplus0603 12 years ago
Hello,

So i have made a game using C# synchronous sockets and have noticed it is really causing a huge drop in my frame rate. I am using UDP with non-blocking. I don't understand why its effecting my frame rate so much?

I know asynchronous is the preferred method with c# but I don't quite understand how that can work in a game environment. Asynchronous makes sense to me in a event driven application as i have seen many examples of this done, just never for games.

this is a sample of my code. Any input on this would be appreciated.



public override Packet UDPReceive()
{

byte[] data = new byte[1024];
try
{

int result = UDPSocket.ReceiveFrom(data, ref epSender);
Packet receivedPacket = new Packet(data);
return receivedPacket;
}
catch (Exception ex)
{
return null;
}
}



Many thanks
Advertisement
ReceiveFrom blocks until it receives a packet from the network.

It will not continue until that happens.

So if 10 packets arrive each second to the listening port, framerate will be limited to 10 FPS. If no packet arrives, application will appear to hang for a while or even forever.

Here's a temporary bandaid.

Set timeout to something small, perhaps one millisecond. Doing so will still put an upper limit to "framerate". If main loop isn't busy or has no use for CPU time, that won't matter much. Otherwise, each ms spent waiting for the packet means wasted time which could be put to better use.


Having converted to async approach doing that, you can later change the receive handler. instead of waiting for a packet by blocking, have handler put it into a queue and restart listening. Main loop then runs independently, each time taking all the packets from the queue and handling them.
Ok thank you for your help.

If receiveFrom blocks should I be using receive or something similar to that? It is set to non-blocking and when stepping through the method the catch is invoked when no packets are received like you would expect for a non-blocking socket.

I will try to get async working later but for now I just want to get the whole game working this way and then improve upon it. This seems to be the best way of learning for me.

Edit: I am guessing the async runs on its own thread and when data is received places the information into an array or some class that will store the message. Is this a correct statement?
Easiest is to create a queue of incoming data, and have your main loop "reap" that queue.

You start receiving at the beginning using BeginReceive() or ReceiveAsync(). Then in the callback, you queue another receive function.
In the receive function, you put the data into a queue of some sort. Could be a Queue<byte[]> but could also be a List<Packet> or whatever.
In the main thread, once each frame, you empty the queue and handle all the packets there.
Use the lock {} construct to make adding to, and emptying, the queue thread safe.
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement