12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- #ifndef CLIENT_H
- #define CLIENT_H
- #include "network/ENet.h"
- #include "network/Packet.h"
- #include "utils/StringBuffer.h"
- class Client final {
- typedef enet_uint16 Port;
- typedef StringBuffer<256> Error;
- private:
- ENetHost* client;
- ENetPeer* connection;
- Error error;
- public:
- Client();
- Client(const Client&) = delete;
- Client(Client&&) = delete;
- ~Client();
- Client& operator=(const Client&) = delete;
- Client& operator=(Client&&) = delete;
- bool hasError() const;
- const Error& getError() const;
- bool connect(const char* server, Port port, int timeout);
- void disconnect();
- template<typename T>
- void consumeEvents(T& consumer) {
- ENetEvent e;
- while(enet_host_service(client, &e, 0) > 0) {
- switch(e.type) {
- case ENET_EVENT_TYPE_CONNECT: break;
- case ENET_EVENT_TYPE_DISCONNECT_TIMEOUT:
- case ENET_EVENT_TYPE_DISCONNECT: consumer.onDisconnect(); break;
- case ENET_EVENT_TYPE_NONE: return;
- case ENET_EVENT_TYPE_RECEIVE:
- InPacket in(e.packet);
- consumer.onPacket(in);
- enet_packet_destroy(e.packet);
- break;
- }
- }
- }
- void send(OutPacket& p);
- };
- #endif
|