2020-11-12 09:24:30 +01:00
|
|
|
/* Copyright (C) 2020 Wildfire Games.
|
2009-04-18 19:00:33 +02:00
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2009-04-11 19:00:39 +02:00
|
|
|
#ifndef NETSESSION_H
|
|
|
|
#define NETSESSION_H
|
|
|
|
|
2021-01-06 16:26:11 +01:00
|
|
|
#include "lib/external_libraries/enet.h"
|
2010-06-30 23:41:04 +02:00
|
|
|
#include "network/fsm.h"
|
2011-10-27 18:46:48 +02:00
|
|
|
#include "network/NetFileTransfer.h"
|
2010-06-30 23:41:04 +02:00
|
|
|
#include "network/NetHost.h"
|
|
|
|
#include "ps/CStr.h"
|
2009-04-11 19:00:39 +02:00
|
|
|
|
2021-01-06 16:26:11 +01:00
|
|
|
#include <boost/lockfree/queue.hpp>
|
|
|
|
|
|
|
|
#include <atomic>
|
2016-02-04 18:14:46 +01:00
|
|
|
|
2016-02-18 14:10:59 +01:00
|
|
|
/**
|
2021-01-06 16:26:11 +01:00
|
|
|
* Report the peer if we didn't receive a packet after this time (milliseconds).
|
2016-02-18 14:10:59 +01:00
|
|
|
*/
|
2021-01-06 16:26:11 +01:00
|
|
|
inline constexpr u32 NETWORK_WARNING_TIMEOUT = 2000;
|
2016-02-18 14:10:59 +01:00
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
class CNetClient;
|
2010-10-31 23:00:28 +01:00
|
|
|
class CNetServerWorker;
|
2009-04-11 19:00:39 +02:00
|
|
|
|
2010-07-03 15:15:01 +02:00
|
|
|
class CNetStatsTable;
|
|
|
|
|
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-06-01 08:33:52 +02:00
|
|
|
typedef struct _ENetHost ENetHost;
|
|
|
|
|
2010-06-30 23:41:04 +02: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.
|
|
|
|
*/
|
|
|
|
|
2011-10-27 18:46:48 +02:00
|
|
|
/**
|
|
|
|
* Interface for sessions to which messages can be sent.
|
|
|
|
*/
|
|
|
|
class INetSession
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual ~INetSession() {}
|
|
|
|
virtual bool SendMessage(const CNetMessage* message) = 0;
|
|
|
|
};
|
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
/**
|
|
|
|
* The client end of a network session.
|
|
|
|
* Provides an abstraction of the network interface, allowing communication with the server.
|
2021-01-06 16:26:11 +01:00
|
|
|
* The NetClientSession is threaded, so all calls to the public interface must be thread-safe.
|
2010-06-30 23:41:04 +02:00
|
|
|
*/
|
2011-10-27 18:46:48 +02:00
|
|
|
class CNetClientSession : public INetSession
|
2009-04-11 19:00:39 +02:00
|
|
|
{
|
2010-06-30 23:41:04 +02:00
|
|
|
NONCOPYABLE(CNetClientSession);
|
|
|
|
|
|
|
|
public:
|
|
|
|
CNetClientSession(CNetClient& client);
|
2010-07-06 21:54:17 +02:00
|
|
|
~CNetClientSession();
|
2010-06-30 23:41:04 +02:00
|
|
|
|
2021-02-27 18:44:59 +01:00
|
|
|
bool Connect(const CStr& server, const u16 port, ENetHost* enetClient);
|
2009-04-11 19:00:39 +02:00
|
|
|
|
2010-07-06 21:54:17 +02:00
|
|
|
/**
|
2021-01-06 16:26:11 +01:00
|
|
|
* The client NetSession is threaded to avoid getting timeouts if the main thread hangs.
|
|
|
|
* Call Connect() before starting this loop.
|
2010-07-06 21:54:17 +02:00
|
|
|
*/
|
2021-01-06 16:26:11 +01:00
|
|
|
static void RunNetLoop(CNetClientSession* session);
|
2010-06-30 23:41:04 +02:00
|
|
|
|
2010-07-06 21:54:17 +02:00
|
|
|
/**
|
2021-01-06 16:26:11 +01:00
|
|
|
* Shut down the net session.
|
2010-07-06 21:54:17 +02:00
|
|
|
*/
|
2021-01-06 16:26:11 +01:00
|
|
|
void Shutdown();
|
2010-06-30 23:41:04 +02:00
|
|
|
|
2010-07-06 21:54:17 +02:00
|
|
|
/**
|
2021-01-06 16:26:11 +01:00
|
|
|
* Processes pending messages.
|
2010-07-06 21:54:17 +02:00
|
|
|
*/
|
2021-01-06 16:26:11 +01:00
|
|
|
void ProcessPolledMessages();
|
2010-06-30 23:41:04 +02:00
|
|
|
|
2010-07-06 21:54:17 +02:00
|
|
|
/**
|
2021-01-06 16:26:11 +01:00
|
|
|
* Queue up a message to send to the server on the next Loop() call.
|
2010-07-06 21:54:17 +02:00
|
|
|
*/
|
2021-01-06 16:26:11 +01:00
|
|
|
virtual bool SendMessage(const CNetMessage* message) override;
|
2011-10-27 18:46:48 +02:00
|
|
|
|
2016-02-04 18:14:46 +01: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 16:26:11 +01: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-07 00:09:38 +02:00
|
|
|
/**
|
2021-01-06 16:26:11 +01: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-07 00:09:38 +02:00
|
|
|
*/
|
2021-01-06 16:26:11 +01: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-07 00:09:38 +02:00
|
|
|
|
2021-01-06 16:26:11 +01:00
|
|
|
/**
|
|
|
|
* Flush queued outgoing network messages.
|
|
|
|
*/
|
|
|
|
void Flush();
|
2010-06-30 23:41:04 +02:00
|
|
|
|
2010-07-06 21:54:17 +02:00
|
|
|
CNetClient& m_Client;
|
2011-10-27 18:46:48 +02:00
|
|
|
|
|
|
|
CNetFileTransferer m_FileTransferer;
|
|
|
|
|
2021-01-06 16:26:11 +01:00
|
|
|
// Net messages received and waiting for fetching.
|
|
|
|
boost::lockfree::queue<ENetEvent> m_IncomingMessages;
|
|
|
|
// Net messages to send on the next flush() call.
|
|
|
|
boost::lockfree::queue<ENetPacket*> m_OutgoingMessages;
|
|
|
|
|
2021-01-15 08:54:55 +01:00
|
|
|
// Last know state. If false, flushing errors are silenced.
|
|
|
|
bool m_Connected = false;
|
|
|
|
|
2021-01-06 16:26:11 +01:00
|
|
|
// Wrapper around enet stats - those are atomic as the code is lock-free.
|
|
|
|
std::atomic<u32> m_LastReceivedTime;
|
|
|
|
std::atomic<u32> m_MeanRTT;
|
|
|
|
|
|
|
|
// If this is true, calling Connect() or deleting the session is an error.
|
|
|
|
std::atomic<bool> m_LoopRunning;
|
|
|
|
std::atomic<bool> m_ShouldShutdown;
|
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
ENetHost* m_Host;
|
|
|
|
ENetPeer* m_Server;
|
2010-07-03 15:15:01 +02:00
|
|
|
CNetStatsTable* m_Stats;
|
2010-06-30 23:41:04 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
2010-10-31 23:00:28 +01:00
|
|
|
*
|
|
|
|
* Thread-safety:
|
|
|
|
* - This is constructed and used by CNetServerWorker in the network server thread.
|
2010-06-30 23:41:04 +02:00
|
|
|
*/
|
2011-10-27 18:46:48 +02:00
|
|
|
class CNetServerSession : public CFsm, public INetSession
|
2010-06-30 23:41:04 +02:00
|
|
|
{
|
|
|
|
NONCOPYABLE(CNetServerSession);
|
|
|
|
|
|
|
|
public:
|
2010-10-31 23:00:28 +01:00
|
|
|
CNetServerSession(CNetServerWorker& server, ENetPeer* peer);
|
2010-06-30 23:41:04 +02:00
|
|
|
|
2010-10-31 23:00:28 +01:00
|
|
|
CNetServerWorker& GetServer() { return m_Server; }
|
2010-06-30 23:41:04 +02:00
|
|
|
|
|
|
|
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; }
|
|
|
|
|
2016-05-08 18:25:00 +02:00
|
|
|
u32 GetIPAddress() const;
|
2016-05-08 13:46:19 +02:00
|
|
|
|
2016-02-04 18:14:46 +01:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
2010-07-06 21:54:17 +02:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2019-09-26 13:36:03 +02:00
|
|
|
void Disconnect(NetDisconnectReason reason);
|
2010-07-06 21:54:17 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2019-09-26 13:36:03 +02:00
|
|
|
void DisconnectNow(NetDisconnectReason reason);
|
2010-07-06 21:54:17 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a message to the client.
|
|
|
|
*/
|
2011-10-27 18:46:48 +02:00
|
|
|
virtual bool SendMessage(const CNetMessage* message);
|
|
|
|
|
|
|
|
CNetFileTransferer& GetFileTransferer() { return m_FileTransferer; }
|
2010-06-30 23:41:04 +02:00
|
|
|
|
|
|
|
private:
|
2010-10-31 23:00:28 +01:00
|
|
|
CNetServerWorker& m_Server;
|
2010-06-30 23:41:04 +02:00
|
|
|
|
2011-10-27 18:46:48 +02:00
|
|
|
CNetFileTransferer m_FileTransferer;
|
|
|
|
|
2010-07-06 21:54:17 +02:00
|
|
|
ENetPeer* m_Peer;
|
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
CStr m_GUID;
|
|
|
|
CStrW m_UserName;
|
|
|
|
u32 m_HostID;
|
2021-01-23 19:04:36 +01:00
|
|
|
CStr m_Password;
|
2010-06-30 23:41:04 +02:00
|
|
|
};
|
|
|
|
|
2009-04-11 19:00:39 +02:00
|
|
|
#endif // NETSESSION_H
|