Sign in to follow this  
agisler

Synchronous socket performance

Recommended Posts

agisler    156
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.

[code]

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;
}
}

[/code]

Many thanks

Share this post


Link to post
Share on other sites
Antheus    2409
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 [url="http://stackoverflow.com/questions/2281441/can-i-set-the-timeout-for-udpclient-in-c"]temporary bandaid[/url].

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.

Share this post


Link to post
Share on other sites
agisler    156
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?

Share this post


Link to post
Share on other sites
hplus0603    11356
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this