MWSE/Source/IPC.js

246 lines
5.8 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

process.on('message',data => {
const { Client } = require("./Client.js");
const { Room } = require("./Services/Room");
switch(data.type)
{
case "CLIENT_CREATED":{
slog("CLIENT_CREATED");
let client = new Client();
client.isProxy = true;
client.proxyProcess = data.pid;
client.id = data.uuid;
if(Client.clients.has(client.id))
{
console.error("IPC: Zaten var olan kullanıcı oluşturuluyor")
}else{
Client.clients.set(client.id, client);
}
break;
}
case "CLIENT_UPDATE_PROP":{
data.value = transformDeserialization(data.value, data.typing);
slog("CLIENT_UPDATE_PROP");
let client = Client.clients.get(data.uuid);
client[data.name] = data.value;
break;
}
case "CLIENT_SEND_MESSAGE":{
//slog("CLIENT_SEND_MESSAGE");
let client = Client.clients.get(data.uuid);
if(client)
{
if(client.isProxy != true)
{
client.send(data.message)
}else{
console.error("IPC: Proxy olmayan bir client için IPC mesajı alındı")
}
}else{
console.error("IPC: Olmayan bir kullanıcı için mesaj gönderiliyor")
}
break;
}
case "CLIENT_DESTROY":{
slog("CLIENT_DESTROY");
if(Client.clients.has(data.uuid))
{
Client.clients.delete(data.uuid);
}else{
console.error("IPC: Olmayan bir kullanıcı için silme gerçekleştiriliyor")
}
break;
}
case "ROOM_CREATED":{
slog("ROOM_CREATED");
let room = Room.fromJSON(data.value);
Room.rooms.set(room.id, room);
break;
}
case "ROOM_UPDATE_PROP":{
data.value = transformDeserialization(data.value, data.typing);
slog("ROOM_UPDATE_PROP");
let room = Room.rooms.get(data.uuid);
room[data.name] = data.value;
break;
}
case "ROOM_JOIN_CLIENT":{
slog("ROOM_JOIN_CLIENT");
let room = Room.rooms.get(data.uuid);
let client = Client.clients.get(data.client);
if(room && client)
{
client.rooms.add(room.id);
room.clients.set(client.id, client);
}
break;
}
case "ROOM_EJECT_CLIENT":{
slog("ROOM_EJECT_CLIENT");
let room = Room.rooms.get(data.uuid);
let client = Client.clients.get(data.client);
if(room && client)
{
client.rooms.delete(room.id);
room.clients.delete(client.id, client);
}
break;
}
case "ROOM_DESTROY":{
slog("ROOM_DESTROY");
Room.rooms.delete(data.value);
break;
}
}
});
function CLIENT_CREATED(uuid)
{
mlog("CLIENT_CREATED");
process.send({
type:'CLIENT_CREATED',
uuid: uuid
})
};
function CLIENT_UPDATE_PROP(uuid, name, value)
{
mlog("CLIENT_UPDATE_PROP");
let typing = value.__proto__.constructor.name;
value = transformSerialization(value);
process.send({
type:'CLIENT_UPDATE_PROP',
uuid: uuid,
name,
value,
typing
})
};
function CLIENT_SEND_MESSAGE(uuid, message, clusterPid)
{
mlog("CLIENT_SEND_MESSAGE");
process.send({
type:'CLIENT_SEND_MESSAGE',
uuid: uuid,
message,
process:clusterPid
})
};
function CLIENT_DESTROY(uuid)
{
mlog("CLIENT_DESTROY");
process.send({
type:'CLIENT_DESTROY',
uuid: uuid
})
};
function ROOM_CREATED(room)
{
mlog("ROOM_CREATED");
let raw = room.toJSON(true);
process.send({
type:'ROOM_CREATED',
value: raw
})
};
function ROOM_UPDATE_PROP(uuid, name, value)
{
mlog("ROOM_UPDATE_PROP");
let typing = value.__proto__.constructor.name;
value = transformSerialization(value);
process.send({
type:'ROOM_UPDATE_PROP',
uuid: uuid,
name,
value,
typing
})
};
function ROOM_JOIN_CLIENT(uuid, client)
{
mlog("ROOM_JOIN_CLIENT");
process.send({
type:'ROOM_JOIN_CLIENT',
uuid: uuid,
client
})
};
function ROOM_EJECT_CLIENT(uuid, client)
{
mlog("ROOM_EJECT_CLIENT");
process.send({
type:'ROOM_EJECT_CLIENT',
uuid: uuid,
client
})
};
function ROOM_DESTROY(room)
{
mlog("ROOM_DESTROY");
process.send({
type:'ROOM_DESTROY',
value: room.id
})
};
function mlog(command)
{
return;
console.log("M",process.pid, command)
}
function slog(command)
{
return;
console.log("S",process.pid, command)
}
function transformSerialization(value)
{
switch(value.__proto__.constructor.name)
{
case "Map":{
return [...value];
}
case "Set":{
return [...value];
}
}
}
function transformDeserialization(value,type)
{
switch(type)
{
case "Map":{
return new Map(value);
}
case "Set":{
return new Set(value)
}
}
}
exports.CLIENT_CREATED = CLIENT_CREATED;
exports.CLIENT_UPDATE_PROP = CLIENT_UPDATE_PROP;
exports.CLIENT_DESTROY = CLIENT_DESTROY;
exports.CLIENT_SEND_MESSAGE = CLIENT_SEND_MESSAGE;
exports.ROOM_CREATED = ROOM_CREATED;
exports.ROOM_UPDATE_PROP = ROOM_UPDATE_PROP;
exports.ROOM_JOIN_CLIENT = ROOM_JOIN_CLIENT;
exports.ROOM_EJECT_CLIENT = ROOM_EJECT_CLIENT;
exports.ROOM_DESTROY = ROOM_DESTROY;
exports.mlog = mlog;
exports.slog = slog;