vectozavr-shooter/engine/network/ClientUDP.cpp

131 lines
3.2 KiB
C++
Raw Normal View History

2021-09-13 15:53:43 +03:00
//
// Created by Neirokan on 30.04.2020
//
#include "ClientUDP.h"
#include "MsgType.h"
#include "../utils/Time.h"
#include "../utils/Log.h"
2021-10-16 20:22:55 +03:00
#include "../Consts.h"
2021-09-13 15:53:43 +03:00
2021-10-30 15:59:55 +03:00
ClientUDP::ClientUDP() {
2021-10-31 11:39:08 +03:00
_socket.setTimeoutCallback([this](sf::Uint16 id) { return ClientUDP::timeout(id); });
2021-09-13 15:53:43 +03:00
}
2021-10-30 15:59:55 +03:00
bool ClientUDP::connected() const {
2021-09-13 15:53:43 +03:00
return _socket.ownId();
}
2021-10-30 15:59:55 +03:00
bool ClientUDP::isWorking() const {
2021-09-13 15:53:43 +03:00
return _working;
}
2021-10-30 15:59:55 +03:00
void ClientUDP::connect(sf::IpAddress ip, sf::Uint16 port) {
2021-10-25 22:32:55 +03:00
_ip = ip;
_port = port;
2021-09-13 15:53:43 +03:00
sf::Packet packet;
2021-10-16 20:22:55 +03:00
packet << MsgType::Connect << Consts::NETWORK_VERSION;
2021-09-13 15:53:43 +03:00
_working = _socket.bind(0);
_socket.addConnection(_socket.serverId(), ip, port);
_socket.sendRely(packet, _socket.serverId());
2021-10-25 04:09:45 +03:00
Log::log("ClientUDP::connect(): connecting to the server...");
2021-09-13 15:53:43 +03:00
}
2021-10-30 15:59:55 +03:00
void ClientUDP::update() {
if (!isWorking()) {
2021-09-13 15:53:43 +03:00
return;
2021-10-30 15:59:55 +03:00
}
2021-09-13 15:53:43 +03:00
2021-10-30 15:59:55 +03:00
while (isWorking() && process()) {}
2021-09-13 15:53:43 +03:00
// Send new client information to server
2021-10-16 20:22:55 +03:00
if (Time::time() - _lastBroadcast > 1.0 / Consts::NETWORK_WORLD_UPDATE_RATE && connected()) {
2021-09-13 15:53:43 +03:00
updatePacket();
_lastBroadcast = Time::time();
}
// Socket update
_socket.update();
}
2021-10-30 15:59:55 +03:00
void ClientUDP::disconnect() {
2021-09-13 15:53:43 +03:00
sf::Packet packet;
packet << MsgType::Disconnect << _socket.ownId();
_socket.send(packet, _socket.serverId());
_socket.unbind();
_working = false;
2021-10-25 04:09:45 +03:00
Log::log("ClientUDP::disconnect(): disconnected from the server.");
2021-09-13 15:53:43 +03:00
processDisconnected();
}
2021-10-30 15:59:55 +03:00
bool ClientUDP::timeout(sf::Uint16 id) {
2021-10-25 04:09:45 +03:00
Log::log("ClientUDP::timeout(): timeout from the server.");
2021-09-13 15:53:43 +03:00
2021-10-30 15:59:55 +03:00
if (id != _socket.serverId()) {
2021-09-13 15:53:43 +03:00
return true;
2021-10-30 15:59:55 +03:00
}
2021-09-13 15:53:43 +03:00
disconnect();
return false;
}
// Recive and process message.
// Returns true, if some message was received.
2021-10-30 15:59:55 +03:00
bool ClientUDP::process() {
2021-09-13 15:53:43 +03:00
sf::Packet packet;
sf::Uint16 senderId;
sf::Uint16 targetId;
2021-10-17 10:21:10 +03:00
MsgType type = _socket.receive(packet, senderId);
2021-10-30 15:59:55 +03:00
if (type == MsgType::Empty) {
2021-09-13 15:53:43 +03:00
return false;
2021-10-30 15:59:55 +03:00
}
if (!connected() && type != MsgType::Init) {
2021-09-13 15:53:43 +03:00
return true;
2021-10-30 15:59:55 +03:00
}
2021-09-13 15:53:43 +03:00
2021-10-28 16:58:02 +03:00
switch (type) {
2021-09-13 15:53:43 +03:00
// here we process any operations based on msg type
case MsgType::Init:
packet >> targetId;
_socket.setId(targetId);
2021-10-25 04:09:45 +03:00
Log::log("ClientUDP::process(): client Id = " + std::to_string(targetId) + " connected.");
2021-09-13 15:53:43 +03:00
processInit(packet);
break;
2021-10-28 16:58:02 +03:00
case MsgType::ServerUpdate:
2021-09-13 15:53:43 +03:00
processUpdate(packet);
break;
case MsgType::NewClient:
2021-10-25 04:09:45 +03:00
Log::log("ClientUDP::process(): new client init...");
2021-09-13 15:53:43 +03:00
processNewClient(packet);
break;
case MsgType::Disconnect:
packet >> targetId;
2021-10-30 15:59:55 +03:00
if (targetId == _socket.ownId()) {
2021-09-13 15:53:43 +03:00
disconnect();
2021-10-30 15:59:55 +03:00
}
2021-09-13 15:53:43 +03:00
2021-10-25 04:09:45 +03:00
Log::log("ClientUDP::process(): client Id = " + std::to_string(targetId) + " disconnected from the server");
2021-09-13 15:53:43 +03:00
processDisconnect(targetId);
break;
2021-10-28 16:58:02 +03:00
case MsgType::Custom:
processCustomPacket(packet);
break;
case MsgType::Error:
Log::log("ClientUDP::process(): Error message");
break;
2021-09-13 15:53:43 +03:00
default:
2021-10-28 16:58:02 +03:00
Log::log("ClientUDP::process(): unknown message type " + std::to_string(static_cast<int>(type)));
2021-09-13 15:53:43 +03:00
}
return true;
}