Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision

Target

Select target project
  • beyondaka/beyond-protocol-js
1 result
Select Git revision
Show changes
Commits on Source (2)
...@@ -45,6 +45,7 @@ export class Peer { ...@@ -45,6 +45,7 @@ export class Peer {
callbacks = {}; callbacks = {};
cbid = 0; cbid = 0;
server = false; server = false;
sendCount = 0;
latency = 0; latency = 0;
...@@ -142,6 +143,17 @@ export class Peer { ...@@ -142,6 +143,17 @@ export class Peer {
this.send("__handshake__", kMagic, kVersion, [my_uuid]); this.send("__handshake__", kMagic, kVersion, [my_uuid]);
} }
isBuffering(): boolean {
return this.sendCount > 0;
}
private _send(msg: Buffer | ReturnType<typeof encode>) {
++this.sendCount;
this.sock.send(msg, () => {
--this.sendCount;
});
}
private _handshake(magic: number, version: number, id: Buffer[]) { private _handshake(magic: number, version: number, id: Buffer[]) {
if (magic == kMagic) { if (magic == kMagic) {
this.status = kConnected; this.status = kConnected;
...@@ -175,25 +187,25 @@ export class Peer { ...@@ -175,25 +187,25 @@ export class Peer {
const res = this.bindings[name].apply(this, args); const res = this.bindings[name].apply(this, args);
if (res instanceof Promise) { if (res instanceof Promise) {
res.then(r => { res.then(r => {
this.sock.send(encode([1,id,null,r])); this._send(encode([1,id,null,r]));
}); });
} else { } else {
this.sock.send(encode([1,id,null,res])); this._send(encode([1,id,null,res]));
} }
} catch(e) { } catch(e) {
// console.error("Could to dispatch or return call", e); // console.error("Could to dispatch or return call", e);
// this.close(); // this.close();
this.sock.send(encode([1,id,e.toString(),null])); this._send(encode([1,id,e.toString(),null]));
} }
} else if (name in this.proxies) { } else if (name in this.proxies) {
//console.log("Proxy for:", name, id); //console.log("Proxy for:", name, id);
args.unshift((res: unknown) => { args.unshift((res: unknown) => {
try { try {
this.sock.send(encode([1,id,null,res])); this._send(encode([1,id,null,res]));
} catch(e) { } catch(e) {
// console.log("ERROR") // console.log("ERROR")
// this.close(); // this.close();
this.sock.send(encode([1,id,e.toString(),null])); this._send(encode([1,id,e.toString(),null]));
} }
}); });
this.proxies[name].apply(this, args); this.proxies[name].apply(this, args);
...@@ -264,7 +276,7 @@ export class Peer { ...@@ -264,7 +276,7 @@ export class Peer {
this.callbacks[id] = (r) => resolve(r); this.callbacks[id] = (r) => resolve(r);
try { try {
this.sock.send(encode([0, id, name, args])); this._send(encode([0, id, name, args]));
} catch(e) { } catch(e) {
this.close(); this.close();
reject(); reject();
...@@ -280,7 +292,7 @@ export class Peer { ...@@ -280,7 +292,7 @@ export class Peer {
*/ */
send(name: string, ...args: unknown[]) { send(name: string, ...args: unknown[]) {
try { try {
this.sock.send(encode([0, name, args])); this._send(encode([0, name, args]));
} catch(e) { } catch(e) {
this.close(); this.close();
} }
...@@ -288,7 +300,7 @@ export class Peer { ...@@ -288,7 +300,7 @@ export class Peer {
sendB(name: string, args: unknown[]) { sendB(name: string, args: unknown[]) {
try { try {
this.sock.send(encode([0, name, args])); this._send(encode([0, name, args]));
} catch(e) { } catch(e) {
this.close(); this.close();
} }
......