Skip to content

Commit

Permalink
jsclient: sync extension folder with examples (lesismal#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
neomantra committed Oct 5, 2023
1 parent 802970d commit 83b5564
Showing 1 changed file with 51 additions and 44 deletions.
95 changes: 51 additions & 44 deletions extension/jsclient/arpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ var _CmdNone = 0;
var _CmdRequest = 1;
var _CmdResponse = 2;
var _CmdNotify = 3;
var _CmdPing = 4;
var _CmdPong = 5;

var _HeaderIndexBodyLenBegin = 0;
var _HeaderIndexBodyLenEnd = 4;
Expand Down Expand Up @@ -79,17 +81,21 @@ function ArpcClient(url, codec, httpUrl, httpMethod) {
}
this.call = function (method, request, timeout, cb, isHttp) {
if (this.state == _SOCK_STATE_CLOSED) {
if (typeof (cb) == 'function') {
cb({ data: null, err: _ErrClosed });
}
return new Promise(function (resolve, reject) {
resolve({ data: null, err: _ErrClosed });
});
}
if (this.state == _SOCK_STATE_CONNECTING) {
if (typeof (cb) == 'function') {
cb({ data: null, err: _ErrReconnecting });
}
return new Promise(function (resolve, reject) {
resolve({ data: null, err: _ErrReconnecting });
});
}
this.seqNum++;
var seq = this.seqNum;
var session = {};
var p = new Promise(function (resolve, reject) {
session.resolve = resolve;
Expand All @@ -105,41 +111,7 @@ function ArpcClient(url, codec, httpUrl, httpMethod) {
session.resolve({ data: null, err: "timeout" });
}, timeout);
}

var buffer;
if (request) {
var data = this.codec.Marshal(request);
if (data) {
buffer = new Uint8Array(16 + method.length + data.length);
for (var i = 0; i < data.length; i++) {
buffer[16 + method.length + i] = data[i];
}
}
} else {
buffer = new Uint8Array(16 + method.length);
}
var bodyLen = buffer.length - 16;
for (var i = _HeaderIndexBodyLenBegin; i < _HeaderIndexBodyLenEnd; i++) {
buffer[i] = (bodyLen >> ((i - _HeaderIndexBodyLenBegin) * 8)) & 0xFF;
}

buffer[_HeaderIndexCmd] = _CmdRequest & 0xFF;
buffer[_HeaderIndexMethodLen] = method.length & 0xFF;
for (var i = _HeaderIndexSeqBegin; i < _HeaderIndexSeqBegin + 4; i++) {
buffer[i] = (seq >> ((i - _HeaderIndexSeqBegin) * 8)) & 0xFF;
}

var methodBuffer = new TextEncoder("utf-8").encode(method);
for (var i = 0; i < methodBuffer.length; i++) {
buffer[16 + i] = methodBuffer[i];
}

if (!isHttp) {
this.ws.send(buffer);
} else {
this.request(buffer, this._onMessage);
}

this.write(_CmdRequest, method, request, this.seqNum, this._onMessage, isHttp);
return p;
}

Expand All @@ -153,10 +125,37 @@ function ArpcClient(url, codec, httpUrl, httpMethod) {
if (this.state == _SOCK_STATE_CONNECTING) {
return _ErrReconnecting;
}
this.seqNum++;
this.write(_CmdNotify, method, notify, function () { }, isHttp);
}
this.ping = function () {
if (client.state == _SOCK_STATE_CLOSED) {
return _ErrClosed;
}
if (client.state == _SOCK_STATE_CONNECTING) {
return _ErrReconnecting;
}
client.write(_CmdPing, "", null, function () { });
}
this.pong = function () {
if (client.state == _SOCK_STATE_CLOSED) {
return _ErrClosed;
}
if (client.state == _SOCK_STATE_CONNECTING) {
return _ErrReconnecting;
}
client.write(_CmdPong, "", null, function () { });
}
this.keepalive = function (timeout) {
if (this._keepaliveInited) return;
this._keepaliveInited = true;
if (!timeout) timeout = 1000 * 30;
setInterval(this.ping, timeout);
}

this.write = function (cmd, method, arg, cb, isHttp) {
var buffer;
if (notify) {
var data = this.codec.Marshal(notify);
if (arg) {
var data = this.codec.Marshal(arg);
if (data) {
buffer = new Uint8Array(16 + method.length + data.length);
for (var i = 0; i < data.length; i++) {
Expand All @@ -166,25 +165,25 @@ function ArpcClient(url, codec, httpUrl, httpMethod) {
} else {
buffer = new Uint8Array(16 + method.length);
}

var bodyLen = buffer.length - 16;
for (var i = _HeaderIndexBodyLenBegin; i < _HeaderIndexBodyLenEnd; i++) {
buffer[i] = (bodyLen >> ((i - _HeaderIndexBodyLenBegin) * 8)) & 0xFF;
}
buffer[_HeaderIndexCmd] = _CmdNotify & 0xFF;
buffer[_HeaderIndexCmd] = cmd & 0xFF;
buffer[_HeaderIndexMethodLen] = method.length & 0xFF;
this.seqNum++;
for (var i = _HeaderIndexSeqBegin; i < _HeaderIndexSeqBegin + 4; i++) {
buffer[i] = (this.seqNum >> ((i - _HeaderIndexSeqBegin) * 8)) & 0xFF;
}

var methodBuffer = new TextEncoder("utf-8").encode(method);
for (var i = 0; i < methodBuffer.length; i++) {
buffer[16 + i] = methodBuffer[i];
}

if (!isHttp) {
this.ws.send(buffer);
} else {
this.request(buffer, function () { });
this.request(buffer, cb);
}
}

Expand Down Expand Up @@ -244,6 +243,14 @@ function ArpcClient(url, codec, httpUrl, httpMethod) {
seq |= headArr[i] << (i - offset - _HeaderIndexSeqBegin);
}

switch (cmd) {
case _CmdPing:
client.pong();
return;
case _CmdPong:
return;
}

if (methodLen == 0) {
console.log("[ArpcClient] onMessage: invalid request message with 0 method length, dropped");
return
Expand Down

0 comments on commit 83b5564

Please sign in to comment.