define -> namespace::const

master
Vectozavr 2021-09-20 16:47:42 +07:00
parent 720b2e0ddd
commit c4b9bd0e35
6 changed files with 14 additions and 12 deletions

View File

@ -27,7 +27,7 @@ bool ClientUDP::isWorking() const
void ClientUDP::connect(sf::IpAddress ip, sf::Uint16 port) void ClientUDP::connect(sf::IpAddress ip, sf::Uint16 port)
{ {
sf::Packet packet; sf::Packet packet;
packet << MsgType::Connect << NETWORK_VERSION; packet << MsgType::Connect << Network::VERSION;
_working = _socket.bind(0); _working = _socket.bind(0);
_socket.addConnection(_socket.serverId(), ip, port); _socket.addConnection(_socket.serverId(), ip, port);
_socket.sendRely(packet, _socket.serverId()); _socket.sendRely(packet, _socket.serverId());
@ -43,7 +43,7 @@ void ClientUDP::update()
while (isWorking() && process()); while (isWorking() && process());
// Send new client information to server // Send new client information to server
if (Time::time() - _lastBroadcast > 1.0 / WORLD_UPDATE_RATE && connected()) { if (Time::time() - _lastBroadcast > 1.0 / Network::WORLD_UPDATE_RATE && connected()) {
updatePacket(); updatePacket();
_lastBroadcast = Time::time(); _lastBroadcast = Time::time();
} }

View File

@ -12,9 +12,9 @@ ReliableMsg::ReliableMsg(const ReliableMsg& msg) : packet(msg.packet), address(m
bool ReliableMsg::trySend(sf::UdpSocket& socket) bool ReliableMsg::trySend(sf::UdpSocket& socket)
{ {
if (Time::time() - firstTry > TIMEOUT_SECONDS) if (Time::time() - firstTry > Network::TIMEOUT)
return false; return false;
if (Time::time() - lastTry > RELIABLE_RETRY_TIME) if (Time::time() - lastTry > Network::RELIABLE_RETRY_TIME)
{ {
lastTry = Time::time(); lastTry = Time::time();
socket.send(packet, address, port); socket.send(packet, address, port);

View File

@ -40,7 +40,7 @@ void ServerUDP::update()
while (process()); while (process());
// World state broadcast // World state broadcast
if (Time::time() - _lastBroadcast > 1.0 / WORLD_UPDATE_RATE) { if (Time::time() - _lastBroadcast > 1.0 / Network::WORLD_UPDATE_RATE) {
broadcast(); broadcast();
_lastBroadcast = Time::time(); _lastBroadcast = Time::time();
} }

View File

@ -25,7 +25,7 @@ sf::Uint16 UDPConnection::port() const
bool UDPConnection::timeout() const bool UDPConnection::timeout() const
{ {
return Time::time() - lastMsg > TIMEOUT_SECONDS; return Time::time() - lastMsg > Network::TIMEOUT;
} }
bool UDPConnection::same(sf::IpAddress& ip, sf::Uint16 port) const bool UDPConnection::same(sf::IpAddress& ip, sf::Uint16 port) const

View File

@ -123,7 +123,7 @@ void UDPSocket::update()
for (auto it = _confirmTimes.begin(); it != _confirmTimes.end();) for (auto it = _confirmTimes.begin(); it != _confirmTimes.end();)
{ {
if (Time::time() - it->second > TIMEOUT_SECONDS) if (Time::time() - it->second > Network::TIMEOUT)
_confirmTimes.erase(it++); _confirmTimes.erase(it++);
else else
++it; ++it;
@ -160,7 +160,7 @@ MsgType UDPSocket::receive(sf::Packet& packet, sf::Uint16& senderId)
if (type == MsgType::Connect) if (type == MsgType::Connect)
{ {
sf::Uint32 version = 0; sf::Uint32 version = 0;
if (!(packet >> version) || version != NETWORK_VERSION) if (!(packet >> version) || version != Network::VERSION)
return MsgType::Error; return MsgType::Error;
sf::Uint16 tmp; sf::Uint16 tmp;
for (tmp = 64; tmp >= 1; tmp--) for (tmp = 64; tmp >= 1; tmp--)

View File

@ -5,9 +5,11 @@
#ifndef ENGINE_CONFIG_H #ifndef ENGINE_CONFIG_H
#define ENGINE_CONFIG_H #define ENGINE_CONFIG_H
#define NETWORK_VERSION 1U namespace Network {
#define TIMEOUT_SECONDS 5 const unsigned VERSION = 1U;
#define WORLD_UPDATE_RATE 30 const int TIMEOUT = 1U;
#define RELIABLE_RETRY_TIME (1.0/20) const int WORLD_UPDATE_RATE = 30;
const double RELIABLE_RETRY_TIME = 1.0/20;
}
#endif //INC_3DZAVR_CONFIG_H #endif //INC_3DZAVR_CONFIG_H