*Refactor RPCConnection to fix design issues
This commit is contained in:
parent
830aa19a4f
commit
46fdc78082
31
src/rpc.ts
31
src/rpc.ts
|
@ -255,21 +255,19 @@ export async function start() {
|
|||
|
||||
class RPCConnection {
|
||||
private _socket: any;
|
||||
private _process = false;
|
||||
constructor(socket: any) {
|
||||
this._socket = socket;
|
||||
socket.rawStream._ondestroy = () => false;
|
||||
socket.once("data", this.checkRpc.bind(this));
|
||||
}
|
||||
|
||||
let isRpc = false;
|
||||
socket.once("data", async (data: any) => {
|
||||
if (data === "rpc") {
|
||||
isRpc = true;
|
||||
private async checkRpc(data: Buffer) {
|
||||
if (data.toString() === "rpc") {
|
||||
this._socket.once("data", this.processRequest);
|
||||
}
|
||||
});
|
||||
socket.once("data", async (data: any) => {
|
||||
if (!isRpc) {
|
||||
return;
|
||||
}
|
||||
|
||||
private async processRequest(data: Buffer) {
|
||||
let request: RPCRequest;
|
||||
try {
|
||||
request = unpack(data) as RPCRequest;
|
||||
|
@ -277,13 +275,18 @@ class RPCConnection {
|
|||
return;
|
||||
}
|
||||
|
||||
const that = this as any;
|
||||
|
||||
try {
|
||||
socket.write(pack(await maybeProcessRequest(request)));
|
||||
that.write(pack(await maybeProcessRequest(request)));
|
||||
} catch (error) {
|
||||
console.trace(error);
|
||||
socket.write(pack({ error }));
|
||||
that.write(pack({ error }));
|
||||
}
|
||||
that.end();
|
||||
}
|
||||
|
||||
public static handleRequest(socket: any) {
|
||||
new RPCConnection(socket);
|
||||
}
|
||||
socket.end();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue