2021-09-13 15:53:43 +03:00
|
|
|
//
|
|
|
|
// Created by Neirokan on 30.04.2020
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "ServerUDP.h"
|
|
|
|
#include "MsgType.h"
|
|
|
|
#include "../utils/Log.h"
|
|
|
|
|
2021-10-30 15:59:55 +03:00
|
|
|
ServerUDP::ServerUDP() {
|
|
|
|
_socket.setTimeoutCallback([this](sf::Uint16 playerId) { return timeout(playerId); });
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
2021-10-28 16:58:02 +03:00
|
|
|
bool ServerUDP::isWorking() const {
|
2021-09-13 15:53:43 +03:00
|
|
|
return _working;
|
|
|
|
}
|
|
|
|
|
2021-10-28 16:58:02 +03:00
|
|
|
bool ServerUDP::start(sf::Uint16 port) {
|
2021-09-13 15:53:43 +03:00
|
|
|
_working = _socket.bind(port);
|
|
|
|
|
2021-10-31 11:39:08 +03:00
|
|
|
if (_working) {
|
2021-10-25 04:16:00 +03:00
|
|
|
Log::log("ServerUDP::start(): the server was successfully started.");
|
2021-10-28 16:58:02 +03:00
|
|
|
} else {
|
2021-10-25 04:09:45 +03:00
|
|
|
Log::log("ServerUDP::start(): failed to start the server.");
|
2021-10-28 16:58:02 +03:00
|
|
|
}
|
2021-09-13 15:53:43 +03:00
|
|
|
|
|
|
|
return _working;
|
|
|
|
}
|
|
|
|
|
2021-10-28 16:58:02 +03:00
|
|
|
void ServerUDP::update() {
|
|
|
|
if (!isWorking()) {
|
2021-09-13 15:53:43 +03:00
|
|
|
return;
|
2021-10-28 16:58:02 +03:00
|
|
|
}
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-10-30 15:59:55 +03:00
|
|
|
while (process()) {}
|
2021-09-13 15:53:43 +03:00
|
|
|
|
|
|
|
// World state broadcast
|
2021-10-16 20:22:55 +03:00
|
|
|
if (Time::time() - _lastBroadcast > 1.0 / Consts::NETWORK_WORLD_UPDATE_RATE) {
|
2021-09-13 15:53:43 +03:00
|
|
|
broadcast();
|
|
|
|
_lastBroadcast = Time::time();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Socket update
|
|
|
|
_socket.update();
|
|
|
|
|
|
|
|
updateInfo();
|
|
|
|
}
|
|
|
|
|
2021-10-28 16:58:02 +03:00
|
|
|
void ServerUDP::stop() {
|
|
|
|
for (auto it = _clients.begin(); it != _clients.end();) {
|
2021-09-13 15:53:43 +03:00
|
|
|
sf::Packet packet;
|
|
|
|
packet << MsgType::Disconnect << *it;
|
|
|
|
_socket.send(packet, *it);
|
|
|
|
_clients.erase(it++);
|
|
|
|
}
|
|
|
|
|
|
|
|
_socket.unbind();
|
|
|
|
_working = false;
|
|
|
|
|
|
|
|
processStop();
|
|
|
|
|
2021-10-25 04:09:45 +03:00
|
|
|
Log::log("ServerUDP::stop(): the server was killed.");
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
2021-10-28 16:58:02 +03:00
|
|
|
bool ServerUDP::timeout(sf::Uint16 playerId) {
|
2021-09-13 15:53:43 +03:00
|
|
|
sf::Packet packet;
|
|
|
|
packet << MsgType::Disconnect << playerId;
|
|
|
|
|
|
|
|
_clients.erase(playerId);
|
|
|
|
|
2021-10-28 16:58:02 +03:00
|
|
|
for (auto client : _clients) {
|
2021-09-13 15:53:43 +03:00
|
|
|
_socket.sendRely(packet, client);
|
2021-10-28 16:58:02 +03:00
|
|
|
}
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-10-25 04:09:45 +03:00
|
|
|
Log::log("ServerUDP::timeout(): client Id = " + std::to_string(playerId) + " disconnected due to timeout.");
|
2021-09-13 15:53:43 +03:00
|
|
|
processDisconnect(playerId);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recive and process message.
|
|
|
|
// Returns true, if some message was received.
|
2021-10-28 16:58:02 +03:00
|
|
|
bool ServerUDP::process() {
|
2021-09-13 15:53:43 +03:00
|
|
|
sf::Packet packet;
|
|
|
|
sf::Packet sendPacket;
|
|
|
|
sf::Uint16 senderId;
|
|
|
|
|
2021-10-17 10:21:10 +03:00
|
|
|
MsgType type = _socket.receive(packet, senderId);
|
|
|
|
|
2021-10-28 16:58:02 +03:00
|
|
|
if (type == MsgType::Empty) {
|
2021-09-13 15:53:43 +03:00
|
|
|
return false;
|
2021-10-28 16:58:02 +03:00
|
|
|
}
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-10-17 10:21:10 +03:00
|
|
|
switch (type) {
|
2021-09-13 15:53:43 +03:00
|
|
|
// here we process any operations based on msg type
|
|
|
|
case MsgType::Connect:
|
2021-10-25 04:09:45 +03:00
|
|
|
Log::log("ServerUDP::process(): client Id = " + std::to_string(senderId) + " connecting...");
|
2021-09-13 15:53:43 +03:00
|
|
|
|
|
|
|
processConnect(senderId);
|
|
|
|
break;
|
|
|
|
case MsgType::ClientUpdate:
|
|
|
|
|
|
|
|
processClientUpdate(senderId, packet);
|
|
|
|
break;
|
|
|
|
case MsgType::Disconnect:
|
2021-10-25 04:09:45 +03:00
|
|
|
Log::log("ServerUDP::process(): client Id = " + std::to_string(senderId) + " disconnected.");
|
2021-09-13 15:53:43 +03:00
|
|
|
|
|
|
|
sendPacket << MsgType::Disconnect << senderId;
|
|
|
|
_clients.erase(senderId);
|
|
|
|
_socket.removeConnection(senderId);
|
2021-10-28 16:58:02 +03:00
|
|
|
for (auto client : _clients) {
|
2021-09-13 15:53:43 +03:00
|
|
|
_socket.sendRely(sendPacket, client);
|
2021-10-28 16:58:02 +03:00
|
|
|
}
|
2021-09-13 15:53:43 +03:00
|
|
|
|
|
|
|
processDisconnect(senderId);
|
|
|
|
break;
|
2021-10-28 16:58:02 +03:00
|
|
|
case MsgType::Custom:
|
|
|
|
processCustomPacket(packet, senderId);
|
|
|
|
break;
|
|
|
|
case MsgType::Error:
|
|
|
|
Log::log("ServerUDP::process(): Error message");
|
|
|
|
break;
|
2021-09-13 15:53:43 +03:00
|
|
|
default:
|
2021-10-28 16:58:02 +03:00
|
|
|
Log::log("ServerUDP::process(): message type " + std::to_string(static_cast<int>(type)));
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2021-10-16 20:22:55 +03:00
|
|
|
|
|
|
|
ServerUDP::~ServerUDP() {
|
|
|
|
stop();
|
|
|
|
_clients.clear();
|
|
|
|
}
|