Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : #include "precompiled.h" 19 : 20 : #include "NetServerSession.h" 21 : 22 : #include "lib/external_libraries/enet.h" 23 : #include "network/NetMessage.h" 24 : #include "network/NetMessages.h" 25 : #include "network/NetServer.h" 26 : #include "ps/CLogger.h" 27 : 28 0 : CNetServerSession::CNetServerSession(CNetServerWorker& server, ENetPeer* peer) : 29 0 : m_Server(server), m_FileTransferer(*this), m_Peer(peer) 30 : { 31 0 : } 32 : 33 0 : std::uint32_t CNetServerSession::GetIPAddress() const 34 : { 35 0 : return m_Peer->address.host; 36 : } 37 : 38 0 : std::uint32_t CNetServerSession::GetLastReceivedTime() const 39 : { 40 0 : if (!m_Peer) 41 : return 0; 42 : 43 0 : return enet_time_get() - m_Peer->lastReceiveTime; 44 : } 45 : 46 0 : std::uint32_t CNetServerSession::GetMeanRTT() const 47 : { 48 0 : if (!m_Peer) 49 : return 0; 50 : 51 0 : return m_Peer->roundTripTime; 52 : } 53 : 54 0 : void CNetServerSession::Disconnect(NetDisconnectReason reason) 55 : { 56 0 : if (reason == NDR_UNKNOWN) 57 0 : LOGWARNING("Disconnecting client without communicating the disconnect reason!"); 58 : 59 0 : Update(static_cast<unsigned int>(NMT_CONNECTION_LOST), nullptr); 60 : 61 0 : enet_peer_disconnect(m_Peer, static_cast<enet_uint32>(reason)); 62 0 : } 63 : 64 0 : void CNetServerSession::DisconnectNow(NetDisconnectReason reason) 65 : { 66 0 : if (reason == NDR_UNKNOWN) 67 0 : LOGWARNING("Disconnecting client without communicating the disconnect reason!"); 68 : 69 0 : enet_peer_disconnect_now(m_Peer, static_cast<enet_uint32>(reason)); 70 0 : } 71 : 72 0 : bool CNetServerSession::SendMessage(const CNetMessage* message) 73 : { 74 0 : return m_Server.SendMessage(m_Peer, message); 75 : }