4 read(size:number):Promise<ArrayBuffer>;
5 write(data:ArrayBufferLike):Promise<void>;
10 public constructor(public io1:Io){
13 protected async nextSid(opcode:number){
14 let sid=opcode|this.nextSid1;
18 protected running=false;
19 protected waitingSessionCb={} as {[key:number]:(e:Error|null)=>void}
20 protected respReadingCb:(e:Error|null)=>void=()=>{};
24 let sid=await this.readUint32();
25 let cb=this.waitingSessionCb[sid];
26 delete this.waitingSessionCb[sid];
27 let respReadingDone=new Promise<undefined>((resolve,reject)=>{
28 this.respReadingCb=(e)=>{
29 if(e==null){resolve(undefined)}else{reject(e)};
33 await respReadingDone;
36 public async readUint32(){
37 let buf=await this.io1.read(4);
38 return new DataView(buf).getUint32(0,true);
40 public async push(destAddr:number,data:ArrayBufferLike){
41 let hdr=new ArrayBuffer(12);
42 let hdr2=new DataView(hdr);
43 let sid=await this.nextSid(1);
44 hdr2.setUint32(0,sid,true);
45 hdr2.setUint32(4,destAddr,true);
46 hdr2.setUint32(8,data.byteLength);
47 let respFut=new Promise((resolve,reject)=>{
48 this.waitingSessionCb[sid]=(e)=>{
49 if(e==null){resolve(e);}else{reject(e)};
52 await this.io1.write(hdr);
53 await this.io1.write(data);
55 this.respReadingCb(null);
57 public async pull(srcAddr:number){
58 let hdr=new ArrayBuffer(8);
59 let hdr2=new DataView(hdr);
60 let sid=await this.nextSid(2);
61 hdr2.setUint32(0,sid,true);
62 hdr2.setUint32(4,srcAddr,true)
63 let respFut=new Promise((resolve,reject)=>{
64 this.waitingSessionCb[sid]=(e)=>{
65 if(e==null){resolve(e);}else{reject(e)};
68 await this.io1.write(hdr);
70 let size=await this.readUint32();
71 let data1=await this.io1.read(size);
72 this.respReadingCb(null);
75 public async assign(destAddr:number,srcAddr:number){
76 let hdr=new ArrayBuffer(12);
77 let hdr2=new DataView(hdr);
78 let sid=await this.nextSid(3);
79 hdr2.setUint32(0,sid,true);
80 hdr2.setUint32(4,destAddr,true);
81 hdr2.setUint32(8,srcAddr,true);
82 let respFut=new Promise((resolve,reject)=>{
83 this.waitingSessionCb[sid]=(e)=>{
84 if(e==null){resolve(e);}else{reject(e)};
87 await this.io1.write(hdr);
89 this.respReadingCb(null);
91 public async unlink(destAddr:number){
92 let hdr=new ArrayBuffer(8);
93 let hdr2=new DataView(hdr);
94 let sid=await this.nextSid(4);
95 hdr2.setUint32(0,await sid,true);
96 hdr2.setUint32(4,destAddr,true)
97 let respFut=new Promise((resolve,reject)=>{
98 this.waitingSessionCb[sid]=(e)=>{
99 if(e==null){resolve(e);}else{reject(e)};
102 await this.io1.write(hdr);
104 this.respReadingCb(null);
106 public async call(destAddr:number,fnAddr:number,args:ArrayBufferLike,sizeOfReturn:number){
107 let hdr=new ArrayBuffer(12);
108 let hdr2=new DataView(hdr);
109 let sid=await this.nextSid(5);
110 hdr2.setUint32(0,await sid,true);
111 hdr2.setUint32(4,destAddr,true)
112 hdr2.setUint32(8,fnAddr,true)
113 let respFut=new Promise((resolve,reject)=>{
114 this.waitingSessionCb[sid]=(e)=>{
115 if(e==null){resolve(e);}else{reject(e)};
118 await this.io1.write(hdr);
119 await this.io1.write(args);
121 let data1=await this.io1.read(sizeOfReturn);
122 this.respReadingCb(null);
125 public async getFunc(destAddr:number,fnNameAddr:number){
126 let hdr=new ArrayBuffer(12);
127 let hdr2=new DataView(hdr);
128 let sid=await this.nextSid(6);
129 hdr2.setUint32(0,sid,true);
130 hdr2.setUint32(4,destAddr,true);
131 hdr2.setUint32(8,fnNameAddr,true);
132 let respFut=new Promise((resolve,reject)=>{
133 this.waitingSessionCb[sid]=(e)=>{
134 if(e==null){resolve(e);}else{reject(e)};
137 await this.io1.write(hdr);
139 this.respReadingCb(null);
141 public async getInfo(){
142 let hdr=new ArrayBuffer(4);
143 let hdr2=new DataView(hdr);
144 let sid=await this.nextSid(8);
145 hdr2.setUint32(0,sid,true);
146 let respFut=new Promise((resolve,reject)=>{
147 this.waitingSessionCb[sid]=(e)=>{
148 if(e==null){resolve(e);}else{reject(e)};
151 await this.io1.write(hdr);
153 let size=await this.readUint32();
154 let data1=new TextDecoder().decode(await this.io1.read(size));
155 this.respReadingCb(null);
158 public async close(){
159 let hdr=new ArrayBuffer(4);
160 let hdr2=new DataView(hdr);
161 let sid=await this.nextSid(6);
162 hdr2.setUint32(0,sid,true);
163 await this.io1.write(hdr);