Main.cpp 961 B

123456789101112131415161718192021222324252627282930313233343536
  1. #include "server/network/Server.h"
  2. #include "server/GameServer.h"
  3. #include "server/Clock.h"
  4. static const u64 NANOS_PER_TICK = 50000000;
  5. int main() {
  6. Socket listener;
  7. if(listener.hasError() || listener.setNonLinger() || listener.listenOnPort(25565, 5)) {
  8. return 0;
  9. }
  10. Clock tps;
  11. GameServer gameServer(tps);
  12. Server server(listener, 20, gameServer);
  13. ServerCommands serverCommands;
  14. CommandEditor editor;
  15. Clock clock;
  16. u64 lag = 0;
  17. while(serverCommands.isRunning()) {
  18. lag += clock.update();
  19. while(lag >= NANOS_PER_TICK) {
  20. lag -= NANOS_PER_TICK;
  21. tps.update();
  22. editor.preTick();
  23. gameServer.handleCommands(editor, serverCommands);
  24. gameServer.tick();
  25. editor.postTick();
  26. }
  27. u64 waitNanos = NANOS_PER_TICK - lag;
  28. if(waitNanos > 300000) {
  29. clock.wait(waitNanos);
  30. }
  31. }
  32. return 0;
  33. }