Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | 1x 1x 1x 1x 8x 8x 8x 4x 4x 4x 1x 4x 3x 3x 2x 1x 4x 1x 5x 1x 1x 4x 1x 1x 3x 3x 8x 8x 8x 6x 6x 1x 1x 5x 4x 1x 4x 1x 1x | import {Transport} from './Transport';
import {
ParseRPCMessage,
RPCMessage,
RPCRequest,
RPCResponse,
RPCResponseResult,
RPCResponseError
} from './Message';
import { JSONRPC_TIMEOUT, RPCMethodError } from './Defines';
import { clearTimeout } from 'timers';
import { EventEmitter } from 'events';
interface RequestMap
{
[index: number]: (result: RPCResponse) => void;
}
// Does not contain transport
abstract class RPCClientBase extends EventEmitter
{
// Request id autoincrement
requestId: number = 0;
// Holds all pending requests
requests: RequestMap = {};
// Time in ms to wait for server response
requestTimeout: number = JSONRPC_TIMEOUT;
// Calls remote RPC function
call(name: string, ...params: any[]): Promise<any>
{
let id = this.requestId++;
return new Promise((resolve, reject) => {
// Set timeout
let timer = setTimeout(() => {
reject(new Error('Request timed out'));
}, this.requestTimeout);
this.requests[id] = (result: RPCResponse) => {
// Response received, clear timeout
clearTimeout(timer);
if (result instanceof RPCResponseResult)
resolve(result.result);
else
reject(new RPCMethodError(result.error.code, result.error.message, result.error.data));
};
this.send(new RPCRequest(id, name, params));
});
}
notify(name: string, ...params: any[])
{
this.send(new RPCRequest(undefined, name, params));
}
handleResponse(res: RPCResponse)
{
if (typeof res.id !== "number")
{
this.emit('error', new Error(`Response id is not a number`));
return;
}
if (!this.requests[res.id])
{
this.emit('error', new Error(`Request with id ${res.id} not found`));
return;
}
// Resolve promise
this.requests[res.id](res);
// Remove request
delete this.requests[res.id];
}
abstract send(msg: RPCMessage): void;
}
class RPCClient extends RPCClientBase
{
// Handles communications
private transport: Transport;
constructor(transport: Transport) {
super();
this.transport = transport;
this.transport.SetDownstreamCb((data: string) => this.parseMessage(data));
}
// Parses received string and handles as request or response
parseMessage(data: string)
{
try {
var message = ParseRPCMessage(data);
} catch (e) {
this.emit('error', new Error(`Message parse failed: ${e.message}`));
return;
}
if (message.isResponse())
this.handleResponse(<RPCResponse>message);
else
this.emit('error', new Error('Received message of non RPCResponse type'));
}
send(msg: RPCMessage): void
{
this.transport.SendUpstream(JSON.stringify(msg));
}
}
export {
RequestMap,
RPCClientBase,
RPCClient
};
|