GameServer.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #include "server/GameServer.h"
  2. #include "common/network/Packets.h"
  3. #include "server/Game.h"
  4. static Server* server = nullptr;
  5. struct Receiver {
  6. void onConnect(Server::Client& client) {
  7. std::cout << "connected\n";
  8. World& w = Game::getTestWorld();
  9. OutPacket out = OutPacket::reliable(
  10. w.getHeight() * w.getSize() * w.getSize() * sizeof(BlockId) + 2);
  11. out.writeU16(ServerPacket::WORLD);
  12. for(int x = 0; x < w.getSize(); x++) {
  13. for(int y = 0; y < w.getHeight(); y++) {
  14. for(int z = 0; z < w.getSize(); z++) {
  15. out.writeU16(w.getBlock(x, y, z).getId());
  16. }
  17. }
  18. }
  19. client.send(out);
  20. }
  21. void onDisconnect(Server::Client& client) {
  22. (void)client;
  23. std::cout << "disconnected\n";
  24. }
  25. void onPacket(Server::Client& client, InPacket& in) {
  26. (void)client;
  27. StringBuffer<256> s;
  28. int8 c;
  29. while(in.readS8(c)) {
  30. s.append(c);
  31. }
  32. std::cout << "Packet: " << s << "\n";
  33. }
  34. };
  35. void GameServer::init(Server* s) {
  36. server = s;
  37. }
  38. void GameServer::tick() {
  39. Receiver r;
  40. server->consumeEvents(r);
  41. }
  42. void GameServer::sendToAll(OutPacket& out) {
  43. server->send(out);
  44. }