123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- #ifndef SERVER_H
- #define SERVER_H
- #include "network/ENet.h"
- #include "network/Packet.h"
- #include "utils/Error.h"
- #include "utils/HashMap.h"
- #include "utils/Logger.h"
- #include "utils/StringBuffer.h"
- struct Server final {
- typedef enet_uint16 Port;
- class Client final {
- friend class Server;
- ENetPeer* peer;
- int id;
- friend HashMap<int, Client>;
- Client(ENetPeer* peer, int id);
- Client(const Client&) = delete;
- Client(Client&& other);
- Client& operator=(const Client&) = delete;
- Client& operator=(Client&& other);
- public:
- ~Client();
- int getId() const;
- void send(OutPacket& p);
- };
- private:
- ENetHost* server;
- HashMap<int, Client> clients;
- int idCounter;
- public:
- Server();
- Server(const Server&) = delete;
- Server(Server&&) = delete;
- ~Server();
- Server& operator=(const Server&) = delete;
- Server& operator=(Server&&) = delete;
- Error start(Port port, int maxClients);
- template<typename T>
- void consumeEvents(T& consumer) {
- ENetEvent e;
- while(enet_host_service(server, &e, 0) > 0) {
- switch(e.type) {
- case ENET_EVENT_TYPE_CONNECT: onConnect(e, consumer); break;
- case ENET_EVENT_TYPE_RECEIVE:
- onPacket(e, consumer);
- enet_packet_destroy(e.packet);
- break;
- case ENET_EVENT_TYPE_DISCONNECT_TIMEOUT:
- case ENET_EVENT_TYPE_DISCONNECT:
- onDisconnect(e, consumer);
- break;
- case ENET_EVENT_TYPE_NONE: return;
- }
- }
- }
- void send(OutPacket& p);
- void disconnect(Client& client);
- private:
- template<typename T>
- void onConnect(ENetEvent& e, T& consumer) {
- int id = idCounter++;
- if(clients.tryEmplace(id, e.peer, id)) {
- LOG_WARNING("id is connected twice");
- return;
- }
- static_assert(sizeof(e.peer->data) >= sizeof(id),
- "private data not big enough for id");
- memcpy(&(e.peer->data), &id, sizeof(id));
- Client* client = clients.search(id);
- if(client != nullptr) {
- consumer.onConnect(*client);
- } else {
- LOG_WARNING("cannot find added client");
- }
- }
- template<typename T>
- void onPacket(ENetEvent& e, T& consumer) {
- if(e.peer->data == nullptr) {
- LOG_WARNING("client without data sent package");
- return;
- }
- int id = -1;
- memcpy(&id, &(e.peer->data), sizeof(id));
- Client* client = clients.search(id);
- if(client != nullptr) {
- InPacket in(e.packet);
- consumer.onPacket(*client, in);
- } else {
- LOG_WARNING("client with invalid id sent package");
- }
- }
- template<typename T>
- void onDisconnect(ENetEvent& e, T& consumer) {
- if(e.peer->data == nullptr) {
- LOG_WARNING("client without data disconnected");
- return;
- }
- int id = -1;
- memcpy(&id, &(e.peer->data), sizeof(id));
- Client* client = clients.search(id);
- if(client != nullptr) {
- consumer.onDisconnect(*client);
- clients.remove(id);
- } else {
- LOG_WARNING("client has invalid id");
- }
- }
- };
- #endif
|