vectozavr-shooter/engine/network/ClientUDP.cpp

129 lines
3.0 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 <thread>
#include "../utils/Time.h"
#include <cmath>
#include "../utils/Log.h"
2021-10-16 20:22:55 +03:00
#include "../Consts.h"
2021-09-13 15:53:43 +03:00
ClientUDP::ClientUDP() : _lastBroadcast(-std::numeric_limits<double>::max()), _working(false)
2021-09-13 15:53:43 +03:00
{
_socket.setTimeoutCallback(std::bind(&ClientUDP::timeout, this, std::placeholders::_1));
}
bool ClientUDP::connected() const
{
return _socket.ownId();
}
bool ClientUDP::isWorking() const
{
return _working;
}
void ClientUDP::connect(sf::IpAddress ip, sf::Uint16 port)
{
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
}
void ClientUDP::update()
{
if (!isWorking())
return;
while (isWorking() && process());
// 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();
}
void ClientUDP::disconnect()
{
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();
}
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
if (id != _socket.serverId())
return true;
disconnect();
return false;
}
// Recive and process message.
// Returns true, if some message was received.
bool ClientUDP::process()
{
sf::Packet packet;
sf::Uint16 senderId;
sf::Uint16 targetId;
2021-10-17 10:21:10 +03:00
MsgType type = _socket.receive(packet, senderId);
if (type == MsgType::Empty)
2021-09-13 15:53:43 +03:00
return false;
if (!connected() && type != MsgType::Init)
return true;
switch (type)
{
// 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;
case MsgType::Update:
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;
if (targetId == _socket.ownId())
disconnect();
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;
default:
processCustomPacket(type, packet);
}
return true;
}