vectozavr-shooter/engine/network/ReliableMsg.cpp

24 lines
809 B
C++
Raw Normal View History

2021-09-13 15:53:43 +03:00
//
// Created by Neirokan on 30.04.2020
//
#include <cmath>
#include "ReliableMsg.h"
2021-10-09 13:41:12 +03:00
#include "../utils/Time.h"
2021-10-16 20:22:55 +03:00
#include "../Consts.h"
2021-09-13 15:53:43 +03:00
ReliableMsg::ReliableMsg(sf::Packet& packet, sf::IpAddress address, sf::Uint16 port) : packet(packet), address(address), port(port), lastTry(-std::numeric_limits<double>::max()), firstTry(Time::time()) {}
2021-09-13 15:53:43 +03:00
ReliableMsg::ReliableMsg(const ReliableMsg& msg) : packet(msg.packet), address(msg.address), port(msg.port), lastTry(msg.lastTry), firstTry(msg.firstTry) {}
bool ReliableMsg::trySend(sf::UdpSocket& socket)
{
2021-10-16 20:22:55 +03:00
if (Time::time() - firstTry > Consts::NETWORK_TIMEOUT)
2021-09-13 15:53:43 +03:00
return false;
2021-10-16 20:22:55 +03:00
if (Time::time() - lastTry > Consts::NETWORK_RELIABLE_RETRY_TIME)
2021-09-13 15:53:43 +03:00
{
lastTry = Time::time();
socket.send(packet, address, port);
}
return true;
}