Well its up to the OP to decide...my impression was that a chat is
just a first step - a proof of concept so to say. But that he wants a
real game server as a final result.
Regarding the threading stuff... that's such a large topic but just a
words:
- threading is not necessary a bad thing in terms of CPU usage (as
with everything else the implementation can be done very poor or very
good)
- threads are served from the .NET thread pool and multiple .NET
threads are actually executed by another (smaller) pool of system
threads - no 1:1 relation. So using background workers helps
conserving system resources.
- threads are cheap today since the CPU's have multiple cores - 4
hardware threads is something most common today while real server HW
has usually even more threads on disposal
- kernel waits are bad. why? too long story... its all about switching
contexts
- sleeps are bad cause they involve context switching as well
- timers are bad for the same reason and few more - you already named
one
- receving/sending threads do not hog any CPU time, they simply
process the data and wait on a signal (check the Borg Network Server
source to see what I mean). Of course mindless looping in circle would
hog a lot CPU but thats why the waitone is there.
Regarding the intermediate storage, that's just a must have for high
performance servers.
The only other option that crosses my mind would be hogging the
receiver loop with reading lidgren buffers, deserializing them an
invoking business logic. This can consume a considerable amount of
processing time during which all "other clients are waiting" (not
literally true, but the clients data is waiting unprocessed).
All the above and in my previous mail applies ONLY to high performance
and scalable server scenarios.
If the OP really wants just a simple chat between say 2-20 clients
than disregards all and I mean ***ALL*** my comments!
On Mar 10, 6:30 pm, Chris Cowherd <
mad...@gmail.com> wrote:
> Riki - Thanks for contributing your input! I would respectfully disagree
> about timers. For the general case of a game engine, sure, don't use a
> timer. It doesn't have the resolution you need to pump messages. He just
> wants to make a chat server to start though and for that its a perfectly
> acceptable solution.
>
> Using a loop spinning on checking for available messages he will pretty much
> peg the CPU doing nothing. Using a timer, you can get sufficient resolution
> and all the waiting is done in kernel mode leaving the CPU free for other
> things.
>
> I would also make a comment regarding the storage of the messages. If you
> have a look at the Lidgren internals, it is already queuing the messages for
> you as the arrive so you don't want to double this effort.
>
> A lot of people are really tempted to use a lot of threads, one for reading
> and handling messages and others for processing etc. but I have written some
> pretty high performance servers (
http://www.youtube.com/watch?v=kCp2-nfibzY) with