rainmeter-studio/Plugins/PluginPing/Ping.cpp

222 lines
5.5 KiB
C++
Raw Normal View History

2009-02-10 18:37:48 +00:00
/*
Copyright (C) 2005 Kimmo Pekkola
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
2009-02-10 18:37:48 +00:00
*/
#include <windows.h>
#include <Ipexport.h>
2012-01-08 17:35:29 +00:00
#include <Icmpapi.h>
2011-02-03 18:09:24 +00:00
#include "../../Library/Export.h" // Rainmeter's exported functions
2009-02-10 18:37:48 +00:00
2012-01-08 17:35:29 +00:00
struct MeasureData
2009-02-10 18:37:48 +00:00
{
IPAddr destAddr;
DWORD timeout;
double timeoutValue;
DWORD updateRate;
DWORD updateCounter;
bool threadActive;
2009-02-10 18:37:48 +00:00
double value;
2012-01-08 17:35:29 +00:00
MeasureData() :
destAddr(),
timeout(),
timeoutValue(),
updateRate(),
updateCounter(),
threadActive(false),
2012-01-08 17:35:29 +00:00
value()
{
}
};
2009-02-10 18:37:48 +00:00
2011-03-29 19:21:57 +00:00
static CRITICAL_SECTION g_CriticalSection;
2009-02-10 18:37:48 +00:00
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
2009-02-10 18:37:48 +00:00
{
switch (fdwReason)
2009-02-10 18:37:48 +00:00
{
case DLL_PROCESS_ATTACH:
2012-01-08 17:35:29 +00:00
InitializeCriticalSection(&g_CriticalSection);
// Disable DLL_THREAD_ATTACH and DLL_THREAD_DETACH notification calls.
DisableThreadLibraryCalls(hinstDLL);
break;
case DLL_PROCESS_DETACH:
DeleteCriticalSection(&g_CriticalSection);
break;
2009-02-10 18:37:48 +00:00
}
2012-01-08 17:35:29 +00:00
return TRUE;
}
PLUGIN_EXPORT void Initialize(void** data, void* rm)
{
MeasureData* measure = new MeasureData;
*data = measure;
2009-02-10 18:37:48 +00:00
}
2012-01-08 17:35:29 +00:00
PLUGIN_EXPORT void Reload(void* data, void* rm, double* maxValue)
2009-02-10 18:37:48 +00:00
{
2012-01-08 17:35:29 +00:00
MeasureData* measure = (MeasureData*)data;
2009-02-10 18:37:48 +00:00
2012-01-08 17:35:29 +00:00
LPCWSTR value = RmReadString(rm, L"DestAddress", L"");
if (*value)
2009-02-10 18:37:48 +00:00
{
2012-01-08 17:35:29 +00:00
int strLen = (int)wcslen(value) + 1;
int bufLen = WideCharToMultiByte(CP_ACP, 0, value, strLen, NULL, 0, NULL, NULL);
if (bufLen > 0)
2009-02-10 18:37:48 +00:00
{
2012-01-08 17:35:29 +00:00
char* buffer = new char[bufLen];
WideCharToMultiByte(CP_ACP, 0, value, strLen, buffer, bufLen, NULL, NULL);
2009-02-10 18:37:48 +00:00
2012-01-08 17:35:29 +00:00
measure->destAddr = inet_addr(buffer);
if (measure->destAddr == INADDR_NONE)
2009-02-10 18:37:48 +00:00
{
2012-01-08 17:35:29 +00:00
WSADATA wsaData;
if (WSAStartup(0x0101, &wsaData) == 0)
2009-02-10 18:37:48 +00:00
{
2012-01-08 17:35:29 +00:00
LPHOSTENT pHost = gethostbyname(buffer);
if (pHost)
{
measure->destAddr = *(DWORD*)pHost->h_addr;
}
else
{
RmLog(LOG_WARNING, L"PingPlugin.dll: Unable to get host by name");
}
WSACleanup();
2009-02-10 18:37:48 +00:00
}
else
{
2012-01-08 17:35:29 +00:00
RmLog(LOG_WARNING, L"PingPlugin.dll: Unable to start WSA");
2009-02-10 18:37:48 +00:00
}
}
2012-01-08 17:35:29 +00:00
delete [] buffer;
2009-02-10 18:37:48 +00:00
}
}
2012-01-08 17:35:29 +00:00
measure->updateRate = RmReadInt(rm, L"UpdateRate", 32);
measure->timeout = RmReadInt(rm, L"Timeout", 30000);
measure->timeoutValue = RmReadDouble(rm, L"TimeoutValue", 30000.0);
}
2009-02-10 18:37:48 +00:00
DWORD WINAPI NetworkThreadProc(void* pParam)
2012-01-08 17:35:29 +00:00
{
// NOTE: Do not use CRT functions (since thread was created by CreateThread())!
2009-02-10 18:37:48 +00:00
MeasureData* measure = (MeasureData*)pParam;
const DWORD bufferSize = sizeof(ICMP_ECHO_REPLY) + 32;
BYTE buffer[bufferSize];
2011-03-29 19:21:57 +00:00
double value = 0.0;
2012-01-08 17:35:29 +00:00
HANDLE hIcmpFile = IcmpCreateFile();
if (hIcmpFile != INVALID_HANDLE_VALUE)
2009-02-10 18:37:48 +00:00
{
IcmpSendEcho(hIcmpFile, measure->destAddr, NULL, 0, NULL, buffer, bufferSize, measure->timeout);
2012-01-08 17:35:29 +00:00
IcmpCloseHandle(hIcmpFile);
ICMP_ECHO_REPLY* reply = (ICMP_ECHO_REPLY*)buffer;
2012-09-16 04:24:53 +00:00
value = (reply->Status != IP_SUCCESS) ? measure->timeoutValue : reply->RoundTripTime;
2009-02-10 18:37:48 +00:00
}
HMODULE module = NULL;
2009-02-10 18:37:48 +00:00
EnterCriticalSection(&g_CriticalSection);
if (measure->threadActive)
2009-02-10 18:37:48 +00:00
{
measure->value = value;
measure->threadActive = false;
2012-01-08 17:35:29 +00:00
}
else
{
// Thread is not attached to an existing measure any longer, so delete
// unreferenced data.
delete measure;
2009-02-10 18:37:48 +00:00
DWORD flags = GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT;
GetModuleHandleEx(flags, (LPCWSTR)DllMain, &module);
}
2012-01-08 17:35:29 +00:00
LeaveCriticalSection(&g_CriticalSection);
2009-02-10 18:37:48 +00:00
if (module)
{
// Decrement the ref count and possibly unload the module if this is
// the last instance.
FreeLibraryAndExitThread(module, 0);
}
2012-01-08 17:35:29 +00:00
return 0;
2009-02-10 18:37:48 +00:00
}
2012-01-08 17:35:29 +00:00
PLUGIN_EXPORT double Update(void* data)
2009-02-10 18:37:48 +00:00
{
2012-01-08 17:35:29 +00:00
MeasureData* measure = (MeasureData*)data;
2009-02-10 18:37:48 +00:00
2012-01-08 17:35:29 +00:00
EnterCriticalSection(&g_CriticalSection);
if (!measure->threadActive)
2012-01-08 17:35:29 +00:00
{
if (measure->updateCounter == 0)
2009-02-10 18:37:48 +00:00
{
2012-01-08 17:35:29 +00:00
// Launch a new thread to fetch the web data
DWORD id;
HANDLE thread = CreateThread(NULL, 0, NetworkThreadProc, measure, 0, &id);
if (thread)
{
CloseHandle(thread);
measure->threadActive = true;
}
2012-01-08 17:35:29 +00:00
}
2009-02-10 18:37:48 +00:00
2012-01-08 17:35:29 +00:00
measure->updateCounter++;
if (measure->updateCounter >= measure->updateRate)
{
measure->updateCounter = 0;
2009-02-10 18:37:48 +00:00
}
}
2012-01-08 17:35:29 +00:00
double value = measure->value;
LeaveCriticalSection(&g_CriticalSection);
2009-02-10 18:37:48 +00:00
return value;
}
2012-01-08 17:35:29 +00:00
PLUGIN_EXPORT void Finalize(void* data)
2009-02-10 18:37:48 +00:00
{
2012-01-08 17:35:29 +00:00
MeasureData* measure = (MeasureData*)data;
2009-02-10 18:37:48 +00:00
2012-01-08 17:35:29 +00:00
EnterCriticalSection(&g_CriticalSection);
if (measure->threadActive)
2012-01-08 17:35:29 +00:00
{
// Increment ref count of this module so that it will not be unloaded prior to
// thread completion.
DWORD flags = GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS;
HMODULE module;
GetModuleHandleEx(flags, (LPCWSTR)DllMain, &module);
// Thread will perform cleanup.
measure->threadActive = false;
2009-02-10 18:37:48 +00:00
}
else
2009-02-10 18:37:48 +00:00
{
delete measure;
2009-02-10 18:37:48 +00:00
}
LeaveCriticalSection(&g_CriticalSection);
}