...
This commit is contained in:
parent
510e0a108d
commit
6647365a80
@ -22,8 +22,9 @@ async function main() {
|
||||
|
||||
console.log(`call result ts->go Div = ${await remoteApi.Div(10, 2)}`);
|
||||
|
||||
const data1 = new Uint8Array().fill(0b10101010, 0, 100);
|
||||
const data2 = new Uint8Array().fill(0b11110000, 0, 100);
|
||||
// todo check empty array
|
||||
const data1 = new Uint8Array(100).fill(0b10101010, 0, 100);
|
||||
const data2 = new Uint8Array(100).fill(0b11110000, 0, 100);
|
||||
console.log(`call result ts->go XorData = ${await remoteApi.XorData(data1, data2)}`);
|
||||
|
||||
await ipc.wait();
|
||||
|
||||
@ -6,6 +6,7 @@ import (
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"os/exec"
|
||||
@ -66,7 +67,7 @@ func (ipc *ipcCommon) readConn() {
|
||||
for scn.Scan() {
|
||||
var msg Message
|
||||
a := scn.Bytes()
|
||||
_ = a
|
||||
log.Println(string(a))
|
||||
if err := json.Unmarshal(scn.Bytes(), &msg); err != nil {
|
||||
ipc.raiseErr(fmt.Errorf("unmarshal message: %w", err))
|
||||
break
|
||||
|
||||
@ -9,6 +9,8 @@ import {AsyncQueue} from './asyncqueue.js';
|
||||
|
||||
const IPC_SOCKET_ARG = 'ipc-socket';
|
||||
|
||||
type JSONSerializable = string | number | boolean;
|
||||
|
||||
enum MsgType {
|
||||
Call = 1,
|
||||
Response = 2,
|
||||
@ -169,7 +171,7 @@ abstract class IPCCommon {
|
||||
callback({result: msg.result || [], error: err});
|
||||
}
|
||||
|
||||
call(method: string, ...params: Vals): Promise<Vals> {
|
||||
call(method: string, ...args: Vals): Promise<Vals> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const id = this.nextId++;
|
||||
|
||||
@ -181,7 +183,7 @@ abstract class IPCCommon {
|
||||
}
|
||||
};
|
||||
try {
|
||||
this.sendMsg({type: MsgType.Call, id, method, args: params});
|
||||
this.sendMsg({type: MsgType.Call, id, method, args: args.map(this.convType)});
|
||||
} catch (e) {
|
||||
delete this.pendingCalls[id];
|
||||
reject(new Error(`send call: ${ e }`));
|
||||
@ -189,6 +191,23 @@ abstract class IPCCommon {
|
||||
});
|
||||
}
|
||||
|
||||
private convType(arg: any): JSONSerializable {
|
||||
// noinspection FallThroughInSwitchStatementJS
|
||||
switch (typeof arg) {
|
||||
case 'string':
|
||||
case 'boolean':
|
||||
case 'number':
|
||||
return arg;
|
||||
// @ts-expect-error TS7029
|
||||
case 'object':
|
||||
if(arg instanceof Uint8Array) {
|
||||
return Buffer.from(arg).toString('base64');
|
||||
}
|
||||
default:
|
||||
throw new Error(`arg type ${typeof arg} is not supported`);
|
||||
}
|
||||
}
|
||||
|
||||
stop() {
|
||||
if (this.stopRequested) {
|
||||
throw new Error('close already requested');
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user