2021-07-04 20:31:23 -06:00
|
|
|
// SPDX-License-Identifier: CC0-1.0
|
2015-02-02 02:56:53 -07:00
|
|
|
|
2015-02-02 03:08:58 -07:00
|
|
|
#include "Common/TraversalClient.h"
|
2017-09-09 13:52:35 -06:00
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstring>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "Common/CommonTypes.h"
|
2015-09-26 15:13:07 -06:00
|
|
|
#include "Common/Logging/Log.h"
|
|
|
|
#include "Common/MsgHandler.h"
|
2018-05-21 07:48:17 -06:00
|
|
|
#include "Common/Random.h"
|
2018-10-18 02:33:05 -06:00
|
|
|
#include "Core/NetPlayProto.h"
|
2015-02-02 02:56:53 -07:00
|
|
|
|
2023-04-24 06:15:55 -06:00
|
|
|
namespace Common
|
|
|
|
{
|
2023-01-12 09:45:49 -07:00
|
|
|
TraversalClient::TraversalClient(ENetHost* netHost, const std::string& server, const u16 port,
|
|
|
|
const u16 port_alt)
|
|
|
|
: m_NetHost(netHost), m_Server(server), m_port(port), m_portAlt(port_alt)
|
2015-02-02 02:56:53 -07:00
|
|
|
{
|
|
|
|
netHost->intercept = TraversalClient::InterceptCallback;
|
|
|
|
|
|
|
|
Reset();
|
|
|
|
|
|
|
|
ReconnectToServer();
|
|
|
|
}
|
|
|
|
|
2018-04-16 14:02:21 -06:00
|
|
|
TraversalClient::~TraversalClient() = default;
|
|
|
|
|
|
|
|
TraversalHostId TraversalClient::GetHostID() const
|
|
|
|
{
|
|
|
|
return m_HostId;
|
|
|
|
}
|
|
|
|
|
2022-03-03 21:08:55 -07:00
|
|
|
TraversalInetAddress TraversalClient::GetExternalAddress() const
|
|
|
|
{
|
|
|
|
return m_external_address;
|
|
|
|
}
|
|
|
|
|
2018-04-16 14:02:21 -06:00
|
|
|
TraversalClient::State TraversalClient::GetState() const
|
|
|
|
{
|
|
|
|
return m_State;
|
|
|
|
}
|
|
|
|
|
|
|
|
TraversalClient::FailureReason TraversalClient::GetFailureReason() const
|
2015-02-02 02:56:53 -07:00
|
|
|
{
|
2018-04-16 14:02:21 -06:00
|
|
|
return m_FailureReason;
|
2015-02-02 02:56:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void TraversalClient::ReconnectToServer()
|
|
|
|
{
|
|
|
|
if (enet_address_set_host(&m_ServerAddress, m_Server.c_str()))
|
|
|
|
{
|
2017-08-08 16:29:47 -06:00
|
|
|
OnFailure(FailureReason::BadHost);
|
2015-02-02 02:56:53 -07:00
|
|
|
return;
|
|
|
|
}
|
2015-02-14 20:51:08 -07:00
|
|
|
m_ServerAddress.port = m_port;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2021-01-19 12:00:01 -07:00
|
|
|
m_State = State::Connecting;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-02-02 02:56:53 -07:00
|
|
|
TraversalPacket hello = {};
|
2021-01-19 12:11:39 -07:00
|
|
|
hello.type = TraversalPacketType::HelloFromClient;
|
2015-02-02 02:56:53 -07:00
|
|
|
hello.helloFromClient.protoVersion = TraversalProtoVersion;
|
|
|
|
SendTraversalPacket(hello);
|
|
|
|
if (m_Client)
|
|
|
|
m_Client->OnTraversalStateChanged();
|
|
|
|
}
|
|
|
|
|
2021-01-19 12:21:04 -07:00
|
|
|
static ENetAddress MakeENetAddress(const TraversalInetAddress& address)
|
2015-02-02 02:56:53 -07:00
|
|
|
{
|
2021-01-19 12:21:04 -07:00
|
|
|
ENetAddress eaddr{};
|
|
|
|
if (address.isIPV6)
|
2015-02-02 02:56:53 -07:00
|
|
|
{
|
|
|
|
eaddr.port = 0; // no support yet :(
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-19 12:21:04 -07:00
|
|
|
eaddr.host = address.address[0];
|
|
|
|
eaddr.port = ntohs(address.port);
|
2015-02-02 02:56:53 -07:00
|
|
|
}
|
|
|
|
return eaddr;
|
|
|
|
}
|
|
|
|
|
2021-01-19 12:26:04 -07:00
|
|
|
void TraversalClient::ConnectToClient(std::string_view host)
|
2015-02-02 02:56:53 -07:00
|
|
|
{
|
|
|
|
if (host.size() > sizeof(TraversalHostId))
|
|
|
|
{
|
2020-12-02 11:17:27 -07:00
|
|
|
PanicAlertFmt("Host too long");
|
2015-02-02 02:56:53 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
TraversalPacket packet = {};
|
2021-01-19 12:11:39 -07:00
|
|
|
packet.type = TraversalPacketType::ConnectPlease;
|
2021-01-19 12:26:04 -07:00
|
|
|
memcpy(packet.connectPlease.hostId.data(), host.data(), host.size());
|
2015-02-02 02:56:53 -07:00
|
|
|
m_ConnectRequestId = SendTraversalPacket(packet);
|
|
|
|
m_PendingConnect = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TraversalClient::TestPacket(u8* data, size_t size, ENetAddress* from)
|
|
|
|
{
|
|
|
|
if (from->host == m_ServerAddress.host && from->port == m_ServerAddress.port)
|
|
|
|
{
|
|
|
|
if (size < sizeof(TraversalPacket))
|
|
|
|
{
|
2020-10-23 12:41:30 -06:00
|
|
|
ERROR_LOG_FMT(NETPLAY, "Received too-short traversal packet.");
|
2015-02-02 02:56:53 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
HandleServerPacket((TraversalPacket*)data);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--Temporary until more of the old netplay branch is moved over
|
|
|
|
void TraversalClient::Update()
|
|
|
|
{
|
|
|
|
ENetEvent netEvent;
|
|
|
|
if (enet_host_service(m_NetHost, &netEvent, 4) > 0)
|
|
|
|
{
|
|
|
|
switch (netEvent.type)
|
|
|
|
{
|
|
|
|
case ENET_EVENT_TYPE_RECEIVE:
|
|
|
|
TestPacket(netEvent.packet->data, netEvent.packet->dataLength, &netEvent.peer->address);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-02-02 02:56:53 -07:00
|
|
|
enet_packet_destroy(netEvent.packet);
|
|
|
|
break;
|
2015-02-02 03:08:58 -07:00
|
|
|
default:
|
|
|
|
break;
|
2015-02-02 02:56:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
HandleResends();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TraversalClient::HandleServerPacket(TraversalPacket* packet)
|
|
|
|
{
|
|
|
|
u8 ok = 1;
|
|
|
|
switch (packet->type)
|
|
|
|
{
|
2021-01-19 12:11:39 -07:00
|
|
|
case TraversalPacketType::Ack:
|
2015-02-02 02:56:53 -07:00
|
|
|
if (!packet->ack.ok)
|
|
|
|
{
|
2017-08-08 16:29:47 -06:00
|
|
|
OnFailure(FailureReason::ServerForgotAboutUs);
|
2015-02-02 02:56:53 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
for (auto it = m_OutgoingTraversalPackets.begin(); it != m_OutgoingTraversalPackets.end(); ++it)
|
|
|
|
{
|
|
|
|
if (it->packet.requestId == packet->requestId)
|
|
|
|
{
|
2023-01-12 09:45:49 -07:00
|
|
|
if (packet->requestId == m_TestRequestId)
|
|
|
|
HandleTraversalTest();
|
2015-02-02 02:56:53 -07:00
|
|
|
m_OutgoingTraversalPackets.erase(it);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2021-01-19 12:11:39 -07:00
|
|
|
case TraversalPacketType::HelloFromServer:
|
2021-01-19 12:00:01 -07:00
|
|
|
if (!IsConnecting())
|
2015-02-02 02:56:53 -07:00
|
|
|
break;
|
|
|
|
if (!packet->helloFromServer.ok)
|
|
|
|
{
|
2017-08-08 16:29:47 -06:00
|
|
|
OnFailure(FailureReason::VersionTooOld);
|
2015-02-02 02:56:53 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
m_HostId = packet->helloFromServer.yourHostId;
|
2022-03-03 21:08:55 -07:00
|
|
|
m_external_address = packet->helloFromServer.yourAddress;
|
2023-01-12 09:45:49 -07:00
|
|
|
NewTraversalTest();
|
2021-01-19 12:00:01 -07:00
|
|
|
m_State = State::Connected;
|
2015-02-02 02:56:53 -07:00
|
|
|
if (m_Client)
|
|
|
|
m_Client->OnTraversalStateChanged();
|
|
|
|
break;
|
2021-01-19 12:11:39 -07:00
|
|
|
case TraversalPacketType::PleaseSendPacket:
|
2015-02-02 02:56:53 -07:00
|
|
|
{
|
|
|
|
// security is overrated.
|
2021-01-19 12:21:04 -07:00
|
|
|
ENetAddress addr = MakeENetAddress(packet->pleaseSendPacket.address);
|
2015-02-02 02:56:53 -07:00
|
|
|
if (addr.port != 0)
|
|
|
|
{
|
|
|
|
char message[] = "Hello from Dolphin Netplay...";
|
|
|
|
ENetBuffer buf;
|
|
|
|
buf.data = message;
|
|
|
|
buf.dataLength = sizeof(message) - 1;
|
2023-01-12 09:45:49 -07:00
|
|
|
if (m_ttlReady)
|
|
|
|
{
|
|
|
|
int oldttl;
|
|
|
|
enet_socket_get_option(m_NetHost->socket, ENET_SOCKOPT_TTL, &oldttl);
|
|
|
|
enet_socket_set_option(m_NetHost->socket, ENET_SOCKOPT_TTL, m_ttl);
|
|
|
|
enet_socket_send(m_NetHost->socket, &addr, &buf, 1);
|
|
|
|
enet_socket_set_option(m_NetHost->socket, ENET_SOCKOPT_TTL, oldttl);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
enet_socket_send(m_NetHost->socket, &addr, &buf, 1);
|
|
|
|
}
|
2015-02-02 02:56:53 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// invalid IPV6
|
|
|
|
ok = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2021-01-19 12:11:39 -07:00
|
|
|
case TraversalPacketType::ConnectReady:
|
|
|
|
case TraversalPacketType::ConnectFailed:
|
2015-02-02 02:56:53 -07:00
|
|
|
{
|
|
|
|
if (!m_PendingConnect || packet->connectReady.requestId != m_ConnectRequestId)
|
|
|
|
break;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-02-02 02:56:53 -07:00
|
|
|
m_PendingConnect = false;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-02-02 02:56:53 -07:00
|
|
|
if (!m_Client)
|
|
|
|
break;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2021-01-19 12:11:39 -07:00
|
|
|
if (packet->type == TraversalPacketType::ConnectReady)
|
2021-01-19 12:21:04 -07:00
|
|
|
m_Client->OnConnectReady(MakeENetAddress(packet->connectReady.address));
|
2015-02-02 02:56:53 -07:00
|
|
|
else
|
|
|
|
m_Client->OnConnectFailed(packet->connectFailed.reason);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2022-01-12 17:47:26 -07:00
|
|
|
WARN_LOG_FMT(NETPLAY, "Received unknown packet with type {}", static_cast<int>(packet->type));
|
2015-02-02 02:56:53 -07:00
|
|
|
break;
|
|
|
|
}
|
2021-01-19 12:11:39 -07:00
|
|
|
if (packet->type != TraversalPacketType::Ack)
|
2015-02-02 02:56:53 -07:00
|
|
|
{
|
|
|
|
TraversalPacket ack = {};
|
2021-01-19 12:11:39 -07:00
|
|
|
ack.type = TraversalPacketType::Ack;
|
2015-02-02 02:56:53 -07:00
|
|
|
ack.requestId = packet->requestId;
|
|
|
|
ack.ack.ok = ok;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-02-02 02:56:53 -07:00
|
|
|
ENetBuffer buf;
|
|
|
|
buf.data = &ack;
|
|
|
|
buf.dataLength = sizeof(ack);
|
|
|
|
if (enet_socket_send(m_NetHost->socket, &m_ServerAddress, &buf, 1) == -1)
|
2017-08-08 16:29:47 -06:00
|
|
|
OnFailure(FailureReason::SocketSendError);
|
2015-02-02 02:56:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-16 23:53:50 -07:00
|
|
|
void TraversalClient::OnFailure(FailureReason reason)
|
2015-02-02 02:56:53 -07:00
|
|
|
{
|
2021-01-19 12:00:01 -07:00
|
|
|
m_State = State::Failure;
|
2015-02-02 02:56:53 -07:00
|
|
|
m_FailureReason = reason;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-02-02 02:56:53 -07:00
|
|
|
if (m_Client)
|
|
|
|
m_Client->OnTraversalStateChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TraversalClient::ResendPacket(OutgoingTraversalPacketInfo* info)
|
|
|
|
{
|
2023-01-12 09:45:49 -07:00
|
|
|
bool testPacket =
|
|
|
|
m_TestSocket != ENET_SOCKET_NULL && info->packet.type == TraversalPacketType::TestPlease;
|
2015-02-02 02:56:53 -07:00
|
|
|
info->sendTime = enet_time_get();
|
|
|
|
info->tries++;
|
|
|
|
ENetBuffer buf;
|
|
|
|
buf.data = &info->packet;
|
|
|
|
buf.dataLength = sizeof(info->packet);
|
2023-01-12 09:45:49 -07:00
|
|
|
if (enet_socket_send(testPacket ? m_TestSocket : m_NetHost->socket, &m_ServerAddress, &buf, 1) ==
|
|
|
|
-1)
|
2017-08-08 16:29:47 -06:00
|
|
|
OnFailure(FailureReason::SocketSendError);
|
2015-02-02 02:56:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void TraversalClient::HandleResends()
|
|
|
|
{
|
2018-04-16 14:28:48 -06:00
|
|
|
const u32 now = enet_time_get();
|
2015-02-02 02:56:53 -07:00
|
|
|
for (auto& tpi : m_OutgoingTraversalPackets)
|
|
|
|
{
|
|
|
|
if (now - tpi.sendTime >= (u32)(300 * tpi.tries))
|
|
|
|
{
|
|
|
|
if (tpi.tries >= 5)
|
|
|
|
{
|
2017-08-08 16:29:47 -06:00
|
|
|
OnFailure(FailureReason::ResendTimeout);
|
2015-02-02 02:56:53 -07:00
|
|
|
m_OutgoingTraversalPackets.clear();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ResendPacket(&tpi);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
HandlePing();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TraversalClient::HandlePing()
|
|
|
|
{
|
2018-04-16 14:28:48 -06:00
|
|
|
const u32 now = enet_time_get();
|
2021-01-19 12:00:01 -07:00
|
|
|
if (IsConnected() && now - m_PingTime >= 500)
|
2015-02-02 02:56:53 -07:00
|
|
|
{
|
2015-02-16 23:53:50 -07:00
|
|
|
TraversalPacket ping = {};
|
2021-01-19 12:11:39 -07:00
|
|
|
ping.type = TraversalPacketType::Ping;
|
2015-02-02 02:56:53 -07:00
|
|
|
ping.ping.hostId = m_HostId;
|
|
|
|
SendTraversalPacket(ping);
|
|
|
|
m_PingTime = now;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-12 09:45:49 -07:00
|
|
|
void TraversalClient::NewTraversalTest()
|
|
|
|
{
|
|
|
|
// create test socket
|
|
|
|
if (m_TestSocket != ENET_SOCKET_NULL)
|
|
|
|
enet_socket_destroy(m_TestSocket);
|
|
|
|
m_TestSocket = enet_socket_create(ENET_SOCKET_TYPE_DATAGRAM);
|
|
|
|
ENetAddress addr = {ENET_HOST_ANY, 0};
|
|
|
|
if (m_TestSocket == ENET_SOCKET_NULL || enet_socket_bind(m_TestSocket, &addr) < 0)
|
|
|
|
{
|
|
|
|
// error, abort
|
|
|
|
if (m_TestSocket != ENET_SOCKET_NULL)
|
|
|
|
{
|
|
|
|
enet_socket_destroy(m_TestSocket);
|
|
|
|
m_TestSocket = ENET_SOCKET_NULL;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
enet_socket_set_option(m_TestSocket, ENET_SOCKOPT_NONBLOCK, 1);
|
|
|
|
// create holepunch packet
|
|
|
|
TraversalPacket packet = {};
|
|
|
|
packet.type = TraversalPacketType::Ping;
|
|
|
|
packet.ping.hostId = m_HostId;
|
|
|
|
packet.requestId = Common::Random::GenerateValue<TraversalRequestId>();
|
|
|
|
// create buffer
|
|
|
|
ENetBuffer buf;
|
|
|
|
buf.data = &packet;
|
|
|
|
buf.dataLength = sizeof(packet);
|
|
|
|
// send to alt port
|
|
|
|
ENetAddress altAddress = m_ServerAddress;
|
|
|
|
altAddress.port = m_portAlt;
|
|
|
|
// set up ttl and send
|
|
|
|
int oldttl;
|
|
|
|
enet_socket_get_option(m_TestSocket, ENET_SOCKOPT_TTL, &oldttl);
|
|
|
|
enet_socket_set_option(m_TestSocket, ENET_SOCKOPT_TTL, m_ttl);
|
|
|
|
if (enet_socket_send(m_TestSocket, &altAddress, &buf, 1) == -1)
|
|
|
|
{
|
|
|
|
// error, abort
|
|
|
|
enet_socket_destroy(m_TestSocket);
|
|
|
|
m_TestSocket = ENET_SOCKET_NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
enet_socket_set_option(m_TestSocket, ENET_SOCKOPT_TTL, oldttl);
|
|
|
|
// send the test request
|
|
|
|
packet.type = TraversalPacketType::TestPlease;
|
|
|
|
m_TestRequestId = SendTraversalPacket(packet);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TraversalClient::HandleTraversalTest()
|
|
|
|
{
|
|
|
|
if (m_TestSocket != ENET_SOCKET_NULL)
|
|
|
|
{
|
|
|
|
// check for packet on test socket (with timeout)
|
|
|
|
u32 deadline = enet_time_get() + 50;
|
|
|
|
u32 waitCondition;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
waitCondition = ENET_SOCKET_WAIT_RECEIVE | ENET_SOCKET_WAIT_INTERRUPT;
|
|
|
|
u32 currentTime = enet_time_get();
|
|
|
|
if (currentTime > deadline ||
|
|
|
|
enet_socket_wait(m_TestSocket, &waitCondition, deadline - currentTime) != 0)
|
|
|
|
{
|
|
|
|
// error or timeout, exit the loop and assume test failure
|
|
|
|
waitCondition = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (waitCondition & ENET_SOCKET_WAIT_RECEIVE)
|
|
|
|
{
|
|
|
|
// try reading the packet and see if it's relevant
|
|
|
|
ENetAddress raddr;
|
|
|
|
TraversalPacket packet;
|
|
|
|
ENetBuffer buf;
|
|
|
|
buf.data = &packet;
|
|
|
|
buf.dataLength = sizeof(packet);
|
|
|
|
int rv = enet_socket_receive(m_TestSocket, &raddr, &buf, 1);
|
|
|
|
if (rv < 0)
|
|
|
|
{
|
|
|
|
// error, exit the loop and assume test failure
|
|
|
|
waitCondition = 0;
|
|
|
|
break;
|
|
|
|
}
|
2023-12-11 16:01:29 -07:00
|
|
|
else if (rv < int(sizeof(packet)) || raddr.host != m_ServerAddress.host ||
|
2023-01-12 09:45:49 -07:00
|
|
|
raddr.host != m_portAlt || packet.requestId != m_TestRequestId)
|
|
|
|
{
|
|
|
|
// irrelevant packet, ignore
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (waitCondition & ENET_SOCKET_WAIT_INTERRUPT);
|
|
|
|
// regardless of what happens next, we can throw out the socket
|
|
|
|
enet_socket_destroy(m_TestSocket);
|
|
|
|
m_TestSocket = ENET_SOCKET_NULL;
|
|
|
|
if (waitCondition & ENET_SOCKET_WAIT_RECEIVE)
|
|
|
|
{
|
|
|
|
// success, we can stop now
|
|
|
|
m_ttlReady = true;
|
2023-01-12 11:46:02 -07:00
|
|
|
m_Client->OnTtlDetermined(m_ttl);
|
2023-01-12 09:45:49 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// fail, increment and retry
|
|
|
|
if (++m_ttl < 32)
|
|
|
|
NewTraversalTest();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-02 02:56:53 -07:00
|
|
|
TraversalRequestId TraversalClient::SendTraversalPacket(const TraversalPacket& packet)
|
|
|
|
{
|
|
|
|
OutgoingTraversalPacketInfo info;
|
|
|
|
info.packet = packet;
|
2018-06-14 08:26:11 -06:00
|
|
|
info.packet.requestId = Common::Random::GenerateValue<TraversalRequestId>();
|
2015-02-02 02:56:53 -07:00
|
|
|
info.tries = 0;
|
|
|
|
m_OutgoingTraversalPackets.push_back(info);
|
|
|
|
ResendPacket(&m_OutgoingTraversalPackets.back());
|
|
|
|
return info.packet.requestId;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TraversalClient::Reset()
|
|
|
|
{
|
|
|
|
m_PendingConnect = false;
|
|
|
|
m_Client = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ENET_CALLBACK TraversalClient::InterceptCallback(ENetHost* host, ENetEvent* event)
|
|
|
|
{
|
|
|
|
auto traversalClient = g_TraversalClient.get();
|
2015-03-14 08:19:18 -06:00
|
|
|
if (traversalClient->TestPacket(host->receivedData, host->receivedDataLength,
|
|
|
|
&host->receivedAddress) ||
|
|
|
|
(host->receivedDataLength == 1 && host->receivedData[0] == 0))
|
2015-02-02 02:56:53 -07:00
|
|
|
{
|
2023-11-03 09:15:04 -06:00
|
|
|
event->type = static_cast<ENetEventType>(Common::ENet::SKIPPABLE_EVENT);
|
2015-02-02 02:56:53 -07:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<TraversalClient> g_TraversalClient;
|
2023-04-24 06:15:55 -06:00
|
|
|
ENet::ENetHostPtr g_MainNetHost;
|
2015-02-02 02:56:53 -07:00
|
|
|
|
|
|
|
// The settings at the previous TraversalClient reset - notably, we
|
|
|
|
// need to know not just what port it's on, but whether it was
|
|
|
|
// explicitly requested.
|
|
|
|
static std::string g_OldServer;
|
2015-09-20 05:08:37 -06:00
|
|
|
static u16 g_OldServerPort;
|
2023-01-12 09:45:49 -07:00
|
|
|
static u16 g_OldServerPortAlt;
|
2015-09-20 05:08:37 -06:00
|
|
|
static u16 g_OldListenPort;
|
2015-02-02 02:56:53 -07:00
|
|
|
|
2023-01-12 09:45:49 -07:00
|
|
|
bool EnsureTraversalClient(const std::string& server, u16 server_port, u16 server_port_alt,
|
|
|
|
u16 listen_port)
|
2015-02-02 02:56:53 -07:00
|
|
|
{
|
2015-09-20 05:08:37 -06:00
|
|
|
if (!g_MainNetHost || !g_TraversalClient || server != g_OldServer ||
|
2023-01-12 09:45:49 -07:00
|
|
|
server_port != g_OldServerPort || server_port_alt != g_OldServerPortAlt ||
|
|
|
|
listen_port != g_OldListenPort)
|
2015-02-02 02:56:53 -07:00
|
|
|
{
|
|
|
|
g_OldServer = server;
|
2015-09-20 05:08:37 -06:00
|
|
|
g_OldServerPort = server_port;
|
2023-01-12 09:45:49 -07:00
|
|
|
g_OldServerPortAlt = server_port_alt;
|
2015-09-20 05:08:37 -06:00
|
|
|
g_OldListenPort = listen_port;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-09-20 05:08:37 -06:00
|
|
|
ENetAddress addr = {ENET_HOST_ANY, listen_port};
|
2023-04-19 10:25:21 -06:00
|
|
|
auto host = Common::ENet::ENetHostPtr{enet_host_create(&addr, // address
|
|
|
|
50, // peerCount
|
|
|
|
NetPlay::CHANNEL_COUNT, // channelLimit
|
|
|
|
0, // incomingBandwidth
|
|
|
|
0)}; // outgoingBandwidth
|
2015-02-02 02:56:53 -07:00
|
|
|
if (!host)
|
|
|
|
{
|
|
|
|
g_MainNetHost.reset();
|
|
|
|
return false;
|
|
|
|
}
|
2023-04-02 09:21:26 -06:00
|
|
|
host->mtu = std::min(host->mtu, NetPlay::MAX_ENET_MTU);
|
2023-04-19 10:25:21 -06:00
|
|
|
g_MainNetHost = std::move(host);
|
2023-01-12 09:45:49 -07:00
|
|
|
g_TraversalClient.reset(
|
|
|
|
new TraversalClient(g_MainNetHost.get(), server, server_port, server_port_alt));
|
2015-02-02 02:56:53 -07:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReleaseTraversalClient()
|
|
|
|
{
|
|
|
|
if (!g_TraversalClient)
|
|
|
|
return;
|
|
|
|
|
2017-07-22 16:19:22 -06:00
|
|
|
g_TraversalClient.reset();
|
|
|
|
g_MainNetHost.reset();
|
2015-03-01 08:17:32 -07:00
|
|
|
}
|
2023-04-24 06:15:55 -06:00
|
|
|
} // namespace Common
|