HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Basic 30 TPS server loop

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
servertpsloopbasic

Problem

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;
using System.Threading;

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);
                    //Thread.Sleep((int)(msstopwatch.ElapsedMilliseconds - frameInterval));
                    //Console.WriteLine(msstopwatch.ElapsedMilliseconds);
                    msstopwatch.Reset();
                }
                else
                    Thread.Sleep(1);
                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?

Solution

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


Thread.sleep is not trustworthy, neither might be StopWatch.

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?

Code Snippets

class Program
//------------------Variables------------------------
double frameInterval = 1000/30;
Stopwatch msstopwatch = new Stopwatch();
Stopwatch tpsstopwatch = new Stopwatch();
int tpscount = 0;
static void Main(string[] args)
void Server()

Context

StackExchange Code Review Q#75554, answer score: 3

Revisions (0)

No revisions yet.