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.
This commit is contained in:
mathieui 2015-03-14 15:19:18 +01:00
parent 8ee402863d
commit e0ef8fc03f
5 changed files with 21 additions and 2 deletions

View File

@ -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;
}
}

View File

@ -11,5 +11,6 @@ namespace ENetUtil
{
void WakeupThread(ENetHost* host);
int ENET_CALLBACK InterceptCallback(ENetHost* host, ENetEvent* event);
}

View File

@ -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;

View File

@ -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()));

View File

@ -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)
{