GameServer.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include <iostream>
  2. #include "server/GameServer.h"
  3. #include "server/commands/CommandManager.h"
  4. GameServer::GameServer(const Clock& tps) : tps(tps) {
  5. }
  6. void GameServer::tick() {
  7. }
  8. void GameServer::handleCommands(CommandEditor& editor, ServerCommands& serverCommands) {
  9. while(editor.hasCommand()) {
  10. String s = editor.readCommand();
  11. commandManager.execute(serverCommands, s);
  12. }
  13. }
  14. void GameServer::onFullServerClientConnect(int socket) {
  15. Stream answer;
  16. answer.write("Sorry, the server is full");
  17. answer.sendToSocket(socket);
  18. }
  19. void GameServer::onClientConnect(int socket) {
  20. std::cout << socket << " has connected\n";
  21. Stream answer;
  22. answer.write("Welcome to the server.");
  23. answer.sendToSocket(socket);
  24. }
  25. void GameServer::onClientPackage(int socket, Stream& in) {
  26. String s = "";
  27. while(in.hasData()) {
  28. char c;
  29. in.read(&c, 1);
  30. s.append(c);
  31. }
  32. Stream answer;
  33. answer.write(s, s.getLength());
  34. answer.sendToSocket(socket);
  35. }
  36. void GameServer::onClientDisconnect(int socket) {
  37. std::cout << socket << " has disconnected\n";
  38. Stream answer;
  39. answer.write("Bye.");
  40. answer.sendToSocket(socket);
  41. }