mirror of
https://github.com/esiur/esiur-js.git
synced 2026-03-18 15:30:38 +00:00
ES6 modules
This commit is contained in:
1998
src/Net/IIP/DistributedConnection.js
Normal file
1998
src/Net/IIP/DistributedConnection.js
Normal file
File diff suppressed because it is too large
Load Diff
43
src/Net/IIP/DistributedPropertyContext.js
Normal file
43
src/Net/IIP/DistributedPropertyContext.js
Normal file
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright (c) 2017-2018 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
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Created by Ahmed Zamil on 27/10/2018.
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
|
||||
export default class DistributedPropertyContext
|
||||
{
|
||||
constructor(p1, p2)
|
||||
{
|
||||
if(arguments.length == 1)
|
||||
{
|
||||
this.method = p1;
|
||||
}
|
||||
else if (arguments.length == 2)
|
||||
{
|
||||
this.connection = p1;
|
||||
this.value = p2;
|
||||
}
|
||||
}
|
||||
}
|
||||
208
src/Net/IIP/DistributedResource.js
Normal file
208
src/Net/IIP/DistributedResource.js
Normal file
@@ -0,0 +1,208 @@
|
||||
/*
|
||||
* Copyright (c) 2017 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
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Created by Ahmed Zamil on 25/07/2017.
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
|
||||
import IResource from '../../Resource/IResource.js';
|
||||
import AsyncReply from '../../Engine/AsyncReply.js';
|
||||
import Codec from '../../Data/Codec.js';
|
||||
|
||||
export default class DistributedResource extends IResource
|
||||
{
|
||||
destroy()
|
||||
{
|
||||
this.destroyed = true;
|
||||
this._emit("destroy", this);
|
||||
}
|
||||
|
||||
constructor(connection, instanceId, age, link)
|
||||
{
|
||||
super();
|
||||
|
||||
this._p = {
|
||||
isAttached: false,
|
||||
connection: connection,
|
||||
instanceId: instanceId,
|
||||
age: age,
|
||||
link: link,
|
||||
properties: []
|
||||
};
|
||||
}
|
||||
|
||||
_serialize()
|
||||
{
|
||||
var props = [];
|
||||
|
||||
for (var i = 0; i < this._p.properties.length; i++)
|
||||
props.push(new PropertyValue(this._p.properties[i],
|
||||
this.instance.getAge(i),
|
||||
this.instance.getModificationDate(i)));
|
||||
|
||||
return props;
|
||||
}
|
||||
|
||||
_attached(properties)
|
||||
{
|
||||
|
||||
if (this._isAttached)
|
||||
return false;
|
||||
else
|
||||
{
|
||||
for(var i = 0; i < properties.length; i++)
|
||||
{
|
||||
this.instance.setAge(i, properties[i].age);
|
||||
this.instance.setModificationDate(i, properties[i].date);
|
||||
this._p.properties.push(properties[i].value);
|
||||
}
|
||||
|
||||
|
||||
this._p.isAttached = true;
|
||||
|
||||
var self = this;
|
||||
|
||||
var makeFunc = function(index)
|
||||
{
|
||||
var func = function () {
|
||||
|
||||
if ( arguments.length = 1
|
||||
&& arguments[0] instanceof Object
|
||||
&& arguments[0].constructor.name == "Object")
|
||||
{
|
||||
var namedArgs = new Structure(arguments[0]);
|
||||
return self._invokeByNamedArguments(index, namedArgs);
|
||||
}
|
||||
else
|
||||
{
|
||||
return self._invokeByArrayArguments(index, arguments);
|
||||
}
|
||||
};
|
||||
|
||||
// get expansion
|
||||
func.help = self.instance.template.functions[index].expansion;
|
||||
return func;
|
||||
};
|
||||
|
||||
var makeGetter = function(index)
|
||||
{
|
||||
return function () {
|
||||
return self._get(index);
|
||||
};
|
||||
};
|
||||
|
||||
var makeSetter = function(index)
|
||||
{
|
||||
return function (value) {
|
||||
self._set(index, value);
|
||||
};
|
||||
};
|
||||
|
||||
for(var i = 0; i < this.instance.template.functions.length; i++)
|
||||
{
|
||||
var ft = this.instance.template.functions[i];
|
||||
this[ft.name] = makeFunc(ft.index);
|
||||
}
|
||||
|
||||
for(var i = 0; i < this.instance.template.properties.length; i++)
|
||||
{
|
||||
var pt = this.instance.template.properties[i];
|
||||
|
||||
Object.defineProperty(this, pt.name, {
|
||||
get: makeGetter(pt.index),
|
||||
set: makeSetter(pt.index),
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
_emitEventByIndex(index, args)
|
||||
{
|
||||
var et = this.instance.template.getEventTemplateByIndex(index);
|
||||
this._emitArgs(et.name, args);
|
||||
this.instance._emitResourceEvent(null, null, et.name, args);
|
||||
}
|
||||
|
||||
_invokeByArrayArguments(index, args) {
|
||||
if (this.destroyed)
|
||||
throw new Exception("Trying to access destroyed object");
|
||||
|
||||
if (index >= this.instance.template.functions.length)
|
||||
throw new Exception("Function index is incorrect");
|
||||
|
||||
return this._p.connection.sendInvokeByArrayArguments(this._p.instanceId, index, args);
|
||||
}
|
||||
|
||||
_invokeByNamedArguments(index, namedArgs) {
|
||||
if (this.destroyed)
|
||||
throw new Exception("Trying to access destroyed object");
|
||||
|
||||
if (index >= this.instance.template.functions.length)
|
||||
throw new Exception("Function index is incorrect");
|
||||
|
||||
return this._p.connection.sendInvokeByNamedArguments(this._p.instanceId, index, namedArgs);
|
||||
}
|
||||
|
||||
_get(index)
|
||||
{
|
||||
if (index >= this._p.properties.length)
|
||||
return null;
|
||||
return this._p.properties[index];
|
||||
}
|
||||
|
||||
|
||||
_updatePropertyByIndex(index, value)
|
||||
{
|
||||
var pt = this.instance.template.getPropertyTemplateByIndex(index);
|
||||
this._p.properties[index] = value;
|
||||
this.instance.emitModification(pt, value);
|
||||
}
|
||||
|
||||
_set(index, value)
|
||||
{
|
||||
if (index >= this._p.properties.length)
|
||||
return null;
|
||||
|
||||
var reply = new AsyncReply();
|
||||
|
||||
var parameters = Codec.compose(value, this._p.connection);
|
||||
var self = this;
|
||||
|
||||
this._p.connection.sendRequest(IIPPacketAction.SetProperty)
|
||||
.addUint32(self._p.instanceId).addUint8(index).addUint8Array(parameters)
|
||||
.done()
|
||||
.then(function(res)
|
||||
{
|
||||
// not really needed, server will always send property modified, this only happens if the programmer forgot to emit in property setter
|
||||
self._p.properties[index] = value;
|
||||
reply.trigger(null);
|
||||
});
|
||||
|
||||
return reply;
|
||||
}
|
||||
}
|
||||
42
src/Net/IIP/DistributedResourceQueueItem.js
Normal file
42
src/Net/IIP/DistributedResourceQueueItem.js
Normal file
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright (c) 2017 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
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Created by Ahmed Zamil on 25/07/2017.
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
|
||||
export const DistributedResourceQueueItemType =
|
||||
{
|
||||
Propery: 0,
|
||||
Event: 1
|
||||
};
|
||||
|
||||
export default class DistributedResourceQueueItem {
|
||||
constructor(resource, type, value, index) {
|
||||
this.resource = resource;
|
||||
this.index = index;
|
||||
this.type = type;
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
268
src/Net/Packets/IIPAuthPacket.js
Normal file
268
src/Net/Packets/IIPAuthPacket.js
Normal file
@@ -0,0 +1,268 @@
|
||||
/*
|
||||
* Copyright (c) 2017 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
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Created by Ahmed Zamil on 25/07/2017.
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
|
||||
|
||||
export const IIPAuthPacketCommand =
|
||||
{
|
||||
Action: 0,
|
||||
Declare: 1,
|
||||
Acknowledge: 2,
|
||||
Error: 3
|
||||
};
|
||||
|
||||
export const IIPAuthPacketAction =
|
||||
{
|
||||
// Authenticate
|
||||
AuthenticateHash: 0,
|
||||
NewConnection: 0x20,
|
||||
ResumeConnection: 0x21,
|
||||
ConnectionEstablished: 0x28
|
||||
};
|
||||
|
||||
|
||||
export const IIPAuthPacketMethod =
|
||||
{
|
||||
None: 0,
|
||||
Certificate: 1,
|
||||
Credentials: 2,
|
||||
Token: 3
|
||||
};
|
||||
|
||||
export default class IIPAuthPacket
|
||||
{
|
||||
constructor()
|
||||
{
|
||||
this.command = 0;
|
||||
this.action = 0;
|
||||
this.errorCode = 0;
|
||||
this.errorMessage = "";
|
||||
this.localMethod = 0;
|
||||
this.sourceInfo = "";
|
||||
this.hash = "";
|
||||
this.sessionId = "";
|
||||
this.remoteMethod = 0;
|
||||
this.domain = "";
|
||||
this.CertificateId = 0;
|
||||
this.localUsername = "";
|
||||
this.remoteUsername = "";
|
||||
this.localPassword = "";
|
||||
this.remotePassword = "";
|
||||
this.localToken = [];
|
||||
this.reemoteToken = [];
|
||||
this.asymetricEncryptionKey = [];
|
||||
this.localNonce = [];
|
||||
this.remoteNonce = [];
|
||||
this.dataLengthNeeded = 0;
|
||||
}
|
||||
|
||||
notEnough(offset, ends, needed)
|
||||
{
|
||||
if (offset + needed > ends)
|
||||
{
|
||||
this.dataLengthNeeded = needed - (ends - offset);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
parse(data, offset, ends)
|
||||
{
|
||||
var oOffset = offset;
|
||||
|
||||
if (this.notEnough(offset, ends, 1))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
this.command = data.getUint8(offset) >> 6;
|
||||
|
||||
if (this.command == IIPAuthPacketCommand.Action)
|
||||
{
|
||||
this.action = data[offset++] & 0x3f;
|
||||
|
||||
if (this.action == IIPAuthPacketAction.AuthenticateHash)
|
||||
{
|
||||
if (this.notEnough(offset, ends, 32))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
this.hash = data.getUint8Array(offset, 32);
|
||||
|
||||
offset += 32;
|
||||
}
|
||||
else if (this.action == IIPAuthPacketAction.NewConnection)
|
||||
{
|
||||
if (this.notEnough(offset, ends, 2))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
var length = data.getUint16(offset);
|
||||
|
||||
offset += 2;
|
||||
|
||||
if (this.notEnough(offset, ends, length))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
this.sourceInfo = data.clip(offset, length);
|
||||
|
||||
offset += 32;
|
||||
}
|
||||
else if (this.action == IIPAuthPacketAction.ResumeConnection
|
||||
|| this.action == IIPAuthPacketAction.ConnectionEstablished)
|
||||
{
|
||||
if (this.notEnough(offset, ends, 32))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
this.sessionId = data.clip(offset, 32);
|
||||
|
||||
offset += 32;
|
||||
}
|
||||
}
|
||||
else if (this.command == IIPAuthPacketCommand.Declare)
|
||||
{
|
||||
this.remoteMethod = ((data.getUint8(offset) >> 4) & 0x3);
|
||||
this.localMethod = ((data.getUint8(offset) >> 2) & 0x3);
|
||||
|
||||
var encrypt = ((data.getUint8(offset++) & 0x2) == 0x2);
|
||||
|
||||
|
||||
if (this.notEnough(offset, ends, 1))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
var domainLength = data.getUint8(offset++);
|
||||
if (this.notEnough(offset, ends, domainLength))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
this.domain = data.getString(offset, domainLength);
|
||||
|
||||
offset += domainLength;
|
||||
|
||||
|
||||
if (this.remoteMethod == IIPAuthPacketMethod.Credentials)
|
||||
{
|
||||
if (this.localMethod == IIPAuthPacketMethod.None)
|
||||
{
|
||||
if (this.notEnough(offset, ends, 33))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
this.remoteNonce = data.clip(offset, 32);
|
||||
|
||||
offset += 32;
|
||||
|
||||
var length = data.getUint8(offset++);
|
||||
|
||||
if (this.notEnough(offset, ends, length))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
this.remoteUsername = data.getString(offset, length);
|
||||
|
||||
|
||||
offset += length;
|
||||
}
|
||||
}
|
||||
|
||||
if (encrypt)
|
||||
{
|
||||
if (this.notEnough(offset, ends, 2))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
var keyLength = data.getUint16(offset);
|
||||
|
||||
offset += 2;
|
||||
|
||||
if (this.notEnough(offset, ends, keyLength))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
this.asymetricEncryptionKey = data.clip(offset, keyLength);
|
||||
|
||||
offset += keyLength;
|
||||
}
|
||||
}
|
||||
else if (this.command == IIPAuthPacketCommand.Acknowledge)
|
||||
{
|
||||
this.remoteMethod = (data.getUint8(offset) >> 4) & 0x3;
|
||||
this.localMethod = (data.getUint8(offset) >> 2) & 0x3;
|
||||
var encrypt = ((data.getUint8(offset++) & 0x2) == 0x2);
|
||||
|
||||
if (this.notEnough(offset, ends, 1))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
|
||||
if (this.remoteMethod == IIPAuthPacketMethod.Credentials)
|
||||
{
|
||||
if (this.localMethod == IIPAuthPacketMethod.None)
|
||||
{
|
||||
if (this.notEnough(offset, ends, 32))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
this.remoteNonce = data.clip(offset, 32);
|
||||
|
||||
offset += 32;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if (encrypt)
|
||||
{
|
||||
if (this.notEnough(offset, ends, 2))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
var keyLength = data.getUint16(offset);
|
||||
|
||||
offset += 2;
|
||||
|
||||
if (this.notEnough(offset, ends, keyLength))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
this.asymetricEncryptionKey = data.clip(offset, keyLength);
|
||||
|
||||
offset += keyLength;
|
||||
}
|
||||
}
|
||||
else if (this.command == IIPAuthPacketCommand.Error)
|
||||
{
|
||||
if (this.notEnough(offset, ends, 5))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
offset++;
|
||||
this.errorCode = data.getUint8(offset++);
|
||||
|
||||
|
||||
var cl = data.getUint16(offset);
|
||||
offset += 2;
|
||||
|
||||
if (this.notEnough(offset, ends, cl))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
this.errorMessage = data.getString(offset, cl);
|
||||
offset += cl;
|
||||
|
||||
}
|
||||
|
||||
|
||||
return offset - oOffset;
|
||||
|
||||
}
|
||||
}
|
||||
725
src/Net/Packets/IIPPacket.js
Normal file
725
src/Net/Packets/IIPPacket.js
Normal file
@@ -0,0 +1,725 @@
|
||||
/*
|
||||
* Copyright (c) 2017 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
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Created by Ahmed Zamil on 25/07/2017.
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
|
||||
export const IIPPacketCommand =
|
||||
{
|
||||
Event: 0,
|
||||
Request: 1,
|
||||
Reply: 2,
|
||||
Report: 3
|
||||
};
|
||||
|
||||
export const IIPPacketReport =
|
||||
{
|
||||
ManagementError: 0,
|
||||
ExecutionError: 1,
|
||||
ProgressReport: 0x8,
|
||||
ChunkStream: 0x9
|
||||
};
|
||||
|
||||
export const IIPPacketEvent =
|
||||
{
|
||||
// Event Manage
|
||||
ResourceReassigned : 0,
|
||||
ResourceDestroyed: 1,
|
||||
ChildAdded: 2,
|
||||
ChildRemoved: 3,
|
||||
Renamed: 4,
|
||||
|
||||
// Event Invoke
|
||||
PropertyUpdated : 0x10,
|
||||
EventOccurred: 0x11,
|
||||
|
||||
// Attribute
|
||||
AttributesUpdated: 0x18
|
||||
|
||||
};
|
||||
|
||||
export const IIPPacketAction =
|
||||
{
|
||||
// Request Manage
|
||||
AttachResource: 0,
|
||||
ReattachResource: 1,
|
||||
DetachResource: 2,
|
||||
CreateResource: 3,
|
||||
DeleteResource: 4,
|
||||
AddChild: 5,
|
||||
RemoveChild: 6,
|
||||
RenameResource: 7,
|
||||
|
||||
// Request Inquire
|
||||
TemplateFromClassName: 8,
|
||||
TemplateFromClassId: 9,
|
||||
TemplateFromResourceId: 10,
|
||||
QueryLink: 11,
|
||||
ResourceHistory: 12,
|
||||
ResourceChildren: 13,
|
||||
ResourceParents: 14,
|
||||
|
||||
// Request Invoke
|
||||
InvokeFunctionArrayArguments: 16,
|
||||
GetProperty: 17,
|
||||
GetPropertyIfModified: 18,
|
||||
SetProperty: 19,
|
||||
InvokeFunctionNamedArguments: 20,
|
||||
|
||||
// Request Attribute
|
||||
GetAllAttributes: 24,
|
||||
UpdateAllAttributes: 25,
|
||||
ClearAllAttributes: 26,
|
||||
GetAttributes: 27,
|
||||
UpdateAttributes: 28,
|
||||
ClearAttributes: 29
|
||||
};
|
||||
|
||||
|
||||
export default class IIPPacket
|
||||
{
|
||||
constructor()
|
||||
{
|
||||
this.command = 0;
|
||||
this.action = 0;
|
||||
this.event = 0;
|
||||
this.resourceId = 0;
|
||||
this.newResourceId = 0;
|
||||
this.resourceAge = 0;
|
||||
this.content = [];
|
||||
this.errorCode = 0;
|
||||
this.errorMessage = "";
|
||||
this.className = "";
|
||||
this.resourceLink = "";
|
||||
this.classId = "";
|
||||
this.methodIndex = "";
|
||||
this.methodName = "";
|
||||
this.callbackId = 0;
|
||||
this.dataLengthNeeded = 0;
|
||||
this.originalOffset = 0;
|
||||
}
|
||||
|
||||
notEnough(offset, ends, needed)
|
||||
{
|
||||
if (offset + needed > ends)
|
||||
{
|
||||
this.dataLengthNeeded = needed - (ends - this.originalOffset);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
parse(data, offset, ends)
|
||||
{
|
||||
this.originalOffset = offset;
|
||||
|
||||
if (this.notEnough(offset, ends, 1))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
this.command = (data.getUint8(offset) >> 6);
|
||||
|
||||
if (this.command == IIPPacketCommand.Event)
|
||||
{
|
||||
this.event = (data.getUint8(offset++) & 0x3f);
|
||||
|
||||
if (this.notEnough(offset, ends, 4))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
this.resourceId = data.getUint32(offset);
|
||||
offset += 4;
|
||||
}
|
||||
else if (this.command == IIPPacketCommand.Report)
|
||||
{
|
||||
this.report = (data.getUint8(offset++) & 0x3f);
|
||||
|
||||
if (this.notEnough(offset, ends, 4))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
this.callbackId = data.getUint32(offset);
|
||||
offset += 4;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.action = (data.getUint8(offset++) & 0x3f);
|
||||
|
||||
if (this.notEnough(offset, ends, 4))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
this.callbackId = data.getUint32(offset);
|
||||
offset += 4;
|
||||
}
|
||||
|
||||
if (this.command == IIPPacketCommand.Event)
|
||||
{
|
||||
if (this.event == IIPPacketEvent.ResourceReassigned)
|
||||
{
|
||||
if (this.notEnough(offset, ends, 4))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
this.newResourceId = data.getUint32( offset);
|
||||
offset += 4;
|
||||
|
||||
}
|
||||
else if (this.event == IIPPacketEvent.ResourceDestroyed)
|
||||
{
|
||||
// nothing to parse
|
||||
}
|
||||
else if (this.event == IIPPacketEvent.ChildAdded
|
||||
|| this.event == IIPPacketEvent.ChildRemoved)
|
||||
{
|
||||
if (this.notEnough(offset, ends, 4))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
this.childId = data.getUint32(offset);
|
||||
offset += 4;
|
||||
}
|
||||
else if(this.event == IIPPacketEvent.Renamed)
|
||||
{
|
||||
if (this.notEnough(offset, ends, 2))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
var cl = data.getUint16(offset);
|
||||
offset += 2;
|
||||
|
||||
if (this.notEnough(offset, ends, cl))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
this.content = data.clip(offset, cl);
|
||||
|
||||
offset += cl;
|
||||
}
|
||||
else if (this.event == IIPPacketEvent.PropertyUpdated)
|
||||
{
|
||||
if (this.notEnough(offset, ends, 2))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
this.methodIndex = data[offset++];
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
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;
|
||||
|
||||
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);
|
||||
offset += 4;
|
||||
|
||||
if (this.notEnough(offset, ends, cl))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
this.content = data.clip(offset, cl);
|
||||
|
||||
offset += cl;
|
||||
}
|
||||
}
|
||||
else if (this.command == IIPPacketCommand.Request)
|
||||
{
|
||||
if (this.action == IIPPacketAction.AttachResource)
|
||||
{
|
||||
if (this.notEnough(offset, ends, 4))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
this.resourceId = data.getUint32(offset);
|
||||
offset += 4;
|
||||
}
|
||||
else if (this.action == IIPPacketAction.ReattachResource)
|
||||
{
|
||||
if (this.notEnough(offset, ends, 12))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
this.resourceId = data.getUint32(offset);
|
||||
offset += 4;
|
||||
|
||||
this.resourceAge = data.getUint64(offset);
|
||||
offset += 8;
|
||||
}
|
||||
else if (this.action == IIPPacketAction.DetachResource)
|
||||
{
|
||||
if (this.notEnough(offset, ends, 4))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
this.resourceId = data.getUint32(offset);
|
||||
offset += 4;
|
||||
|
||||
}
|
||||
else if (this.action == IIPPacketAction.CreateResource)
|
||||
{
|
||||
if (this.notEnough(offset, ends, 12))
|
||||
return -dataLengthNeeded;
|
||||
|
||||
this.storeId = data.getUint32(offset);
|
||||
offset += 4;
|
||||
this.resourceId = data.getUint32(offset);
|
||||
offset += 4;
|
||||
|
||||
var cl = data.getUint32(offset);
|
||||
offset += 4;
|
||||
|
||||
if (this.notEnough(offset, ends, cl))
|
||||
return -dataLengthNeeded;
|
||||
|
||||
this.content = data.clip(offset, cl);
|
||||
}
|
||||
else if (this.action == IIPPacketAction.DeleteResource)
|
||||
{
|
||||
if (this.notEnough(offset, ends, 4))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
this.resourceId = data.getUint32(offset);
|
||||
offset += 4;
|
||||
|
||||
}
|
||||
else if (this.action == IIPPacketAction.AddChild
|
||||
|| this.action == IIPPacketAction.RemoveChild)
|
||||
{
|
||||
if (this.notEnough(offset, ends, 8))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
this.resourceId = data.getUint32(offset);
|
||||
offset += 4;
|
||||
|
||||
this.childId = data.getUint32(offset);
|
||||
offset += 4;
|
||||
|
||||
}
|
||||
else if (this.action == IIPPacketAction.RenameResource)
|
||||
{
|
||||
if (this.notEnough(offset, ends, 6))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
this.resourceId = data.getUint32(offset);
|
||||
offset += 4;
|
||||
|
||||
var cl = data.getUint16(offset);
|
||||
offset += 2;
|
||||
|
||||
if (this.notEnough(offset, ends, cl))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
this.content = data.clip(offset, cl);
|
||||
offset += cl;
|
||||
|
||||
}
|
||||
else if (this.action == IIPPacketAction.TemplateFromClassName)
|
||||
{
|
||||
if (this.notEnough(offset, ends, 1))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
var cl = data.getUint8(offset++);
|
||||
|
||||
if (this.notEnough(offset, ends, cl))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
this.className = data.getString(offset, cl);
|
||||
offset += cl;
|
||||
|
||||
}
|
||||
else if (this.action == IIPPacketAction.TemplateFromClassId)
|
||||
{
|
||||
if (this.notEnough(offset, ends, 16))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
this.classId = data.getGuid(offset);
|
||||
offset += 16;
|
||||
}
|
||||
else if (this.action == IIPPacketAction.TemplateFromResourceId)
|
||||
{
|
||||
if (this.notEnough(offset, ends, 4))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
this.resourceId = data.getUint32(offset);
|
||||
offset += 4;
|
||||
}
|
||||
else if (this.action == IIPPacketAction.QueryLink)
|
||||
{
|
||||
if (this.notEnough(offset, ends, 2))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
var cl = data.getUint16(offset);
|
||||
offset += 2;
|
||||
|
||||
if (this.notEnough(offset, ends, cl))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
this.resourceLink = data.getString(offset, cl);
|
||||
offset += cl;
|
||||
}
|
||||
else if (this.action == IIPPacketAction.ResourceChildren
|
||||
|| this.action == IIPPacketAction.ResourceParents)
|
||||
{
|
||||
if (this.notEnough(offset, ends, 4))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
this.resourceId = data.getUint32(offset);
|
||||
offset += 4;
|
||||
}
|
||||
else if (this.action == IIPPacketAction.ResourceHistory)
|
||||
{
|
||||
if (this.notEnough(offset, ends, 20))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
this.resourceId = data.getUint32(offset);
|
||||
offset += 4;
|
||||
|
||||
this.fromDate = data.getDateTime(offset);
|
||||
offset += 8;
|
||||
|
||||
this.toDate = data.getDateTime(offset);
|
||||
offset += 8;
|
||||
|
||||
}
|
||||
else if ( this.action == IIPPacket.InvokeFunctionArrayArguments
|
||||
|| this.action == IIPPacketAction.InvokeFunctionNamedArguments)
|
||||
{
|
||||
if (this.notEnough(offset, ends, 9))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
this.resourceId = data.getUint32(offset);
|
||||
offset += 4;
|
||||
|
||||
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;
|
||||
|
||||
}
|
||||
else if (this.action == IIPPacketAction.GetProperty)
|
||||
{
|
||||
if (this.notEnough(offset, ends, 5))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
this.resourceId = data.getUint32(offset);
|
||||
offset += 4;
|
||||
|
||||
this.methodIndex = data.getUint8(offset++);
|
||||
|
||||
}
|
||||
else if (this.action == IIPPacketAction.GetPropertyIfModified)
|
||||
{
|
||||
if (this.notEnough(offset, ends, 9))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
this.resourceId = data.getUint32(offset);
|
||||
offset += 4;
|
||||
|
||||
this.methodIndex = data[offset++];
|
||||
|
||||
this.resourceAge = data.getUint64(offset);
|
||||
offset += 8;
|
||||
|
||||
}
|
||||
else if (this.action == IIPPacketAction.SetProperty)
|
||||
{
|
||||
if (this.notEnough(offset, ends, 6))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
this.resourceId = data.getUint32(offset);
|
||||
offset += 4;
|
||||
|
||||
this.methodIndex = data[offset++];
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
// Attribute
|
||||
else if (this.action == IIPPacketAction.UpdateAllAttributes
|
||||
|| this.action == IIPPacketAction.GetAttributes
|
||||
|| this.action == IIPPacketAction.UpdateAttributes
|
||||
|| this.action == IIPPacketAction.ClearAttributes)
|
||||
{
|
||||
if (this.notEnough(offset, ends, 8))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
this.resourceId = data.getUint32(offset);
|
||||
offset += 4;
|
||||
var cl = data.getUint32(offset);
|
||||
offset += 4;
|
||||
|
||||
if (this.notEnough(offset, ends, cl))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
this.content = data.clip(offset, cl);
|
||||
offset += cl;
|
||||
}
|
||||
|
||||
}
|
||||
else if (this.command == IIPPacketCommand.Reply)
|
||||
{
|
||||
if (this.action == IIPPacketAction.AttachResource
|
||||
|| this.action == IIPPacketAction.ReattachResource)
|
||||
{
|
||||
if (this.notEnough(offset, ends, 26))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
this.classId = data.getGuid(offset);
|
||||
offset += 16;
|
||||
|
||||
this.resourceAge = data.getUint64(offset);
|
||||
offset += 8;
|
||||
|
||||
var cl = data.getUint16(offset);
|
||||
offset+=2;
|
||||
|
||||
if (this.notEnough(offset, ends, cl))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
this.resourceLink = data.getString(offset, cl);
|
||||
offset += cl;
|
||||
|
||||
if (this.notEnough(offset, ends, 4))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
cl = data.getUint32(offset);
|
||||
offset += 4;
|
||||
|
||||
if (this.notEnough(offset, ends, cl))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
this.content = data.clip(offset, cl);
|
||||
offset += cl;
|
||||
}
|
||||
else if (this.action == IIPPacketAction.DetachResource)
|
||||
{
|
||||
// nothing to do
|
||||
}
|
||||
else if (this.action == IIPPacketAction.CreateResource)
|
||||
{
|
||||
if (this.notEnough(offset, ends, 20))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
this.resourceId = data.getUint32(offset);
|
||||
offset += 4;
|
||||
|
||||
}
|
||||
else if (this.action == IIPPacketAction.DetachResource)
|
||||
{
|
||||
// nothing to do
|
||||
}
|
||||
else if (this.action == IIPPacketAction.TemplateFromClassName
|
||||
|| this.action == IIPPacketAction.TemplateFromClassId
|
||||
|| this.action == IIPPacketAction.TemplateFromResourceId
|
||||
|| this.action == IIPPacketAction.QueryLink
|
||||
|| this.action == IIPPacketAction.ResourceChildren
|
||||
|| this.action == IIPPacketAction.ResourceParents
|
||||
|| this.action == IIPPacketAction.ResourceHistory
|
||||
// Attribute
|
||||
|| 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, cl))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
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)
|
||||
{
|
||||
if (this.notEnough(offset, ends, 1))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
else if (this.action == IIPPacketAction.SetProperty)
|
||||
{
|
||||
// nothing to do
|
||||
}
|
||||
}
|
||||
else if (this.command == IIPPacketCommand.Report)
|
||||
{
|
||||
if (this.report == IIPPacketReport.ManagementError)
|
||||
{
|
||||
if (this.notEnough(offset, ends, 2))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
this.errorCode = data.getUint16(offset);
|
||||
offset += 2;
|
||||
}
|
||||
else if (this.report == IIPPacketReport.ExecutionError)
|
||||
{
|
||||
if (this.notEnough(offset, ends, 2))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
this.errorCode = data.getUint16(offset);
|
||||
offset += 2;
|
||||
|
||||
if (this.notEnough(offset, ends, 2))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
var cl = data.getUint16(offset);
|
||||
offset += 2;
|
||||
|
||||
if (this.notEnough(offset, ends, cl))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
this.errorMessage = data.getString(offset, cl);
|
||||
offset += cl;
|
||||
}
|
||||
else if (this.report == IIPPacketReport.ProgressReport)
|
||||
{
|
||||
if (this.notEnough(offset, ends, 8))
|
||||
return -this.dataLengthNeeded;
|
||||
|
||||
this.progressValue = data.getInt32(offset);
|
||||
offset += 4;
|
||||
this.progressMax = data.getInt32(offset);
|
||||
offset += 4;
|
||||
}
|
||||
else if (this.report == IIPPacketReport.ChunkStream)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return offset - this.originalOffset;
|
||||
}
|
||||
}
|
||||
45
src/Net/SendList.js
Normal file
45
src/Net/SendList.js
Normal file
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright (c) 2017 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
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Created by Ahmed Zamil on 02/09/2017.
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
|
||||
import BinaryList from '../Data/BinaryList.js';
|
||||
|
||||
export default class SendList extends BinaryList
|
||||
{
|
||||
constructor(connection, doneReply)
|
||||
{
|
||||
super();
|
||||
this.connection = connection;
|
||||
this.reply = doneReply;
|
||||
}
|
||||
|
||||
done()
|
||||
{
|
||||
this.connection.send(this.toArray());
|
||||
return this.reply;
|
||||
}
|
||||
}
|
||||
119
src/Net/Sockets/NetworkBuffer.js
Normal file
119
src/Net/Sockets/NetworkBuffer.js
Normal file
@@ -0,0 +1,119 @@
|
||||
/*
|
||||
* Copyright (c) 2017 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
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Created by Ahmed Zamil on 01/09/2017.
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
|
||||
import DC from '../../Data/DataConverter.js';
|
||||
|
||||
export default class NetworkBuffer {
|
||||
constructor() {
|
||||
this.neededDataLength = 0;
|
||||
this.data = new DC(0);
|
||||
}
|
||||
|
||||
get protected() {
|
||||
return this.neededDataLength > this.data.length;
|
||||
}
|
||||
|
||||
get available() {
|
||||
|
||||
return this.data.length;
|
||||
}
|
||||
|
||||
holdAllForNextWrite(src) {
|
||||
this.holdFor(src, src.length + 1);
|
||||
}
|
||||
|
||||
holdForNextWrite(src, offset, size) {
|
||||
this.holdFor(src, offset, size, size + 1);
|
||||
}
|
||||
|
||||
|
||||
holdFor(src, offset, size, needed) {
|
||||
if (size >= needed)
|
||||
throw new Exception("Size >= Needed !");
|
||||
|
||||
this.data = DC.combine(src, offset, size, this.data, 0, this.data.length);
|
||||
this.neededDataLength = needed;
|
||||
}
|
||||
|
||||
holdAllFor(src, needed) {
|
||||
this.holdFor(src, 0, src.length, needed);
|
||||
}
|
||||
|
||||
protect(data, offset, needed) {
|
||||
var dataLength = data.length - offset;
|
||||
|
||||
// protection
|
||||
if (dataLength < needed) {
|
||||
this.holdFor(data, offset, dataLength, needed);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
writeAll(src) {
|
||||
this.write(src, 0, src.length ? src.length : src.byteLength);
|
||||
}
|
||||
|
||||
write(src, offset, length) {
|
||||
this.data = this.data.append(src, offset, length);
|
||||
}
|
||||
|
||||
get canRead() {
|
||||
if (this.data.length == 0)
|
||||
return false;
|
||||
else if (this.data.length < this.neededDataLength)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
read() {
|
||||
if (this.data.length == 0)
|
||||
return null;
|
||||
|
||||
var rt = null;
|
||||
|
||||
if (this.neededDataLength == 0) {
|
||||
rt = this.data;
|
||||
this.data = new DC(0);
|
||||
}
|
||||
else {
|
||||
if (this.data.length >= this.neededDataLength) {
|
||||
rt = this.data;
|
||||
this.data = new DC(0);
|
||||
this.neededDataLength = 0;
|
||||
return rt;
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return rt;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user