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