-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHSend.cpp
More file actions
130 lines (99 loc) · 3.53 KB
/
HSend.cpp
File metadata and controls
130 lines (99 loc) · 3.53 KB
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
#include "Plugin.h"
#include <winhttp.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#pragma comment(lib, "winhttp.lib")
struct SiteInterface gSite;
static bool SendHTTPSignal(int port, const char* jsonPayload) {
HINTERNET hSession = WinHttpOpen(L"HSend/1.0", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
if (!hSession) return false;
HINTERNET hConnect = WinHttpConnect(hSession, L"localhost", port, 0);
if (!hConnect) {
WinHttpCloseHandle(hSession);
return false;
}
HINTERNET hRequest = WinHttpOpenRequest(hConnect, L"POST", L"/amibroker_signal", NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, 0);
if (!hRequest) {
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return false;
}
const wchar_t* headers = L"Content-Type: application/json\r\n";
int jsonLen = strlen(jsonPayload);
bool success = WinHttpSendRequest(hRequest, headers, -1, (LPVOID)jsonPayload, jsonLen, jsonLen, 0);
if (success) {
success = WinHttpReceiveResponse(hRequest, NULL);
}
WinHttpCloseHandle(hRequest);
WinHttpCloseHandle(hConnect);
WinHttpCloseHandle(hSession);
return success;
}
static AmiVar HSend(int NumArgs, AmiVar* ArgsTable) {
AmiVar result = gSite.AllocArrayResult();
if (NumArgs < 1) {
float* resultArray = result.array;
int arraySize = gSite.GetArraySize();
for (int i = 0; i < arraySize; i++) {
resultArray[i] = -1.0f;
}
return result;
}
const char* payload = (ArgsTable[0].type == VAR_STRING && ArgsTable[0].string) ? ArgsTable[0].string : "";
AmiVar chartIdResult = gSite.CallFunction("GetChartID", 0, NULL);
int chartId = (int)chartIdResult.val;
SYSTEMTIME st;
GetLocalTime(&st);
char timeStr[15];
sprintf(timeStr, "%02d%02d%02d.%03d", st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
char jsonPayload[2048];
sprintf(jsonPayload,
"{\"timestamp\":\"%s\",\"chartID\":%d,\"payload\":\"%s\"}",
timeStr, chartId, payload);
bool httpSuccess = SendHTTPSignal(5000, jsonPayload);
float* resultArray = result.array;
int arraySize = gSite.GetArraySize();
float resultValue = httpSuccess ? 1.0f : 0.0f;
for (int i = 0; i < arraySize; i++) {
resultArray[i] = resultValue;
}
return result;
}
static AmiVar VTest(int NumArgs, AmiVar* ArgsTable) {
AmiVar result = gSite.AllocArrayResult();
float* resultArray = result.array;
int arraySize = gSite.GetArraySize();
for (int i = 0; i < arraySize; i++) {
resultArray[i] = 42.0f;
}
return result;
}
FunctionTag gFunctionTable[] = {
"VTest", { VTest, 0, 0, 0, 0, NULL },
"HSend", { HSend, 0, 1, 0, 0, NULL }
};
int gFunctionTableSize = sizeof(gFunctionTable) / sizeof(FunctionTag);
PLUGINAPI int GetPluginInfo(struct PluginInfo* pInfo) {
pInfo->nStructSize = sizeof(struct PluginInfo);
pInfo->nType = PLUGIN_TYPE_AFL;
pInfo->nVersion = 10000;
pInfo->nIDCode = PIDCODE('H','S','N','D');
strcpy(pInfo->szName, "HSend Plugin");
strcpy(pInfo->szVendor, "Test");
return TRUE;
}
PLUGINAPI int SetSiteInterface(struct SiteInterface* pSite) {
gSite = *pSite;
return TRUE;
}
PLUGINAPI int GetFunctionTable(FunctionTag** ppFunctionTable) {
*ppFunctionTable = gFunctionTable;
return gFunctionTableSize;
}
PLUGINAPI int Init() {
return TRUE;
}
PLUGINAPI int Release() {
return TRUE;
}