From e0ef8fc03f1fafc801ca5d857347f9fa1ea58881 Mon Sep 17 00:00:00 2001 From: mathieui Date: Sat, 14 Mar 2015 15:19:18 +0100 Subject: [PATCH] NetPlay: Make the enet interrupts work Otherwise, it would work but any async sending would be delayed by 4ms or wait until the next packet was received. Also increase the client timeout to 250ms, since enet_host_service is now really interrupted. --- Source/Core/Common/ENetUtil.cpp | 12 ++++++++++++ Source/Core/Common/ENetUtil.h | 1 + Source/Core/Common/TraversalClient.cpp | 3 ++- Source/Core/Core/NetPlayClient.cpp | 5 ++++- Source/Core/Core/NetPlayServer.cpp | 2 ++ 5 files changed, 21 insertions(+), 2 deletions(-) diff --git a/Source/Core/Common/ENetUtil.cpp b/Source/Core/Common/ENetUtil.cpp index 7269893f90..ae5b19b2c3 100644 --- a/Source/Core/Common/ENetUtil.cpp +++ b/Source/Core/Common/ENetUtil.cpp @@ -25,4 +25,16 @@ void WakeupThread(ENetHost* host) enet_socket_send(host->socket, &address, &buf, 1); } +int ENET_CALLBACK InterceptCallback(ENetHost* host, ENetEvent* event) +{ + // wakeup packet received + if (host->receivedDataLength == 1 && host->receivedData[0] == 0) + { + event->type = (ENetEventType) 42; + return 1; + } + return 0; +} + + } diff --git a/Source/Core/Common/ENetUtil.h b/Source/Core/Common/ENetUtil.h index e9789673ad..219f51d425 100644 --- a/Source/Core/Common/ENetUtil.h +++ b/Source/Core/Common/ENetUtil.h @@ -11,5 +11,6 @@ namespace ENetUtil { void WakeupThread(ENetHost* host); +int ENET_CALLBACK InterceptCallback(ENetHost* host, ENetEvent* event); } diff --git a/Source/Core/Common/TraversalClient.cpp b/Source/Core/Common/TraversalClient.cpp index 72d18244f3..02fe5ee26e 100644 --- a/Source/Core/Common/TraversalClient.cpp +++ b/Source/Core/Common/TraversalClient.cpp @@ -301,7 +301,8 @@ void TraversalClient::Reset() int ENET_CALLBACK TraversalClient::InterceptCallback(ENetHost* host, ENetEvent* event) { auto traversalClient = g_TraversalClient.get(); - if (traversalClient->TestPacket(host->receivedData, host->receivedDataLength, &host->receivedAddress)) + if (traversalClient->TestPacket(host->receivedData, host->receivedDataLength, &host->receivedAddress) + || (host->receivedDataLength == 1 && host->receivedData[0] == 0)) { event->type = (ENetEventType)42; return 1; diff --git a/Source/Core/Core/NetPlayClient.cpp b/Source/Core/Core/NetPlayClient.cpp index cbc5b26981..b424932e77 100644 --- a/Source/Core/Core/NetPlayClient.cpp +++ b/Source/Core/Core/NetPlayClient.cpp @@ -103,7 +103,10 @@ NetPlayClient::NetPlayClient(const std::string& address, const u16 port, NetPlay if (net > 0 && netEvent.type == ENET_EVENT_TYPE_CONNECT) { if (Connect()) + { + m_client->intercept = ENetUtil::InterceptCallback; m_thread = std::thread(&NetPlayClient::ThreadFunc, this); + } } else { @@ -497,7 +500,7 @@ void NetPlayClient::ThreadFunc() int net; if (m_traversal_client) m_traversal_client->HandleResends(); - net = enet_host_service(m_client, &netEvent, 4); + net = enet_host_service(m_client, &netEvent, 250); while (!m_async_queue.Empty()) { Send(*(m_async_queue.Front().get())); diff --git a/Source/Core/Core/NetPlayServer.cpp b/Source/Core/Core/NetPlayServer.cpp index 499a6088cb..977d87e1fc 100644 --- a/Source/Core/Core/NetPlayServer.cpp +++ b/Source/Core/Core/NetPlayServer.cpp @@ -91,6 +91,8 @@ NetPlayServer::NetPlayServer(const u16 port, bool traversal, std::string central serverAddr.host = ENET_HOST_ANY; serverAddr.port = port; m_server = enet_host_create(&serverAddr, 10, 3, 0, 0); + if (m_server != nullptr) + m_server->intercept = ENetUtil::InterceptCallback; } if (m_server != nullptr) {