-
-
Notifications
You must be signed in to change notification settings - Fork 523
/
Process.ts
167 lines (128 loc) · 5.07 KB
/
Process.ts
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
import * as express from "express";
import * as msgpack from "msgpack-lite";
import * as memshared from "memshared";
import { Server as WebSocketServer } from "uws";
import { Protocol, send } from "../Protocol";
import { Client, generateId } from "../";
import { handleUpgrade, setUserId } from "../cluster/Worker";
import { debugMatchMaking } from "../Debug";
const app = new express();
const server = app.listen(0, "localhost");
let wss = new WebSocketServer({
server: server,
verifyClient: function (info, done) {
// console.log("Verify client!", info, done);
done(true);
}
});
// setInterval(() => console.log("MatchMaking connections:", wss.clients.length), 1000);
wss.on('connection', onConnect);
//
// Listen to "redirect" messages from main process, to redirect the connection
// to match-making process.
//
let callbacks: {[requestId:string]: Function} = {};
process.on('message', (message, socket) => {
if (message[0] === Protocol.PASS_WEBSOCKET) {
handleUpgrade(server, socket, message);
return;
} else if (Array.isArray(message) && callbacks[ message[0] ]) {
let callback = callbacks[ message.shift() ];
callback(...message);
return;
}
});
// Process spawned successfully!
debugMatchMaking("MatchMaking process spawned with pid %d", process.pid);
function onConnect (client: Client) {
setUserId(client);
client.on('message', (message) => {
// try to decode message received from client
try {
message = msgpack.decode(Buffer.from(message));
} catch (e) {
console.error("Couldn't decode message:", message, e.stack);
return;
}
if (message[0] !== Protocol.JOIN_ROOM) {
console.error("MatchMaking couldn't process message:", message);
return;
}
let roomName = message[1];
let joinOptions = message[2];
// has room handler avaialble?
memshared.lindex("handlers", roomName, (err, index) => {
if (index === null) {
send(client, [Protocol.JOIN_ERROR, roomName, `Error: no available handler for "${ roomName }"`]);
return;
}
// Request to join an existing sessions for requested handler
memshared.smembers(roomName, (err, availableWorkerIds) => {
//
// TODO:
// remove a room from match-making cache when it reaches maxClients.
//
joinOptions.clientId = client.id;
if (availableWorkerIds.length > 0) {
broadcastJoinRoomRequest(availableWorkerIds, client, roomName, joinOptions);
} else {
// retrieve active worker ids
requestCreateRoom(client, roomName, joinOptions);
}
});
});
});
client.on('error', (e) => {
console.error("[ERROR]", client, e);
});
}
function broadcastJoinRoomRequest (availableWorkerIds: string[], client: Client, roomName: string, joinOptions: any) {
let responsesReceived = [];
callbacks[ client.id ] = (workerId, roomId, score) => {
responsesReceived.push({
roomId: roomId,
score: score,
workerId: workerId
});
debugMatchMaking("JOIN_ROOM, receiving responses (%d/%d)", responsesReceived.length, availableWorkerIds.length);
if (responsesReceived.length === availableWorkerIds.length) {
// sort responses by score
responsesReceived.sort((a, b) => b.score - a.score);
let { workerId, roomId, score } = responsesReceived[0];
if (score === 0) {
debugMatchMaking("JOIN_ROOM, best score: %d, (options: %j)", score, joinOptions);
// highest score is 0, let's request to create a room instead of joining.
requestCreateRoom(client, roomName, joinOptions);
} else {
debugMatchMaking("JOIN_ROOM, best score: %d, (options: %j)", score, joinOptions);
// send join room request to worker id with best score
joinRoomRequest(workerId, client, roomId, joinOptions);
}
}
}
availableWorkerIds.forEach(availableWorkerId => {
// Send JOIN_ROOM command to selected worker process.
process.send([ availableWorkerId, Protocol.REQUEST_JOIN_ROOM, roomName, joinOptions ]);
});
}
function joinRoomRequest (workerId, client, roomName, joinOptions) {
// forward data received from worker process to the client
callbacks[ client.id ] = (data) => send(client, data);
// Send JOIN_ROOM command to selected worker process.
process.send([ workerId, Protocol.JOIN_ROOM, roomName, joinOptions ]);
}
function requestCreateRoom (client, roomName, joinOptions) {
// forward data received from worker process to the client
callbacks[ client.id ] = (data) => send(client, data);
memshared.lrange("workerIds", 0, -1, (err, workerIds) => {
memshared.mget(workerIds, (err, spawnedRoomCounts) => {
spawnedRoomCounts = spawnedRoomCounts.filter(count => count);
let selectedWorkerId = (spawnedRoomCounts.length > 0)
? workerIds[ spawnedRoomCounts.indexOf(Math.min(...spawnedRoomCounts)) ]
: workerIds[0];
debugMatchMaking("requesting CREATE_ROOM");
// Send CREATE_ROOM command to selected worker process.
process.send([ selectedWorkerId, Protocol.CREATE_ROOM, roomName, joinOptions ]);
});
});
}