2
0
mirror of https://github.com/esiur/esiur-js.git synced 2026-03-18 15:30:38 +00:00
This commit is contained in:
2022-03-13 11:28:38 +03:00
parent 7b1b844753
commit 15eb1453e7
55 changed files with 24308 additions and 4587 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -29,12 +29,15 @@
import IResource from '../../Resource/IResource.js';
import AsyncReply from '../../Core/AsyncReply.js';
import Codec from '../../Data/Codec.js';
import Structure from '../../Data/Structure.js';
import IIPPacketAction from '../Packets//IIPPacketAction.js';
import EventTemplate from '../../Resource/Template/EventTemplate.js';
import AsyncException from '../../Core/AsyncException.js';
import ExceptionCode from '../../Core//ExceptionCode.js';
import ErrorType from '../../Core/ErrorType.js';
import {UInt8} from '../../Data/ExtendedTypes.js';
import TypedMap from '../../Data/TypedMap.js';
import PropertyValueArray from '../../Data/PropertyValueArray.js';
import PropertyValue from '../../Data/PropertyValue.js';
export default class DistributedResource extends IResource
{
@@ -74,7 +77,7 @@ export default class DistributedResource extends IResource
_serialize()
{
var props = [];
var props = new PropertyValueArray();
for (var i = 0; i < this._p.properties.length; i++)
props.push(new PropertyValue(this._p.properties[i],
@@ -112,25 +115,37 @@ export default class DistributedResource extends IResource
var self = this;
var makeFunc = function(index)
var makeFunc = function(ft)
{
var func = function () {
var argsMap = new (TypedMap.of(UInt8, Object));
if ( arguments.length == 1
&& arguments[0] instanceof Object
&& arguments[0].constructor.name == "Object")
{
var namedArgs = new Structure(arguments[0]);
return self._invokeByNamedArguments(index, namedArgs);
// named args
for (let i = 0; i < ft.args.length; i++){
let arg = ft.args[i];
if (arguments[arg.name] != undefined) {
argsMap.set(new UInt8(arg.index), arguments[arg.name]);
}
}
return self._invoke(ft.index, argsMap);
}
else
{
return self._invokeByArrayArguments(index, arguments);
for(let i = 0; (i < arguments.length) && (i < ft.args.length); i++)
argsMap.set(new UInt8(i), arguments[i]);
return self._invoke(ft.index, argsMap);
}
};
// get expansion
func.help = self.instance.template.functions[index].expansion;
func.help = self.instance.template.functions[ft.index].expansion;
return func;
};
@@ -148,15 +163,15 @@ export default class DistributedResource extends IResource
};
};
for(var i = 0; i < this.instance.template.functions.length; i++)
for(let i = 0; i < this.instance.template.functions.length; i++)
{
var ft = this.instance.template.functions[i];
this[ft.name] = makeFunc(ft.index);
let ft = this.instance.template.functions[i];
this[ft.name] = makeFunc(ft);
}
for(var i = 0; i < this.instance.template.properties.length; i++)
for(let i = 0; i < this.instance.template.properties.length; i++)
{
var pt = this.instance.template.properties[i];
let pt = this.instance.template.properties[i];
Object.defineProperty(this, pt.name, {
get: makeGetter(pt.index),
@@ -202,10 +217,10 @@ export default class DistributedResource extends IResource
//@TODO if array _emitArgs
//this._emitArgs(et.name, [args]);
this._emit(et.name, args);
this.instance._emitResourceEvent(null, null, et.name, args);
this.instance._emitResourceEvent(null, null, et, args);
}
_invokeByArrayArguments(index, args) {
_invoke(index, args) {
if (this.destroyed)
throw new Error("Trying to access destroyed object");
@@ -215,20 +230,7 @@ export default class DistributedResource extends IResource
if (index >= this.instance.template.functions.length)
throw new Error("Function index is incorrect");
return this._p.connection.sendInvokeByArrayArguments(this._p.instanceId, index, args);
}
_invokeByNamedArguments(index, namedArgs) {
if (this.destroyed)
throw new Error("Trying to access destroyed object");
if (this._p.suspended)
throw new Error("Trying to access suspended object");
if (index >= this.instance.template.functions.length)
throw new Error("Function index is incorrect");
return this._p.connection.sendInvokeByNamedArguments(this._p.instanceId, index, namedArgs);
return this._p.connection.sendInvoke(this._p.instanceId, index, args);
}
_get(index)

View File

@@ -24,9 +24,8 @@
* Created by Ahmed Zamil on 01/09/2017.
*/
"use strict";
import DC from '../Data/DataConverter.js';
import DC from '../Data/DC.js';
export default class NetworkBuffer {
constructor() {

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017 Ahmed Kh. Zamil
* Copyright (c) 2017-2022 Ahmed Kh. Zamil
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -31,6 +31,7 @@ import IIPPacketCommand from "./IIPPacketCommand.js";
import IIPPacketEvent from "./IIPPacketEvent.js";
import IIPPacketReport from "./IIPPacketReport.js";
import DataType from '../../Data/DataType.js';
import TransmissionType from '../../Data/TransmissionType.js';
export default class IIPPacket
{
@@ -42,7 +43,7 @@ export default class IIPPacket
this.resourceId = 0;
this.newResourceId = 0;
this.resourceAge = 0;
this.content = [];
//this.content = [];
this.errorCode = 0;
this.errorMessage = "";
this.className = "";
@@ -53,6 +54,8 @@ export default class IIPPacket
this.callbackId = 0;
this.dataLengthNeeded = 0;
this.originalOffset = 0;
this.resourceName = "";
this.dataType = null;
}
notEnough(offset, ends, needed)
@@ -136,14 +139,13 @@ export default class IIPPacket
if (this.notEnough(offset, ends, 2))
return -this.dataLengthNeeded;
var cl = data.getUint16(offset);
let cl = data.getUint16(offset);
offset += 2;
if (this.notEnough(offset, ends, cl))
return -this.dataLengthNeeded;
this.content = data.clip(offset, cl);
this.resourceName = data.getString(offset, cl);
offset += cl;
}
else if (this.event == IIPPacketEvent.PropertyUpdated
@@ -154,61 +156,28 @@ export default class IIPPacket
this.methodIndex = data[offset++];
var dt = data.getUint8(offset++);
var size = DataType.sizeOf(dt);
var parsed = TransmissionType.parse(data, offset, ends);
if (size < 0)
{
if (this.notEnough(offset, ends, 4))
return -this.dataLengthNeeded;
var cl = data.getUint32(offset);
offset += 4;
if (this.notEnough(offset, ends, cl))
return -this.dataLengthNeeded;
this.content = data.clip(offset - 5, cl + 5);
offset += cl;
}
else
{
if (this.notEnough(offset, ends, size))
return -this.dataLengthNeeded;
this.content = data.clip(offset - 1, size + 1);
offset += size;
}
if (parsed.type == null) return -parsed.size;
this.dataType = parsed.type;
offset += parsed.size;
}
// else if (this.event == IIPPacketEvent.EventOccurred)
// {
// if (this.notEnough(offset, ends, 5))
// return -this.dataLengthNeeded;
// this.methodIndex = data.getUint8(offset++);
// var cl = data.getUint32(offset);
// offset += 4;
// if (this.notEnough(offset, ends, cl))
// return -this.dataLengthNeeded;
// this.content = data.clip(offset, cl);
// offset += cl;
// }
// Attribute
else if (this.event == IIPPacketEvent.AttributesUpdated)
{
if (this.notEnough(offset, ends, 4))
return -this.dataLengthNeeded;
var cl = data.getUint32(offset);
let cl = data.getUint32(offset);
offset += 4;
if (this.notEnough(offset, ends, cl))
return -this.dataLengthNeeded;
this.content = data.clip(offset, cl);
//@TODO: fix this
//this.content = data.clip(offset, cl);
offset += cl;
}
@@ -246,20 +215,21 @@ export default class IIPPacket
else if (this.action == IIPPacketAction.CreateResource)
{
if (this.notEnough(offset, ends, 12))
return -dataLengthNeeded;
return -this.dataLengthNeeded;
this.storeId = data.getUint32(offset);
offset += 4;
this.resourceId = data.getUint32(offset);
offset += 4;
var cl = data.getUint32(offset);
let cl = data.getUint32(offset);
offset += 4;
if (this.notEnough(offset, ends, cl))
return -dataLengthNeeded;
return -this.dataLengthNeeded;
this.content = data.clip(offset, cl);
//@TODO: fix this
//this.content = data.clip(offset, cl);
}
else if (this.action == IIPPacketAction.DeleteResource)
{
@@ -291,13 +261,13 @@ export default class IIPPacket
this.resourceId = data.getUint32(offset);
offset += 4;
var cl = data.getUint16(offset);
let cl = data.getUint16(offset);
offset += 2;
if (this.notEnough(offset, ends, cl))
return -this.dataLengthNeeded;
this.content = data.clip(offset, cl);
this.resourceName = data.getString(offset, cl);
offset += cl;
}
@@ -306,7 +276,7 @@ export default class IIPPacket
if (this.notEnough(offset, ends, 1))
return -this.dataLengthNeeded;
var cl = data.getUint8(offset++);
let cl = data.getUint8(offset++);
if (this.notEnough(offset, ends, cl))
return -this.dataLengthNeeded;
@@ -337,7 +307,7 @@ export default class IIPPacket
if (this.notEnough(offset, ends, 2))
return -this.dataLengthNeeded;
var cl = data.getUint16(offset);
let cl = data.getUint16(offset);
offset += 2;
if (this.notEnough(offset, ends, cl))
@@ -370,8 +340,7 @@ export default class IIPPacket
offset += 8;
}
else if ( this.action == IIPPacketAction.InvokeFunctionArrayArguments
|| this.action == IIPPacketAction.InvokeFunctionNamedArguments)
else if ( this.action == IIPPacketAction.InvokeFunction )
{
if (this.notEnough(offset, ends, 9))
@@ -382,14 +351,13 @@ export default class IIPPacket
this.methodIndex = data.getUint8(offset++);
var cl = data.getUint32(offset);
offset += 4;
let parsed = TransmissionType.parse(data, offset, ends);
if (this.notEnough(offset, ends, cl))
return -this.dataLengthNeeded;
if (parsed.type == null) return -parsed.size;
this.content = data.clip(offset, cl);
offset += cl;
this.dataType = parsed.type;
offset += parsed.size;
}
else if (this.action == IIPPacketAction.Listen
@@ -428,33 +396,13 @@ export default class IIPPacket
offset += 4;
this.methodIndex = data[offset++];
let parsed = TransmissionType.parse(data, offset, ends);
var dt = data.getUint8(offset++);
var size = DataType.sizeOf(dt);
if (size < 0)
{
if (this.notEnough(offset, ends, 4))
return -this.dataLengthNeeded;
var cl = data.getUint32(offset);
offset += 4;
if (this.notEnough(offset, ends, cl))
return -this.dataLengthNeeded;
this.content = data.clip(offset-5, cl + 5);
offset += cl;
}
else
{
if (this.notEnough(offset, ends, size))
return -this.dataLengthNeeded;
this.content = data.clip(offset-1, size + 1);
offset += size;
}
if (parsed.type == null) return -parsed.size;
this.dataType = parsed.type;
offset += parsed.size;
}
// Attribute
@@ -468,13 +416,14 @@ export default class IIPPacket
this.resourceId = data.getUint32(offset);
offset += 4;
var cl = data.getUint32(offset);
let cl = data.getUint32(offset);
offset += 4;
if (this.notEnough(offset, ends, cl))
return -this.dataLengthNeeded;
this.content = data.clip(offset, cl);
// @TODO: fix this
//this.content = data.clip(offset, cl);
offset += cl;
}
@@ -493,7 +442,7 @@ export default class IIPPacket
this.resourceAge = data.getUint64(offset);
offset += 8;
var cl = data.getUint16(offset);
let cl = data.getUint16(offset);
offset+=2;
if (this.notEnough(offset, ends, cl))
@@ -502,17 +451,13 @@ export default class IIPPacket
this.resourceLink = data.getString(offset, cl);
offset += cl;
if (this.notEnough(offset, ends, 4))
return -this.dataLengthNeeded;
let parsed = TransmissionType.parse(data, offset, ends);
cl = data.getUint32(offset);
offset += 4;
if (this.notEnough(offset, ends, cl))
return -this.dataLengthNeeded;
this.content = data.clip(offset, cl);
offset += cl;
if (parsed.type == null) return -parsed.size;
this.dataType = parsed.type;
offset += parsed.size;
}
else if (this.action == IIPPacketAction.DetachResource)
{
@@ -544,52 +489,30 @@ export default class IIPPacket
|| this.action == IIPPacketAction.GetAllAttributes
|| this.action == IIPPacketAction.GetAttributes)
{
if (this.notEnough(offset, ends, 4))
return -this.dataLengthNeeded;
var cl = data.getUint32(offset);
offset += 4;
if (this.notEnough(offset, ends, 1)) return -this.dataLengthNeeded;
if (this.notEnough(offset, ends, cl))
return -this.dataLengthNeeded;
let parsed = TransmissionType.parse(data, offset, ends);
if (parsed.type == null) return -parsed.size;
this.dataType = parsed.type;
offset += parsed.size;
this.content = data.clip(offset, cl);
offset += cl;
}
else if (this.action == IIPPacketAction.InvokeFunctionArrayArguments
|| this.action == IIPPacketAction.InvokeFunctionNamedArguments)
//|| this.action == IIPPacketAction.GetProperty
//|| this.action == IIPPacketAction.GetPropertyIfModified)
else if (this.action == IIPPacketAction.InvokeFunction)
{
if (this.notEnough(offset, ends, 1))
return -this.dataLengthNeeded;
var dt = data.getUint8(offset++);
var size = DataType.sizeOf(dt);
let parsed = TransmissionType.parse(data, offset, ends);
if (size < 0)
{
if (this.notEnough(offset, ends, 4))
return -this.dataLengthNeeded;
var cl = data.getUint32(offset);
offset += 4;
if (this.notEnough(offset, ends, cl))
return -this.dataLengthNeeded;
this.content = data.clip(offset - 5, cl + 5);
offset += cl;
}
else
{
if (this.notEnough(offset, ends, size))
return -this.dataLengthNeeded;
this.content = data.clip(offset - 1, size + 1);
offset += size;
}
if (parsed.type == null) return -parsed.size;
this.dataType = parsed.type;
offset += parsed.size;
}
else if (this.action == IIPPacketAction.SetProperty
|| this.action == IIPPacketAction.Listen
@@ -619,7 +542,7 @@ export default class IIPPacket
if (this.notEnough(offset, ends, 2))
return -this.dataLengthNeeded;
var cl = data.getUint16(offset);
let cl = data.getUint16(offset);
offset += 2;
if (this.notEnough(offset, ends, cl))
@@ -640,31 +563,16 @@ export default class IIPPacket
}
else if (this.report == IIPPacketReport.ChunkStream)
{
var dt = data.getUint8(offset++);
var size = DataType.sizeOf(dt);
if (this.notEnough(offset, ends, 1))
return -this.dataLengthNeeded;
if (size < 0)
{
if (this.notEnough(offset, ends, 4))
return -this.dataLengthNeeded;
var cl = data.getUint32(offset);
offset += 4;
if (this.notEnough(offset, ends, cl))
return -this.dataLengthNeeded;
this.content = data.clip(offset - 5, cl + 5);
offset += cl;
}
else
{
if (this.notEnough(offset, ends, size))
return -this.dataLengthNeeded;
this.content = data.clip(offset - 1, size + 1);
offset += size;
}
let parsed = TransmissionType.parse(data, offset, ends);
if (parsed.type == null) return -parsed.size;
this.dataType = parsed.type;
offset += parsed.size;
}
}

View File

@@ -21,8 +21,8 @@ export default // const IIPPacketAction =
LinkTemplates: 15,
// Request Invoke
InvokeFunctionArrayArguments: 16,
InvokeFunctionNamedArguments: 17,
InvokeFunction: 16,
Reserved: 17,
Listen: 18,
Unlisten: 19,
SetProperty: 20,

View File

@@ -4,22 +4,18 @@ import ExceptionCode from "../../Core/ExceptionCode.js";
import ISocket from "./ISocket.js";
import SocketState from "./SocketState.js";
import NetworkBuffer from "../NetworkBuffer.js";
export default class WSocket extends ISocket
{
//SocketState State { get; }
//INetworkReceiver<ISocket> Receiver { get; set; }
constructor(websocket){
super();
this.receiveNetworkBuffer = new NetworkBuffer();
this.sendNetworkBuffer = new NetworkBuffer();
this.held = false;
if (websocket != null)// instanceof WebSocket)
if (websocket != null)
{
//websocket.onerror = () => {
// self.state = SocketState.Closed;
//};
websocket.onopen = () => {
self.state = SocketState.Established;
};
@@ -47,6 +43,8 @@ export default class WSocket extends ISocket
sendAll(message)
{
// console.log("Out ", message.byteLength);
if (this.held)
this.sendNetworkBuffer.writeAll(message);
else
@@ -68,31 +66,53 @@ export default class WSocket extends ISocket
this.ws.close();
}
static webSocket = null;
static async getWebScoket(){
if (WSocket.webSocket == null) {
if (typeof window === 'undefined') {
const wsModule = await import('ws');
WSocket.webSocket = wsModule.default;
}
else
{
WSocket.webSocket = WebSocket;
}
}
return WSocket.webSocket;
}
connect(hostname, port, secure = false) {
let self = this;
var rt = new AsyncReply();
this.state = SocketState.Connecting;
this.url = `ws${secure ? 's' : ''}://${hostname}:${port}`;
let ws = new WebSocket(this.url, "iip");
ws.binaryType = "arraybuffer";
WSocket.getWebScoket().then(webSocket =>
{
ws.onopen = () => {
self.state = SocketState.Established;
rt.trigger(true);
};
let ws;
ws.onerror = () => {
self.state = SocketState.Closed;
rt.triggerError(ErrorType.Management, ExceptionCode.HostNotReachable);
};
ws = new webSocket(this.url, "iip");
ws.binaryType = "arraybuffer";
ws.onopen = () => {
self.state = SocketState.Established;
rt.trigger(true);
};
ws.onerror = () => {
self.state = SocketState.Closed;
rt.triggerError(ErrorType.Management, ExceptionCode.HostNotReachable);
};
self._assign(ws);
});
this._assign(ws);
return rt;// new AsyncReply(true);
}
@@ -106,6 +126,7 @@ export default class WSocket extends ISocket
};
ws.onmessage = function(msg) {
//console.log("WREC ", msg.data.byteLength);
self.receiveNetworkBuffer.writeAll(msg.data);
self.receiver.networkReceive(this, self.receiveNetworkBuffer);
//self.lastAction = new Date();
@@ -155,11 +176,4 @@ export default class WSocket extends ISocket
this.state = SocketState.Closed;
}
}
}
// if (this.holdSending) {
// //console.log("hold ", data.length);
// this.sendBuffer.writeAll(data);
// }
// else
// //console.log("Send", data.length);
}