2026-03-03 12:51:22 -08:00
|
|
|
/* Copyright (C) 2026 Wildfire Games.
|
2023-12-02 16:30:12 -08:00
|
|
|
* This file is part of 0 A.D.
|
2009-04-18 10:00:33 -07:00
|
|
|
*
|
2023-12-02 16:30:12 -08:00
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
2009-04-18 10:00:33 -07:00
|
|
|
* 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.
|
|
|
|
|
*
|
2023-12-02 16:30:12 -08:00
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
2009-04-18 10:00:33 -07:00
|
|
|
* 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
|
2023-12-02 16:30:12 -08:00
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
2009-04-18 10:00:33 -07:00
|
|
|
*/
|
|
|
|
|
|
2009-04-11 10:00:39 -07:00
|
|
|
#ifndef NETSESSION_H
|
|
|
|
|
#define NETSESSION_H
|
|
|
|
|
|
2025-07-06 11:15:27 -07:00
|
|
|
#include "lib/code_annotation.h"
|
2021-01-06 07:26:11 -08:00
|
|
|
#include "lib/external_libraries/enet.h"
|
2025-07-06 11:15:27 -07:00
|
|
|
#include "lib/types.h"
|
2011-10-27 09:46:48 -07:00
|
|
|
#include "network/NetFileTransfer.h"
|
2010-06-30 14:41:04 -07:00
|
|
|
#include "network/NetHost.h"
|
2009-04-11 10:00:39 -07:00
|
|
|
|
2025-07-06 11:15:27 -07:00
|
|
|
#include <atomic>
|
2021-01-06 07:26:11 -08:00
|
|
|
#include <boost/lockfree/queue.hpp>
|
|
|
|
|
|
2025-07-06 11:15:27 -07:00
|
|
|
class CNetClient;
|
|
|
|
|
class CNetMessage;
|
|
|
|
|
class CNetStatsTable;
|
2026-03-07 06:52:39 -08:00
|
|
|
class CStr;
|
2016-02-18 05:10:59 -08:00
|
|
|
|
STUN + XMPP ICE implementation.
Allows lobby players to host games without having to configure their
router.
Differential Revision: https://code.wildfiregames.com/D364
Fixes #2305
Patch By: fcxSanya.
StunClient based on code by SuperTuxKart, relicensed with approval of
the according authors hilnius, hiker, Auria, deveee, Flakebi, leper,
konstin and KroArtem.
Added rfc5245 (ejabberd) support, a GUI option, refactoring and segfault
fixes by myself.
Tested By: user1, Sandarac, Sestroretsk1714, Vladislav, Grugnas,
javiergodas
Partially Reviewed By: leper, Philip, echotangoecho
This was SVN commit r19703.
2017-05-31 23:33:52 -07:00
|
|
|
typedef struct _ENetHost ENetHost;
|
|
|
|
|
|
2010-06-30 14:41:04 -07:00
|
|
|
/**
|
|
|
|
|
* @file
|
|
|
|
|
* Network client/server sessions.
|
|
|
|
|
*
|
|
|
|
|
* Each session has two classes: CNetClientSession runs on the client,
|
|
|
|
|
* and CNetServerSession runs on the server.
|
|
|
|
|
* A client runs one session at once; a server typically runs many.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The client end of a network session.
|
|
|
|
|
* Provides an abstraction of the network interface, allowing communication with the server.
|
2021-01-06 07:26:11 -08:00
|
|
|
* The NetClientSession is threaded, so all calls to the public interface must be thread-safe.
|
2010-06-30 14:41:04 -07:00
|
|
|
*/
|
2026-03-03 12:51:22 -08:00
|
|
|
class CNetClientSession
|
2009-04-11 10:00:39 -07:00
|
|
|
{
|
2010-06-30 14:41:04 -07:00
|
|
|
NONCOPYABLE(CNetClientSession);
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
CNetClientSession(CNetClient& client);
|
2010-07-06 12:54:17 -07:00
|
|
|
~CNetClientSession();
|
2010-06-30 14:41:04 -07:00
|
|
|
|
2021-02-27 09:44:59 -08:00
|
|
|
bool Connect(const CStr& server, const u16 port, ENetHost* enetClient);
|
2009-04-11 10:00:39 -07:00
|
|
|
|
2010-07-06 12:54:17 -07:00
|
|
|
/**
|
2021-01-06 07:26:11 -08:00
|
|
|
* The client NetSession is threaded to avoid getting timeouts if the main thread hangs.
|
|
|
|
|
* Call Connect() before starting this loop.
|
2010-07-06 12:54:17 -07:00
|
|
|
*/
|
2021-01-06 07:26:11 -08:00
|
|
|
static void RunNetLoop(CNetClientSession* session);
|
2010-06-30 14:41:04 -07:00
|
|
|
|
2010-07-06 12:54:17 -07:00
|
|
|
/**
|
2021-01-06 07:26:11 -08:00
|
|
|
* Shut down the net session.
|
2010-07-06 12:54:17 -07:00
|
|
|
*/
|
2021-01-06 07:26:11 -08:00
|
|
|
void Shutdown();
|
2010-06-30 14:41:04 -07:00
|
|
|
|
2010-07-06 12:54:17 -07:00
|
|
|
/**
|
2021-01-06 07:26:11 -08:00
|
|
|
* Processes pending messages.
|
2010-07-06 12:54:17 -07:00
|
|
|
*/
|
2021-01-06 07:26:11 -08:00
|
|
|
void ProcessPolledMessages();
|
2010-06-30 14:41:04 -07:00
|
|
|
|
2010-07-06 12:54:17 -07:00
|
|
|
/**
|
2021-01-06 07:26:11 -08:00
|
|
|
* Queue up a message to send to the server on the next Loop() call.
|
2010-07-06 12:54:17 -07:00
|
|
|
*/
|
2026-03-03 12:51:22 -08:00
|
|
|
bool SendMessage(const CNetMessage* message);
|
2011-10-27 09:46:48 -07:00
|
|
|
|
2016-02-04 09:14:46 -08:00
|
|
|
/**
|
|
|
|
|
* Number of milliseconds since the most recent packet of the server was received.
|
|
|
|
|
*/
|
|
|
|
|
u32 GetLastReceivedTime() const;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Average round trip time to the server.
|
|
|
|
|
*/
|
|
|
|
|
u32 GetMeanRTT() const;
|
|
|
|
|
|
2021-01-06 07:26:11 -08:00
|
|
|
CNetFileTransferer& GetFileTransferer() { return m_FileTransferer; }
|
|
|
|
|
private:
|
Prevent players from disconnecting during the loading screen by increasing the timeout tolerance to 60 seconds for that period, fixes #5163.
The NetClient runs in the main thread, so any part of the loading screen
consuming several seconds makes that client timeout.
This is a workaround because threading the NetClient would have prevent
these timeouts, refs #3700.
Coutnerintuitively, since enet timeout tolerance is proportional to the
latency, the better the connection of the player, the more likely it was
to drop on gamestart.
This problem became very frequent in Alpha 23, at least due to the Aura
bugfix 583b6ec625, AIInterface being particularly slow and that not
having been disabled yet in the loading screen resulting in additional
10 second freezes during the loading screen, even on empty maps, refs
#5200, 8e168f85e6.
Differential Revision: https://code.wildfiregames.com/D1513
Based on patch by: causative
This was SVN commit r21842.
2018-06-06 15:09:38 -07:00
|
|
|
/**
|
2021-01-06 07:26:11 -08:00
|
|
|
* Process queued incoming messages.
|
Prevent players from disconnecting during the loading screen by increasing the timeout tolerance to 60 seconds for that period, fixes #5163.
The NetClient runs in the main thread, so any part of the loading screen
consuming several seconds makes that client timeout.
This is a workaround because threading the NetClient would have prevent
these timeouts, refs #3700.
Coutnerintuitively, since enet timeout tolerance is proportional to the
latency, the better the connection of the player, the more likely it was
to drop on gamestart.
This problem became very frequent in Alpha 23, at least due to the Aura
bugfix 583b6ec625, AIInterface being particularly slow and that not
having been disabled yet in the loading screen resulting in additional
10 second freezes during the loading screen, even on empty maps, refs
#5200, 8e168f85e6.
Differential Revision: https://code.wildfiregames.com/D1513
Based on patch by: causative
This was SVN commit r21842.
2018-06-06 15:09:38 -07:00
|
|
|
*/
|
2021-01-06 07:26:11 -08:00
|
|
|
void Poll();
|
Prevent players from disconnecting during the loading screen by increasing the timeout tolerance to 60 seconds for that period, fixes #5163.
The NetClient runs in the main thread, so any part of the loading screen
consuming several seconds makes that client timeout.
This is a workaround because threading the NetClient would have prevent
these timeouts, refs #3700.
Coutnerintuitively, since enet timeout tolerance is proportional to the
latency, the better the connection of the player, the more likely it was
to drop on gamestart.
This problem became very frequent in Alpha 23, at least due to the Aura
bugfix 583b6ec625, AIInterface being particularly slow and that not
having been disabled yet in the loading screen resulting in additional
10 second freezes during the loading screen, even on empty maps, refs
#5200, 8e168f85e6.
Differential Revision: https://code.wildfiregames.com/D1513
Based on patch by: causative
This was SVN commit r21842.
2018-06-06 15:09:38 -07:00
|
|
|
|
2021-01-06 07:26:11 -08:00
|
|
|
/**
|
|
|
|
|
* Flush queued outgoing network messages.
|
|
|
|
|
*/
|
|
|
|
|
void Flush();
|
2010-06-30 14:41:04 -07:00
|
|
|
|
2010-07-06 12:54:17 -07:00
|
|
|
CNetClient& m_Client;
|
2011-10-27 09:46:48 -07:00
|
|
|
|
|
|
|
|
CNetFileTransferer m_FileTransferer;
|
|
|
|
|
|
2021-01-06 07:26:11 -08:00
|
|
|
// Net messages received and waiting for fetching.
|
2026-03-07 05:57:49 -08:00
|
|
|
boost::lockfree::queue<ENetEvent> m_IncomingMessages{16};
|
2021-01-06 07:26:11 -08:00
|
|
|
// Net messages to send on the next flush() call.
|
2026-03-07 05:57:49 -08:00
|
|
|
boost::lockfree::queue<ENetPacket*> m_OutgoingMessages{16};
|
2021-01-06 07:26:11 -08:00
|
|
|
|
2025-02-09 09:31:44 -08:00
|
|
|
// Last known state. If false, flushing errors are silenced.
|
2026-03-07 06:52:39 -08:00
|
|
|
bool m_Connected{false};
|
2021-01-14 23:54:55 -08:00
|
|
|
|
2025-02-09 09:31:44 -08:00
|
|
|
// Whether this session was ever connected to the server.
|
2026-03-07 06:52:39 -08:00
|
|
|
bool m_WasConnected{false};
|
2025-02-09 09:31:44 -08:00
|
|
|
|
2021-01-06 07:26:11 -08:00
|
|
|
// Wrapper around enet stats - those are atomic as the code is lock-free.
|
2026-03-07 05:57:49 -08:00
|
|
|
std::atomic<u32> m_LastReceivedTime{0};
|
|
|
|
|
std::atomic<u32> m_MeanRTT{0};
|
2021-01-06 07:26:11 -08:00
|
|
|
|
|
|
|
|
// If this is true, calling Connect() or deleting the session is an error.
|
2026-03-07 05:57:49 -08:00
|
|
|
std::atomic<bool> m_LoopRunning{false};
|
|
|
|
|
std::atomic<bool> m_ShouldShutdown{false};
|
2021-01-06 07:26:11 -08:00
|
|
|
|
2026-04-16 09:34:08 -07:00
|
|
|
std::unique_ptr<ENetHost, DestroyHost> m_Host;
|
2026-04-16 10:12:13 -07:00
|
|
|
std::unique_ptr<ENetPeer, DestroyPeer> m_Server;
|
2026-04-16 10:18:12 -07:00
|
|
|
std::unique_ptr<CNetStatsTable> m_Stats;
|
2010-06-30 14:41:04 -07:00
|
|
|
};
|
|
|
|
|
|
2009-04-11 10:00:39 -07:00
|
|
|
#endif // NETSESSION_H
|