forked from thqby/ahk2_lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebSocket.ahk
318 lines (297 loc) · 13.7 KB
/
WebSocket.ahk
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/************************************************************************
* @author thqby
* @date 2024/01/27
* @version 1.0.7
***********************************************************************/
#DllLoad winhttp.dll
class WebSocket {
Ptr := 0, async := 0, readyState := 0, url := ''
; The array of HINTERNET handles, [hSession, hConnect, hRequest(onOpen) | hWebSocket?]
HINTERNETs := []
; when request is opened
onOpen() => 0
; when server sent a close frame
onClose(status, reason) => 0
; when server sent binary message
onData(data, size) => 0
; when server sent UTF-8 message
onMessage(msg) => 0
reconnect() => 0
/**
* @param {String} Url the url of websocket
* @param {Object} Events an object of `{open:(this)=>void,data:(this, data, size)=>bool,message:(this, msg)=>bool,close:(this, status, reason)=>void}`
* @param {Integer} Async Use asynchronous mode
* @param {Object|Map|String} Headers Request header
* @param {Integer} TimeOut Set resolve, connect, send and receive timeout
*/
__New(Url, Events := 0, Async := true, Headers := '', TimeOut := 0, InitialSize := 8192) {
static contexts := Map()
if (!RegExMatch(Url, 'i)^((?<SCHEME>wss?)://)?((?<USERNAME>[^:]+):(?<PASSWORD>.+)@)?(?<HOST>[^/:\s]+)(:(?<PORT>\d+))?(?<PATH>/\S*)?$', &m))
Throw WebSocket.Error('Invalid websocket url')
if !hSession := DllCall('Winhttp\WinHttpOpen', 'ptr', 0, 'uint', 0, 'ptr', 0, 'ptr', 0, 'uint', Async ? 0x10000000 : 0, 'ptr')
Throw WebSocket.Error()
this.async := Async := !!Async, this.url := Url
this.HINTERNETs.Push(hSession)
port := m.PORT ? Integer(m.PORT) : m.SCHEME = 'ws' ? 80 : 443
dwFlags := m.SCHEME = 'wss' ? 0x800000 : 0
if TimeOut
DllCall('Winhttp\WinHttpSetTimeouts', 'ptr', hSession, 'int', TimeOut, 'int', TimeOut, 'int', TimeOut, 'int', TimeOut, 'int')
if !hConnect := DllCall('Winhttp\WinHttpConnect', 'ptr', hSession, 'wstr', m.HOST, 'ushort', port, 'uint', 0, 'ptr')
Throw WebSocket.Error()
this.HINTERNETs.Push(hConnect)
switch Type(Headers) {
case 'Object', 'Map':
s := ''
for k, v in Headers is Map ? Headers : Headers.OwnProps()
s .= '`r`n' k ': ' v
Headers := LTrim(s, '`r`n')
case 'String':
default:
Headers := ''
}
if (Events) {
for k, v in Events.OwnProps()
if (k ~= 'i)^(open|data|message|close)$')
this.DefineProp('on' k, { call: v })
}
if (Async) {
this.DefineProp('shutdown', { call: async_shutdown })
.DefineProp('receive', { call: receive })
.DefineProp('_send', { call: async_send })
} else this.__cache_size := InitialSize
connect(this), this.DefineProp('reconnect', { call: connect })
connect(self) {
if !self.HINTERNETs.Length
Throw WebSocket.Error('The connection is closed')
self.shutdown()
if !hRequest := DllCall('Winhttp\WinHttpOpenRequest', 'ptr', hConnect, 'wstr', 'GET', 'wstr', m.PATH, 'ptr', 0, 'ptr', 0, 'ptr', 0, 'uint', dwFlags, 'ptr')
Throw WebSocket.Error()
self.HINTERNETs.Push(hRequest), self.onOpen()
if (Headers)
DllCall('Winhttp\WinHttpAddRequestHeaders', 'ptr', hRequest, 'wstr', Headers, 'uint', -1, 'uint', 0x20000000, 'int')
if (!DllCall('Winhttp\WinHttpSetOption', 'ptr', hRequest, 'uint', 114, 'ptr', 0, 'uint', 0, 'int')
|| !DllCall('Winhttp\WinHttpSendRequest', 'ptr', hRequest, 'ptr', 0, 'uint', 0, 'ptr', 0, 'uint', 0, 'uint', 0, 'uptr', 0, 'int')
|| !DllCall('Winhttp\WinHttpReceiveResponse', 'ptr', hRequest, 'ptr', 0)
|| !DllCall('Winhttp\WinHttpQueryHeaders', 'ptr', hRequest, 'uint', 19, 'ptr', 0, 'wstr', status := '00000', 'uint*', 10, 'ptr', 0, 'int')
|| status != '101')
Throw IsSet(status) ? WebSocket.Error('Invalid status: ' status) : WebSocket.Error()
if !self.Ptr := DllCall('Winhttp\WinHttpWebSocketCompleteUpgrade', 'ptr', hRequest, 'ptr', 0)
Throw WebSocket.Error()
DllCall('Winhttp\WinHttpCloseHandle', 'ptr', self.HINTERNETs.Pop())
self.HINTERNETs.Push(self.Ptr), self.readyState := 1
(Async && async_receive(self))
}
async_receive(self) {
static on_read_complete := get_sync_callback(), hHeap := DllCall('GetProcessHeap', 'ptr')
static msg_gui := Gui(), wm_ahkmsg := DllCall('RegisterWindowMessage', 'str', 'AHK_WEBSOCKET_STATUSCHANGE', 'uint')
static pHeapReAlloc := DllCall('GetProcAddress', 'ptr', DllCall('GetModuleHandle', 'str', 'kernel32', 'ptr'), 'astr', 'HeapReAlloc', 'ptr')
static pSendMessageW := DllCall('GetProcAddress', 'ptr', DllCall('GetModuleHandle', 'str', 'user32', 'ptr'), 'astr', 'SendMessageW', 'ptr')
static pWinHttpWebSocketReceive := DllCall('GetProcAddress', 'ptr', DllCall('GetModuleHandle', 'str', 'winhttp', 'ptr'), 'astr', 'WinHttpWebSocketReceive', 'ptr')
static _ := (OnMessage(wm_ahkmsg, WEBSOCKET_READ_WRITE_COMPLETE, 0xff), DllCall('SetParent', 'ptr', msg_gui.Hwnd, 'ptr', -3))
; #DllLoad E:\projects\test\test\x64\Debug\test.dll
; on_read_complete := DllCall('GetProcAddress', 'ptr', DllCall('GetModuleHandle', 'str', 'test', 'ptr'), 'astr', 'WINHTTP_STATUS_READ_COMPLETE', 'ptr')
NumPut('ptr', pws := ObjPtr(self), 'ptr', msg_gui.Hwnd, 'uint', wm_ahkmsg, 'uint', InitialSize, 'ptr', hHeap,
'ptr', cache := DllCall('HeapAlloc', 'ptr', hHeap, 'uint', 0, 'uptr', InitialSize, 'ptr'), 'uptr', 0, 'uptr', InitialSize,
'ptr', pHeapReAlloc, 'ptr', pSendMessageW, 'ptr', pWinHttpWebSocketReceive,
contexts[pws] := context := Buffer(11 * A_PtrSize)), self.__send_queue := []
context.DefineProp('__Delete', { call: self => DllCall('HeapFree', 'ptr', hHeap, 'uint', 0, 'ptr', NumGet(self, 3 * A_PtrSize + 8, 'ptr')) })
DllCall('Winhttp\WinHttpSetOption', 'ptr', self, 'uint', 45, 'ptr*', context.Ptr, 'uint', A_PtrSize)
DllCall('Winhttp\WinHttpSetStatusCallback', 'ptr', self, 'ptr', on_read_complete, 'uint', 0x80000, 'uptr', 0, 'ptr')
if err := DllCall('Winhttp\WinHttpWebSocketReceive', 'ptr', self, 'ptr', cache, 'uint', InitialSize, 'uint*', 0, 'uint*', 0)
self.onError(err)
}
static WEBSOCKET_READ_WRITE_COMPLETE(wp, lp, msg, hwnd) {
static map_has := Map.Prototype.Has
if !map_has(contexts, ws := NumGet(wp, 'ptr')) || (ws := ObjFromPtrAddRef(ws)).readyState != 1
return
switch lp {
case 5: ; WRITE_COMPLETE
try ws.__send_queue.Pop()
case 4: ; WINHTTP_WEB_SOCKET_CLOSE_BUFFER_TYPE
if err := NumGet(wp, A_PtrSize, 'uint')
return ws.onError(err)
rea := ws.QueryCloseStatus(), ws.shutdown()
return ws.onClose(rea.status, rea.reason)
default: ; WINHTTP_WEB_SOCKET_BINARY_MESSAGE_BUFFER_TYPE, WINHTTP_WEB_SOCKET_UTF8_MESSAGE_BUFFER_TYPE
data := NumGet(wp, A_PtrSize, 'ptr')
size := NumGet(wp, 2 * A_PtrSize, 'uptr')
if lp == 2
return ws.onMessage(StrGet(data, size, 'utf-8'))
else return ws.onData(data, size)
}
}
static async_send(self, type, buf, size) {
if (self.readyState != 1)
Throw WebSocket.Error('websocket is disconnected')
(q := self.__send_queue).InsertAt(1, buf)
while (err := DllCall('Winhttp\WinHttpWebSocketSend', 'ptr', self, 'uint', type, 'ptr', buf, 'uint', size, 'uint')) = 4317 && A_Index < 60
Sleep(15)
if err
q.RemoveAt(1), self.onError(err)
}
static async_shutdown(self) {
if self.Ptr
DllCall('Winhttp\WinHttpSetOption', 'ptr', self, 'uint', 45, 'ptr*', 0, 'uint', A_PtrSize)
(WebSocket.Prototype.shutdown)(self)
try contexts.Delete(ObjPtr(self))
}
static get_sync_callback() {
mcodes := ['g+wMVot0JBiF9g+E0QAAAItEJBw9AAAQAHUVi0YkagVW/3YI/3YE/9Beg8QMwhQAPQAACAAPhaYAAACLBolEJASLRCQgU1VXi1AEx0QkFAAAAADHRCQYAAAAAIP6BHRsi04Yi+qLAI0MAYlOGIPlAXV2i0YUiUQkFI1EJBBSUP92CItGJP92BIlMJCjHRhgAAAAA/9CNfhyFwHQHi14MOx91UYsHK0YYagBqAFCLRhQDRhhQ/3QkMItGKP/QhcB0HT3dEAAAdBaJRCQUagSNRCQUUP92CItGJP92BP/QX11bXoPEDMIUAIteHI1+HDvLcrED24tGIFP/dhRqAP92EP/QhcB0B4lGFIkf65aF7XSSx0QkFA4AB4DrsQ==',
'SIXSD4QvAQAASIlcJCBBVkiD7FBIi9pMi/FBgfgAABAAdR9Ii0sITIvCi1IQQbkFAAAA/1NASItcJHhIg8RQQV7DQYH4AAAIAA+F3gAAAEiLAkljUQRIiWwkYEiJRCQwM8BIiXQkaEiJfCRwSMdEJDgAAAAASIlEJECD+gQPhIYAAABFiwGL6kiLQyhNjQQATIlDKIPlAQ+FnAAAAEiLQyBMi8qLUxBIi0sITIlEJEBMjUQkMEiJRCQ4SMdDKAAAAAD/U0BIjXswSIXAdAiLcxRIOzd1c0SLB0UzyUiLUyBJi85EK0MoSANTKEjHRCQgAAAAAP9TSIXAdCM93RAAAHQci8BIiUQkOItTEEyNRCQwSItLCEG5BAAAAP9TQEiLdCRoSItsJGBIi3wkcEiLXCR4SIPEUEFew0iLczBIjXswTDvGcpBIA/ZMi0MgTIvOSItLGDPS/1M4SIXAdAxIiUMgSIk36Wz///+F7Q+EZP///0jHRCQ4DgAHgOuM']
DllCall('crypt32\CryptStringToBinary', 'str', hex := mcodes[A_PtrSize >> 2], 'uint', 0, 'uint', 1, 'ptr', 0, 'uint*', &s := 0, 'ptr', 0, 'ptr', 0) &&
DllCall('crypt32\CryptStringToBinary', 'str', hex, 'uint', 0, 'uint', 1, 'ptr', code := Buffer(s), 'uint*', &s, 'ptr', 0, 'ptr', 0) &&
DllCall('VirtualProtect', 'ptr', code, 'uint', s, 'uint', 0x40, 'uint*', 0)
return code
/*c++ source, /FAc /O2 /GS-
struct Context {
void *obj;
HWND hwnd;
UINT msg;
UINT initial_size;
HANDLE heap;
BYTE *data;
size_t size;
size_t capacity;
decltype(&HeapReAlloc) ReAlloc;
decltype(&SendMessageW) Send;
decltype(&WinHttpWebSocketReceive) Receive;
};
void __stdcall WINHTTP_STATUS_READ_WRITE_COMPLETE(
void *hInternet,
Context *dwContext,
DWORD dwInternetStatus,
WINHTTP_WEB_SOCKET_STATUS *lpvStatusInformation,
DWORD dwStatusInformationLength) {
if (!dwContext)
return;
auto &context = *dwContext;
if (dwInternetStatus == WINHTTP_CALLBACK_FLAG_WRITE_COMPLETE)
return (void)context.Send(context.hwnd, context.msg, (WPARAM)dwContext, 5);
else if (dwInternetStatus != WINHTTP_CALLBACK_FLAG_READ_COMPLETE)
return;
UINT_PTR param[3] = { (UINT_PTR)context.obj, 0 };
DWORD err;
switch (auto bt = lpvStatusInformation->eBufferType)
{
case WINHTTP_WEB_SOCKET_CLOSE_BUFFER_TYPE:
goto close;
default:
size_t new_size;
auto is_fragment = bt & 1;
context.size += lpvStatusInformation->dwBytesTransferred;
if (!is_fragment) {
param[1] = (UINT_PTR)context.data;
param[2] = context.size;
context.size = 0;
if (!context.Send(context.hwnd, context.msg, (WPARAM)param, bt) ||
(new_size = (size_t)context.initial_size) == context.capacity)
break;
}
else if (context.size >= context.capacity)
new_size = context.capacity << 1;
else break;
if (auto p = context.ReAlloc(context.heap, 0, context.data, new_size))
context.data = (BYTE *)p, context.capacity = new_size;
else if (is_fragment) {
param[1] = E_OUTOFMEMORY;
goto close;
}
break;
}
err = context.Receive(hInternet, context.data + context.size, DWORD(context.capacity - context.size), 0, 0);
if (err && err != ERROR_INVALID_OPERATION) {
param[1] = err;
close: context.Send(context.hwnd, context.msg, (WPARAM)param, WINHTTP_WEB_SOCKET_CLOSE_BUFFER_TYPE);
}
}*/
}
static receive(*) {
Throw WebSocket.Error('Used only in synchronous mode')
}
}
__Delete() {
this.shutdown()
while (this.HINTERNETs.Length)
DllCall('Winhttp\WinHttpCloseHandle', 'ptr', this.HINTERNETs.Pop())
}
onError(err, what := 0) {
if err != 12030
Throw WebSocket.Error(err, what - 5)
if this.readyState == 3
return
this.readyState := 3
try this.onClose(1006, '')
}
class Error extends Error {
__New(err := A_LastError, what := -4) {
static module := DllCall('GetModuleHandle', 'str', 'winhttp', 'ptr')
if err is Integer
if (DllCall("FormatMessage", "uint", 0x900, "ptr", module, "uint", err, "uint", 0, "ptr*", &pstr := 0, "uint", 0, "ptr", 0), pstr)
err := (msg := StrGet(pstr), DllCall('LocalFree', 'ptr', pstr), msg)
else err := OSError(err).Message
super.__New(err, what)
}
}
queryCloseStatus() {
if (!DllCall('Winhttp\WinHttpWebSocketQueryCloseStatus', 'ptr', this, 'ushort*', &usStatus := 0, 'ptr', vReason := Buffer(123), 'uint', 123, 'uint*', &len := 0))
return { status: usStatus, reason: StrGet(vReason, len, 'utf-8') }
else if (this.readyState > 1)
return { status: 1006, reason: '' }
}
/** @param type BINARY_MESSAGE = 0, BINARY_FRAGMENT = 1, UTF8_MESSAGE = 2, UTF8_FRAGMENT = 3 */
_send(type, buf, size) {
if (this.readyState != 1)
Throw WebSocket.Error('websocket is disconnected')
if err := DllCall('Winhttp\WinHttpWebSocketSend', 'ptr', this, 'uint', type, 'ptr', buf, 'uint', size, 'uint')
return this.onError(err)
}
; sends a utf-8 string to the server
sendText(str) {
if (size := StrPut(str, 'utf-8') - 1) {
StrPut(str, buf := Buffer(size), 'utf-8')
this._send(2, buf, size)
} else
this._send(2, 0, 0)
}
send(buf) => this._send(0, buf, buf.Size)
receive() {
if (this.readyState != 1)
Throw WebSocket.Error('websocket is disconnected')
ptr := (cache := Buffer(size := this.__cache_size)).Ptr, offset := 0
while (!err := DllCall('Winhttp\WinHttpWebSocketReceive', 'ptr', this, 'ptr', ptr + offset, 'uint', size - offset, 'uint*', &dwBytesRead := 0, 'uint*', &eBufferType := 0)) {
switch eBufferType {
case 1, 3:
offset += dwBytesRead
if offset == size
cache.Size := size *= 2, ptr := cache.Ptr
case 0, 2:
offset += dwBytesRead
if eBufferType == 2
return StrGet(ptr, offset, 'utf-8')
cache.Size := offset
return cache
case 4:
rea := this.QueryCloseStatus(), this.shutdown()
try this.onClose(rea.status, rea.reason)
return
}
}
(err != 4317 && this.onError(err))
}
shutdown() {
if (this.readyState = 1) {
this.readyState := 2
DllCall('Winhttp\WinHttpWebSocketClose', 'ptr', this, 'ushort', 1006, 'ptr', 0, 'uint', 0)
this.readyState := 3
}
while (this.HINTERNETs.Length > 2)
DllCall('Winhttp\WinHttpCloseHandle', 'ptr', this.HINTERNETs.Pop())
this.Ptr := 0
}
}
; ws := WebSocket(wss_or_ws_url, {
; message: (self, data) => FileAppend(Data '`n', '*', 'utf-8'),
; close: (self, status, reason) => FileAppend(status ' ' reason '`n', '*', 'utf-8')
; })
; ws.sendText('hello'), Sleep(100)
; ws.send(0, Buffer(10), 10), Sleep(100)