Files
Cubed/include/Cubed/gameplay/network_server.hpp
zhenyan121 03259f323c refactor: config (#30)
* refactor(config): replace singleton with dependency injection

* refactor(client_player): return ChunkPosSet by value in non-const getter

Copy the internal set under the mutex lock to avoid exposing a mutable reference that could be used unsafely after the lock is released. This fixes a potential data race when the caller holds the returned reference beyond the critical section. Also update the caller in `client_world` to remove the now-unnecessary `std::move`.

* fix(gameplay): prevent use-after-free in pending delete queues during destruction

* refactor(config): separate game and server configuration with explicit paths
2026-07-11 09:22:40 +08:00

36 lines
857 B
C++

#pragma once
#include "Cubed/config.hpp"
#include "Cubed/gameplay/server_world.hpp"
#include "Cubed/gameplay/session.hpp"
#include <asio.hpp>
#include <thread>
namespace Cubed {
class NetworkServer {
public:
explicit NetworkServer();
~NetworkServer();
void stop();
// Run in another thread after initialization is complete
void start_server(int port);
void start_server();
int port() const;
ServerWorld& server_world();
private:
Config m_config;
asio::io_context m_io;
std::thread m_net_thread;
int m_port = 25530;
std::atomic<bool> m_stopped{false};
std::atomic<bool> m_started{false};
ServerWorld m_world;
std::mutex m_session_mutex;
std::unordered_map<std::string, std::shared_ptr<Session>> m_session;
asio::awaitable<void> listen();
void net_run();
};
} // namespace Cubed