Network.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include "client/Network.h"
  2. #include "client/Chat.h"
  3. #include "client/Main.h"
  4. #include "client/World.h"
  5. #include "common/NetworkPackets.h"
  6. #include "network/Client.h"
  7. #include "utils/Logger.h"
  8. static bool onWorldPacket(InPacket& in) {
  9. IntVector3 size = World::getSize();
  10. for(int x = 0; x < size[0]; x++) {
  11. for(int y = 0; y < size[1]; y++) {
  12. for(int z = 0; z < size[2]; z++) {
  13. uint8 data = 0;
  14. if(in.read(data)) {
  15. return true;
  16. }
  17. World::set(x, y, z, data);
  18. }
  19. }
  20. }
  21. return false;
  22. }
  23. static bool onSetBlockPacket(InPacket& in) {
  24. IntVector3 pos;
  25. uint8 type;
  26. if(in.read(pos) || in.read(type)) {
  27. return true;
  28. }
  29. World::set(pos[0], pos[1], pos[2], type);
  30. return false;
  31. }
  32. static bool onPlayerPacket(InPacket& in) {
  33. Vector3 pos;
  34. int client;
  35. if(in.read(pos) || in.read(client)) {
  36. return true;
  37. }
  38. OtherPlayer* p = players.search(client);
  39. if(p != nullptr) {
  40. p->position = pos;
  41. } else {
  42. players.add(client, {pos});
  43. }
  44. return false;
  45. }
  46. static bool onChatPacket(InPacket& in) {
  47. Chat::Message msg;
  48. uint32 u;
  49. while(!in.read(u)) {
  50. msg.appendUnicode(u);
  51. }
  52. Chat::add(msg);
  53. return false;
  54. }
  55. static bool handlePacket(ToClientPacket tcp, InPacket& in) {
  56. switch(tcp) {
  57. case ToClientPacket::WORLD: return onWorldPacket(in);
  58. case ToClientPacket::SET_BLOCK: return onSetBlockPacket(in);
  59. case ToClientPacket::PLAYER: return onPlayerPacket(in);
  60. case ToClientPacket::CHAT: return onChatPacket(in);
  61. }
  62. LOG_WARNING(StringBuffer<100>("unknown packet of type ")
  63. .append(static_cast<uint32>(tcp)));
  64. return false;
  65. }
  66. static void onPacket(InPacket& in) {
  67. uint8 type = 0;
  68. if(in.read(type)) {
  69. LOG_WARNING("received packet without type");
  70. return;
  71. }
  72. if(handlePacket(static_cast<ToClientPacket>(type), in)) {
  73. LOG_WARNING(StringBuffer<100>("invalid packet of type ")
  74. .append(static_cast<uint32>(type)));
  75. }
  76. }
  77. static void onDisconnect() {
  78. LOG_INFO("Disconnect");
  79. }
  80. bool Network::start(const char* server) {
  81. Error e = Client::start();
  82. if(e.has()) {
  83. LOG_ERROR(e.message);
  84. return true;
  85. }
  86. e = Client::connect(server, 11196, 40);
  87. if(e.has()) {
  88. LOG_ERROR(e.message);
  89. Client::stop();
  90. return true;
  91. }
  92. Client::setPacketHandler(onPacket);
  93. Client::setDisconnectHandler(onDisconnect);
  94. return false;
  95. }
  96. void Network::stop() {
  97. Client::stop();
  98. }
  99. void Network::tick() {
  100. Client::tick();
  101. }