diff --git a/source/network/NetClient.cpp b/source/network/NetClient.cpp index 9b6720d5e1..b682820e8a 100644 --- a/source/network/NetClient.cpp +++ b/source/network/NetClient.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2025 Wildfire Games. +/* Copyright (C) 2026 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -25,12 +25,12 @@ #include "lib/status.h" #include "lib/utf8.h" #include "lobby/IXmppClient.h" +#include "network/NetClientSession.h" #include "network/NetClientTurnManager.h" #include "network/NetEnet.h" #include "network/NetFileTransfer.h" #include "network/NetMessage.h" #include "network/NetProtocol.h" -#include "network/NetSession.h" #include "network/StunClient.h" #include "ps/CLogger.h" #include "ps/CStr.h" diff --git a/source/network/NetSession.cpp b/source/network/NetClientSession.cpp similarity index 83% rename from source/network/NetSession.cpp rename to source/network/NetClientSession.cpp index f94a3da097..0577981250 100644 --- a/source/network/NetSession.cpp +++ b/source/network/NetClientSession.cpp @@ -17,14 +17,13 @@ #include "precompiled.h" -#include "NetSession.h" +#include "NetClientSession.h" #include "lib/code_generation.h" #include "lib/debug.h" #include "network/NetClient.h" #include "network/NetEnet.h" #include "network/NetMessage.h" -#include "network/NetServer.h" #include "network/NetStats.h" #include "ps/CLogger.h" #include "ps/ProfileViewer.h" @@ -236,51 +235,3 @@ u32 CNetClientSession::GetMeanRTT() const return m_MeanRTT; } -CNetServerSession::CNetServerSession(CNetServerWorker& server, ENetPeer* peer) : - m_Server(server), m_FileTransferer(*this), m_Peer(peer) -{ -} - -u32 CNetServerSession::GetIPAddress() const -{ - return m_Peer->address.host; -} - -u32 CNetServerSession::GetLastReceivedTime() const -{ - if (!m_Peer) - return 0; - - return enet_time_get() - m_Peer->lastReceiveTime; -} - -u32 CNetServerSession::GetMeanRTT() const -{ - if (!m_Peer) - return 0; - - return m_Peer->roundTripTime; -} - -void CNetServerSession::Disconnect(NetDisconnectReason reason) -{ - if (reason == NDR_UNKNOWN) - LOGWARNING("Disconnecting client without communicating the disconnect reason!"); - - Update((uint)NMT_CONNECTION_LOST, NULL); - - enet_peer_disconnect(m_Peer, static_cast(reason)); -} - -void CNetServerSession::DisconnectNow(NetDisconnectReason reason) -{ - if (reason == NDR_UNKNOWN) - LOGWARNING("Disconnecting client without communicating the disconnect reason!"); - - enet_peer_disconnect_now(m_Peer, static_cast(reason)); -} - -bool CNetServerSession::SendMessage(const CNetMessage* message) -{ - return m_Server.SendMessage(m_Peer, message); -} diff --git a/source/network/NetSession.h b/source/network/NetClientSession.h similarity index 60% rename from source/network/NetSession.h rename to source/network/NetClientSession.h index 30fcf269a8..d50b8a9de6 100644 --- a/source/network/NetSession.h +++ b/source/network/NetClientSession.h @@ -21,23 +21,16 @@ #include "lib/code_annotation.h" #include "lib/external_libraries/enet.h" #include "lib/types.h" -#include "network/FSM.h" #include "network/NetFileTransfer.h" #include "network/NetHost.h" -#include "ps/CStr.h" #include #include class CNetClient; class CNetMessage; -class CNetServerWorker; class CNetStatsTable; - -/** - * Report the peer if we didn't receive a packet after this time (milliseconds). - */ -inline constexpr u32 NETWORK_WARNING_TIMEOUT = 2000; +class CStr; typedef struct _ENetHost ENetHost; @@ -118,10 +111,10 @@ private: boost::lockfree::queue m_OutgoingMessages{16}; // Last known state. If false, flushing errors are silenced. - bool m_Connected = false; + bool m_Connected{false}; // Whether this session was ever connected to the server. - bool m_WasConnected = false; + bool m_WasConnected{false}; // Wrapper around enet stats - those are atomic as the code is lock-free. std::atomic m_LastReceivedTime{0}; @@ -136,78 +129,4 @@ private: CNetStatsTable* m_Stats{nullptr}; }; - -/** - * The server's end of a network session. - * Represents an abstraction of the state of the client, storing all the per-client data - * needed by the server. - * - * Thread-safety: - * - This is constructed and used by CNetServerWorker in the network server thread. - */ -class CNetServerSession : public CFsm -{ - NONCOPYABLE(CNetServerSession); - -public: - CNetServerSession(CNetServerWorker& server, ENetPeer* peer); - - CNetServerWorker& GetServer() { return m_Server; } - - const CStr& GetGUID() const { return m_GUID; } - void SetGUID(const CStr& guid) { m_GUID = guid; } - - const CStrW& GetUserName() const { return m_UserName; } - void SetUserName(const CStrW& name) { m_UserName = name; } - - u32 GetHostID() const { return m_HostID; } - void SetHostID(u32 id) { m_HostID = id; } - - u32 GetIPAddress() const; - - /** - * Number of milliseconds since the latest packet of that client was received. - */ - u32 GetLastReceivedTime() const; - - /** - * Average round trip time to the client. - */ - u32 GetMeanRTT() const; - - /** - * Sends a disconnection notification to the client, - * and sends a NMT_CONNECTION_LOST message to the session FSM. - * The server will receive a disconnection notification after a while. - * The server will not receive any further messages sent via this session. - */ - void Disconnect(NetDisconnectReason reason); - - /** - * Sends an unreliable disconnection notification to the client. - * The server will not receive any disconnection notification. - * The server will not receive any further messages sent via this session. - */ - void DisconnectNow(NetDisconnectReason reason); - - /** - * Send a message to the client. - */ - bool SendMessage(const CNetMessage* message); - - CNetFileTransferer& GetFileTransferer() { return m_FileTransferer; } - -private: - CNetServerWorker& m_Server; - - CNetFileTransferer m_FileTransferer; - - ENetPeer* m_Peer; - - CStr m_GUID; - CStrW m_UserName; - u32 m_HostID{0}; - CStr m_Password; -}; - #endif // NETSESSION_H diff --git a/source/network/NetFileTransfer.cpp b/source/network/NetFileTransfer.cpp index d989ec1136..06caf8cede 100644 --- a/source/network/NetFileTransfer.cpp +++ b/source/network/NetFileTransfer.cpp @@ -23,7 +23,6 @@ #include "lib/posix/posix_types.h" #include "lib/timer.h" #include "network/NetMessage.h" -#include "network/NetSession.h" #include "ps/CLogger.h" #include "ps/CStr.h" diff --git a/source/network/NetProtocol.h b/source/network/NetProtocol.h index 96074c9964..7faa82b721 100644 --- a/source/network/NetProtocol.h +++ b/source/network/NetProtocol.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2025 Wildfire Games. +/* Copyright (C) 2026 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -27,6 +27,11 @@ #include #include +/** + * Report the peer if we didn't receive a packet after this time (milliseconds). + */ +inline constexpr u32 NETWORK_WARNING_TIMEOUT{2000}; + struct HandshakeError { std::string componentType; diff --git a/source/network/NetServer.cpp b/source/network/NetServer.cpp index d562dd0392..bc6cfe4dcb 100644 --- a/source/network/NetServer.cpp +++ b/source/network/NetServer.cpp @@ -33,7 +33,7 @@ #include "network/NetMessage.h" #include "network/NetProtocol.h" #include "network/NetServerTurnManager.h" -#include "network/NetSession.h" +#include "network/NetServerSession.h" #include "network/NetStats.h" #include "network/StunClient.h" #include "ps/CLogger.h" diff --git a/source/network/NetServerSession.cpp b/source/network/NetServerSession.cpp new file mode 100644 index 0000000000..4bd1793420 --- /dev/null +++ b/source/network/NetServerSession.cpp @@ -0,0 +1,75 @@ +/* Copyright (C) 2026 Wildfire Games. + * This file is part of 0 A.D. + * + * 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 + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * 0 A.D. is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with 0 A.D. If not, see . + */ + +#include "precompiled.h" + +#include "NetServerSession.h" + +#include "lib/external_libraries/enet.h" +#include "network/NetMessage.h" +#include "network/NetMessages.h" +#include "network/NetServer.h" +#include "ps/CLogger.h" + +CNetServerSession::CNetServerSession(CNetServerWorker& server, ENetPeer* peer) : + m_Server(server), m_FileTransferer(*this), m_Peer(peer) +{ +} + +u32 CNetServerSession::GetIPAddress() const +{ + return m_Peer->address.host; +} + +u32 CNetServerSession::GetLastReceivedTime() const +{ + if (!m_Peer) + return 0; + + return enet_time_get() - m_Peer->lastReceiveTime; +} + +u32 CNetServerSession::GetMeanRTT() const +{ + if (!m_Peer) + return 0; + + return m_Peer->roundTripTime; +} + +void CNetServerSession::Disconnect(NetDisconnectReason reason) +{ + if (reason == NDR_UNKNOWN) + LOGWARNING("Disconnecting client without communicating the disconnect reason!"); + + Update((uint)NMT_CONNECTION_LOST, NULL); + + enet_peer_disconnect(m_Peer, static_cast(reason)); +} + +void CNetServerSession::DisconnectNow(NetDisconnectReason reason) +{ + if (reason == NDR_UNKNOWN) + LOGWARNING("Disconnecting client without communicating the disconnect reason!"); + + enet_peer_disconnect_now(m_Peer, static_cast(reason)); +} + +bool CNetServerSession::SendMessage(const CNetMessage* message) +{ + return m_Server.SendMessage(m_Peer, message); +} diff --git a/source/network/NetServerSession.h b/source/network/NetServerSession.h new file mode 100644 index 0000000000..635e33b252 --- /dev/null +++ b/source/network/NetServerSession.h @@ -0,0 +1,103 @@ +/* Copyright (C) 2026 Wildfire Games. + * This file is part of 0 A.D. + * + * 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 + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * 0 A.D. is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with 0 A.D. If not, see . + */ + +#ifndef NET_SERVER_SESSION_H +#define NET_SERVER_SESSION_H + +#include "network/FSM.h" +#include "network/NetFileTransfer.h" +#include "network/NetHost.h" +#include "ps/CStr.h" + +class CNetServerWorker; + +typedef struct _ENetPeer ENetPeer; + +/** + * The server's end of a network session. + * Represents an abstraction of the state of the client, storing all the per-client data + * needed by the server. + * + * Thread-safety: + * - This is constructed and used by CNetServerWorker in the network server thread. + */ +class CNetServerSession : public CFsm +{ + NONCOPYABLE(CNetServerSession); + +public: + CNetServerSession(CNetServerWorker& server, ENetPeer* peer); + + CNetServerWorker& GetServer() { return m_Server; } + + const CStr& GetGUID() const { return m_GUID; } + void SetGUID(const CStr& guid) { m_GUID = guid; } + + const CStrW& GetUserName() const { return m_UserName; } + void SetUserName(const CStrW& name) { m_UserName = name; } + + u32 GetHostID() const { return m_HostID; } + void SetHostID(u32 id) { m_HostID = id; } + + u32 GetIPAddress() const; + + /** + * Number of milliseconds since the latest packet of that client was received. + */ + u32 GetLastReceivedTime() const; + + /** + * Average round trip time to the client. + */ + u32 GetMeanRTT() const; + + /** + * Sends a disconnection notification to the client, + * and sends a NMT_CONNECTION_LOST message to the session FSM. + * The server will receive a disconnection notification after a while. + * The server will not receive any further messages sent via this session. + */ + void Disconnect(NetDisconnectReason reason); + + /** + * Sends an unreliable disconnection notification to the client. + * The server will not receive any disconnection notification. + * The server will not receive any further messages sent via this session. + */ + void DisconnectNow(NetDisconnectReason reason); + + /** + * Send a message to the client. + */ + bool SendMessage(const CNetMessage* message); + + CNetFileTransferer& GetFileTransferer() { return m_FileTransferer; } + +private: + CNetServerWorker& m_Server; + + CNetFileTransferer m_FileTransferer; + + ENetPeer* m_Peer; + + CStr m_GUID; + CStrW m_UserName; + u32 m_HostID{0}; + CStr m_Password; +}; + +#endif // NET_SERVER_SESSION_H diff --git a/source/network/NetServerTurnManager.cpp b/source/network/NetServerTurnManager.cpp index bf0edc6b55..eba3ef722c 100644 --- a/source/network/NetServerTurnManager.cpp +++ b/source/network/NetServerTurnManager.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2025 Wildfire Games. +/* Copyright (C) 2026 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -24,7 +24,7 @@ #include "network/NetHost.h" #include "network/NetMessage.h" #include "network/NetServer.h" -#include "network/NetSession.h" +#include "network/NetServerSession.h" #include "ps/CLogger.h" #include "ps/ConfigDB.h" #include "simulation2/system/TurnManager.h" diff --git a/source/network/tests/test_FileTransfer.h b/source/network/tests/test_FileTransfer.h index 8727402be5..1753286d36 100644 --- a/source/network/tests/test_FileTransfer.h +++ b/source/network/tests/test_FileTransfer.h @@ -19,7 +19,6 @@ #include "network/NetFileTransfer.h" #include "network/NetMessage.h" -#include "network/NetSession.h" #include #include