void Game::threadPing()
{
if (m_realPingEvent) { m_realPingEvent->cancel(); m_realPingEvent = nullptr; }
m_realPingEvent = g_dispatcher.scheduleEvent([=] { threadPing(); }, 3000);
std::async(std::launch::async, []() { g_game.getNewPing(); });
}
int Game::getNewPing()
{
unsigned long destAdress = inet_addr(m_worldHost.c_str());
char SendData[32] = "Data Buffer";
LPVOID ReplyBuffer = NULL;
DWORD ReplySize = 0;
DWORD dwRetVal = 0;
if (destAdress == INADDR_NONE) {
return pingTime = 0;
}
HANDLE hIcmpFile = IcmpCreateFile();
if (hIcmpFile == INVALID_HANDLE_VALUE) {
return pingTime = 0;
}
ReplySize = sizeof(ICMP_ECHO_REPLY) + sizeof(SendData);
ReplyBuffer = (VOID*)malloc(ReplySize);
if (ReplyBuffer == NULL) {
return pingTime = 0;
}
dwRetVal = IcmpSendEcho(hIcmpFile, destAdress, SendData, sizeof(SendData), NULL, ReplyBuffer, ReplySize, 5000);
if (dwRetVal != 0) {
PICMP_ECHO_REPLY pEchoReply = (PICMP_ECHO_REPLY)ReplyBuffer;
return pingTime = pEchoReply->RoundTripTime;
}
return pingTime = 0;
}
Funckja g_game.getNewPing() ma byc wywolywana asynchronicznie poniewaz 'IcmpSendEcho' jest to funkcja blokujaca ktora czeka na ping i jest lekka scinka.
Dodam ze kod w takiej postaci działa asynchronicznie bez zadnego laga w visualu 2013 a w 2015 na ktorym mi zalezy juz nie.