Announce users the local peers knows to remote peers when they connect. Fix RPC system to correctly serialize and multiple params. Add detailed logging for rpc calls.
This commit is contained in:
@@ -379,7 +379,7 @@ export class PeerManager {
|
||||
|
||||
console.log.apply(null, log(`PeerManager.disconnect: disconnecting peer ${remotePeerID}`));
|
||||
await peer.disconnect();
|
||||
this.peers.delete(remotePeerID);
|
||||
this.onPeerDisconnected(remotePeerID);
|
||||
}
|
||||
|
||||
async call(peerID: string, functionName: string, args: any) {
|
||||
@@ -400,11 +400,11 @@ export class PeerManager {
|
||||
throw new Error(`callFromRemote: got RPC we don't know about: ${functionName}, ${args}`);
|
||||
}
|
||||
|
||||
return func(args);
|
||||
return func.apply(null, args);
|
||||
}
|
||||
|
||||
registerRPC(functionName: string, func: Function) {
|
||||
this.rpc[functionName] = (peerID: string, args: any) => {
|
||||
this.rpc[functionName] = (peerID: string, ...args: any) => {
|
||||
return this.call(peerID, functionName, args);
|
||||
};
|
||||
this.RPC_remote.set(functionName, func);
|
||||
@@ -450,6 +450,12 @@ class PeerConnection {
|
||||
private polite = true;
|
||||
private webRTCSuperlog = false;
|
||||
private dataChannelSuperlog = false;
|
||||
pendingRPCs: Map<
|
||||
string,
|
||||
{ resolve: Function; reject: Function; functionName: string }
|
||||
> = new Map();
|
||||
messageSuperlog: boolean = true;
|
||||
connectionPromise: { resolve: (value?: unknown) => void; reject: (reason?: any) => void; } | null = null;
|
||||
|
||||
// private makingOffer:boolean = false;
|
||||
// private ignoreOffer:boolean = false;
|
||||
@@ -467,13 +473,8 @@ class PeerConnection {
|
||||
// { urls: "stun:stun4.l.google.com" },
|
||||
],
|
||||
};
|
||||
rpcSuperlog: boolean = true;
|
||||
|
||||
pendingRPCs: Map<
|
||||
string,
|
||||
{ resolve: Function; reject: Function; functionName: string }
|
||||
> = new Map();
|
||||
messageSuperlog: boolean = false;
|
||||
connectionPromise: { resolve: (value?: unknown) => void; reject: (reason?: any) => void; } | null = null;
|
||||
|
||||
async RPCHandler(message: any) {
|
||||
}
|
||||
@@ -521,7 +522,7 @@ class PeerConnection {
|
||||
}
|
||||
|
||||
this.dataChannel.onmessage = (e: MessageEvent) => {
|
||||
this.dataChannelSuperlog && console.log.apply(null, log("->datachannel: ", e.data));
|
||||
this.messageSuperlog && console.log.apply(null, log(`[${logID(this.remotePeerID)}]->datachannel[${logID(this.peerManager.peerID)}]: `, e.data));
|
||||
this.onMessage(e.data);
|
||||
}
|
||||
|
||||
@@ -695,9 +696,11 @@ class PeerConnection {
|
||||
}
|
||||
|
||||
send(message: any) {
|
||||
this.messageSuperlog && console.log.apply(null, log("<-datachannel:", message.type, message));
|
||||
|
||||
|
||||
let messageJSON = JSON.stringify(message);
|
||||
|
||||
this.messageSuperlog && console.log.apply(null, log(`[${logID(this.remotePeerID)}]<-datachannel[${logID(this.peerManager.peerID)}]:`, message.type, message, `message size:${messageJSON.length}`));
|
||||
|
||||
this.dataChannel?.send(messageJSON);
|
||||
|
||||
// this.onMessage(messageJSON);
|
||||
@@ -718,6 +721,9 @@ class PeerConnection {
|
||||
args: args,
|
||||
};
|
||||
|
||||
this.rpcSuperlog && console.log.apply(null, log(`[${logID(this.remotePeerID)}]<-[rpc][${logID(this.peerManager.peerID)}]`,message.function_name, message.transaction_id, JSON.stringify(message.args, null, 2)));
|
||||
|
||||
|
||||
this.send(message);
|
||||
|
||||
return promise;
|
||||
@@ -732,10 +738,12 @@ class PeerConnection {
|
||||
console.log.apply(null, log("PeerConnection.onMessage:", e));
|
||||
}
|
||||
|
||||
this.messageSuperlog && console.log.apply(null, log("->", message.type, message));
|
||||
this.messageSuperlog && console.log.apply(null, log(`[${logID(this.remotePeerID)}]->datachannel[${logID(this.peerManager.peerID)}]`, message.type, message));
|
||||
let type = message.type;
|
||||
|
||||
if (type === "rpc_response") {
|
||||
this.rpcSuperlog && console.log.apply(null, log(`[${logID(this.remotePeerID)}]<-[rpc][${logID(this.peerManager.peerID)}] response: `,message.function_name, message.transaction_id, JSON.stringify(message.args, null, 2)));
|
||||
|
||||
let pendingRPC = this.pendingRPCs.get(message.transaction_id);
|
||||
|
||||
if (!pendingRPC) {
|
||||
@@ -747,10 +755,16 @@ class PeerConnection {
|
||||
|
||||
if (type === "rpc_call") {
|
||||
|
||||
this.rpcSuperlog && console.log.apply(null, log(`[${logID(this.remotePeerID)}]->[rpc][${logID(this.peerManager.peerID)}] response: `,message.function_name, message.transaction_id, JSON.stringify(message.args, null, 2)));
|
||||
|
||||
|
||||
let response = this.peerManager.callFromRemote(message.function_name, message.args);
|
||||
|
||||
let responseMessage = { type: 'rpc_response', transaction_id: message.transaction_id, response: response };
|
||||
if (response === undefined) {
|
||||
return;
|
||||
}
|
||||
|
||||
let responseMessage = { type: 'rpc_response', function_name: message.function_name, transaction_id: message.transaction_id, response: response };
|
||||
this.send(responseMessage);
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user