GameServer.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include "GameServer.h"
  2. #include "../network/server/Server.h"
  3. GameServer::GameServer()
  4. {
  5. }
  6. GameServer::~GameServer()
  7. {
  8. }
  9. void GameServer::start(unsigned short port, unsigned short maxClients)
  10. {
  11. std::cout << port << std::endl;
  12. Server server(port, maxClients);
  13. server.start(this);
  14. while(true)
  15. {
  16. string in;
  17. cin >> in;
  18. if(onServerCommand(in))
  19. {
  20. break;
  21. }
  22. }
  23. server.stop();
  24. }
  25. bool GameServer::onServerCommand(string& command)
  26. {
  27. return command == "q";
  28. }
  29. void GameServer::onFullServerClientConnect(int socket)
  30. {
  31. string s = "sorry, the server is full";
  32. Stream answer;
  33. answer.write(s.data(), s.length());
  34. answer.sendToSocket(socket);
  35. }
  36. void GameServer::onClientConnect(int socket)
  37. {
  38. cout << socket << " has connected" << endl;
  39. string s = "Welcome to the server.";
  40. Stream answer;
  41. answer.write(s.data(), s.length());
  42. answer.sendToSocket(socket);
  43. }
  44. void GameServer::onClientPackage(int socket, Stream& in)
  45. {
  46. string s = "";
  47. while(in.hasData())
  48. {
  49. s = in.readChar() + s;
  50. }
  51. Stream answer;
  52. answer.write(s.data(), s.length());
  53. answer.sendToSocket(socket);
  54. }
  55. void GameServer::onClientDisconnect(int socket)
  56. {
  57. cout << socket << " has disconnected" << endl;
  58. string s = "Bye.";
  59. Stream answer;
  60. answer.write(s.data(), s.length());
  61. answer.sendToSocket(socket);
  62. }