-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
218 lines (179 loc) · 5.57 KB
/
index.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
const child_process = require('child_process');
const path = require('path');
const readline = require('readline');
const shm = require('nodeshm');
const mmap = require('mmap.js');
const fs = require('fs');
const EventEmitter = require('events');
const MAX_WIDTH = 2048;
const MAX_FILENAME = 24; // srsly osx
const MAX_SIZE = MAX_WIDTH * MAX_WIDTH * 4;
const CLIENT_TIMEOUT = 2000;
function filenameForServer(server) {
const parts = server.uuid.split('.');
const last = parts[parts.length - 1];
const uuid = last.split('-').join('').slice(-MAX_FILENAME);
return uuid;
}
class SyphonClient extends EventEmitter {
constructor(registry, server) {
super();
this.pending = true;
this.server = server;
this.registry = registry;
this.lastFrame = null;
}
connectAsync() {
const filename = filenameForServer(this.server);
shm.shm_unlink(filename);
this.fd = shm.shm_open(filename, shm.O_CREAT | shm.O_RDWR | shm.O_EXCL, 0o600);
fs.ftruncateSync(this.fd, MAX_SIZE);
this.shmBuf = mmap.alloc(
MAX_SIZE,
mmap.PROT_READ | mmap.PROT_WRITE,
mmap.MAP_SHARED,
this.fd,
0,
);
this.copyBufs = [
Buffer.alloc(MAX_SIZE),
Buffer.alloc(MAX_SIZE),
];
this.curBuf = 0;
const promise = new Promise((resolve, reject) => {
this.onConnected = () => resolve(this);
this.registry.send('createClient', {
uuid: this.server.uuid,
});
});
return promise;
}
checkAlive() {
if (this.lastFrame === null) {
return true;
}
const delta = Date.now() - this.lastFrame;
return delta < CLIENT_TIMEOUT;
}
disconnect() {
this.cleanup();
this.registry.send('disconnectClient', {
uuid: this.server.uuid,
});
this.emit('disconnected');
}
cleanup() {
if (this.pending) {
const filename = filenameForServer(this.server);
shm.shm_unlink(filename);
this.pending = false;
}
}
onFrame(width, height) {
const buf = this.copyBufs[this.curBuf];
this.curBuf = 1 - this.curBuf;
const size = 4 * width * height;
this.lastFrame = Date.now();
this.shmBuf.copy(buf);
this.emit('frame', buf, width, height);
}
}
class SyphonRegistry extends EventEmitter {
constructor() {
super();
this.serversById = new Map();
this.clientsByServerId = new Map();
this.onExit = () => process.exit();
}
start() {
process.on('exit', this.cleanup.bind(this));
process.on('SIGINT', this.onExit);
process.on('SIGTERM', this.onExit);
const syphonBufferPath = path.resolve(__dirname, 'native/bin/SyphonBuffer');
this.ipc = child_process.spawn(syphonBufferPath);
this.ipc.stderr.on('data', data => console.error(data.toString()));
this.ipc.on('error', err => {
this.emit('error', err);
});
this.interface = readline.createInterface({
input: this.ipc.stdout,
});
this.interface.on('line', this._onLine.bind(this));
setInterval(this.checkAlive.bind(this), CLIENT_TIMEOUT * 2);
}
checkAlive() {
for (const client of this.clientsByServerId.values()) {
if (!client.checkAlive()) {
client.disconnect();
}
}
}
onUpdateServers(servers) {
const uuids = new Set(servers.map(server => server.uuid));
const previous = [...this.serversById.keys()];
const removed = previous.filter(uuid => !uuids.has(uuid));
this.serversById = new Map(servers.map(server => [server.uuid, server]));
for (const uuid of removed) {
const client = this.clientsByServerId.get(uuid);
if (!client) {
continue;
}
client.disconnect();
}
this.emit('servers-updated');
}
createClientForServer(server) {
const client = new SyphonClient(this, server);
client.on('disconnected', () => {
this.clientsByServerId.delete(server.uuid);
});
this.clientsByServerId.set(server.uuid, client);
return client;
}
onClientCreated(server) {
const client = this.clientsByServerId.get(server.uuid);
if (!client) {
return;
}
client.cleanup();
client.onConnected();
}
onFrame({server, width, height}) {
const client = this.clientsByServerId.get(server.uuid);
if (!client) {
return;
}
client.onFrame(width, height);
}
_onLine(line) {
const {command, data} = JSON.parse(line);
switch (command) {
case 'updateServers':
this.onUpdateServers(data);
break;
case 'clientCreated':
this.onClientCreated(data);
break;
case 'frame':
this.onFrame(data);
break;
default:
this.onUnknownCommand(command, data);
}
}
send(command, data) {
const output = JSON.stringify({command, data}) + '\n';
this.ipc.stdin.write(output);
}
cleanup() {
for (const client of this.clientsByServerId.values()) {
client.cleanup();
}
this.ipc.kill();
process.exit();
}
}
module.exports = {
SyphonClient,
SyphonRegistry,
};