English 中文(简体)
Optimizing a LAN server for a game
原标题:

I m the network programmer on a school game project. We want to have up to 16 players at once on a LAN. I am using the Server-Client model and am creating a new thread per client that joins. However, a lot of CPU time is wasted just checking on each thread if the non-blocking port has received anything from the client.

I ve been reading "Network Programming for Microsoft Windows" by Anthony Johns and Jim Ohlund. They mention two different models for server-client apps.

1.)

  • Using overlapped IO socket options
  • Pass the overlapped struct and a WorkerRoutine to WSARecv
  • Call WSAWaitForMultipleEvents() or SleepEX() to set thread to alertable.
  • Handle Received data in the WorkerRoutines.

2.)

  • Using overlapped IO socket options
  • Create Io Completion Port
  • Create ServerWorkerThreads (however many CUPs you have)
  • Associate Completion port with Socket.
  • Call GetQueuedCompletionStatus in the ServerWorkerThread and handle received data.

I wanted to know which method would best fit my circumstance. The book says the Completion Port model is great for thousands of clients but that makes me think its made for a big server not for a small LAN game. The WorkerRoutines/Event system seems simpler.

最佳回答

You should stop using non-blocking ports. They re really most useful for concurrency, and you have threads for that.

Just make each thread in the team wait on the port, and when it unblocks, it ll have something to do so you don t waste time spinning. If you have other things to do (like update game state), do that with other threads that don t do any network I/O at all.

And make sure to use synchronization primitives (semaphores) to keep from getting out of sync with yourself.

That s the super multi-threaded way to do it, and you probably should use it. The other way is to have a lock-step advancing state machine that updates all clients round-robin. This is rather useless if you want to play across the internet since varying pings will make your game run at varying speeds.

问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签