12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- #include "client/GameClient.h"
- #include "client/Game.h"
- #include "client/packets/WorldPackets.h"
- #include "common/network/Packets.h"
- #include "utils/Logger.h"
- static constexpr const char* INVALID_PACKAGE = "invalid package from server";
- static Client client;
- static void handleEntityUpdate(InPacket& in) {
- EntityUpdatePacket p;
- if(p.read(in)) {
- LOG_WARNING(INVALID_PACKAGE);
- return;
- }
- Game::onEntityUpdate(p);
- }
- struct Receiver {
- void onConnect() {
- }
- void onDisconnect() {
- }
- void onPacket(InPacket& in) {
- uint16 id;
- if(in.readU16(id)) {
- return;
- }
- switch(id) {
- case Packets::S_CHAT: {
- StringBuffer<256> s;
- in.readString(s);
- puts(s);
- break;
- }
- case Packets::S_WORLD_SEGMENT:
- WorldPackets::receiveChunk(in);
- break;
- case Packets::S_ENTITY_UPDATE: handleEntityUpdate(in); break;
- default: LOG_WARNING(INVALID_PACKAGE);
- }
- }
- };
- bool GameClient::init() {
- Error error = client.start();
- if(error.has()) {
- LOG_ERROR(error.message);
- }
- return error.has();
- }
- Error GameClient::connect(const char* address, Client::Port port, int timeout) {
- return client.connect(address, port, timeout);
- }
- void GameClient::consumeEvents() {
- Receiver r;
- client.consumeEvents(r);
- }
- void GameClient::send(OutPacket& out) {
- client.send(out);
- }
|