GIGA REFACTOR
This commit is contained in:
@@ -6,7 +6,7 @@ namespace td {
|
||||
namespace server {
|
||||
|
||||
Server::Server(const std::string& worldFilePath) : m_ServerRunning(false) {
|
||||
m_Game.getWorld()->loadMapFromFile(worldFilePath);
|
||||
m_Game.GetWorld()->LoadMapFromFile(worldFilePath);
|
||||
}
|
||||
|
||||
Server::~Server() {
|
||||
@@ -14,117 +14,117 @@ Server::~Server() {
|
||||
m_Thread.join();
|
||||
}
|
||||
|
||||
void Server::startThread() {
|
||||
void Server::StartThread() {
|
||||
m_Thread = std::thread([this]() {
|
||||
std::uint64_t lastTime = td::utils::getTime();
|
||||
std::uint64_t lastTime = td::utils::GetTime();
|
||||
while (m_ServerRunning) {
|
||||
std::uint64_t time = td::utils::getTime();
|
||||
std::uint64_t time = td::utils::GetTime();
|
||||
|
||||
std::uint64_t delta = time - lastTime;
|
||||
|
||||
if (delta >= SERVER_TICK) {
|
||||
tick(delta);
|
||||
lastTime = td::utils::getTime();
|
||||
Tick(delta);
|
||||
lastTime = td::utils::GetTime();
|
||||
std::uint64_t sleepTime = SERVER_TICK - (delta - SERVER_TICK);
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(sleepTime));
|
||||
}
|
||||
|
||||
}
|
||||
clean();
|
||||
Clean();
|
||||
});
|
||||
}
|
||||
|
||||
void Server::close() {
|
||||
stopThread();
|
||||
void Server::Close() {
|
||||
StopThread();
|
||||
}
|
||||
|
||||
void Server::stopThread() {
|
||||
void Server::StopThread() {
|
||||
m_ServerRunning = false;
|
||||
}
|
||||
|
||||
bool Server::start(std::uint16_t port) {
|
||||
if (!m_Listener.listen(port, 10)) {
|
||||
bool Server::Start(std::uint16_t port) {
|
||||
if (!m_Listener.Listen(port, 10)) {
|
||||
std::cout << "Failed to bind port " << port << " !\n";
|
||||
return false;
|
||||
}
|
||||
if (!m_Listener.setBlocking(false)) {
|
||||
if (!m_Listener.SetBlocking(false)) {
|
||||
std::cout << "Failed to block server socket !\n";
|
||||
return false;
|
||||
}
|
||||
std::cout << "Server started at port " << port << " !\n";
|
||||
m_TickCounter.reset();
|
||||
m_TickCounter.Reset();
|
||||
m_ServerRunning = true;
|
||||
startThread();
|
||||
StartThread();
|
||||
return true;
|
||||
}
|
||||
|
||||
void Server::clean() {
|
||||
m_Listener.close();
|
||||
m_Listener.destroy();
|
||||
void Server::Clean() {
|
||||
m_Listener.Close();
|
||||
m_Listener.Destroy();
|
||||
|
||||
m_Connections.clear();
|
||||
getPlayers().clear();
|
||||
GetPlayers().clear();
|
||||
|
||||
std::cout << "Server successfully stopped !\n";
|
||||
}
|
||||
|
||||
void Server::stop() {
|
||||
void Server::Stop() {
|
||||
if (!m_ServerRunning)
|
||||
return;
|
||||
|
||||
protocol::DisconnectPacket packet("Server closed");
|
||||
broadcastPacket(&packet);
|
||||
BroadcastPacket(&packet);
|
||||
|
||||
stopThread();
|
||||
StopThread();
|
||||
}
|
||||
|
||||
void Server::tick(std::uint64_t delta) {
|
||||
accept();
|
||||
updateSockets();
|
||||
m_Lobby.tick();
|
||||
m_Game.tick(delta);
|
||||
if (m_TickCounter.update()) {
|
||||
protocol::ServerTpsPacket packet(m_TickCounter.getTPS(), utils::getTime());
|
||||
broadcastPacket(&packet);
|
||||
void Server::Tick(std::uint64_t delta) {
|
||||
Accept();
|
||||
UpdateSockets();
|
||||
m_Lobby.Tick();
|
||||
m_Game.Tick(delta);
|
||||
if (m_TickCounter.Update()) {
|
||||
protocol::ServerTpsPacket packet(m_TickCounter.GetTPS(), utils::GetTime());
|
||||
BroadcastPacket(&packet);
|
||||
}
|
||||
}
|
||||
|
||||
void Server::accept() {
|
||||
void Server::Accept() {
|
||||
static std::uint8_t newPlayerID = 0;
|
||||
network::TCPSocket newSocket;
|
||||
if (m_Listener.accept(newSocket)) {
|
||||
if (m_Listener.Accept(newSocket)) {
|
||||
ServerConnexion con(newSocket, newPlayerID);
|
||||
m_Connections.insert(std::move(ConnexionMap::value_type{ newPlayerID, std::move(con) }));
|
||||
OnPlayerJoin(newPlayerID);
|
||||
m_Connections[newPlayerID].setServer(this);
|
||||
m_Connections[newPlayerID].SetServer(this);
|
||||
newPlayerID++;
|
||||
}
|
||||
}
|
||||
|
||||
void Server::updateSockets() {
|
||||
void Server::UpdateSockets() {
|
||||
std::int16_t closedConnexionID = -1;
|
||||
for (auto& connection : m_Connections) {
|
||||
ServerConnexion& con = connection.second;
|
||||
if (con.getSocketStatus() != network::Socket::Status::Connected) {
|
||||
if (con.GetSocketStatus() != network::Socket::Status::Connected) {
|
||||
closedConnexionID = connection.first;
|
||||
} else {
|
||||
con.updateSocket();
|
||||
con.UpdateSocket();
|
||||
}
|
||||
}
|
||||
if (closedConnexionID != -1) {
|
||||
removeConnexion(closedConnexionID);
|
||||
RemoveConnexion(closedConnexionID);
|
||||
}
|
||||
}
|
||||
|
||||
void Server::broadcastPacket(const protocol::Packet* packet) {
|
||||
void Server::BroadcastPacket(const protocol::Packet* packet) {
|
||||
for (auto& connection : m_Connections) {
|
||||
ServerConnexion& con = connection.second;
|
||||
con.sendPacket(packet);
|
||||
con.SendPacket(packet);
|
||||
}
|
||||
}
|
||||
|
||||
void Server::removeConnexion(std::uint8_t connexionID) {
|
||||
getPlayers().erase(getPlayers().find(connexionID));
|
||||
void Server::RemoveConnexion(std::uint8_t connexionID) {
|
||||
GetPlayers().erase(GetPlayers().find(connexionID));
|
||||
m_Connections.erase(connexionID);
|
||||
m_Lobby.OnPlayerLeave(connexionID);
|
||||
OnPlayerLeave(connexionID);
|
||||
@@ -133,12 +133,12 @@ void Server::removeConnexion(std::uint8_t connexionID) {
|
||||
void Server::OnPlayerJoin(std::uint8_t id) {
|
||||
m_Lobby.OnPlayerJoin(id);
|
||||
|
||||
getPlayers().insert({ id, game::Player{id} });
|
||||
GetPlayers().insert({ id, game::Player{id} });
|
||||
}
|
||||
|
||||
void Server::OnPlayerLeave(std::uint8_t id) {
|
||||
protocol::PlayerLeavePacket packet(id);
|
||||
broadcastPacket(&packet);
|
||||
BroadcastPacket(&packet);
|
||||
}
|
||||
|
||||
} // namespace server
|
||||
|
||||
Reference in New Issue
Block a user