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.
This commit is contained in:
Techjar 2018-08-24 04:17:18 -04:00
commit 5519efa66e
9 changed files with 367 additions and 51 deletions

View file

@ -6,6 +6,7 @@
#include <SFML/Network/Packet.hpp>
#include <array>
#include <chrono>
#include <map>
#include <memory>
#include <mutex>
@ -43,6 +44,7 @@ public:
virtual void OnMsgStartGame() = 0;
virtual void OnMsgStopGame() = 0;
virtual void OnPadBufferChanged(u32 buffer) = 0;
virtual void OnHostInputAuthorityChanged(bool enabled) = 0;
virtual void OnDesync(u32 frame, const std::string& player) = 0;
virtual void OnConnectionLost() = 0;
virtual void OnConnectionError(const std::string& message) = 0;
@ -74,6 +76,8 @@ public:
std::string revision;
u32 ping;
PlayerGameStatus game_status;
bool IsHost() const { return pid == 1; }
};
class NetPlayClient : public TraversalClientClient
@ -101,7 +105,7 @@ public:
// Send and receive pads values
bool WiimoteUpdate(int _number, u8* data, const u8 size, u8 reporting_mode);
bool GetNetPads(int pad_nb, GCPadStatus* pad_status);
bool GetNetPads(int pad_nb, bool from_vi, GCPadStatus* pad_status);
u64 GetInitialRTCValue() const;
@ -121,6 +125,8 @@ public:
const PadMappingArray& GetPadMapping() const;
const PadMappingArray& GetWiimoteMapping() const;
void AdjustPadBufferSize(unsigned int size);
protected:
void ClearBuffers();
@ -137,6 +143,10 @@ protected:
std::array<Common::SPSCQueue<GCPadStatus>, 4> m_pad_buffer;
std::array<Common::SPSCQueue<NetWiimote>, 4> m_wiimote_buffer;
std::array<bool, 4> m_first_pad_status_received{};
std::chrono::time_point<std::chrono::steady_clock> m_buffer_under_target_last;
NetPlayUI* m_dialog = nullptr;
ENetHost* m_client = nullptr;
@ -148,6 +158,7 @@ protected:
Common::Flag m_do_loop{true};
unsigned int m_target_buffer_size = 20;
bool m_host_input_authority = false;
Player* m_local_player = nullptr;
@ -178,6 +189,9 @@ private:
bool DecompressPacketIntoFile(sf::Packet& packet, const std::string& file_path);
std::optional<std::vector<u8>> DecompressPacketIntoBuffer(sf::Packet& packet);
bool PollLocalPad(int local_pad, sf::Packet& packet);
void SendPadHostPoll(PadMapping pad_num);
void UpdateDevices();
void AddPadStateToPacket(int in_game_pad, const GCPadStatus& np, sf::Packet& packet);
void SendWiimoteState(int in_game_pad, const NetWiimote& nw);
@ -203,6 +217,7 @@ private:
bool m_should_compute_MD5 = false;
Common::Event m_gc_pad_event;
Common::Event m_wii_pad_event;
Common::Event m_first_pad_status_received_event;
u8 m_sync_save_data_count = 0;
u8 m_sync_save_data_success_count = 0;