-
Notifications
You must be signed in to change notification settings - Fork 1
/
PluginCheckNet.cs
172 lines (150 loc) · 4.97 KB
/
PluginCheckNet.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
using System;
using System.Net;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Net.NetworkInformation;
using System.Threading;
using Rainmeter;
namespace PluginCheckNet
{
internal class Measure
{
public string ConnectionType;
public double ReturnValue;
public int UpdateRate;
public int UpdateCounter;
public IntPtr SkinHandle;
public string FinishAction;
public Thread ConnectionThread;
public readonly object ThreadLocker = new object();
private void CheckConnection(string CurrentType)
{
lock (ThreadLocker)
{
if (CurrentType == "NETWORK" || CurrentType == "INTERNET")
{
if (System.Convert.ToDouble(NetworkInterface.GetIsNetworkAvailable()) == 0)
{
ReturnValue = -1.0;
}
else
{
ReturnValue = 1.0;
}
}
if (ReturnValue == 1.0 && CurrentType == "INTERNET")
{
try
{
IPAddress[] addresslist = Dns.GetHostAddresses("www.msftncsi.com");
if (addresslist[0].ToString().Length > 6)
{
ReturnValue = 1.0;
}
else
{
ReturnValue = -1.0;
}
}
catch
{
ReturnValue = -1.0;
}
}
if (!String.IsNullOrEmpty(FinishAction))
{
API.Execute(SkinHandle, FinishAction);
}
}
}
internal Measure()
{
}
internal void Reload(Rainmeter.API rm, ref double maxValue)
{
SkinHandle = rm.GetSkin();
FinishAction = rm.ReadString("FinishAction", "");
ConnectionType = rm.ReadString("ConnectionType", "INTERNET").ToUpperInvariant();
if (ConnectionType != "NETWORK" && ConnectionType != "INTERNET")
{
API.Log(API.LogType.Error, "CheckNet.dll: ConnectionType=" + ConnectionType + " not valid");
}
UpdateRate = rm.ReadInt("UpdateRate", 20);
if (UpdateRate <= 0)
{
UpdateRate = 20;
}
}
internal double Update()
{
if (UpdateCounter == 0)
{
if (ConnectionThread == null || ConnectionThread.ThreadState == ThreadState.Stopped)
{
ConnectionThread = new Thread(() =>
{
try
{
CheckConnection(ConnectionType);
}
catch (OperationCanceledException) { }
});
ConnectionThread.Start();
}
}
UpdateCounter = UpdateCounter + 1;
if (UpdateCounter >= UpdateRate)
{
UpdateCounter = 0;
}
return ReturnValue;
}
//internal string GetString()
//{
// return "";
//}
//internal void ExecuteBang(string args)
//{
//}
}
public static class Plugin
{
[DllExport]
public unsafe static void Initialize(void** data, void* rm)
{
uint id = (uint)((void*)*data);
Measures.Add(id, new Measure());
}
[DllExport]
public unsafe static void Finalize(void* data)
{
uint id = (uint)data;
Measures.Remove(id);
}
[DllExport]
public unsafe static void Reload(void* data, void* rm, double* maxValue)
{
uint id = (uint)data;
Measures[id].Reload(new Rainmeter.API((IntPtr)rm), ref *maxValue);
}
[DllExport]
public unsafe static double Update(void* data)
{
uint id = (uint)data;
return Measures[id].Update();
}
//[DllExport]
//public unsafe static char* GetString(void* data)
//{
// uint id = (uint)data;
// fixed (char* s = Measures[id].GetString()) return s;
//}
//[DllExport]
//public unsafe static void ExecuteBang(void* data, char* args)
//{
// uint id = (uint)data;
// Measures[id].ExecuteBang(new string(args));
//}
internal static Dictionary<uint, Measure> Measures = new Dictionary<uint, Measure>();
}
}