12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- #include <iostream>
- #include "server/network/Server.h"
- #include "common/network/Packet.h"
- Server::Server(Port port) : server(nullptr) {
- if(enet_initialize() != 0) {
- std::cout << "an error occurred while initializing ENet\n";
- return;
- }
- ENetAddress address;
- address.host = ENET_HOST_ANY; // localhost
- static_assert(sizeof (enet_uint16) == sizeof (Port), "enet_uint16 and u16 are not of equal size");
- address.port = port;
- server = enet_host_create(&address, clients.getLength(), 2, 0, 0);
- if(server == nullptr) {
- std::cout << "an error occurred while trying to create an ENet server host\n";
- }
- }
- Server::~Server() {
- enet_host_destroy(server); // safe to call for nullptr
- enet_deinitialize();
- }
- bool Server::hasError() const {
- return server == nullptr;
- }
- void Server::consumeEvents(GameServer& gameServer) {
- ENetEvent event;
- while(enet_host_service(server, &event, 0) > 0) {
- switch(event.type) {
- case ENET_EVENT_TYPE_CONNECT:
- onConnect(event, gameServer);
- break;
- case ENET_EVENT_TYPE_RECEIVE:
- onPackage(event, gameServer);
- break;
- case ENET_EVENT_TYPE_DISCONNECT_TIMEOUT:
- case ENET_EVENT_TYPE_DISCONNECT:
- onDisconnect(event, gameServer);
- break;
- case ENET_EVENT_TYPE_NONE:
- return;
- }
- }
- }
- void Server::onConnect(ENetEvent& event, GameServer& gameServer) {
- event.peer->data = nullptr;
- int index = getFreeIndex();
- std::cout << "INDEX: " << index << "\n";
- if(index >= clients.getLength()) {
- Client c;
- c.connect(event.peer);
- gameServer.onFullServerClientConnect(c);
- return;
- }
- clients[index].connect(event.peer);
- event.peer->data = &(clients[index]);
- gameServer.onClientConnect(clients[index]);
- }
- void Server::onPackage(ENetEvent& event, GameServer& gameServer) {
- if(event.peer->data != nullptr) {
- Client* c = static_cast<Client*> (event.peer->data);
- Packet p(event.packet);
- gameServer.onClientPackage(*c, p);
- } else {
- std::cout << "receiving package from client without a client structure\n";
- }
- }
- void Server::onDisconnect(ENetEvent& event, GameServer& gameServer) {
- if(event.peer->data != nullptr) {
- Client* c = static_cast<Client*> (event.peer->data);
- gameServer.onClientDisconnect(*c);
- c->disconnect();
- } else {
- std::cout << "disconnecting a client without a client structure\n";
- }
- }
- int Server::getFreeIndex() const {
- for(int i = 0; i < clients.getLength(); i++) {
- if(clients[i].isDisconnected()) {
- return i;
- }
- }
- return clients.getLength();
- }
|