Network.cpp 2.6 KB

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