Server.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include <iostream>
  2. #include "server/network/Server.h"
  3. #include "common/network/Packet.h"
  4. Server::Server(Port port) : server(nullptr) {
  5. if(enet_initialize() != 0) {
  6. std::cout << "an error occurred while initializing ENet\n";
  7. return;
  8. }
  9. ENetAddress address;
  10. address.host = ENET_HOST_ANY; // localhost
  11. static_assert(sizeof (enet_uint16) == sizeof (Port), "enet_uint16 and u16 are not of equal size");
  12. address.port = port;
  13. server = enet_host_create(&address, clients.getLength(), 2, 0, 0);
  14. if(server == nullptr) {
  15. std::cout << "an error occurred while trying to create an ENet server host\n";
  16. }
  17. }
  18. Server::~Server() {
  19. enet_host_destroy(server); // safe to call for nullptr
  20. enet_deinitialize();
  21. }
  22. bool Server::hasError() const {
  23. return server == nullptr;
  24. }
  25. void Server::consumeEvents(GameServer& gameServer) {
  26. ENetEvent event;
  27. while(enet_host_service(server, &event, 0) > 0) {
  28. switch(event.type) {
  29. case ENET_EVENT_TYPE_CONNECT:
  30. onConnect(event, gameServer);
  31. break;
  32. case ENET_EVENT_TYPE_RECEIVE:
  33. onPackage(event, gameServer);
  34. break;
  35. case ENET_EVENT_TYPE_DISCONNECT_TIMEOUT:
  36. case ENET_EVENT_TYPE_DISCONNECT:
  37. onDisconnect(event, gameServer);
  38. break;
  39. case ENET_EVENT_TYPE_NONE:
  40. return;
  41. }
  42. }
  43. }
  44. void Server::onConnect(ENetEvent& event, GameServer& gameServer) {
  45. event.peer->data = nullptr;
  46. int index = getFreeIndex();
  47. std::cout << "INDEX: " << index << "\n";
  48. if(index >= clients.getLength()) {
  49. Client c;
  50. c.connect(event.peer);
  51. gameServer.onFullServerClientConnect(c);
  52. return;
  53. }
  54. clients[index].connect(event.peer);
  55. event.peer->data = &(clients[index]);
  56. gameServer.onClientConnect(clients[index]);
  57. }
  58. void Server::onPackage(ENetEvent& event, GameServer& gameServer) {
  59. if(event.peer->data != nullptr) {
  60. Client* c = static_cast<Client*> (event.peer->data);
  61. Packet p(event.packet);
  62. gameServer.onClientPackage(*c, p);
  63. } else {
  64. std::cout << "receiving package from client without a client structure\n";
  65. }
  66. }
  67. void Server::onDisconnect(ENetEvent& event, GameServer& gameServer) {
  68. if(event.peer->data != nullptr) {
  69. Client* c = static_cast<Client*> (event.peer->data);
  70. gameServer.onClientDisconnect(*c);
  71. c->disconnect();
  72. } else {
  73. std::cout << "disconnecting a client without a client structure\n";
  74. }
  75. }
  76. int Server::getFreeIndex() const {
  77. for(int i = 0; i < clients.getLength(); i++) {
  78. if(clients[i].isDisconnected()) {
  79. return i;
  80. }
  81. }
  82. return clients.getLength();
  83. }