typescript backend
[PxpRpc.git] / typescript / pxprpc / base.ts
blobc7b7a30bcd787fa806b473096c124380a67c0681
3 export interface Io{
4     read(size:number):Promise<ArrayBuffer>;
5     write(data:ArrayBufferLike):Promise<void>;
8 export class Client{
9     protected nextSid1=0
10     public constructor(public io1:Io){
11         this.nextSid1=0;
12     }
13     protected async nextSid(opcode:number){
14         let sid=opcode|this.nextSid1;
15         this.nextSid1+=0x100;
16         return sid;
17     }
18     protected running=false;
19     protected waitingSessionCb={} as {[key:number]:(e:Error|null)=>void}
20     protected respReadingCb:(e:Error|null)=>void=()=>{};
21     public async run(){
22         this.running=true;
23         while(this.running){
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)};
30                 }
31             });
32             cb(null);
33             await respReadingDone;
34         }
35     }
36     public async readUint32(){
37         let buf=await this.io1.read(4);
38         return new DataView(buf).getUint32(0,true);
39     }
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)};
50             }
51         });
52         await this.io1.write(hdr);
53         await this.io1.write(data);
54         await respFut;
55         this.respReadingCb(null);
56     }
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)};
66             }
67         });
68         await this.io1.write(hdr);
69         await respFut;
70         let size=await this.readUint32();
71         let data1=await this.io1.read(size);
72         this.respReadingCb(null);
73         return data1;
74     }
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)};
85             }
86         });
87         await this.io1.write(hdr);
88         await respFut;
89         this.respReadingCb(null);
90     }
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)};
100             }
101         });
102         await this.io1.write(hdr);
103         await respFut;
104         this.respReadingCb(null);
105     }
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)};
116             }
117         });
118         await this.io1.write(hdr);
119         await this.io1.write(args);
120         await respFut;
121         let data1=await this.io1.read(sizeOfReturn);
122         this.respReadingCb(null);
123         return data1
124     }
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)};
135             }
136         });
137         await this.io1.write(hdr);
138         await respFut;
139         this.respReadingCb(null);
140     }
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)};
149             }
150         });
151         await this.io1.write(hdr);
152         await respFut;
153         let size=await this.readUint32();
154         let data1=new TextDecoder().decode(await this.io1.read(size));
155         this.respReadingCb(null);
156         return data1;
157     }
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);
164         this.running=false;
165     }