2021-09-13 15:53:43 +03:00
|
|
|
//
|
|
|
|
// Created by Neirokan on 30.04.2020
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "UDPConnection.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
|
|
|
|
2021-10-31 11:39:08 +03:00
|
|
|
UDPConnection::UDPConnection(sf::Uint16 id, sf::IpAddress ip, sf::Uint16 port) : _id(id), _ip(ip), _port(port),
|
|
|
|
lastMsg(Time::time()) {}
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-10-28 16:58:02 +03:00
|
|
|
sf::Uint16 UDPConnection::id() const {
|
2021-09-13 15:53:43 +03:00
|
|
|
return _id;
|
|
|
|
}
|
|
|
|
|
2021-10-31 11:39:08 +03:00
|
|
|
const sf::IpAddress &UDPConnection::ip() const {
|
2021-09-13 15:53:43 +03:00
|
|
|
return _ip;
|
|
|
|
}
|
|
|
|
|
2021-10-28 16:58:02 +03:00
|
|
|
sf::Uint16 UDPConnection::port() const {
|
2021-09-13 15:53:43 +03:00
|
|
|
return _port;
|
|
|
|
}
|
|
|
|
|
2021-10-28 16:58:02 +03:00
|
|
|
bool UDPConnection::timeout() const {
|
2021-10-16 20:22:55 +03:00
|
|
|
return Time::time() - lastMsg > Consts::NETWORK_TIMEOUT;
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
2021-10-31 11:39:08 +03:00
|
|
|
bool UDPConnection::same(sf::IpAddress &ip, sf::Uint16 port) const {
|
2021-09-13 15:53:43 +03:00
|
|
|
return _ip == ip && _port == port;
|
|
|
|
}
|
|
|
|
|
2021-10-28 16:58:02 +03:00
|
|
|
void UDPConnection::update() {
|
2021-09-13 15:53:43 +03:00
|
|
|
lastMsg = Time::time();
|
|
|
|
}
|
|
|
|
|
2021-10-31 11:39:08 +03:00
|
|
|
void UDPConnection::send(sf::UdpSocket &socket, sf::Packet &packet) {
|
2021-09-13 15:53:43 +03:00
|
|
|
socket.send(packet, _ip, _port);
|
|
|
|
}
|