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
|
2012-01-23 06:36:15 +00:00
|
|
|
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
|
|
|
|
2011-02-03 18:09:24 +00:00
|
|
|
#include "../../Library/DisableThreadLibraryCalls.h" // contains DllMain entry point
|
2011-01-30 09:31:41 +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;
|
|
|
|
HANDLE threadHandle;
|
|
|
|
double value;
|
|
|
|
|
2012-01-08 17:35:29 +00:00
|
|
|
MeasureData() :
|
|
|
|
destAddr(),
|
|
|
|
timeout(),
|
|
|
|
timeoutValue(),
|
|
|
|
updateRate(),
|
|
|
|
updateCounter(),
|
|
|
|
threadHandle(),
|
|
|
|
value()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2011-03-29 19:21:57 +00:00
|
|
|
static CRITICAL_SECTION g_CriticalSection;
|
2012-01-08 17:35:29 +00:00
|
|
|
static UINT g_Instances = 0;
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2012-01-08 17:35:29 +00:00
|
|
|
PLUGIN_EXPORT void Initialize(void** data)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2012-01-08 17:35:29 +00:00
|
|
|
MeasureData* measure = new MeasureData;
|
|
|
|
*data = measure;
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2012-01-08 17:35:29 +00:00
|
|
|
if (g_Instances == 0)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2012-01-08 17:35:29 +00:00
|
|
|
InitializeCriticalSection(&g_CriticalSection);
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
2012-01-08 17:35:29 +00:00
|
|
|
|
|
|
|
++g_Instances;
|
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
|
|
|
|
2012-01-08 17:35:29 +00:00
|
|
|
DWORD WINAPI NetworkThreadProc(LPVOID pParam)
|
|
|
|
{
|
|
|
|
MeasureData* measure = (MeasureData*)pParam;
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2012-01-08 17:35:29 +00:00
|
|
|
const DWORD replySize = sizeof(ICMP_ECHO_REPLY) + 32;
|
|
|
|
BYTE* reply = new BYTE[replySize];
|
2011-03-29 19:21:57 +00:00
|
|
|
|
2012-01-08 17:35:29 +00:00
|
|
|
HANDLE hIcmpFile = IcmpCreateFile();
|
|
|
|
|
|
|
|
if (hIcmpFile != INVALID_HANDLE_VALUE)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2012-01-08 17:35:29 +00:00
|
|
|
IcmpSendEcho(hIcmpFile, measure->destAddr, NULL, 0, NULL, reply, replySize, measure->timeout);
|
|
|
|
IcmpCloseHandle(hIcmpFile);
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
|
2012-01-08 17:35:29 +00:00
|
|
|
EnterCriticalSection(&g_CriticalSection);
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2012-01-08 17:35:29 +00:00
|
|
|
ICMP_ECHO_REPLY* pReply = (ICMP_ECHO_REPLY*)reply;
|
|
|
|
if (pReply->Status == IP_REQ_TIMED_OUT)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2012-01-08 17:35:29 +00:00
|
|
|
measure->value = measure->timeoutValue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
measure->value = pReply->RoundTripTime;
|
|
|
|
}
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2012-01-08 17:35:29 +00:00
|
|
|
delete [] reply;
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2012-01-08 17:35:29 +00:00
|
|
|
CloseHandle(measure->threadHandle);
|
|
|
|
measure->threadHandle = NULL;
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2012-01-08 17:35:29 +00:00
|
|
|
LeaveCriticalSection(&g_CriticalSection);
|
2009-02-10 18:37:48 +00:00
|
|
|
|
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
|
|
|
double value = 0.0;
|
|
|
|
|
2012-01-08 17:35:29 +00:00
|
|
|
EnterCriticalSection(&g_CriticalSection);
|
|
|
|
value = measure->value;
|
|
|
|
LeaveCriticalSection(&g_CriticalSection);
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2012-01-08 17:35:29 +00:00
|
|
|
if (measure->threadHandle == NULL)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
measure->threadHandle = CreateThread(NULL, 0, NetworkThreadProc, measure, 0, &id);
|
|
|
|
}
|
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
|
|
|
|
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->threadHandle)
|
|
|
|
{
|
|
|
|
// Should really wait until the thread finishes instead terminating it...
|
|
|
|
TerminateThread(measure->threadHandle, 0);
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
2012-01-08 17:35:29 +00:00
|
|
|
LeaveCriticalSection(&g_CriticalSection);
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2012-01-08 17:35:29 +00:00
|
|
|
--g_Instances;
|
|
|
|
if (g_Instances == 0)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2012-01-08 17:35:29 +00:00
|
|
|
DeleteCriticalSection(&g_CriticalSection);
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
|
2012-01-08 17:35:29 +00:00
|
|
|
delete measure;
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|