GameClient.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. {
  29. StringBuffer<256> s;
  30. in.readString(s);
  31. puts(s);
  32. break;
  33. }
  34. case Packets::S_WORLD_SEGMENT:
  35. WorldPackets::receiveChunk(Game::world, in);
  36. break;
  37. case Packets::S_ENTITY_UPDATE: handleEntityUpdate(in); break;
  38. default: LOG_WARNING(INVALID_PACKAGE);
  39. }
  40. }
  41. };
  42. bool GameClient::init() {
  43. Error error = client.start();
  44. if(error.has()) {
  45. LOG_ERROR(error.message);
  46. }
  47. return error.has();
  48. }
  49. Error GameClient::connect(const char* address, Client::Port port, int timeout) {
  50. return client.connect(address, port, timeout);
  51. }
  52. void GameClient::consumeEvents() {
  53. Receiver r;
  54. client.consumeEvents(r);
  55. }
  56. void GameClient::send(OutPacket& out) {
  57. client.send(out);
  58. }