Changeset View
Changeset View
Standalone View
Standalone View
source/network/NetSession.cpp
/* Copyright (C) 2016 Wildfire Games. | /* Copyright (C) 2017 Wildfire Games. | ||||
* This file is part of 0 A.D. | * This file is part of 0 A.D. | ||||
* | * | ||||
* 0 A.D. is free software: you can redistribute it and/or modify | * 0 A.D. is free software: you can redistribute it and/or modify | ||||
* it under the terms of the GNU General Public License as published by | * it under the terms of the GNU General Public License as published by | ||||
* the Free Software Foundation, either version 2 of the License, or | * the Free Software Foundation, either version 2 of the License, or | ||||
* (at your option) any later version. | * (at your option) any later version. | ||||
* | * | ||||
* 0 A.D. is distributed in the hope that it will be useful, | * 0 A.D. is distributed in the hope that it will be useful, | ||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
* GNU General Public License for more details. | * GNU General Public License for more details. | ||||
* | * | ||||
* You should have received a copy of the GNU General Public License | * You should have received a copy of the GNU General Public License | ||||
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. | * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. | ||||
*/ | */ | ||||
#include "precompiled.h" | #include "precompiled.h" | ||||
#include "NetSession.h" | #include "NetSession.h" | ||||
#include "NetClient.h" | #include "NetClient.h" | ||||
#include "NetServer.h" | #include "NetServer.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/Profile.h" | #include "ps/Profile.h" | ||||
#include "scriptinterface/ScriptInterface.h" | #include "scriptinterface/ScriptInterface.h" | ||||
const u32 NETWORK_WARNING_TIMEOUT = 4000; | const u32 NETWORK_WARNING_TIMEOUT = 4000; | ||||
▲ Show 20 Lines • Show All 104 Lines • ▼ Show 20 Lines | case ENET_EVENT_TYPE_DISCONNECT: | ||||
ENSURE(event.peer == m_Server); | ENSURE(event.peer == m_Server); | ||||
LOGMESSAGE("Net client: Disconnected"); | LOGMESSAGE("Net client: Disconnected"); | ||||
m_Client.HandleDisconnect(event.data); | m_Client.HandleDisconnect(event.data); | ||||
return; | return; | ||||
} | } | ||||
case ENET_EVENT_TYPE_RECEIVE: | case ENET_EVENT_TYPE_RECEIVE: | ||||
{ | { | ||||
CNetMessage* msg = CNetMessageFactory::CreateMessage(event.packet->data, event.packet->dataLength, m_Client.GetScriptInterface()); | CNetMessageBuffer msg(event.packet->data, event.packet->dataLength); | ||||
leper: Why does this hunk differ in the (`if (msg)`) check from the one in net server? | |||||
if (msg) | |||||
{ | |||||
LOGMESSAGE("Net client: Received message %s of size %lu from server", msg->ToString().c_str(), (unsigned long)msg->GetSerializedLength()); | |||||
m_Client.HandleMessage(msg); | m_Client.HandleMessage(msg); | ||||
delete msg; | |||||
} | |||||
enet_packet_destroy(event.packet); | enet_packet_destroy(event.packet); | ||||
break; | break; | ||||
} | } | ||||
case ENET_EVENT_TYPE_NONE: | case ENET_EVENT_TYPE_NONE: | ||||
break; | break; | ||||
} | } | ||||
} | } | ||||
} | } | ||||
void CNetClientSession::Flush() | void CNetClientSession::Flush() | ||||
{ | { | ||||
PROFILE3("net client flush"); | PROFILE3("net client flush"); | ||||
ENSURE(m_Host && m_Server); | ENSURE(m_Host && m_Server); | ||||
enet_host_flush(m_Host); | enet_host_flush(m_Host); | ||||
} | } | ||||
bool CNetClientSession::SendMessage(const CNetMessage* message) | bool CNetClientSession::SendMessage(const CNetMessageBuffer& message) | ||||
{ | { | ||||
ENSURE(m_Host && m_Server); | ENSURE(m_Host && m_Server); | ||||
return CNetHost::SendMessage(message, m_Server, "server"); | return CNetHost::SendMessage(message, m_Server, "server"); | ||||
} | } | ||||
u32 CNetClientSession::GetLastReceivedTime() const | u32 CNetClientSession::GetLastReceivedTime() const | ||||
{ | { | ||||
Show All 9 Lines | if (!m_Server) | ||||
return 0; | return 0; | ||||
return m_Server->roundTripTime; | return m_Server->roundTripTime; | ||||
} | } | ||||
CNetServerSession::CNetServerSession(CNetServerWorker& server, ENetPeer* peer) : | CNetServerSession::CNetServerSession(CNetServerWorker& server, ENetPeer* peer) : | ||||
m_Server(server), m_FileTransferer(this), m_Peer(peer) | CFsm(NSS_HANDSHAKE), m_Server(server), m_FileTransferer(this), m_Peer(peer) | ||||
{ | { | ||||
} | } | ||||
u32 CNetServerSession::GetIPAddress() const | u32 CNetServerSession::GetIPAddress() const | ||||
{ | { | ||||
return m_Peer->address.host; | return m_Peer->address.host; | ||||
} | } | ||||
Show All 20 Lines | void CNetServerSession::Disconnect(u32 reason) | ||||
enet_peer_disconnect(m_Peer, reason); | enet_peer_disconnect(m_Peer, reason); | ||||
} | } | ||||
void CNetServerSession::DisconnectNow(u32 reason) | void CNetServerSession::DisconnectNow(u32 reason) | ||||
{ | { | ||||
enet_peer_disconnect_now(m_Peer, reason); | enet_peer_disconnect_now(m_Peer, reason); | ||||
} | } | ||||
bool CNetServerSession::SendMessage(const CNetMessage* message) | bool CNetServerSession::SendMessage(const CNetMessageBuffer& message) | ||||
{ | { | ||||
return m_Server.SendMessage(m_Peer, message); | return m_Server.SendMessage(m_Peer, message); | ||||
} | } | ||||
bool CNetServerSession::IsLocalClient() const | bool CNetServerSession::IsLocalClient() const | ||||
{ | { | ||||
return m_IsLocalClient; | return m_IsLocalClient; | ||||
} | } | ||||
Show All 13 Lines |
Wildfire Games · Phabricator
Why does this hunk differ in the (if (msg)) check from the one in net server?