Bunnies Server, Now with More C#
It took about 5 hours of work to translate the existing PHP based server into a C# based server. That may say something about my coding abilities or the simplicity of the server.
Just to test I ran the PHP server and two instances of Bunnies on the same Dual Core 2Ghz system with 2GB of RAM. It handled just fine with position updates being sent every tenth of a second from each client. A single core 1.7Ghz system with 896MB of ram couldn’t handle it. We’ll see later how that system handles with the C# based server.
One of the historical features of game servers that I’ve written is that they use double buffering. What that means is that data coming in is simply stored. It’s not processed right away. Data going out is also simply stored. Once per iteration of the main loop all the stored data is processed and outgoing messages are generated. Once all the incomming messages have been processed then the outgoing messages are actually sent out on the line.
The reason for this is simple: TCP is a stream based protocol. Trying to treat it like a message based protocol is going to introduce software induced lag. Not only is it a stream based protocol but it has an algorithm built into it which causes messages to be held by the network card until certain conditions are met (one being the amount of data) and then the data actually gets sent which cuts down on overhead. Every TCP packet that goes out has a header tacked on that is several bytes. So it’s in your best interest to not force multiple packets when one would suffice.
You may be thinking, “why not use UDP?” Because it’s unreliable and unless your an expert at UDP you’re going to end up with performance no better (and possibly worse) than TCP. TCP is very fast if you know what you’re doing. Packaging multiple game message into one TCP packet is the easiest way to boost performance significantly.
No matter what your server is doing it has to create the messages. You can’t save time there except by simplifying message generation. But you can save a lot of processing time by sending at most one message per connection per iteration instead of many. If it takes N seconds to send a message it will take 5 * N to send five messages. If it takes N seconds to send a 10 byte message it will not take 2 * N to send a 20 byte message. It will still take very close to N seconds.
The next trick of course is to make your main loop really fast. The amount of time it takes to read in a message, process it and send out a message is the amount of software induced lag. So if it takes 0.25 seconds to process a text message and forward it off to all the clients then that 0.25 seconds of lag you just created.
The game server should be running thousands if not millions of iterations per second under light load. As more people connect and more messages come in that is going to start to drop. If your game loop drops to say 10 iterations per second that means your server is adding 1/6 of a second in lag which is going to be noticable.
That’s the biggest reason for going to C#. The Bunnies server needs to be able to handle large numbers of players simultaniously. PHP may do fine for light loads but there’s no reason to keep developing it knowing it won’t scale.
After my initial look into setting up sockets and MySQL in C# I realized pretty quickly this wasn’t a difficult task.
Leave a comment
You must be logged in to post a comment.