# Basic 30 TPS server loop

I've created a console server which is supposed to update 30 times per second, I'd like you to review it and also I'm curious about the accuracy of the server's loop.

using System;
using System.Diagnostics;

namespace ZGServer
{
class Program
{
//------------------Variables------------------------
double frameInterval = 1000/30;
Stopwatch msstopwatch = new Stopwatch();
Stopwatch tpsstopwatch = new Stopwatch();
int tpscount = 0;
//---------------------------------------------------
static void Main(string[] args)
{
Program program = new Program();
program.Server();
}
void Server()
{
while (true)
{
msstopwatch.Start();
tpsstopwatch.Start();
if (msstopwatch.ElapsedMilliseconds > frameInterval)
{
tpscount++;
//Console.WriteLine(msstopwatch.ElapsedMilliseconds - frameInterval);
//Console.WriteLine(msstopwatch.ElapsedMilliseconds);
msstopwatch.Reset();
}
else
if (tpscount == 30)
{
Console.WriteLine(tpsstopwatch.ElapsedMilliseconds);
tpscount = 0;
tpsstopwatch.Reset();
}
}
}
}
}


This is the output (It's random every time):

1037, 1038, 1039, 1034, 1037, 1033, 1033, 1034.


This is the amount of time it takes for it to perform 30 loops.

Is this accurate enough for a real-time server over the internet serving multiple clients?

• Is this accurate enough for a real-time server over the internet serving multiple clients? Your question is too vague to get a good answer. When is accuracy enough in your case? It might be accurate enough for lets say a turn based computer game, but probably not accurate enough for intersatellite communication (assuming the comm is over internet :) ). Can you give some more context? – Balanikas Jan 3 '15 at 12:25
• My bad, let me rephrase the question. Will it be able to host multiple clients in reference to a top-down 2d shooter game? – Croccy Jan 3 '15 at 12:32
• "...accurate enough for a real-time server..." just for the sake of clarity. Real-Time Operating Systems are not about being fast for any operations, they're about being able to run the same code in the same amount of time. S if you have some calculation that takes 45ms to complete, the RTOS will guarantee that it will always run in the same amount of time. I'd be surprised if an internet facing server would have a RTOS on it. – Bobby Jan 3 '15 at 22:35
• One small detail (too small for an answer) - C# number literals are implicitly integer values. As such, your frameInterval is slightly off. You should change it to double frameInterval = 100d/3d - the d suffix forces the literals to be doubles and the extra power of 10 for both numerator and denominator are unnecessary. – Dan Lyons Jan 6 '15 at 18:38

class Program


For clarity it would be nice if the main class and method would be declared public.

Also that name is not that good, you should find a better one.

//------------------Variables------------------------


If you need such comments, I'd argue that there is something wrong with your class layout.

double frameInterval = 1000/30;
Stopwatch msstopwatch = new Stopwatch();
Stopwatch tpsstopwatch = new Stopwatch();
int tpscount = 0;


Are these variables used outside of your main class? If no, than these should be private. Everything should be the least accessible as possible, for clarity and encapsulation reasons.

static void Main(string[] args)


Same here, for clarity it should be public.

void Server()


That's a bad name for a function, even the simple Run() would have been a better choice.

Thread.Sleep(1);


If you do not have any constraint on ho much time needs to be between requests, then do not time it at all, perform the requests as fast as possible and if you've done 30 in this second, wait as precisely as possible for the next second.

Is this accurate enough for a real-time server over the internet serving multiple clients?

Definition of a real time Wikipedia:

A key characteristic of an RTOS is the level of its consistency concerning the amount of time it takes to accept and complete an application's task; the variability is jitter.[1] A hard real-time operating system has less jitter than a soft real-time operating system. The chief design goal is not high throughput, but rather a guarantee of a soft or hard performance category. An RTOS that can usually or generally meet a deadline is a soft real-time OS, but if it can meet a deadline deterministically it is a hard real-time OS.[2]

So my first guess as answer to your question would be: no.

However, the more sane approach to that question should be: What are your specs and does this fall into your specs?

• Yes, I know the class looks horrible but it's very barebones at the moment and elegance is not much of a problem, I just want to get certain features working before I make the code easier to read, thanks for the suggestions though. Also, you've said that Thread.Sleep(1) is not trustworthy, though it is the only method I've been able to find which doesn't use an entire cpu core while still keeping track of time. What alternative to StopWatch would you suggest? – Croccy Jan 6 '15 at 21:21
• @Croccy: I can't suggest an alternative, I never was in the position to have such an requirement. The only helpful thing I can tell you is, if you have the requirement to be exact, and I mean down to the millisecond, then .NET might not be the best way to go (heck, even Microsoft Windows might be a problem in that). If you need it even more exact, in the nanosecond range, there's a good chance conventional OS' won't fit your requirements, VxWorks or Linux with RT patches might be an alternative. Or you have to sacrifice the CPU (or a goat, depends on the OS). – Bobby Jan 8 '15 at 17:49