-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule.js
220 lines (179 loc) · 4.86 KB
/
module.js
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
const sharedMemoryAddon = require("./build/Release/sharedMemory");
const messagingAddon = require("bindings")("messaging.node");
function isBuffer(value) {
return (
value &&
value.buffer instanceof ArrayBuffer &&
value.byteLength !== undefined
);
}
function strEncodeUTF16(str) {
var buf = new ArrayBuffer(str.length * 2);
var bufView = new Uint16Array(buf);
for (var i = 0, strLen = str.length; i < strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return bufView;
}
function bufferFromData(data, encoding) {
if (isBuffer(data)) {
return data;
} else if (data && typeof data === "string") {
if (encoding === "utf16") {
return strEncodeUTF16(data);
} else {
return Buffer.from(data, encoding || "utf8");
}
}
return Buffer.from(data);
}
const sendMessageTimeoutFlags = {
SMTO_NORMAL: 0x00,
SMTO_BLOCK: 0x01,
SMTO_ABORTIFHUNG: 0x02,
SMTO_NOTIMEOUTIFNOTHUNG: 0x08,
SMTO_ERRORONEXIT: 0x20, // WARNING: this flag is only available on Windows Vista and higher. If you use it on Windows XP SP3 or older, SendMessageTimeout will return ERROR_INVALID_PARAMETER.
};
// shared memory
function createSharedMemory(
name,
pageAccess,
fileMapAccess,
memorySize,
sddlString
) {
const handle = Buffer.alloc(sharedMemoryAddon.sizeof_SharedMemoryHandle);
const res = sharedMemoryAddon.CreateSharedMemory(
name,
pageAccess,
fileMapAccess,
memorySize,
sddlString || "",
handle
);
if (res > 0) {
throw `could not create file mapping for object ${name}: ${res}`;
} else if (res < 0) {
throw `could not map view of file ${name}: ${0 - res}`;
}
return handle;
}
function openSharedMemory(name, fileMapAccess, memorySize) {
const handle = Buffer.alloc(sharedMemoryAddon.sizeof_SharedMemoryHandle);
const res = sharedMemoryAddon.OpenSharedMemory(
name,
fileMapAccess,
memorySize,
handle
);
if (res > 0) {
throw `could not open file mapping for object ${name}: ${res}`;
} else if (res < 0) {
throw `could not map view of file ${name}: ${0 - res}`;
}
return handle;
}
function writeSharedData(handle, data, encoding) {
const buf = bufferFromData(data, encoding);
const res = sharedMemoryAddon.WriteSharedData(handle, buf, buf.byteLength);
if (res === 1) {
throw `data size (${data.length()}) exceeded maximum shared memory size`;
}
}
function readSharedData(handle, encoding, bufferSize) {
const dataSize = bufferSize || sharedMemoryAddon.GetSharedMemorySize(handle);
const buf = Buffer.alloc(dataSize);
const res = sharedMemoryAddon.ReadSharedData(handle, buf, dataSize);
if (res === 1) {
throw `data size (${data.length()}) exceeded maximum shared memory size`;
}
if (encoding) {
// is a string
return buf.toString(encoding).replace(/\0/g, ""); // remove trailing \0 characters
}
return buf;
}
function closeSharedMemory(handle) {
sharedMemoryAddon.CloseSharedMemory(handle);
}
// messaging
function stringToHwnd(strHwnd) {
const handle = Buffer.alloc(messagingAddon.sizeof_WindowHandle);
const res = messagingAddon.StringToHwnd(strHwnd, handle);
if (res > 0) {
throw `could not convert string ${strHwnd} to hwnd: ${res}`;
}
return handle;
}
function hwndToString(hwnd) {
const hwndVal = messagingAddon.HwndToUint(hwnd);
return `0x${hwndVal.toString(16)}`;
}
function sendCopyDataMessageTimeout(
targetHwnd,
senderHwnd,
data,
encoding,
sendMessageFlags,
timeout
) {
const buf = bufferFromData(data, encoding);
const finalFlags =
sendMessageFlags ||
sendMessageTimeoutFlags.SMTO_NOTIMEOUTIFNOTHUNG |
sendMessageTimeoutFlags.SMTO_ERRORONEXIT;
const res = messagingAddon.SendCopyDataMessageTimeout(
targetHwnd,
senderHwnd,
buf,
buf.byteLength,
finalFlags,
timeout || 2000
);
if (res === 1) {
throw `could not send WM_COPYDATA message`;
}
}
function createCopyDataListener(onMessage) {
const dataListener = Buffer.alloc(messagingAddon.sizeof_CopyDataListener);
const hwnd = messagingAddon.CreateCopyDataListener(
dataListener,
onMessage || function () {}
);
if (hwnd === 0) {
throw `could not create WM_COPYDATA listener`;
}
return {
dataListener,
listenerHwnd: `0x${hwnd.toString(16)}`,
};
}
function disposeCopyDataListener(listener) {
const res = messagingAddon.DisposeCopyDataListener(listener);
if (res === 1) {
throw `could not dispose WM_COPYDATA listener`;
}
}
module.exports = {
createSharedMemory,
openSharedMemory,
writeSharedData,
readSharedData,
closeSharedMemory,
sharedMemoryPageAccess: {
ReadOnly: 0x02,
WriteCopy: 0x08,
ReadWrite: 0x04,
},
sharedMemoryFileMapAccess: {
Read: 0x0004,
Write: 0x0002,
AllAccess: 0xf001f,
},
stringToHwnd,
hwndToString,
sendCopyDataMessageTimeout,
sendMessageTimeoutFlags,
createCopyDataListener,
disposeCopyDataListener,
};