fork download
  1. // inetcmon(InterNET-Connected-MONitor) by (-_-)
  2.  
  3. #pragma comment(lib, "Kernel32.lib")
  4.  
  5. #include <cstdio>
  6. #include <cstdlib>
  7. #include <deque>
  8. #include <windows.h>
  9.  
  10. using namespace std;
  11.  
  12. typedef HRESULT (*IS_INTERNET_CONNECTED_PROC)(void);
  13.  
  14. typedef struct {
  15. bool connected;
  16. DWORD tickCount;
  17. } RECORD;
  18.  
  19. static const DWORD DEFAULT_INTERVAL_SECS = 1;
  20. static const DWORD M1_MILLISECS = 60 * 1000;
  21. static const DWORD M10_MILLISECS = 10 * M1_MILLISECS;
  22. static const DWORD M30_MILLISECS = 30 * M1_MILLISECS;
  23. static const DWORD H1_MILLISECS = 60 * M1_MILLISECS;
  24. static const DWORD H2_MILLISECS = 2 * H1_MILLISECS;
  25.  
  26. int main(int argc, char** argv) {
  27. if (argc > 2) {
  28. fprintf(stderr, "usage: %s [intervalSecs=%d]\n", argv[0], DEFAULT_INTERVAL_SECS);
  29. return 1;
  30. }
  31. DWORD intervalSecs = DEFAULT_INTERVAL_SECS;
  32. if (argc >= 2) {
  33. DWORD intervalSecs = atoi(argv[1]);
  34. }
  35. DWORD intervalMillisecs = intervalSecs * 1000;
  36.  
  37. HMODULE connectDllModuleHandle = LoadLibrary("Connect.dll");
  38. if (connectDllModuleHandle == NULL) {
  39. fprintf(stderr, "error: GetModuleHandle\n");
  40. return 1;
  41. }
  42. IS_INTERNET_CONNECTED_PROC IsInternetConnectedProc =
  43. (IS_INTERNET_CONNECTED_PROC)GetProcAddress(connectDllModuleHandle, "IsInternetConnected");
  44. if (IsInternetConnectedProc == NULL) {
  45. fprintf(stderr, "error: GetProcAddress\n");
  46. return 1;
  47. }
  48.  
  49. deque<RECORD> recordDeque;
  50. for (;;) {
  51. DWORD now = GetTickCount();
  52. RECORD record;
  53. record.tickCount = now;
  54. record.connected = IsInternetConnectedProc() == S_OK;
  55. recordDeque.push_front(record);
  56.  
  57. unsigned int m1DisconnectedSectionCount = 0;
  58. unsigned int m10DisconnectedSectionCount = 0;
  59. unsigned int m30DisconnectedSectionCount = 0;
  60. unsigned int h1DisconnectedSectionCount = 0;
  61. unsigned int h2DisconnectedSectionCount = 0;
  62. bool previousConnected = true;
  63. DWORD lastConnectedSectionStart = now;
  64. bool lastConnectedSectionInside = true;
  65. for (deque<RECORD>::iterator iter = recordDeque.begin(); iter != recordDeque.end(); iter++) {
  66. DWORD elapsedMillisecs = now - iter->tickCount;
  67. if (elapsedMillisecs >= H2_MILLISECS) {
  68. recordDeque.erase(iter, recordDeque.end());
  69. break;
  70. }
  71. bool disconnectedSectionEntering = previousConnected && !iter->connected;
  72. if (disconnectedSectionEntering) h2DisconnectedSectionCount++;
  73. if (elapsedMillisecs < H1_MILLISECS) {
  74. if (disconnectedSectionEntering) h1DisconnectedSectionCount++;
  75. if (elapsedMillisecs < M30_MILLISECS) {
  76. if (disconnectedSectionEntering) m30DisconnectedSectionCount++;
  77. if (elapsedMillisecs < M10_MILLISECS) {
  78. if (disconnectedSectionEntering) m10DisconnectedSectionCount++;
  79. if (elapsedMillisecs < M1_MILLISECS) {
  80. if (disconnectedSectionEntering) m1DisconnectedSectionCount++;
  81. }
  82. }
  83. }
  84. }
  85. if (lastConnectedSectionInside) {
  86. if (!iter->connected) {
  87. lastConnectedSectionInside = false;
  88. }
  89. else {
  90. lastConnectedSectionStart = iter->tickCount;
  91. }
  92. }
  93. previousConnected = iter->connected;
  94. }
  95. DWORD lastConnectedSectionMillisecs = now - lastConnectedSectionStart;
  96. DWORD lastConnectedSectionSeconds = lastConnectedSectionMillisecs / 1000;
  97. DWORD lastConnectedSectionMinutes = lastConnectedSectionSeconds / 60;
  98. DWORD lastConnectedSectionHours = lastConnectedSectionMinutes / 60;
  99. printf("%-12s 1m:%3d 10m:%3d 30m:%3d 1h:%3d 2h:%3d lastc:%02d:%02d:%02d\n",
  100. record.connected ? "CONNECTED" : "DISCONNECTED",
  101. m1DisconnectedSectionCount,
  102. m10DisconnectedSectionCount,
  103. m30DisconnectedSectionCount,
  104. h1DisconnectedSectionCount,
  105. h2DisconnectedSectionCount,
  106. lastConnectedSectionHours % 24,
  107. lastConnectedSectionMinutes % 60,
  108. lastConnectedSectionSeconds % 60);
  109.  
  110. Sleep(intervalMillisecs);
  111. }
  112. return 0;
  113. }
  114.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty