Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor SlippiNetplayClient to enable attempting connections to multiple endpoints per player #389

Open
wants to merge 4 commits into
base: slippi
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 32 additions & 56 deletions Source/Core/Core/Slippi/SlippiMatchmaking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,12 +454,11 @@ void SlippiMatchmaking::handleMatchmaking()
return;
}

m_isSwapAttempt = false;
m_netplayClient = nullptr;

// Clear old users
m_remoteIps.clear();
m_playerInfo.clear();
m_remotePlayers.clear();

std::string matchId = getResp.value("matchId", "");
WARN_LOG(SLIPPI_ONLINE, "Match ID: %s", matchId.c_str());
Expand Down Expand Up @@ -493,40 +492,40 @@ void SlippiMatchmaking::handleMatchmaking()

if (isLocal)
{
std::vector<std::string> localIpParts;
SplitString(el.value("ipAddress", "1.1.1.1:123"), ':', localIpParts);
localExternalIp = localIpParts[0];
m_localPlayerIndex = playerInfo.port - 1;
}
};

// Loop a second time to get the correct remote IPs
for (json::iterator it = queue.begin(); it != queue.end(); ++it)
{
json el = *it;

if (el.value("port", 0) - 1 == m_localPlayerIndex)
continue;

auto extIp = el.value("ipAddress", "1.1.1.1:123");
std::vector<std::string> exIpParts;
SplitString(extIp, ':', exIpParts);

auto lanIp = el.value("ipAddressLan", "1.1.1.1:123");

WARN_LOG(SLIPPI_ONLINE, "LAN IP: %s", lanIp.c_str());
std::string ownExIp = el.value("ipAddress", "");
if (!ownExIp.empty())
{
std::vector<std::string> ownExIpParts;
SplitString(ownExIp, ':', ownExIpParts);
ENetAddress addr;
enet_address_set_host(&addr, ownExIpParts[0].c_str());
m_ownExternalAddress = addr.host;
}

if (exIpParts[0] != localExternalIp || lanIp.empty())
}
else
{
// If external IPs are different, just use that address
m_remoteIps.push_back(extIp);
continue;
struct RemotePlayer remotePlayer;
remotePlayer.index = playerInfo.port - 1;
std::string extIp = el.value("ipAddress", "");
if (!extIp.empty())
{
std::vector<std::string> exIpParts;
SplitString(extIp, ':', exIpParts);
enet_address_set_host(&remotePlayer.externalAddress, exIpParts[0].c_str());
remotePlayer.externalAddress.port = std::stoi(exIpParts[1]);
}
std::string lanIp = el.value("ipAddressLan", "");
if (!lanIp.empty())
{
std::vector<std::string> lanIpParts;
SplitString(lanIp, ':', lanIpParts);
enet_address_set_host(&remotePlayer.localAddress, lanIpParts[0].c_str());
remotePlayer.localAddress.port = std::stoi(lanIpParts[1]);
}
m_remotePlayers.push_back(remotePlayer);
}

// TODO: Instead of using one or the other, it might be better to try both

// If external IPs are the same, try using LAN IPs
m_remoteIps.push_back(lanIp);
}
}
m_isHost = getResp.value("isHost", false);
Expand Down Expand Up @@ -610,33 +609,10 @@ u8 SlippiMatchmaking::RemotePlayerCount()

void SlippiMatchmaking::handleConnecting()
{
auto userInfo = m_user->GetUserInfo();

m_isSwapAttempt = false;
m_netplayClient = nullptr;

u8 remotePlayerCount = (u8)m_remoteIps.size();
std::vector<std::string> remoteParts;
std::vector<std::string> addrs;
std::vector<u16> ports;
for (int i = 0; i < m_remoteIps.size(); i++)
{
remoteParts.clear();
SplitString(m_remoteIps[i], ':', remoteParts);
addrs.push_back(remoteParts[0]);
ports.push_back(std::stoi(remoteParts[1]));
}

std::stringstream ipLog;
ipLog << "Remote player IPs: ";
for (int i = 0; i < m_remoteIps.size(); i++)
{
ipLog << m_remoteIps[i] << ", ";
}
// INFO_LOG(SLIPPI_ONLINE, "[Matchmaking] My port: %d || %s", m_hostPort, ipLog.str());

// Is host is now used to specify who the decider is
auto client = std::make_unique<SlippiNetplayClient>(addrs, ports, remotePlayerCount, m_hostPort, m_isHost,
auto client = std::make_unique<SlippiNetplayClient>(m_remotePlayers, m_ownExternalAddress, m_hostPort, m_isHost,
m_localPlayerIndex);

while (!m_netplayClient)
Expand Down
9 changes: 3 additions & 6 deletions Source/Core/Core/Slippi/SlippiMatchmaking.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,15 @@ class SlippiMatchmaking

SlippiUser *m_user;

int m_isSwapAttempt = false;

int m_hostPort;
int m_localPlayerIndex;
std::vector<std::string> m_remoteIps;
u8 m_localPlayerIndex;
std::vector<struct RemotePlayer> m_remotePlayers;
MatchmakeResult m_mmResult;
std::vector<SlippiUser::UserInfo> m_playerInfo;
std::vector<u16> m_allowedStages;
bool m_joinedLobby;
bool m_isHost;
enet_uint32 m_ownExternalAddress;

std::unique_ptr<SlippiNetplayClient> m_netplayClient;

Expand All @@ -115,8 +114,6 @@ class SlippiMatchmaking
void sendMessage(json msg);
int receiveMessage(json &msg, int maxAttempts);

void sendHolePunchMsg(std::string remoteIp, u16 remotePort, u16 localPort);

void startMatchmaking();
void handleMatchmaking();
void handleConnecting();
Expand Down
Loading