2013-07-22 04:21:56 -04:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
2015-05-18 01:08:10 +02:00
|
|
|
// Licensed under GPLv2+
|
2013-07-22 04:21:56 -04:00
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2014-02-10 13:54:46 -05:00
|
|
|
#pragma once
|
2013-07-22 04:21:56 -04:00
|
|
|
|
2015-02-02 02:08:58 -08:00
|
|
|
#include <SFML/Network/Packet.hpp>
|
2013-07-22 04:21:56 -04:00
|
|
|
#include <map>
|
2017-10-28 01:42:25 +02:00
|
|
|
#include <memory>
|
2015-05-26 17:23:43 -04:00
|
|
|
#include <mutex>
|
2013-07-22 04:21:56 -04:00
|
|
|
#include <queue>
|
|
|
|
#include <sstream>
|
2015-05-26 17:23:43 -04:00
|
|
|
#include <thread>
|
2015-06-05 19:00:26 -04:00
|
|
|
#include <unordered_map>
|
2015-02-02 01:27:06 -08:00
|
|
|
#include <unordered_set>
|
2018-10-18 04:33:05 -04:00
|
|
|
#include <utility>
|
|
|
|
#include "Common/Event.h"
|
2017-10-28 01:42:25 +02:00
|
|
|
#include "Common/QoSSession.h"
|
2017-08-23 16:45:42 -07:00
|
|
|
#include "Common/SPSCQueue.h"
|
2014-02-17 05:18:15 -05:00
|
|
|
#include "Common/Timer.h"
|
2015-02-02 02:08:58 -08:00
|
|
|
#include "Common/TraversalClient.h"
|
2014-02-17 05:18:15 -05:00
|
|
|
#include "Core/NetPlayProto.h"
|
NetPlay host input authority mode
Currently, each player buffers their own inputs and sends them to the
host. The host then relays those inputs to everyone else. Every player
waits on inputs from all players to be buffered before continuing. What
this means is all clients run in lockstep, and the total latency of
inputs cannot be lower than the sum of the 2 highest client ping times
in the game (in 3+ player sessions with people across the world, the
latency can be very high).
Host input authority mode changes it so players no longer buffer their
own inputs, and only send them to the host. The host stores only the
most recent input received from a player. The host then sends inputs
for all pads at the SI poll interval, similar to the existing code. If
a player sends inputs to slowly, their last received input is simply
sent again. If they send too quickly, inputs are dropped. This means
that the host has full control over what inputs are actually read by
the game, hence the name of the mode. Also, because the rate at which
inputs are received by SI is decoupled from the rate at which players
are sending inputs, clients are no longer dependent on each other. They
only care what the host is doing. This means that they can set their
buffer individually based on their latency to the host, rather than the
highest latency between any 2 players, allowing someone with lower ping
to the host to have less latency than someone else.
This is a catch to this: as a necessity of how the host's input sending
works, the host has 0 latency. There isn't a good way to fix this, as
input delay is now solely dependent on the real latency to the host's
server. Having differing latency between players would be considered
unfair for competitive play, but for casual play we don't really care.
For this reason though, combined with the potential for a few inputs to
be dropped on a bad connection, the old mode will remain and this new
mode is entirely optional.
2018-08-24 04:17:18 -04:00
|
|
|
#include "InputCommon/GCPadStatus.h"
|
2019-03-30 14:50:41 +01:00
|
|
|
#include "UICommon/NetPlayIndex.h"
|
2014-02-17 05:18:15 -05:00
|
|
|
|
2018-07-06 19:39:42 -04:00
|
|
|
namespace NetPlay
|
|
|
|
{
|
2015-02-02 01:56:53 -08:00
|
|
|
class NetPlayUI;
|
2016-07-14 00:45:38 +02:00
|
|
|
enum class PlayerGameStatus;
|
2015-02-02 01:56:53 -08:00
|
|
|
|
|
|
|
class NetPlayServer : public TraversalClientClient
|
2013-07-22 04:21:56 -04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
void ThreadFunc();
|
2018-10-18 04:33:05 -04:00
|
|
|
void SendAsync(sf::Packet&& packet, PlayerId pid, u8 channel_id = DEFAULT_CHANNEL);
|
|
|
|
void SendAsyncToClients(sf::Packet&& packet, PlayerId skip_pid = 0,
|
|
|
|
u8 channel_id = DEFAULT_CHANNEL);
|
|
|
|
void SendChunked(sf::Packet&& packet, PlayerId pid, const std::string& title = "");
|
|
|
|
void SendChunkedToClients(sf::Packet&& packet, PlayerId skip_pid = 0,
|
|
|
|
const std::string& title = "");
|
2013-07-22 04:21:56 -04:00
|
|
|
|
2020-02-05 17:23:22 -05:00
|
|
|
NetPlayServer(u16 port, bool forward_port, NetPlayUI* dialog,
|
|
|
|
const NetTraversalConfig& traversal_config);
|
2013-07-22 04:21:56 -04:00
|
|
|
~NetPlayServer();
|
|
|
|
|
|
|
|
bool ChangeGame(const std::string& game);
|
2016-07-14 00:45:38 +02:00
|
|
|
bool ComputeMD5(const std::string& file_identifier);
|
|
|
|
bool AbortMD5();
|
2013-07-22 04:21:56 -04:00
|
|
|
void SendChatMessage(const std::string& msg);
|
|
|
|
|
|
|
|
void SetNetSettings(const NetSettings& settings);
|
|
|
|
|
2018-07-19 18:10:37 -04:00
|
|
|
bool DoAllPlayersHaveIPLDump() const;
|
2014-08-02 18:28:26 -04:00
|
|
|
bool StartGame();
|
2018-07-04 17:01:50 -04:00
|
|
|
bool RequestStartGame();
|
2019-03-30 06:20:24 -04:00
|
|
|
void AbortGameStart();
|
2013-07-22 04:21:56 -04:00
|
|
|
|
2015-08-16 00:58:15 -04:00
|
|
|
PadMappingArray GetPadMapping() const;
|
|
|
|
void SetPadMapping(const PadMappingArray& mappings);
|
2013-07-22 04:21:56 -04:00
|
|
|
|
2015-08-16 00:58:15 -04:00
|
|
|
PadMappingArray GetWiimoteMapping() const;
|
|
|
|
void SetWiimoteMapping(const PadMappingArray& mappings);
|
2013-08-06 23:48:52 -04:00
|
|
|
|
2013-07-22 04:21:56 -04:00
|
|
|
void AdjustPadBufferSize(unsigned int size);
|
NetPlay host input authority mode
Currently, each player buffers their own inputs and sends them to the
host. The host then relays those inputs to everyone else. Every player
waits on inputs from all players to be buffered before continuing. What
this means is all clients run in lockstep, and the total latency of
inputs cannot be lower than the sum of the 2 highest client ping times
in the game (in 3+ player sessions with people across the world, the
latency can be very high).
Host input authority mode changes it so players no longer buffer their
own inputs, and only send them to the host. The host stores only the
most recent input received from a player. The host then sends inputs
for all pads at the SI poll interval, similar to the existing code. If
a player sends inputs to slowly, their last received input is simply
sent again. If they send too quickly, inputs are dropped. This means
that the host has full control over what inputs are actually read by
the game, hence the name of the mode. Also, because the rate at which
inputs are received by SI is decoupled from the rate at which players
are sending inputs, clients are no longer dependent on each other. They
only care what the host is doing. This means that they can set their
buffer individually based on their latency to the host, rather than the
highest latency between any 2 players, allowing someone with lower ping
to the host to have less latency than someone else.
This is a catch to this: as a necessity of how the host's input sending
works, the host has 0 latency. There isn't a good way to fix this, as
input delay is now solely dependent on the real latency to the host's
server. Having differing latency between players would be considered
unfair for competitive play, but for casual play we don't really care.
For this reason though, combined with the potential for a few inputs to
be dropped on a bad connection, the old mode will remain and this new
mode is entirely optional.
2018-08-24 04:17:18 -04:00
|
|
|
void SetHostInputAuthority(bool enable);
|
2013-07-22 04:21:56 -04:00
|
|
|
|
2015-02-02 01:27:06 -08:00
|
|
|
void KickPlayer(PlayerId player);
|
|
|
|
|
2017-03-19 09:14:03 -04:00
|
|
|
u16 GetPort() const;
|
2014-05-29 18:59:07 -04:00
|
|
|
|
2017-03-19 09:14:03 -04:00
|
|
|
std::unordered_set<std::string> GetInterfaceSet() const;
|
|
|
|
std::string GetInterfaceHost(const std::string& inter) const;
|
2015-02-02 01:56:53 -08:00
|
|
|
|
2016-01-26 20:49:54 +01:00
|
|
|
bool is_connected = false;
|
2013-07-22 04:21:56 -04:00
|
|
|
|
|
|
|
private:
|
|
|
|
class Client
|
|
|
|
{
|
|
|
|
public:
|
2014-02-16 15:30:18 -05:00
|
|
|
PlayerId pid;
|
|
|
|
std::string name;
|
|
|
|
std::string revision;
|
2016-07-10 10:13:34 +02:00
|
|
|
PlayerGameStatus game_status;
|
2018-07-19 18:10:37 -04:00
|
|
|
bool has_ipl_dump;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2015-02-02 01:27:06 -08:00
|
|
|
ENetPeer* socket;
|
2013-08-05 05:50:26 -04:00
|
|
|
u32 ping;
|
2013-07-22 04:21:56 -04:00
|
|
|
u32 current_game;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2017-10-28 01:42:25 +02:00
|
|
|
Common::QoSSession qos_session;
|
|
|
|
|
2014-11-09 22:30:06 +00:00
|
|
|
bool operator==(const Client& other) const { return this == &other; }
|
NetPlay host input authority mode
Currently, each player buffers their own inputs and sends them to the
host. The host then relays those inputs to everyone else. Every player
waits on inputs from all players to be buffered before continuing. What
this means is all clients run in lockstep, and the total latency of
inputs cannot be lower than the sum of the 2 highest client ping times
in the game (in 3+ player sessions with people across the world, the
latency can be very high).
Host input authority mode changes it so players no longer buffer their
own inputs, and only send them to the host. The host stores only the
most recent input received from a player. The host then sends inputs
for all pads at the SI poll interval, similar to the existing code. If
a player sends inputs to slowly, their last received input is simply
sent again. If they send too quickly, inputs are dropped. This means
that the host has full control over what inputs are actually read by
the game, hence the name of the mode. Also, because the rate at which
inputs are received by SI is decoupled from the rate at which players
are sending inputs, clients are no longer dependent on each other. They
only care what the host is doing. This means that they can set their
buffer individually based on their latency to the host, rather than the
highest latency between any 2 players, allowing someone with lower ping
to the host to have less latency than someone else.
This is a catch to this: as a necessity of how the host's input sending
works, the host has 0 latency. There isn't a good way to fix this, as
input delay is now solely dependent on the real latency to the host's
server. Having differing latency between players would be considered
unfair for competitive play, but for casual play we don't really care.
For this reason though, combined with the potential for a few inputs to
be dropped on a bad connection, the old mode will remain and this new
mode is entirely optional.
2018-08-24 04:17:18 -04:00
|
|
|
bool IsHost() const { return pid == 1; }
|
2013-07-22 04:21:56 -04:00
|
|
|
};
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2018-10-18 04:33:05 -04:00
|
|
|
enum class TargetMode
|
|
|
|
{
|
|
|
|
Only,
|
|
|
|
AllExcept
|
|
|
|
};
|
|
|
|
|
|
|
|
struct AsyncQueueEntry
|
|
|
|
{
|
|
|
|
sf::Packet packet;
|
|
|
|
PlayerId target_pid;
|
|
|
|
TargetMode target_mode;
|
|
|
|
u8 channel_id;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ChunkedDataQueueEntry
|
|
|
|
{
|
|
|
|
sf::Packet packet;
|
|
|
|
PlayerId target_pid;
|
|
|
|
TargetMode target_mode;
|
|
|
|
std::string title;
|
|
|
|
};
|
|
|
|
|
2018-07-04 17:01:50 -04:00
|
|
|
bool SyncSaveData();
|
2018-10-04 18:49:41 -04:00
|
|
|
bool SyncCodes();
|
|
|
|
void CheckSyncAndStartGame();
|
2018-07-04 17:01:50 -04:00
|
|
|
bool CompressFileIntoPacket(const std::string& file_path, sf::Packet& packet);
|
|
|
|
bool CompressBufferIntoPacket(const std::vector<u8>& in_buffer, sf::Packet& packet);
|
|
|
|
|
2018-07-14 18:55:17 -04:00
|
|
|
u64 GetInitialNetPlayRTC() const;
|
|
|
|
|
2018-10-18 04:33:05 -04:00
|
|
|
void SendToClients(const sf::Packet& packet, PlayerId skip_pid = 0,
|
|
|
|
u8 channel_id = DEFAULT_CHANNEL);
|
|
|
|
void Send(ENetPeer* socket, const sf::Packet& packet, u8 channel_id = DEFAULT_CHANNEL);
|
2018-09-12 06:41:46 -04:00
|
|
|
unsigned int OnConnect(ENetPeer* socket, sf::Packet& rpac);
|
2017-03-19 09:14:03 -04:00
|
|
|
unsigned int OnDisconnect(const Client& player);
|
2014-11-09 22:30:06 +00:00
|
|
|
unsigned int OnData(sf::Packet& packet, Client& player);
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2015-09-05 22:32:05 -04:00
|
|
|
void OnTraversalStateChanged() override;
|
|
|
|
void OnConnectReady(ENetAddress) override {}
|
|
|
|
void OnConnectFailed(u8) override {}
|
2013-07-22 04:21:56 -04:00
|
|
|
void UpdatePadMapping();
|
2013-08-06 23:48:52 -04:00
|
|
|
void UpdateWiimoteMapping();
|
2017-03-19 09:14:03 -04:00
|
|
|
std::vector<std::pair<std::string, std::string>> GetInterfaceListInternal() const;
|
2018-10-18 04:33:05 -04:00
|
|
|
void ChunkedDataThreadFunc();
|
|
|
|
void ChunkedDataSend(sf::Packet&& packet, PlayerId pid, const TargetMode target_mode);
|
2019-03-28 02:32:06 -04:00
|
|
|
void ChunkedDataAbort();
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2019-03-28 02:32:06 -04:00
|
|
|
void SetupIndex();
|
2019-04-02 08:08:27 -04:00
|
|
|
bool PlayerHasControllerMapped(PlayerId pid) const;
|
|
|
|
|
2013-07-22 04:21:56 -04:00
|
|
|
NetSettings m_settings;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2016-01-26 20:49:54 +01:00
|
|
|
bool m_is_running = false;
|
|
|
|
bool m_do_loop = false;
|
2014-02-09 18:29:13 -05:00
|
|
|
Common::Timer m_ping_timer;
|
2016-01-26 20:49:54 +01:00
|
|
|
u32 m_ping_key = 0;
|
|
|
|
bool m_update_pings = false;
|
|
|
|
u32 m_current_game = 0;
|
|
|
|
unsigned int m_target_buffer_size = 0;
|
2015-08-16 00:08:09 -04:00
|
|
|
PadMappingArray m_pad_map;
|
|
|
|
PadMappingArray m_wiimote_map;
|
2018-07-04 17:01:50 -04:00
|
|
|
unsigned int m_save_data_synced_players = 0;
|
2018-10-04 18:49:41 -04:00
|
|
|
unsigned int m_codes_synced_players = 0;
|
|
|
|
bool m_saves_synced = true;
|
|
|
|
bool m_codes_synced = true;
|
2018-07-04 17:01:50 -04:00
|
|
|
bool m_start_pending = false;
|
NetPlay host input authority mode
Currently, each player buffers their own inputs and sends them to the
host. The host then relays those inputs to everyone else. Every player
waits on inputs from all players to be buffered before continuing. What
this means is all clients run in lockstep, and the total latency of
inputs cannot be lower than the sum of the 2 highest client ping times
in the game (in 3+ player sessions with people across the world, the
latency can be very high).
Host input authority mode changes it so players no longer buffer their
own inputs, and only send them to the host. The host stores only the
most recent input received from a player. The host then sends inputs
for all pads at the SI poll interval, similar to the existing code. If
a player sends inputs to slowly, their last received input is simply
sent again. If they send too quickly, inputs are dropped. This means
that the host has full control over what inputs are actually read by
the game, hence the name of the mode. Also, because the rate at which
inputs are received by SI is decoupled from the rate at which players
are sending inputs, clients are no longer dependent on each other. They
only care what the host is doing. This means that they can set their
buffer individually based on their latency to the host, rather than the
highest latency between any 2 players, allowing someone with lower ping
to the host to have less latency than someone else.
This is a catch to this: as a necessity of how the host's input sending
works, the host has 0 latency. There isn't a good way to fix this, as
input delay is now solely dependent on the real latency to the host's
server. Having differing latency between players would be considered
unfair for competitive play, but for casual play we don't really care.
For this reason though, combined with the potential for a few inputs to
be dropped on a bad connection, the old mode will remain and this new
mode is entirely optional.
2018-08-24 04:17:18 -04:00
|
|
|
bool m_host_input_authority = false;
|
2019-04-02 08:08:27 -04:00
|
|
|
PlayerId m_current_golfer = 1;
|
|
|
|
PlayerId m_pending_golfer = 0;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2015-02-05 22:16:34 -08:00
|
|
|
std::map<PlayerId, Client> m_players;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2015-06-05 19:00:26 -04:00
|
|
|
std::unordered_map<u32, std::vector<std::pair<PlayerId, u64>>> m_timebase_by_frame;
|
|
|
|
bool m_desync_detected;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2013-07-22 04:21:56 -04:00
|
|
|
struct
|
|
|
|
{
|
|
|
|
std::recursive_mutex game;
|
|
|
|
// lock order
|
2015-03-09 17:31:13 +01:00
|
|
|
std::recursive_mutex players;
|
2015-03-13 02:03:09 +01:00
|
|
|
std::recursive_mutex async_queue_write;
|
2018-10-18 04:33:05 -04:00
|
|
|
std::recursive_mutex chunked_data_queue_write;
|
2013-07-22 04:21:56 -04:00
|
|
|
} m_crit;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2018-10-18 04:33:05 -04:00
|
|
|
Common::SPSCQueue<AsyncQueueEntry, false> m_async_queue;
|
|
|
|
Common::SPSCQueue<ChunkedDataQueueEntry, false> m_chunked_data_queue;
|
|
|
|
|
2013-07-22 04:21:56 -04:00
|
|
|
std::string m_selected_game;
|
|
|
|
std::thread m_thread;
|
2018-10-18 04:33:05 -04:00
|
|
|
Common::Event m_chunked_data_event;
|
|
|
|
Common::Event m_chunked_data_complete_event;
|
|
|
|
std::thread m_chunked_data_thread;
|
|
|
|
u32 m_next_chunked_data_id;
|
|
|
|
std::unordered_map<u32, unsigned int> m_chunked_data_complete_count;
|
2019-03-28 02:32:06 -04:00
|
|
|
bool m_abort_chunked_data = false;
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2016-01-26 20:49:54 +01:00
|
|
|
ENetHost* m_server = nullptr;
|
|
|
|
TraversalClient* m_traversal_client = nullptr;
|
|
|
|
NetPlayUI* m_dialog = nullptr;
|
2019-03-30 14:50:41 +01:00
|
|
|
NetPlayIndex m_index;
|
2015-02-05 22:16:34 -08:00
|
|
|
};
|
2018-07-06 19:39:42 -04:00
|
|
|
} // namespace NetPlay
|