Changeset View
Changeset View
Standalone View
Standalone View
source/network/NetSession.cpp
Show All 21 Lines | |||||
#include "NetMessage.h" | #include "NetMessage.h" | ||||
#include "NetStats.h" | #include "NetStats.h" | ||||
#include "lib/external_libraries/enet.h" | #include "lib/external_libraries/enet.h" | ||||
#include "ps/CLogger.h" | #include "ps/CLogger.h" | ||||
#include "ps/ConfigDB.h" | #include "ps/ConfigDB.h" | ||||
#include "ps/Profile.h" | #include "ps/Profile.h" | ||||
#include "scriptinterface/ScriptInterface.h" | #include "scriptinterface/ScriptInterface.h" | ||||
const u32 NETWORK_WARNING_TIMEOUT = 2000; | |||||
const u32 MAXIMUM_HOST_TIMEOUT = std::numeric_limits<u32>::max(); | const u32 MAXIMUM_HOST_TIMEOUT = std::numeric_limits<u32>::max(); | ||||
const double PACKET_LOSS_SCALE = (1 << 16); | |||||
const double PACKET_LOSS_SCALE_FACTOR = PACKET_LOSS_SCALE / ENET_PEER_PACKET_LOSS_SCALE; | |||||
static const int CHANNEL_COUNT = 1; | static const int CHANNEL_COUNT = 1; | ||||
// Only disable long timeouts after a packet from the remote enet peer has been processed. | // Only disable long timeouts after a packet from the remote enet peer has been processed. | ||||
// Otherwise a long timeout can still be in progress when disabling it here. | // Otherwise a long timeout can still be in progress when disabling it here. | ||||
void SetEnetLongTimeout(ENetPeer* peer, bool isLocalClient, bool enabled) | void SetEnetLongTimeout(ENetPeer* peer, bool isLocalClient, bool enabled) | ||||
{ | { | ||||
#if (ENET_VERSION >= ENET_VERSION_CREATE(1, 3, 4)) | #if (ENET_VERSION >= ENET_VERSION_CREATE(1, 3, 4)) | ||||
if (!peer || isLocalClient) | if (!peer || isLocalClient) | ||||
▲ Show 20 Lines • Show All 102 Lines • ▼ Show 20 Lines | while (enet_host_service(m_Host, &event, 0) > 0) | ||||
{ | { | ||||
case ENET_EVENT_TYPE_CONNECT: | case ENET_EVENT_TYPE_CONNECT: | ||||
{ | { | ||||
ENSURE(event.peer == m_Server); | ENSURE(event.peer == m_Server); | ||||
// Report the server address | // Report the server address | ||||
char hostname[256] = "(error)"; | char hostname[256] = "(error)"; | ||||
enet_address_get_host_ip(&event.peer->address, hostname, ARRAY_SIZE(hostname)); | enet_address_get_host_ip(&event.peer->address, hostname, ARRAY_SIZE(hostname)); | ||||
LOGMESSAGE("Net client: Connected to %s:%u", hostname, (unsigned int)event.peer->address.port); | LOGMESSAGE("Net client: Connected to %s:%u", hostname, (unsigned int)event.peer->address.port); | ||||
m_Client.HandleConnect(); | m_Client.HandleConnect(); | ||||
break; | break; | ||||
} | } | ||||
case ENET_EVENT_TYPE_DISCONNECT: | case ENET_EVENT_TYPE_DISCONNECT: | ||||
▲ Show 20 Lines • Show All 56 Lines • ▼ Show 20 Lines | |||||
u32 CNetClientSession::GetMeanRTT() const | u32 CNetClientSession::GetMeanRTT() const | ||||
{ | { | ||||
if (!m_Server) | if (!m_Server) | ||||
return 0; | return 0; | ||||
return m_Server->roundTripTime; | return m_Server->roundTripTime; | ||||
} | } | ||||
double CNetClientSession::GetPacketLossRatio() const | |||||
{ | |||||
if (!m_Server) | |||||
return 0; | |||||
Stan: Maybe 0.0 to mark it as a double. | |||||
return (double) m_Server->packetLoss / (double) ENET_PEER_PACKET_LOSS_SCALE; | |||||
} | |||||
void CNetClientSession::SetLongTimeout(bool enabled) | void CNetClientSession::SetLongTimeout(bool enabled) | ||||
{ | { | ||||
SetEnetLongTimeout(m_Server, m_IsLocalClient, enabled); | SetEnetLongTimeout(m_Server, m_IsLocalClient, enabled); | ||||
} | } | ||||
CNetServerSession::CNetServerSession(CNetServerWorker& server, ENetPeer* peer) : | CNetServerSession::CNetServerSession(CNetServerWorker& server, ENetPeer* peer) : | ||||
m_Server(server), m_FileTransferer(this), m_Peer(peer), m_IsLocalClient(false), m_HostID(0), m_GUID(), m_UserName() | m_Server(server), m_FileTransferer(this), m_Peer(peer), m_IsLocalClient(false), m_HostID(0), m_GUID(), m_UserName() | ||||
{ | { | ||||
char hostname[256] = "(error)"; | |||||
if (enet_address_get_host(&peer->address, hostname, ARRAY_SIZE(hostname)) == 0) | |||||
m_Hostname = hostname; | |||||
Done Inline ActionsShould test performance impact on connection build. elexis: Should test performance impact on connection build. | |||||
} | } | ||||
u32 CNetServerSession::GetIPAddress() const | u32 CNetServerSession::GetIPAddress() const | ||||
{ | { | ||||
return m_Peer->address.host; | return m_Peer->address.host; | ||||
} | } | ||||
std::string CNetServerSession::GetIPAddressString() const | |||||
{ | |||||
char ipAddress[256]; | |||||
ipAddress[255] = '\0'; | |||||
if (enet_address_get_host_ip(&m_Peer->address, ipAddress, sizeof ipAddress) != 0) | |||||
return std::string(); | |||||
return ipAddress; | |||||
} | |||||
std::string CNetServerSession::GetHostname() const | |||||
{ | |||||
return m_Hostname; | |||||
} | |||||
u32 CNetServerSession::GetLastReceivedTime() const | u32 CNetServerSession::GetLastReceivedTime() const | ||||
{ | { | ||||
if (!m_Peer) | if (!m_Peer) | ||||
return 0; | return 0; | ||||
return enet_time_get() - m_Peer->lastReceiveTime; | return enet_time_get() - m_Peer->lastReceiveTime; | ||||
} | } | ||||
u32 CNetServerSession::GetMeanRTT() const | u32 CNetServerSession::GetMeanRTT() const | ||||
{ | { | ||||
if (!m_Peer) | if (!m_Peer) | ||||
return 0; | return 0; | ||||
return m_Peer->roundTripTime; | return m_Peer->roundTripTime; | ||||
} | } | ||||
u32 CNetServerSession::GetPacketLoss() const | |||||
{ | |||||
if (!m_Peer) | |||||
return 0; | |||||
return (double) m_Peer->packetLoss * PACKET_LOSS_SCALE_FACTOR; | |||||
} | |||||
void CNetServerSession::Disconnect(u32 reason) | void CNetServerSession::Disconnect(u32 reason) | ||||
{ | { | ||||
Update((uint)NMT_CONNECTION_LOST, NULL); | Update((uint)NMT_CONNECTION_LOST, NULL); | ||||
enet_peer_disconnect(m_Peer, reason); | enet_peer_disconnect(m_Peer, reason); | ||||
} | } | ||||
void CNetServerSession::DisconnectNow(u32 reason) | void CNetServerSession::DisconnectNow(u32 reason) | ||||
Show All 31 Lines |
Wildfire Games · Phabricator
Maybe 0.0 to mark it as a double.