Server.cpp 2.8 KB

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