GameClient.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include "client/GameClient.h"
  2. #include "client/Game.h"
  3. #include "client/packets/WorldPackets.h"
  4. #include "common/network/Packets.h"
  5. #include "utils/Logger.h"
  6. static constexpr const char* INVALID_PACKAGE = "invalid package from server";
  7. static Client client;
  8. static void handleEntityUpdate(InPacket& in) {
  9. EntityUpdatePacket p;
  10. if(p.read(in)) {
  11. LOG_WARNING(INVALID_PACKAGE);
  12. return;
  13. }
  14. Game::onEntityUpdate(p);
  15. }
  16. struct Receiver {
  17. void onConnect() {
  18. }
  19. void onDisconnect() {
  20. }
  21. void onPacket(InPacket& in) {
  22. uint16 id;
  23. if(in.readU16(id)) {
  24. return;
  25. }
  26. switch(id) {
  27. case Packets::S_CHAT: {
  28. StringBuffer<256> s;
  29. in.readString(s);
  30. puts(s);
  31. break;
  32. }
  33. case Packets::S_WORLD_SEGMENT:
  34. WorldPackets::receiveChunk(in);
  35. break;
  36. case Packets::S_ENTITY_UPDATE: handleEntityUpdate(in); break;
  37. default: LOG_WARNING(INVALID_PACKAGE);
  38. }
  39. }
  40. };
  41. bool GameClient::init() {
  42. Error error = client.start();
  43. if(error.has()) {
  44. LOG_ERROR(error.message);
  45. }
  46. return error.has();
  47. }
  48. Error GameClient::connect(const char* address, Client::Port port, int timeout) {
  49. return client.connect(address, port, timeout);
  50. }
  51. void GameClient::consumeEvents() {
  52. Receiver r;
  53. client.consumeEvents(r);
  54. }
  55. void GameClient::send(OutPacket& out) {
  56. client.send(out);
  57. }