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
parent e92443e467
commit 5519efa66e
9 changed files with 367 additions and 51 deletions

View File

@ -307,13 +307,13 @@ unsigned int NetPlayServer::OnConnect(ENetPeer* socket)
// send join message to already connected clients
sf::Packet spac;
spac << (MessageId)NP_MSG_PLAYER_JOIN;
spac << static_cast<MessageId>(NP_MSG_PLAYER_JOIN);
spac << player.pid << player.name << player.revision;
SendToClients(spac);
// send new client success message with their id
spac.clear();
spac << (MessageId)0;
spac << static_cast<MessageId>(0);
spac << player.pid;
Send(player.socket, spac);
@ -321,15 +321,24 @@ unsigned int NetPlayServer::OnConnect(ENetPeer* socket)
if (m_selected_game != "")
{
spac.clear();
spac << (MessageId)NP_MSG_CHANGE_GAME;
spac << static_cast<MessageId>(NP_MSG_CHANGE_GAME);
spac << m_selected_game;
Send(player.socket, spac);
}
// send the pad buffer value
if (!m_host_input_authority)
{
// send the pad buffer value
spac.clear();
spac << static_cast<MessageId>(NP_MSG_PAD_BUFFER);
spac << static_cast<u32>(m_target_buffer_size);
Send(player.socket, spac);
}
// send input authority state
spac.clear();
spac << (MessageId)NP_MSG_PAD_BUFFER;
spac << (u32)m_target_buffer_size;
spac << static_cast<MessageId>(NP_MSG_HOST_INPUT_AUTHORITY);
spac << m_host_input_authority;
Send(player.socket, spac);
// sync GC SRAM with new client
@ -340,7 +349,7 @@ unsigned int NetPlayServer::OnConnect(ENetPeer* socket)
g_SRAM_netplay_initialized = true;
}
spac.clear();
spac << (MessageId)NP_MSG_SYNC_GC_SRAM;
spac << static_cast<MessageId>(NP_MSG_SYNC_GC_SRAM);
for (size_t i = 0; i < sizeof(g_SRAM.p_SRAM); ++i)
{
spac << g_SRAM.p_SRAM[i];
@ -497,6 +506,24 @@ void NetPlayServer::AdjustPadBufferSize(unsigned int size)
SendAsyncToClients(std::move(spac));
}
void NetPlayServer::SetHostInputAuthority(const bool enable)
{
std::lock_guard<std::recursive_mutex> lkg(m_crit.game);
m_host_input_authority = enable;
// tell clients about the new value
sf::Packet spac;
spac << static_cast<MessageId>(NP_MSG_HOST_INPUT_AUTHORITY);
spac << m_host_input_authority;
SendAsyncToClients(std::move(spac));
// resend pad buffer to clients when disabled
if (!m_host_input_authority)
AdjustPadBufferSize(m_target_buffer_size);
}
void NetPlayServer::SendAsyncToClients(sf::Packet&& packet)
{
{
@ -559,13 +586,59 @@ unsigned int NetPlayServer::OnData(sf::Packet& packet, Client& player)
packet >> pad.button >> pad.analogA >> pad.analogB >> pad.stickX >> pad.stickY >>
pad.substickX >> pad.substickY >> pad.triggerLeft >> pad.triggerRight >> pad.isConnected;
// Add to packet for relay to clients
spac << map << pad.button << pad.analogA << pad.analogB << pad.stickX << pad.stickY
if (m_host_input_authority)
{
m_last_pad_status[map] = pad;
if (!m_first_pad_status_received[map])
{
m_first_pad_status_received[map] = true;
SendFirstReceivedToHost(map, true);
}
}
else
{
spac << map << pad.button << pad.analogA << pad.analogB << pad.stickX << pad.stickY
<< pad.substickX << pad.substickY << pad.triggerLeft << pad.triggerRight
<< pad.isConnected;
}
}
if (!m_host_input_authority)
SendToClients(spac, player.pid);
}
break;
case NP_MSG_PAD_HOST_POLL:
{
PadMapping pad_num;
packet >> pad_num;
sf::Packet spac;
spac << static_cast<MessageId>(NP_MSG_PAD_DATA);
if (pad_num < 0)
{
for (size_t i = 0; i < m_pad_map.size(); i++)
{
if (m_pad_map[i] == -1)
continue;
const GCPadStatus& pad = m_last_pad_status[i];
spac << static_cast<PadMapping>(i) << pad.button << pad.analogA << pad.analogB << pad.stickX
<< pad.stickY << pad.substickX << pad.substickY << pad.triggerLeft << pad.triggerRight
<< pad.isConnected;
}
}
else if (m_pad_map.at(pad_num) != -1)
{
const GCPadStatus& pad = m_last_pad_status[pad_num];
spac << pad_num << pad.button << pad.analogA << pad.analogB << pad.stickX << pad.stickY
<< pad.substickX << pad.substickY << pad.triggerLeft << pad.triggerRight
<< pad.isConnected;
}
SendToClients(spac, player.pid);
SendToClients(spac);
}
break;
@ -911,7 +984,10 @@ bool NetPlayServer::StartGame()
m_current_game = Common::Timer::GetTimeMs();
// no change, just update with clients
AdjustPadBufferSize(m_target_buffer_size);
if (!m_host_input_authority)
AdjustPadBufferSize(m_target_buffer_size);
m_first_pad_status_received.fill(false);
const u64 initial_rtc = GetInitialNetPlayRTC();
@ -1291,6 +1367,15 @@ bool NetPlayServer::CompressBufferIntoPacket(const std::vector<u8>& in_buffer, s
return true;
}
void NetPlayServer::SendFirstReceivedToHost(const PadMapping map, const bool state)
{
sf::Packet pac;
pac << static_cast<MessageId>(NP_MSG_PAD_FIRST_RECEIVED);
pac << map;
pac << state;
Send(m_players.at(1).socket, pac);
}
u64 NetPlayServer::GetInitialNetPlayRTC() const
{
const auto& config = SConfig::GetInstance();