mirror of
https://github.com/esiur/esiur-dart.git
synced 2025-06-27 14:53:11 +00:00
2.0.0
This commit is contained in:
@ -1,3 +1,6 @@
|
||||
|
||||
import '../Core/IEventHandler.dart';
|
||||
|
||||
import '../Core/IDestructible.dart';
|
||||
import 'Codec.dart';
|
||||
import 'dart:collection';
|
||||
@ -8,7 +11,7 @@ class AutoList<T, ST> extends IDestructible with IterableMixin<T> {
|
||||
ST? _state;
|
||||
late bool _removableList;
|
||||
|
||||
sort(int Function(T, T)? compare) {
|
||||
void sort(int Function(T, T)? compare) {
|
||||
_list.sort(compare);
|
||||
}
|
||||
|
||||
@ -38,36 +41,17 @@ class AutoList<T, ST> extends IDestructible with IterableMixin<T> {
|
||||
register("cleared");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Synchronization lock of the list
|
||||
/// </summary>
|
||||
//public object SyncRoot
|
||||
//{
|
||||
// get
|
||||
// {
|
||||
// return syncRoot;
|
||||
// }
|
||||
//}
|
||||
|
||||
/// <summary>
|
||||
/// First item in the list
|
||||
/// </summary>
|
||||
//T first()
|
||||
//{
|
||||
// return _list.first;
|
||||
//}
|
||||
|
||||
operator [](index) {
|
||||
T operator [](int index) {
|
||||
return _list[index];
|
||||
}
|
||||
|
||||
operator []=(index, value) {
|
||||
void operator []=(int index, T value) {
|
||||
var oldValue = _list[index];
|
||||
|
||||
if (_removableList) {
|
||||
if (oldValue != null)
|
||||
(oldValue as IDestructible).off("destroy", _itemDestroyed);
|
||||
if (value != null) value.on("destroy", _itemDestroyed);
|
||||
if (value != null) (value as IEventHandler).on("destroy", _itemDestroyed);
|
||||
}
|
||||
|
||||
//lock (syncRoot)
|
||||
@ -79,7 +63,7 @@ class AutoList<T, ST> extends IDestructible with IterableMixin<T> {
|
||||
/// <summary>
|
||||
/// Add item to the list
|
||||
/// </summary>
|
||||
add(T value) {
|
||||
void add(T value) {
|
||||
if (_removableList) if (value != null)
|
||||
(value as IDestructible).on("destroy", _itemDestroyed);
|
||||
|
||||
@ -92,18 +76,18 @@ class AutoList<T, ST> extends IDestructible with IterableMixin<T> {
|
||||
/// <summary>
|
||||
/// Add an array of items to the list
|
||||
/// </summary>
|
||||
addRange(List<T> values) {
|
||||
void addRange(List<T> values) {
|
||||
values.forEach((x) => add(x));
|
||||
}
|
||||
|
||||
_itemDestroyed(T sender) {
|
||||
void _itemDestroyed(T sender) {
|
||||
remove(sender);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clear the list
|
||||
/// </summary>
|
||||
clear() {
|
||||
void clear() {
|
||||
if (_removableList)
|
||||
_list.forEach((x) => (x as IDestructible).off("destroy", _itemDestroyed));
|
||||
|
||||
@ -117,7 +101,7 @@ class AutoList<T, ST> extends IDestructible with IterableMixin<T> {
|
||||
/// Remove an item from the list
|
||||
/// <param name="value">Item to remove</param>
|
||||
/// </summary>
|
||||
remove(T value) {
|
||||
void remove(T value) {
|
||||
if (!_list.contains(value)) return;
|
||||
|
||||
if (_removableList) if (value != null)
|
||||
@ -145,7 +129,7 @@ class AutoList<T, ST> extends IDestructible with IterableMixin<T> {
|
||||
/// Check if any item of the given array is in the list
|
||||
/// </summary>
|
||||
/// <param name="values">Array of items</param>
|
||||
containsAny(values) {
|
||||
bool containsAny(dynamic values) {
|
||||
if (values is List<T>) {
|
||||
for (var v in values) {
|
||||
if (_list.contains(v)) return true;
|
||||
|
@ -27,7 +27,6 @@
|
||||
import '../Core/AsyncReply.dart';
|
||||
import 'dart:typed_data';
|
||||
import 'DC.dart';
|
||||
import 'DataType.dart';
|
||||
import 'Guid.dart';
|
||||
|
||||
class BinaryList {
|
||||
@ -35,20 +34,22 @@ class BinaryList {
|
||||
|
||||
int get length => _list.length;
|
||||
|
||||
void addDateTime(DateTime value) {
|
||||
_list.addAll(DC.dateTimeToBytes(value));
|
||||
void addDateTime(DateTime value, [Endian endian = Endian.little]) {
|
||||
_list.addAll(DC.dateTimeToBytes(value, endian));
|
||||
}
|
||||
|
||||
void insertDateTime(int position, DateTime value) {
|
||||
_list.insertAll(position, DC.dateTimeToBytes(value));
|
||||
void insertDateTime(int position, DateTime value,
|
||||
[Endian endian = Endian.little]) {
|
||||
_list.insertAll(position, DC.dateTimeToBytes(value, endian));
|
||||
}
|
||||
|
||||
void addDateTimeArray(List<DateTime> value) {
|
||||
_list.addAll(DC.dateTimeArrayToBytes(value));
|
||||
void addDateTimeArray(List<DateTime> value, [Endian endian = Endian.little]) {
|
||||
_list.addAll(DC.dateTimeArrayToBytes(value, endian));
|
||||
}
|
||||
|
||||
void insertDateTimeArray(int position, List<DateTime> value) {
|
||||
_list.insertAll(position, DC.dateTimeArrayToBytes(value));
|
||||
void insertDateTimeArray(int position, List<DateTime> value,
|
||||
[Endian endian = Endian.little]) {
|
||||
_list.insertAll(position, DC.dateTimeArrayToBytes(value, endian));
|
||||
}
|
||||
|
||||
void addGuid(Guid value) {
|
||||
@ -59,14 +60,6 @@ class BinaryList {
|
||||
_list.insertAll(position, DC.guidToBytes(value));
|
||||
}
|
||||
|
||||
void addGuidArray(List<Guid> value) {
|
||||
_list.addAll(DC.guidArrayToBytes(value));
|
||||
}
|
||||
|
||||
void insertGuidArray(int position, List<Guid> value) {
|
||||
_list.insertAll(position, DC.guidArrayToBytes(value));
|
||||
}
|
||||
|
||||
void addUint8Array(Uint8List value) {
|
||||
_list.addAll(value);
|
||||
}
|
||||
@ -79,18 +72,6 @@ class BinaryList {
|
||||
_list.insertAll(position, value);
|
||||
}
|
||||
|
||||
/*
|
||||
BinaryList addHex(String value)
|
||||
{
|
||||
return this.addUint8Array(DC.fromHex(value, null));
|
||||
}
|
||||
|
||||
BinaryList insertHex(int position, String value)
|
||||
{
|
||||
return this.insertUint8Array(position, DC.fromHex(value, null));
|
||||
}
|
||||
*/
|
||||
|
||||
void addString(String value) {
|
||||
_list.addAll(DC.stringToBytes(value));
|
||||
}
|
||||
@ -99,14 +80,6 @@ class BinaryList {
|
||||
_list.insertAll(position, DC.stringToBytes(value));
|
||||
}
|
||||
|
||||
void addStringArray(List<String> value) {
|
||||
_list.addAll(DC.stringArrayToBytes(value));
|
||||
}
|
||||
|
||||
void insertStringArray(int position, List<String> value) {
|
||||
_list.insertAll(position, DC.stringArrayToBytes(value));
|
||||
}
|
||||
|
||||
void insertUint8(int position, int value) {
|
||||
_list.insert(position, value);
|
||||
}
|
||||
@ -123,30 +96,14 @@ class BinaryList {
|
||||
_list.insert(position, value);
|
||||
}
|
||||
|
||||
void addInt8Array(Int8List value) {
|
||||
_list.addAll(DC.int8ArrayToBytes(value));
|
||||
}
|
||||
|
||||
void insertInt8Array(int position, Int8List value) {
|
||||
_list.insertAll(position, DC.int8ArrayToBytes(value));
|
||||
}
|
||||
|
||||
void addChar(int value) {
|
||||
_list.addAll(DC.charToBytes(value));
|
||||
}
|
||||
|
||||
void InsertChar(int position, int value) {
|
||||
void insertChar(int position, int value) {
|
||||
_list.insertAll(position, DC.charToBytes(value));
|
||||
}
|
||||
|
||||
void addCharArray(Uint16List value) {
|
||||
_list.addAll(DC.charArrayToBytes(value));
|
||||
}
|
||||
|
||||
void InsertCharArray(int position, Uint16List value) {
|
||||
_list.insertAll(position, DC.charArrayToBytes(value));
|
||||
}
|
||||
|
||||
void addBoolean(bool value) {
|
||||
_list.addAll(DC.boolToBytes(value));
|
||||
}
|
||||
@ -155,237 +112,70 @@ class BinaryList {
|
||||
_list.insertAll(position, DC.boolToBytes(value));
|
||||
}
|
||||
|
||||
void addBooleanArray(List<bool> value) {
|
||||
_list.addAll(DC.boolToBytes(value));
|
||||
void addUint16(int value, [Endian endian = Endian.little]) {
|
||||
_list.addAll(DC.uint16ToBytes(value, endian));
|
||||
}
|
||||
|
||||
void insertBooleanArray(int position, List<bool> value) {
|
||||
_list.insertAll(position, DC.boolToBytes(value));
|
||||
void insertUint16(int position, int value, [Endian endian = Endian.little]) {
|
||||
_list.insertAll(position, DC.uint16ToBytes(value, endian));
|
||||
}
|
||||
|
||||
void addUint16(int value) {
|
||||
_list.addAll(DC.uint16ToBytes(value));
|
||||
void addInt16(int value, [Endian endian = Endian.little]) {
|
||||
_list.addAll(DC.int16ToBytes(value, endian));
|
||||
}
|
||||
|
||||
void insertUint16(int position, int value) {
|
||||
_list.insertAll(position, DC.uint16ToBytes(value));
|
||||
void insertInt16(int position, int value, [Endian endian = Endian.little]) {
|
||||
_list.insertAll(position, DC.int16ToBytes(value, endian));
|
||||
}
|
||||
|
||||
void addUint16Array(Uint16List value) {
|
||||
_list.addAll(DC.uint16ArrayToBytes(value));
|
||||
void addUint32(int value, [Endian endian = Endian.little]) {
|
||||
_list.addAll(DC.uint32ToBytes(value, endian));
|
||||
}
|
||||
|
||||
void insertUint16Array(int position, Uint16List value) {
|
||||
_list.insertAll(position, DC.uint16ArrayToBytes(value));
|
||||
void insertUint32(int position, int value, [Endian endian = Endian.little]) {
|
||||
_list.insertAll(position, DC.uint32ToBytes(value, endian));
|
||||
}
|
||||
|
||||
void addInt16(int value) {
|
||||
_list.addAll(DC.int16ToBytes(value));
|
||||
void addInt32(int value, [Endian endian = Endian.little]) {
|
||||
_list.addAll(DC.int32ToBytes(value, endian));
|
||||
}
|
||||
|
||||
void insertInt16(int position, int value) {
|
||||
_list.insertAll(position, DC.int16ToBytes(value));
|
||||
void insertInt32(int position, int value, [Endian endian = Endian.little]) {
|
||||
_list.insertAll(position, DC.int32ToBytes(value, endian));
|
||||
}
|
||||
|
||||
void addInt16Array(Int16List value) {
|
||||
_list.addAll(DC.int16ArrayToBytes(value));
|
||||
void addUint64(int value, [Endian endian = Endian.little]) {
|
||||
_list.addAll(DC.uint64ToBytes(value, endian));
|
||||
}
|
||||
|
||||
void insertInt16Array(int position, Int16List value) {
|
||||
_list.insertAll(position, DC.int16ArrayToBytes(value));
|
||||
void insertUint64(int position, int value, [Endian endian = Endian.little]) {
|
||||
_list.insertAll(position, DC.uint64ToBytes(value, endian));
|
||||
}
|
||||
|
||||
void addUint32(int value) {
|
||||
_list.addAll(DC.uint32ToBytes(value));
|
||||
void addInt64(int value, [Endian endian = Endian.little]) {
|
||||
_list.addAll(DC.int64ToBytes(value, endian));
|
||||
}
|
||||
|
||||
void insertUint32(int position, int value) {
|
||||
_list.insertAll(position, DC.uint32ToBytes(value));
|
||||
void insertInt64(int position, int value, [Endian endian = Endian.little]) {
|
||||
_list.insertAll(position, DC.int64ToBytes(value, endian));
|
||||
}
|
||||
|
||||
void addUint32Array(Uint32List value) {
|
||||
_list.addAll(DC.uint32ArrayToBytes(value));
|
||||
void addFloat32(double value, [Endian endian = Endian.little]) {
|
||||
_list.addAll(DC.float32ToBytes(value, endian));
|
||||
}
|
||||
|
||||
void InsertUint32Array(int position, Uint32List value) {
|
||||
_list.insertAll(position, DC.uint32ArrayToBytes(value));
|
||||
void insertFloat32(int position, double value,
|
||||
[Endian endian = Endian.little]) {
|
||||
_list.insertAll(position, DC.float32ToBytes(value, endian));
|
||||
}
|
||||
|
||||
void addInt32(int value) {
|
||||
_list.addAll(DC.int32ToBytes(value));
|
||||
void addFloat64(double value, [Endian endian = Endian.little]) {
|
||||
_list.addAll(DC.float64ToBytes(value, endian));
|
||||
}
|
||||
|
||||
void insertInt32(int position, int value) {
|
||||
_list.insertAll(position, DC.int32ToBytes(value));
|
||||
}
|
||||
|
||||
void addInt32Array(Int32List value) {
|
||||
_list.addAll(DC.int32ArrayToBytes(value));
|
||||
}
|
||||
|
||||
void insertInt32Array(int position, Int32List value) {
|
||||
_list.insertAll(position, DC.int32ArrayToBytes(value));
|
||||
}
|
||||
|
||||
void addUint64(int value) {
|
||||
_list.addAll(DC.uint64ToBytes(value));
|
||||
}
|
||||
|
||||
void insertUint64(int position, int value) {
|
||||
_list.insertAll(position, DC.uint64ToBytes(value));
|
||||
}
|
||||
|
||||
void addUint64Array(Uint64List value) {
|
||||
_list.addAll(DC.uint64ArrayToBytes(value));
|
||||
}
|
||||
|
||||
void InsertUint64Array(int position, Uint64List value) {
|
||||
_list.insertAll(position, DC.uint64ArrayToBytes(value));
|
||||
}
|
||||
|
||||
void addInt64(int value) {
|
||||
_list.addAll(DC.int64ToBytes(value));
|
||||
}
|
||||
|
||||
void insertInt64(int position, int value) {
|
||||
_list.insertAll(position, DC.int64ToBytes(value));
|
||||
}
|
||||
|
||||
void addInt64Array(Int64List value) {
|
||||
_list.addAll(DC.int64ArrayToBytes(value));
|
||||
}
|
||||
|
||||
void insertInt64Array(int position, Int64List value) {
|
||||
_list.insertAll(position, DC.int64ArrayToBytes(value));
|
||||
}
|
||||
|
||||
void addFloat32(double value) {
|
||||
_list.addAll(DC.float32ToBytes(value));
|
||||
}
|
||||
|
||||
void insertFloat32(int position, double value) {
|
||||
_list.insertAll(position, DC.float32ToBytes(value));
|
||||
}
|
||||
|
||||
void addFloat32Array(Float32List value) {
|
||||
_list.addAll(DC.float32ArrayToBytes(value));
|
||||
}
|
||||
|
||||
void insertFloat32Array(int position, Float32List value) {
|
||||
_list.insertAll(position, DC.float32ArrayToBytes(value));
|
||||
}
|
||||
|
||||
void addFloat64(double value) {
|
||||
_list.addAll(DC.float64ToBytes(value));
|
||||
}
|
||||
|
||||
void insertFloat64(int position, double value) {
|
||||
_list.insertAll(position, DC.float64ToBytes(value));
|
||||
}
|
||||
|
||||
void addFloat64Array(Float64List value) {
|
||||
_list.addAll(DC.float64ArrayToBytes(value));
|
||||
}
|
||||
|
||||
void insertFloat64Array(int position, Float64List value) {
|
||||
_list.insertAll(position, DC.float64ArrayToBytes(value));
|
||||
}
|
||||
|
||||
void add(type, value) {
|
||||
switch (type) {
|
||||
case DataType.Bool:
|
||||
addBoolean(value);
|
||||
return;
|
||||
case DataType.BoolArray:
|
||||
addBooleanArray(value);
|
||||
return;
|
||||
case DataType.UInt8:
|
||||
addUint8(value);
|
||||
return;
|
||||
case DataType.UInt8Array:
|
||||
addUint8Array(value);
|
||||
return;
|
||||
case DataType.Int8:
|
||||
addInt8(value);
|
||||
return;
|
||||
case DataType.Int8Array:
|
||||
addInt8Array(value);
|
||||
return;
|
||||
case DataType.Char:
|
||||
addChar(value);
|
||||
return;
|
||||
case DataType.CharArray:
|
||||
addCharArray(value);
|
||||
return;
|
||||
case DataType.UInt16:
|
||||
addUint16(value);
|
||||
return;
|
||||
case DataType.UInt16Array:
|
||||
addUint16Array(value);
|
||||
return;
|
||||
case DataType.Int16:
|
||||
addInt16(value);
|
||||
return;
|
||||
case DataType.Int16Array:
|
||||
addInt16Array(value);
|
||||
return;
|
||||
case DataType.UInt32:
|
||||
addUint32(value);
|
||||
return;
|
||||
case DataType.UInt32Array:
|
||||
addUint32Array(value);
|
||||
return;
|
||||
case DataType.Int32:
|
||||
addInt32(value);
|
||||
return;
|
||||
case DataType.Int32Array:
|
||||
addInt32Array(value);
|
||||
return;
|
||||
case DataType.UInt64:
|
||||
addUint64(value);
|
||||
return;
|
||||
case DataType.UInt64Array:
|
||||
addUint64Array(value);
|
||||
return;
|
||||
case DataType.Int64:
|
||||
addInt64(value);
|
||||
return;
|
||||
case DataType.Int64Array:
|
||||
addInt64Array(value);
|
||||
return;
|
||||
|
||||
case DataType.Float32:
|
||||
addFloat32(value);
|
||||
return;
|
||||
case DataType.Float32Array:
|
||||
addFloat32Array(value);
|
||||
return;
|
||||
|
||||
case DataType.Float64:
|
||||
addFloat64(value);
|
||||
return;
|
||||
case DataType.Float64Array:
|
||||
addFloat64Array(value);
|
||||
return;
|
||||
|
||||
case DataType.String:
|
||||
addString(value);
|
||||
return;
|
||||
case DataType.StringArray:
|
||||
addStringArray(value);
|
||||
return;
|
||||
|
||||
case DataType.DateTime:
|
||||
addDateTime(value);
|
||||
return;
|
||||
case DataType.DateTimeArray:
|
||||
addDateTimeArray(value);
|
||||
return;
|
||||
|
||||
default:
|
||||
throw new Exception("Not Implemented " + type.ToString());
|
||||
//return this;
|
||||
}
|
||||
void insertFloat64(int position, double value,
|
||||
[Endian endian = Endian.little]) {
|
||||
_list.insertAll(position, DC.float64ToBytes(value, endian));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -20,7 +20,6 @@
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
|
||||
import 'dart:typed_data';
|
||||
import 'dart:convert';
|
||||
import 'BinaryList.dart';
|
||||
@ -55,6 +54,28 @@ class DC with IterableMixin<int> {
|
||||
_dv = ByteData.view(_data.buffer);
|
||||
}
|
||||
|
||||
String toHex([String separator = " ", int? offset, int? length]) {
|
||||
var start = offset ?? 0;
|
||||
var count = length ?? _data.length - start;
|
||||
|
||||
if (count == 0) return "";
|
||||
|
||||
var rt = _data[start].toRadixString(16).padLeft(2, '0');
|
||||
|
||||
for (var i = start + 1; i < count; i++) {
|
||||
rt += separator + _data[i].toRadixString(16).padLeft(2, '0');
|
||||
}
|
||||
|
||||
return rt;
|
||||
}
|
||||
|
||||
DC.fromHex(String hex, [String separator = ' ']) {
|
||||
var list =
|
||||
hex.split(separator).map((e) => int.parse(e, radix: 16)).toList();
|
||||
_data = Uint8List.fromList(list);
|
||||
_dv = ByteData.view(_data.buffer);
|
||||
}
|
||||
|
||||
int operator [](int index) => _data[index];
|
||||
operator []=(int index, int value) => _data[index] = value;
|
||||
int get length => _data.length;
|
||||
@ -69,7 +90,7 @@ class DC with IterableMixin<int> {
|
||||
return rt;
|
||||
}
|
||||
|
||||
static DC boolToBytes(value) {
|
||||
static DC boolToBytes(bool value) {
|
||||
var rt = new DC(1);
|
||||
rt.setBoolean(0, value);
|
||||
return rt;
|
||||
@ -81,19 +102,7 @@ class DC with IterableMixin<int> {
|
||||
return rt;
|
||||
}
|
||||
|
||||
static DC guidArrayToBytes(List<Guid> value) {
|
||||
var rt = new DC(value.length * 16);
|
||||
for (var i = 0; i < value.length; i++) rt.setGuid(i * 16, value[i]);
|
||||
return rt;
|
||||
}
|
||||
|
||||
static DC boolArrayToBytes(List<bool> value) {
|
||||
var rt = new DC(value.length);
|
||||
for (var i = 0; i < value.length; i++) rt[i] = value[i] ? 1 : 0;
|
||||
return rt;
|
||||
}
|
||||
|
||||
static DC int8ToBytes(value) {
|
||||
static DC int8ToBytes(int value) {
|
||||
var rt = new DC(1);
|
||||
rt.setInt8(0, value);
|
||||
return rt;
|
||||
@ -105,7 +114,7 @@ class DC with IterableMixin<int> {
|
||||
return rt;
|
||||
}
|
||||
|
||||
static DC uint8ToBytes(value) {
|
||||
static DC uint8ToBytes(int value) {
|
||||
var rt = new DC(1);
|
||||
rt.setUint8(0, value);
|
||||
return rt;
|
||||
@ -123,28 +132,21 @@ class DC with IterableMixin<int> {
|
||||
return rt;
|
||||
}
|
||||
|
||||
static DC charArrayToBytes(Uint16List value) {
|
||||
var rt = new DC(value.length * 2);
|
||||
for (var i = 0; i < value.length; i++) rt.setChar(i * 2, value[i]);
|
||||
|
||||
return rt;
|
||||
}
|
||||
|
||||
static DC int16ToBytes(int value) {
|
||||
static DC int16ToBytes(int value, [Endian endian = Endian.little]) {
|
||||
var rt = new DC(2);
|
||||
rt.setInt16(0, value);
|
||||
rt.setInt16(0, value, endian);
|
||||
return rt;
|
||||
}
|
||||
|
||||
static DC int16ArrayToBytes(List<int> value) {
|
||||
static DC int16ArrayToBytes(Int16List value) {
|
||||
var rt = new DC(value.length * 2);
|
||||
for (var i = 0; i < value.length; i++) rt.setInt16(i * 2, value[i]);
|
||||
return rt;
|
||||
}
|
||||
|
||||
static DC uint16ToBytes(int value) {
|
||||
static DC uint16ToBytes(int value, [Endian endian = Endian.little]) {
|
||||
var rt = new DC(2);
|
||||
rt.setUint16(0, value);
|
||||
rt.setUint16(0, value, endian);
|
||||
return rt;
|
||||
}
|
||||
|
||||
@ -154,9 +156,9 @@ class DC with IterableMixin<int> {
|
||||
return rt;
|
||||
}
|
||||
|
||||
static DC int32ToBytes(int value) {
|
||||
static DC int32ToBytes(int value, [Endian endian = Endian.little]) {
|
||||
var rt = new DC(4);
|
||||
rt.setInt32(0, value);
|
||||
rt.setInt32(0, value, endian);
|
||||
return rt;
|
||||
}
|
||||
|
||||
@ -166,9 +168,9 @@ class DC with IterableMixin<int> {
|
||||
return rt;
|
||||
}
|
||||
|
||||
static DC uint32ToBytes(int value) {
|
||||
static DC uint32ToBytes(int value, [Endian endian = Endian.little]) {
|
||||
var rt = new DC(4);
|
||||
rt.setUint32(0, value);
|
||||
rt.setUint32(0, value, endian);
|
||||
return rt;
|
||||
}
|
||||
|
||||
@ -178,9 +180,9 @@ class DC with IterableMixin<int> {
|
||||
return rt;
|
||||
}
|
||||
|
||||
static DC float32ToBytes(double value) {
|
||||
static DC float32ToBytes(double value, [Endian endian = Endian.little]) {
|
||||
var rt = new DC(4);
|
||||
rt.setFloat32(0, value);
|
||||
rt.setFloat32(0, value, endian);
|
||||
return rt;
|
||||
}
|
||||
|
||||
@ -190,9 +192,9 @@ class DC with IterableMixin<int> {
|
||||
return rt;
|
||||
}
|
||||
|
||||
static DC int64ToBytes(int value) {
|
||||
static DC int64ToBytes(int value, [Endian endian = Endian.little]) {
|
||||
var rt = new DC(8);
|
||||
rt.setInt64(0, value);
|
||||
rt.setInt64(0, value, endian);
|
||||
return rt;
|
||||
}
|
||||
|
||||
@ -202,9 +204,9 @@ class DC with IterableMixin<int> {
|
||||
return rt;
|
||||
}
|
||||
|
||||
static DC uint64ToBytes(int value) {
|
||||
static DC uint64ToBytes(int value, [Endian endian = Endian.little]) {
|
||||
var rt = new DC(8);
|
||||
rt.setUint64(0, value);
|
||||
rt.setUint64(0, value, endian);
|
||||
return rt;
|
||||
}
|
||||
|
||||
@ -214,9 +216,9 @@ class DC with IterableMixin<int> {
|
||||
return rt;
|
||||
}
|
||||
|
||||
static DC float64ToBytes(double value) {
|
||||
static DC float64ToBytes(double value, [Endian endian = Endian.little]) {
|
||||
var rt = new DC(8);
|
||||
rt.setFloat64(0, value);
|
||||
rt.setFloat64(0, value, endian);
|
||||
return rt;
|
||||
}
|
||||
|
||||
@ -226,15 +228,15 @@ class DC with IterableMixin<int> {
|
||||
return rt;
|
||||
}
|
||||
|
||||
static DC dateTimeToBytes(DateTime value) {
|
||||
static DC dateTimeToBytes(DateTime value, [Endian endian = Endian.little]) {
|
||||
var rt = new DC(8);
|
||||
rt.setDateTime(0, value);
|
||||
rt.setDateTime(0, value, endian);
|
||||
return rt;
|
||||
}
|
||||
|
||||
static DC dateTimeArrayToBytes(List<DateTime> value) {
|
||||
static DC dateTimeArrayToBytes(List<DateTime> value, [Endian endian = Endian.little]) {
|
||||
var rt = new DC(value.length * 8);
|
||||
for (var i = 0; i < value.length; i++) rt.setDateTime(i * 8, value[i]);
|
||||
for (var i = 0; i < value.length; i++) rt.setDateTime(i * 8, value[i], endian);
|
||||
return rt;
|
||||
}
|
||||
|
||||
@ -244,26 +246,14 @@ class DC with IterableMixin<int> {
|
||||
return rt;
|
||||
}
|
||||
|
||||
static DC stringArrayToBytes(List<String> value) {
|
||||
var list = new BinaryList();
|
||||
for (var i = 0; i < value.length; i++) {
|
||||
var s = DC.stringToBytes(value[i]);
|
||||
list
|
||||
..addUint32(s.length)
|
||||
..addUint8Array(s.toArray());
|
||||
}
|
||||
|
||||
return list.toDC();
|
||||
}
|
||||
|
||||
DC append(DC src, int offset, int length) {
|
||||
//if (!(src is DC))
|
||||
// src = new DC(src);
|
||||
|
||||
var appendix = src.clip(offset, length);
|
||||
var rt = new DC(this.length + appendix.length);
|
||||
rt.set(this, 0);
|
||||
rt.set(appendix, this.length);
|
||||
rt.set(this, 0, 0, this.length);
|
||||
rt.set(appendix, 0, this.length, appendix.length);
|
||||
|
||||
this._data = rt._data;
|
||||
this._dv = rt._dv;
|
||||
@ -271,25 +261,25 @@ class DC with IterableMixin<int> {
|
||||
return this;
|
||||
}
|
||||
|
||||
set(DC dc, int offset) {
|
||||
_data.setRange(offset, offset + dc.length, dc._data);
|
||||
void set(DC src, int srcOffset, int dstOffset, int length) {
|
||||
_data.setRange(dstOffset, dstOffset + length, src._data, srcOffset);
|
||||
}
|
||||
|
||||
static combine(a, aOffset, aLength, b, bOffset, bLength) {
|
||||
if (!(a is DC)) a = new DC(a);
|
||||
if (!(b is DC)) b = new DC(b);
|
||||
static DC combine(a, int aOffset, int aLength, b, int bOffset, int bLength) {
|
||||
if (!(a is DC)) a = DC.fromList(a as List<int>);
|
||||
if (!(b is DC)) b = DC.fromList(b as List<int>);
|
||||
|
||||
a = a.clip(aOffset, aLength);
|
||||
b = b.clip(bOffset, bLength);
|
||||
|
||||
var rt = new DC(a.length + b.length);
|
||||
|
||||
rt.set(a, 0);
|
||||
rt.set(b, a.length);
|
||||
rt.set(a, 0, 0, a.length);
|
||||
rt.set(b, 0, a.length, b.length);
|
||||
return rt;
|
||||
}
|
||||
|
||||
DC clip(offset, length) {
|
||||
DC clip(int offset, int length) {
|
||||
return DC.fromUint8Array(
|
||||
Uint8List.fromList(_data.getRange(offset, offset + length).toList()));
|
||||
}
|
||||
@ -302,28 +292,28 @@ class DC with IterableMixin<int> {
|
||||
return _data[offset]; // this.dv.getUint8(offset);
|
||||
}
|
||||
|
||||
int getInt16(int offset) {
|
||||
return _dv.getInt16(offset);
|
||||
int getInt16(int offset, [Endian endian = Endian.little]) {
|
||||
return _dv.getInt16(offset, endian);
|
||||
}
|
||||
|
||||
int getUint16(int offset) {
|
||||
return _dv.getUint16(offset);
|
||||
int getUint16(int offset, [Endian endian = Endian.little]) {
|
||||
return _dv.getUint16(offset, endian);
|
||||
}
|
||||
|
||||
int getInt32(int offset) {
|
||||
return _dv.getInt32(offset);
|
||||
int getInt32(int offset, [Endian endian = Endian.little]) {
|
||||
return _dv.getInt32(offset, endian);
|
||||
}
|
||||
|
||||
int getUint32(int offset) {
|
||||
return _dv.getUint32(offset);
|
||||
int getUint32(int offset, [Endian endian = Endian.little]) {
|
||||
return _dv.getUint32(offset, endian);
|
||||
}
|
||||
|
||||
double getFloat32(int offset) {
|
||||
return _dv.getFloat32(offset);
|
||||
double getFloat32(int offset, [Endian endian = Endian.little]) {
|
||||
return _dv.getFloat32(offset, endian);
|
||||
}
|
||||
|
||||
double getFloat64(int offset) {
|
||||
return _dv.getFloat64(offset);
|
||||
double getFloat64(int offset, [Endian endian = Endian.little]) {
|
||||
return _dv.getFloat64(offset, endian);
|
||||
}
|
||||
|
||||
void setInt8(int offset, int value) {
|
||||
@ -334,28 +324,28 @@ class DC with IterableMixin<int> {
|
||||
return _dv.setUint8(offset, value);
|
||||
}
|
||||
|
||||
void setInt16(int offset, int value) {
|
||||
return _dv.setInt16(offset, value);
|
||||
void setInt16(int offset, int value, [Endian endian = Endian.little]) {
|
||||
return _dv.setInt16(offset, value, endian);
|
||||
}
|
||||
|
||||
void setUint16(int offset, int value) {
|
||||
return _dv.setUint16(offset, value);
|
||||
void setUint16(int offset, int value, [Endian endian = Endian.little]) {
|
||||
return _dv.setUint16(offset, value, endian);
|
||||
}
|
||||
|
||||
void setInt32(int offset, int value) {
|
||||
return _dv.setInt32(offset, value);
|
||||
void setInt32(int offset, int value, [Endian endian = Endian.little]) {
|
||||
return _dv.setInt32(offset, value, endian);
|
||||
}
|
||||
|
||||
void setUint32(int offset, int value) {
|
||||
return _dv.setUint32(offset, value);
|
||||
void setUint32(int offset, int value, [Endian endian = Endian.little]) {
|
||||
return _dv.setUint32(offset, value, endian);
|
||||
}
|
||||
|
||||
void setFloat32(int offset, double value) {
|
||||
return _dv.setFloat32(offset, value);
|
||||
void setFloat32(int offset, double value, [Endian endian = Endian.little]) {
|
||||
return _dv.setFloat32(offset, value, endian);
|
||||
}
|
||||
|
||||
void setFloat64(int offset, double value) {
|
||||
return _dv.setFloat64(offset, value);
|
||||
void setFloat64(int offset, double value, [Endian endian = Endian.little]) {
|
||||
return _dv.setFloat64(offset, value, endian);
|
||||
}
|
||||
|
||||
Int8List getInt8Array(int offset, int length) {
|
||||
@ -420,12 +410,6 @@ class DC with IterableMixin<int> {
|
||||
this.setUint8(offset, value ? 1 : 0);
|
||||
}
|
||||
|
||||
List<bool> getBooleanArray(int offset, int length) {
|
||||
List<bool> rt = [];
|
||||
for (var i = 0; i < length; i++) rt.add(this.getBoolean(offset + i));
|
||||
return rt;
|
||||
}
|
||||
|
||||
String getChar(int offset) {
|
||||
return String.fromCharCode(this.getUint16(offset));
|
||||
}
|
||||
@ -434,13 +418,7 @@ class DC with IterableMixin<int> {
|
||||
this.setUint16(offset, value); //value.codeUnitAt(0));
|
||||
}
|
||||
|
||||
List<String> getCharArray(int offset, int length) {
|
||||
List<String> rt = [];
|
||||
for (var i = 0; i < length; i += 2) rt.add(this.getChar(offset + i));
|
||||
return rt;
|
||||
}
|
||||
|
||||
String getHex(offset, length) {
|
||||
String getHex(int offset, int length) {
|
||||
var rt = "";
|
||||
for (var i = offset; i < offset + length; i++) {
|
||||
var h = _data[i].toRadixString(16);
|
||||
@ -461,13 +439,194 @@ class DC with IterableMixin<int> {
|
||||
|
||||
Uint8List toArray() => _data;
|
||||
|
||||
String getString(offset, length) {
|
||||
String getString(int offset, int length) {
|
||||
var bytes = clip(offset, length)._data; // toList(offset, length);
|
||||
var str = utf8.decode(bytes);
|
||||
return str;
|
||||
}
|
||||
|
||||
List<String> getStringArray(offset, length) {
|
||||
int getInt64(int offset, [Endian endian = Endian.little]) {
|
||||
if (kIsWeb) {
|
||||
if (endian == Endian.big) {
|
||||
var bi = BigInt.from(0);
|
||||
|
||||
bi |= BigInt.from(getUint8(offset++)) << 56;
|
||||
bi |= BigInt.from(getUint8(offset++)) << 48;
|
||||
bi |= BigInt.from(getUint8(offset++)) << 40;
|
||||
bi |= BigInt.from(getUint8(offset++)) << 32;
|
||||
bi |= BigInt.from(getUint8(offset++)) << 24;
|
||||
bi |= BigInt.from(getUint8(offset++)) << 16;
|
||||
bi |= BigInt.from(getUint8(offset++)) << 8;
|
||||
bi |= BigInt.from(getUint8(offset++));
|
||||
|
||||
return bi.toInt();
|
||||
} else {
|
||||
var bi = BigInt.from(0);
|
||||
|
||||
bi |= BigInt.from(getUint8(offset++));
|
||||
bi |= BigInt.from(getUint8(offset++)) << 8;
|
||||
bi |= BigInt.from(getUint8(offset++)) << 16;
|
||||
bi |= BigInt.from(getUint8(offset++)) << 24;
|
||||
bi |= BigInt.from(getUint8(offset++)) << 32;
|
||||
bi |= BigInt.from(getUint8(offset++)) << 40;
|
||||
bi |= BigInt.from(getUint8(offset++)) << 48;
|
||||
bi |= BigInt.from(getUint8(offset++)) << 56;
|
||||
|
||||
return bi.toInt();
|
||||
}
|
||||
|
||||
//var l = this.getUint32(offset);
|
||||
//var h = this.getUint32(offset + 4);
|
||||
//return h * TWO_PWR_32 + ((l >= 0) ? l : TWO_PWR_32 + l);
|
||||
} else {
|
||||
return _dv.getUint64(offset);
|
||||
}
|
||||
}
|
||||
|
||||
int getUint64(int offset, [Endian endian = Endian.little]) {
|
||||
if (kIsWeb) {
|
||||
if (endian == Endian.big) {
|
||||
var bi = BigInt.from(0);
|
||||
|
||||
bi |= BigInt.from(getUint8(offset++)) << 56;
|
||||
bi |= BigInt.from(getUint8(offset++)) << 48;
|
||||
bi |= BigInt.from(getUint8(offset++)) << 40;
|
||||
bi |= BigInt.from(getUint8(offset++)) << 32;
|
||||
bi |= BigInt.from(getUint8(offset++)) << 24;
|
||||
bi |= BigInt.from(getUint8(offset++)) << 16;
|
||||
bi |= BigInt.from(getUint8(offset++)) << 8;
|
||||
bi |= BigInt.from(getUint8(offset++));
|
||||
|
||||
return bi.toInt();
|
||||
} else {
|
||||
var bi = BigInt.from(0);
|
||||
|
||||
bi |= BigInt.from(getUint8(offset++));
|
||||
bi |= BigInt.from(getUint8(offset++)) << 8;
|
||||
bi |= BigInt.from(getUint8(offset++)) << 16;
|
||||
bi |= BigInt.from(getUint8(offset++)) << 24;
|
||||
bi |= BigInt.from(getUint8(offset++)) << 32;
|
||||
bi |= BigInt.from(getUint8(offset++)) << 40;
|
||||
bi |= BigInt.from(getUint8(offset++)) << 48;
|
||||
bi |= BigInt.from(getUint8(offset++)) << 56;
|
||||
|
||||
return bi.toInt();
|
||||
}
|
||||
|
||||
//var l = this.getUint32(offset);
|
||||
//var h = this.getUint32(offset + 4);
|
||||
//return h * TWO_PWR_32 + ((l >= 0) ? l : TWO_PWR_32 + l);
|
||||
} else {
|
||||
return _dv.getUint64(offset);
|
||||
}
|
||||
// if (kIsWeb) {
|
||||
// print("getUint64");
|
||||
// var l = this.getUint32(offset);
|
||||
// var h = this.getUint32(offset + 4);
|
||||
// return h * TWO_PWR_32 + ((l >= 0) ? l : TWO_PWR_32 + l);
|
||||
// } else {
|
||||
// return _dv.getInt64(offset);
|
||||
// }
|
||||
}
|
||||
|
||||
void setInt64(int offset, int value, [Endian endian = Endian.little]) {
|
||||
if (kIsWeb) {
|
||||
var bi = BigInt.from(value);
|
||||
var byte = BigInt.from(0xFF);
|
||||
|
||||
if (endian == Endian.big) {
|
||||
_dv.setUint8(offset++, ((bi >> 56) & byte).toInt());
|
||||
_dv.setUint8(offset++, ((bi >> 48) & byte).toInt());
|
||||
_dv.setUint8(offset++, ((bi >> 40) & byte).toInt());
|
||||
_dv.setUint8(offset++, ((bi >> 32) & byte).toInt());
|
||||
_dv.setUint8(offset++, ((bi >> 24) & byte).toInt());
|
||||
_dv.setUint8(offset++, ((bi >> 16) & byte).toInt());
|
||||
_dv.setUint8(offset++, ((bi >> 8) & byte).toInt());
|
||||
_dv.setUint8(offset++, (bi & byte).toInt());
|
||||
} else {
|
||||
_dv.setUint8(offset++, ((bi) & byte).toInt());
|
||||
_dv.setUint8(offset++, ((bi >> 8) & byte).toInt());
|
||||
_dv.setUint8(offset++, ((bi >> 16) & byte).toInt());
|
||||
_dv.setUint8(offset++, ((bi >> 24) & byte).toInt());
|
||||
_dv.setUint8(offset++, ((bi >> 32) & byte).toInt());
|
||||
_dv.setUint8(offset++, ((bi >> 40) & byte).toInt());
|
||||
_dv.setUint8(offset++, ((bi >> 48) & byte).toInt());
|
||||
_dv.setUint8(offset++, ((bi >> 56) & byte).toInt());
|
||||
}
|
||||
} else {
|
||||
_dv.setInt64(offset, value, endian);
|
||||
}
|
||||
}
|
||||
|
||||
void setUint64(int offset, int value, [Endian endian = Endian.little]) {
|
||||
if (kIsWeb) {
|
||||
// BigInt a = 33 as BigInt;
|
||||
|
||||
// int l = BigInt value & 0xFFFFFFFF;
|
||||
// int h = value >> 32;
|
||||
|
||||
// int h = (value % TWO_PWR_32) | 0;
|
||||
// int l = ((value / TWO_PWR_32)) | 0;
|
||||
// _dv.setInt32(offset, h, endian);
|
||||
// _dv.setInt32(offset + 4, l, endian);
|
||||
var bi = BigInt.from(value);
|
||||
var byte = BigInt.from(0xFF);
|
||||
|
||||
if (endian == Endian.big) {
|
||||
_dv.setUint8(offset++, ((bi >> 56) & byte).toInt());
|
||||
_dv.setUint8(offset++, ((bi >> 48) & byte).toInt());
|
||||
_dv.setUint8(offset++, ((bi >> 40) & byte).toInt());
|
||||
_dv.setUint8(offset++, ((bi >> 32) & byte).toInt());
|
||||
_dv.setUint8(offset++, ((bi >> 24) & byte).toInt());
|
||||
_dv.setUint8(offset++, ((bi >> 16) & byte).toInt());
|
||||
_dv.setUint8(offset++, ((bi >> 8) & byte).toInt());
|
||||
_dv.setUint8(offset++, (bi & byte).toInt());
|
||||
} else {
|
||||
_dv.setUint8(offset++, ((bi) & byte).toInt());
|
||||
_dv.setUint8(offset++, ((bi >> 8) & byte).toInt());
|
||||
_dv.setUint8(offset++, ((bi >> 16) & byte).toInt());
|
||||
_dv.setUint8(offset++, ((bi >> 24) & byte).toInt());
|
||||
_dv.setUint8(offset++, ((bi >> 32) & byte).toInt());
|
||||
_dv.setUint8(offset++, ((bi >> 40) & byte).toInt());
|
||||
_dv.setUint8(offset++, ((bi >> 48) & byte).toInt());
|
||||
_dv.setUint8(offset++, ((bi >> 56) & byte).toInt());
|
||||
}
|
||||
} else {
|
||||
_dv.setUint64(offset, value, endian);
|
||||
}
|
||||
}
|
||||
|
||||
void setDateTime(int offset, DateTime value, [Endian endian = Endian.little]) {
|
||||
// Unix Epoch
|
||||
var ticks = UNIX_EPOCH + (value.millisecondsSinceEpoch * 10000);
|
||||
this.setUint64(offset, ticks, endian);
|
||||
}
|
||||
|
||||
DateTime getDateTime(int offset, [Endian endian = Endian.little]) {
|
||||
var ticks = this.getUint64(offset, endian);
|
||||
// there are 10,000 ticks in a millisecond
|
||||
return DateTime.fromMillisecondsSinceEpoch((ticks - UNIX_EPOCH) ~/ 10000);
|
||||
}
|
||||
|
||||
Guid getGuid(int offset) {
|
||||
return new Guid(this.clip(offset, 16));
|
||||
}
|
||||
|
||||
void setGuid(int offset, Guid guid) {
|
||||
set(guid.value, 0, offset, 16);
|
||||
}
|
||||
|
||||
bool sequenceEqual(ar) {
|
||||
if (ar.length != this.length)
|
||||
return false;
|
||||
else {
|
||||
for (var i = 0; i < this.length; i++) if (ar[i] != this[i]) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
List<String> getStringArray(int offset, int length) {
|
||||
List<String> rt = [];
|
||||
var i = 0;
|
||||
|
||||
@ -481,80 +640,15 @@ class DC with IterableMixin<int> {
|
||||
return rt;
|
||||
}
|
||||
|
||||
int getInt64(offset) {
|
||||
if (kIsWeb) {
|
||||
var h = this.getUint32(offset);
|
||||
var l = this.getUint32(offset + 4);
|
||||
return h * TWO_PWR_32 + ((l >= 0) ? l : TWO_PWR_32 + l);
|
||||
} else {
|
||||
return _dv.getUint64(offset);
|
||||
}
|
||||
}
|
||||
|
||||
int getUint64(offset) {
|
||||
if (kIsWeb) {
|
||||
var h = this.getUint32(offset);
|
||||
var l = this.getUint32(offset + 4);
|
||||
return h * TWO_PWR_32 + ((l >= 0) ? l : TWO_PWR_32 + l);
|
||||
} else {
|
||||
return _dv.getInt64(offset);
|
||||
}
|
||||
}
|
||||
|
||||
void setInt64(offset, value) {
|
||||
_dv.setInt64(offset, value);
|
||||
}
|
||||
|
||||
void setUint64(offset, value) {
|
||||
if (kIsWeb) {
|
||||
var l = (value % TWO_PWR_32) | 0;
|
||||
var h = (value / TWO_PWR_32) | 0;
|
||||
_dv.setInt32(offset, h);
|
||||
_dv.setInt32(offset + 4, l);
|
||||
} else {
|
||||
_dv.setUint64(offset, value);
|
||||
}
|
||||
}
|
||||
|
||||
void setDateTime(offset, DateTime value) {
|
||||
// Unix Epoch
|
||||
var ticks = UNIX_EPOCH + (value.millisecondsSinceEpoch * 10000);
|
||||
this.setUint64(offset, ticks);
|
||||
}
|
||||
|
||||
DateTime getDateTime(int offset) {
|
||||
var ticks = this.getUint64(offset);
|
||||
// there are 10,000 ticks in a millisecond
|
||||
return DateTime.fromMillisecondsSinceEpoch((ticks - UNIX_EPOCH) ~/ 10000);
|
||||
}
|
||||
|
||||
List<DateTime> getDateTimeArray(int offset, int length) {
|
||||
List<DateTime> rt = [];
|
||||
for (var i = 0; i < length; i += 8) rt.add(this.getDateTime(offset + i));
|
||||
return rt;
|
||||
}
|
||||
|
||||
Guid getGuid(int offset) {
|
||||
return new Guid(this.clip(offset, 16));
|
||||
}
|
||||
|
||||
void setGuid(int offset, Guid guid) {
|
||||
set(guid.value, offset);
|
||||
}
|
||||
|
||||
List<Guid> getGuidArray(int offset, int length) {
|
||||
List<Guid> rt = [];
|
||||
for (var i = 0; i < length; i += 16) rt.add(this.getGuid(offset + i));
|
||||
return rt;
|
||||
}
|
||||
|
||||
bool sequenceEqual(ar) {
|
||||
if (ar.length != this.length)
|
||||
return false;
|
||||
else {
|
||||
for (var i = 0; i < this.length; i++) if (ar[i] != this[i]) return false;
|
||||
static DC stringArrayToBytes(List<String> value) {
|
||||
var list = new BinaryList();
|
||||
for (var i = 0; i < value.length; i++) {
|
||||
var s = DC.stringToBytes(value[i]);
|
||||
list
|
||||
..addUint32(s.length)
|
||||
..addUint8Array(s.toArray());
|
||||
}
|
||||
|
||||
return true;
|
||||
return list.toDC();
|
||||
}
|
||||
}
|
||||
|
508
lib/src/Data/DataDeserializer.dart
Normal file
508
lib/src/Data/DataDeserializer.dart
Normal file
@ -0,0 +1,508 @@
|
||||
import 'dart:core';
|
||||
|
||||
import 'IEnum.dart';
|
||||
|
||||
import '../Core/Tuple.dart';
|
||||
import '../Resource/Template/TemplateType.dart';
|
||||
import '../Resource/Warehouse.dart';
|
||||
|
||||
import '../../esiur.dart';
|
||||
import '../Core/AsyncBag.dart';
|
||||
|
||||
import '../Core/AsyncReply.dart';
|
||||
import 'DC.dart';
|
||||
import '../Net/IIP/DistributedConnection.dart';
|
||||
import 'NotModified.dart';
|
||||
import 'RepresentationType.dart';
|
||||
|
||||
class PropertyValueParserResults {
|
||||
final int size;
|
||||
final AsyncReply<PropertyValue> reply;
|
||||
|
||||
PropertyValueParserResults(this.size, this.reply);
|
||||
}
|
||||
|
||||
class DataDeserializer {
|
||||
static AsyncReply nullParser(
|
||||
DC data, int offset, int length, DistributedConnection? connection) {
|
||||
return AsyncReply.ready(null);
|
||||
}
|
||||
|
||||
static AsyncReply booleanTrueParser(
|
||||
DC data, int offset, int length, DistributedConnection? connection) {
|
||||
return new AsyncReply<bool>.ready(true);
|
||||
}
|
||||
|
||||
static AsyncReply booleanFalseParser(
|
||||
DC data, int offset, int length, DistributedConnection? connection) {
|
||||
return new AsyncReply<bool>.ready(false);
|
||||
}
|
||||
|
||||
static AsyncReply notModifiedParser(
|
||||
DC data, int offset, int length, DistributedConnection? connection) {
|
||||
return new AsyncReply<NotModified>.ready(NotModified());
|
||||
}
|
||||
|
||||
static AsyncReply byteParser(
|
||||
DC data, int offset, int length, DistributedConnection? connection) {
|
||||
return new AsyncReply<int>.ready(data[offset]);
|
||||
}
|
||||
|
||||
static AsyncReply sByteParser(
|
||||
DC data, int offset, int length, DistributedConnection? connection) {
|
||||
return new AsyncReply<int>.ready(
|
||||
data[offset] > 127 ? data[offset] - 256 : data[offset]);
|
||||
}
|
||||
|
||||
static AsyncReply char16Parser(
|
||||
DC data, int offset, int length, DistributedConnection? connection) {
|
||||
return AsyncReply<String>.ready(data.getChar(offset));
|
||||
}
|
||||
|
||||
static AsyncReply char8Parser(
|
||||
DC data, int offset, int length, DistributedConnection? connection) {
|
||||
return new AsyncReply<String>.ready(String.fromCharCode(data[offset]));
|
||||
}
|
||||
|
||||
static AsyncReply int16Parser(
|
||||
DC data, int offset, int length, DistributedConnection? connection) {
|
||||
return AsyncReply<int>.ready(data.getInt16(offset));
|
||||
}
|
||||
|
||||
static AsyncReply uInt16Parser(
|
||||
DC data, int offset, int length, DistributedConnection? connection) {
|
||||
return AsyncReply<int>.ready(data.getUint16(offset));
|
||||
}
|
||||
|
||||
static AsyncReply int32Parser(
|
||||
DC data, int offset, int length, DistributedConnection? connection) {
|
||||
return AsyncReply<int>.ready(data.getInt32(offset));
|
||||
}
|
||||
|
||||
static AsyncReply uInt32Parser(
|
||||
DC data, int offset, int length, DistributedConnection? connection) {
|
||||
return AsyncReply<int>.ready(data.getUint32(offset));
|
||||
}
|
||||
|
||||
static AsyncReply float32Parser(
|
||||
DC data, int offset, int length, DistributedConnection? connection) {
|
||||
return AsyncReply<double>.ready(data.getFloat32(offset));
|
||||
}
|
||||
|
||||
static AsyncReply float64Parser(
|
||||
DC data, int offset, int length, DistributedConnection? connection) {
|
||||
return AsyncReply<double>.ready(data.getFloat64(offset));
|
||||
}
|
||||
|
||||
static AsyncReply float128Parser(
|
||||
DC data, int offset, int length, DistributedConnection? connection) {
|
||||
// @TODO
|
||||
return AsyncReply<double>.ready(data.getFloat64(offset));
|
||||
}
|
||||
|
||||
static AsyncReply int128Parser(
|
||||
DC data, int offset, int length, DistributedConnection? connection) {
|
||||
// @TODO
|
||||
return AsyncReply<int>.ready(data.getInt64(offset));
|
||||
}
|
||||
|
||||
static AsyncReply uInt128Parser(
|
||||
DC data, int offset, int length, DistributedConnection? connection) {
|
||||
return AsyncReply<int>.ready(data.getUint64(offset));
|
||||
}
|
||||
|
||||
static AsyncReply int64Parser(
|
||||
DC data, int offset, int length, DistributedConnection? connection) {
|
||||
return AsyncReply<int>.ready(data.getInt64(offset));
|
||||
}
|
||||
|
||||
static AsyncReply uInt64Parser(
|
||||
DC data, int offset, int length, DistributedConnection? connection) {
|
||||
return AsyncReply<int>.ready(data.getUint64(offset));
|
||||
}
|
||||
|
||||
static AsyncReply dateTimeParser(
|
||||
DC data, int offset, int length, DistributedConnection? connection) {
|
||||
return AsyncReply<DateTime>.ready(data.getDateTime(offset));
|
||||
}
|
||||
|
||||
static AsyncReply resourceParser(
|
||||
DC data, int offset, int length, DistributedConnection? connection) {
|
||||
if (connection != null) {
|
||||
var id = data.getUint32(offset);
|
||||
return connection.fetch(id);
|
||||
}
|
||||
throw Exception("Can't parse resource with no connection");
|
||||
}
|
||||
|
||||
static AsyncReply localResourceParser(
|
||||
DC data, int offset, int length, DistributedConnection? connection) {
|
||||
var id = data.getUint32(offset);
|
||||
return Warehouse.getById(id);
|
||||
}
|
||||
|
||||
static AsyncReply rawDataParser(
|
||||
DC data, int offset, int length, DistributedConnection? connection) {
|
||||
return new AsyncReply<DC>.ready(data.clip(offset, length));
|
||||
}
|
||||
|
||||
static AsyncReply stringParser(
|
||||
DC data, int offset, int length, DistributedConnection? connection) {
|
||||
return new AsyncReply<String>.ready(data.getString(offset, length));
|
||||
}
|
||||
|
||||
static AsyncReply recordParser(
|
||||
DC data, int offset, int length, DistributedConnection? connection) {
|
||||
var reply = new AsyncReply<IRecord>();
|
||||
|
||||
var classId = data.getGuid(offset);
|
||||
offset += 16;
|
||||
length -= 16;
|
||||
|
||||
var template = Warehouse.getTemplateByClassId(classId, TemplateType.Record);
|
||||
|
||||
if (template != null) {
|
||||
listParser(data, offset, length, connection).then((r) {
|
||||
var ar = r as List;
|
||||
IRecord record;
|
||||
|
||||
if (template.definedType != null) {
|
||||
record = Warehouse.createInstance(template.definedType!) as IRecord;
|
||||
} else {
|
||||
record = Record();
|
||||
}
|
||||
|
||||
var kv = Map<String, dynamic>();
|
||||
|
||||
for (var i = 0; i < template.properties.length; i++)
|
||||
kv[template.properties[i].name] = ar[i];
|
||||
|
||||
record.deserialize(kv);
|
||||
|
||||
reply.trigger(record);
|
||||
});
|
||||
} else {
|
||||
if (connection == null)
|
||||
throw Exception("Can't parse record with no connection");
|
||||
|
||||
connection.getTemplate(classId).then((tmp) {
|
||||
if (tmp == null)
|
||||
reply.triggerError(Exception("Couldn't fetch record template."));
|
||||
|
||||
listParser(data, offset, length, connection).then((r) {
|
||||
var ar = r as List;
|
||||
|
||||
var record = new Record();
|
||||
|
||||
var kv = Map<String, dynamic>();
|
||||
|
||||
for (var i = 0; i < tmp!.properties.length; i++)
|
||||
kv[tmp.properties[i].name] = ar[i];
|
||||
|
||||
record.deserialize(kv);
|
||||
|
||||
reply.trigger(record);
|
||||
});
|
||||
}).error((x) => reply.triggerError(x));
|
||||
}
|
||||
|
||||
return reply;
|
||||
}
|
||||
|
||||
static AsyncReply constantParser(
|
||||
DC data, int offset, int length, DistributedConnection? connection) {
|
||||
throw Exception("NotImplementedException");
|
||||
}
|
||||
|
||||
static AsyncReply enumParser(
|
||||
DC data, int offset, int length, DistributedConnection? connection) {
|
||||
var classId = data.getGuid(offset);
|
||||
offset += 16;
|
||||
var index = data[offset++];
|
||||
|
||||
var template = Warehouse.getTemplateByClassId(classId, TemplateType.Enum);
|
||||
|
||||
if (template != null) {
|
||||
if (template.definedType != null) {
|
||||
var enumVal = Warehouse.createInstance(template.definedType!) as IEnum;
|
||||
enumVal.index = index;
|
||||
enumVal.name = template.constants[index].name;
|
||||
enumVal.value = template.constants[index].value;
|
||||
return new AsyncReply.ready(enumVal);
|
||||
} else {
|
||||
return AsyncReply.ready(IEnum(index, template.constants[index].value,
|
||||
template.constants[index].name));
|
||||
}
|
||||
} else {
|
||||
var reply = new AsyncReply();
|
||||
|
||||
if (connection == null)
|
||||
throw Exception("Can't parse enum with no connection");
|
||||
connection.getTemplate(classId).then((tmp) {
|
||||
if (tmp != null) {
|
||||
if (tmp.definedType != null) {
|
||||
var enumVal = Warehouse.createInstance(tmp.definedType!) as IEnum;
|
||||
enumVal.index = index;
|
||||
enumVal.name = tmp.constants[index].name;
|
||||
enumVal.value = tmp.constants[index].value;
|
||||
reply.trigger(enumVal);
|
||||
} else {
|
||||
reply.trigger(IEnum(
|
||||
index, tmp.constants[index].value, tmp.constants[index].name));
|
||||
}
|
||||
} else
|
||||
reply.triggerError(Exception("Template not found for enum"));
|
||||
}).error((x) => reply.triggerError(x));
|
||||
|
||||
return reply;
|
||||
}
|
||||
}
|
||||
|
||||
static AsyncReply recordListParser(
|
||||
DC data, int offset, int length, DistributedConnection? connection) {
|
||||
var rt = new AsyncBag();
|
||||
|
||||
while (length > 0) {
|
||||
var parsed = Codec.parse(data, offset, connection);
|
||||
|
||||
rt.add(parsed.reply);
|
||||
|
||||
if (parsed.size > 0) {
|
||||
offset += parsed.size;
|
||||
length -= parsed.size;
|
||||
} else
|
||||
throw new Exception("Error while parsing structured data");
|
||||
}
|
||||
|
||||
rt.seal();
|
||||
return rt;
|
||||
}
|
||||
|
||||
static AsyncReply resourceListParser(
|
||||
DC data, int offset, int length, DistributedConnection? connection) {
|
||||
var rt = new AsyncBag();
|
||||
|
||||
while (length > 0) {
|
||||
var parsed = Codec.parse(data, offset, connection);
|
||||
|
||||
rt.add(parsed.reply);
|
||||
|
||||
if (parsed.size > 0) {
|
||||
offset += parsed.size;
|
||||
length -= parsed.size;
|
||||
} else
|
||||
throw new Exception("Error while parsing structured data");
|
||||
}
|
||||
|
||||
rt.seal();
|
||||
return rt;
|
||||
}
|
||||
|
||||
static AsyncBag listParser(
|
||||
DC data, int offset, int length, DistributedConnection? connection) {
|
||||
var rt = new AsyncBag();
|
||||
|
||||
while (length > 0) {
|
||||
var parsed = Codec.parse(data, offset, connection);
|
||||
|
||||
rt.add(parsed.reply);
|
||||
|
||||
if (parsed.size > 0) {
|
||||
offset += parsed.size;
|
||||
length -= parsed.size;
|
||||
} else
|
||||
throw new Exception("Error while parsing structured data");
|
||||
}
|
||||
|
||||
rt.seal();
|
||||
return rt;
|
||||
}
|
||||
|
||||
static AsyncReply typedMapParser(
|
||||
DC data, int offset, int length, DistributedConnection? connection) {
|
||||
// get key type
|
||||
var keyRep = RepresentationType.parse(data, offset);
|
||||
offset += keyRep.size;
|
||||
length -= keyRep.size;
|
||||
|
||||
var valueRep = RepresentationType.parse(data, offset);
|
||||
offset += valueRep.size;
|
||||
length -= valueRep.size;
|
||||
|
||||
|
||||
|
||||
var map = Map();
|
||||
var rt = new AsyncReply();
|
||||
|
||||
var results = new AsyncBag();
|
||||
|
||||
while (length > 0) {
|
||||
var parsed = Codec.parse(data, offset, connection);
|
||||
|
||||
results.add(parsed.reply);
|
||||
|
||||
if (parsed.size > 0) {
|
||||
offset += parsed.size;
|
||||
length -= parsed.size;
|
||||
} else
|
||||
throw new Exception("Error while parsing structured data");
|
||||
}
|
||||
|
||||
results.seal();
|
||||
|
||||
results.then((ar) {
|
||||
for (var i = 0; i < ar.length; i += 2) map[ar[i]] = ar[i + 1];
|
||||
|
||||
rt.trigger(map);
|
||||
});
|
||||
|
||||
return rt;
|
||||
}
|
||||
|
||||
static AsyncReply tupleParser(
|
||||
DC data, int offset, int length, DistributedConnection? connection) {
|
||||
var results = new AsyncBag();
|
||||
var rt = new AsyncReply();
|
||||
|
||||
var tupleSize = data[offset++];
|
||||
length--;
|
||||
|
||||
var types = <Type>[];
|
||||
|
||||
for (var i = 0; i < tupleSize; i++) {
|
||||
var rep = RepresentationType.parse(data, offset);
|
||||
if (rep.type != null) types.add(rep.type.getRuntimeType() ?? Object);
|
||||
offset += rep.size;
|
||||
length -= rep.size;
|
||||
}
|
||||
|
||||
while (length > 0) {
|
||||
var parsed = Codec.parse(data, offset, connection);
|
||||
|
||||
results.add(parsed.reply);
|
||||
|
||||
if (parsed.size > 0) {
|
||||
offset += parsed.size;
|
||||
length -= parsed.size;
|
||||
} else
|
||||
throw new Exception("Error while parsing structured data");
|
||||
}
|
||||
|
||||
results.seal();
|
||||
|
||||
results.then((ar) {
|
||||
rt.trigger(Tuple(ar));
|
||||
});
|
||||
|
||||
return rt;
|
||||
}
|
||||
|
||||
static AsyncReply typedListParser(
|
||||
DC data, int offset, int length, DistributedConnection? connection) {
|
||||
var rt = new AsyncBag();
|
||||
|
||||
// get the type
|
||||
var rep = RepresentationType.parse(data, offset);
|
||||
|
||||
offset += rep.size;
|
||||
length -= rep.size;
|
||||
|
||||
var runtimeType = rep.type.getRuntimeType();
|
||||
|
||||
rt.arrayType = runtimeType;
|
||||
|
||||
while (length > 0) {
|
||||
var parsed = Codec.parse(data, offset, connection);
|
||||
|
||||
rt.add(parsed.reply);
|
||||
|
||||
if (parsed.size > 0) {
|
||||
offset += parsed.size;
|
||||
length -= parsed.size;
|
||||
} else
|
||||
throw new Exception("Error while parsing structured data");
|
||||
}
|
||||
|
||||
rt.seal();
|
||||
return rt;
|
||||
}
|
||||
|
||||
static AsyncBag<PropertyValue> PropertyValueArrayParser(
|
||||
DC data,
|
||||
int offset,
|
||||
int length,
|
||||
DistributedConnection? connection) //, bool ageIncluded = true)
|
||||
{
|
||||
var rt = new AsyncBag<PropertyValue>();
|
||||
|
||||
listParser(data, offset, length, connection).then((x) {
|
||||
var pvs = <PropertyValue>[];
|
||||
|
||||
for (var i = 0; i < x.length; i += 3)
|
||||
pvs.add(new PropertyValue(x[2], x[0] as int, x[1] as DateTime));
|
||||
|
||||
rt.trigger(pvs);
|
||||
});
|
||||
|
||||
return rt;
|
||||
}
|
||||
|
||||
static PropertyValueParserResults propertyValueParser(DC data, int offset,
|
||||
DistributedConnection? connection) //, bool ageIncluded = true)
|
||||
{
|
||||
var reply = new AsyncReply<PropertyValue>();
|
||||
|
||||
var age = data.getUint64(offset);
|
||||
offset += 8;
|
||||
|
||||
DateTime date = data.getDateTime(offset);
|
||||
offset += 8;
|
||||
|
||||
var parsed = Codec.parse(data, offset, connection);
|
||||
|
||||
parsed.reply.then((value) {
|
||||
reply.trigger(new PropertyValue(value, age, date));
|
||||
});
|
||||
|
||||
return PropertyValueParserResults(16 + parsed.size, reply);
|
||||
}
|
||||
|
||||
static AsyncReply<KeyList<PropertyTemplate, List<PropertyValue>>>
|
||||
historyParser(DC data, int offset, int length, IResource resource,
|
||||
DistributedConnection? connection) {
|
||||
throw Exception("Not implemented");
|
||||
// @TODO
|
||||
// var list = new KeyList<PropertyTemplate, List<PropertyValue>>();
|
||||
|
||||
// var reply = new AsyncReply<KeyList<PropertyTemplate, List<PropertyValue[]>>>();
|
||||
|
||||
// var bagOfBags = new AsyncBag<PropertyValue[]>();
|
||||
|
||||
// var ends = offset + length;
|
||||
// while (offset < ends)
|
||||
// {
|
||||
// var index = data[offset++];
|
||||
// var pt = resource.Instance.Template.GetPropertyTemplateByIndex(index);
|
||||
// list.Add(pt, null);
|
||||
// var cs = data.GetUInt32(offset);
|
||||
// offset += 4;
|
||||
|
||||
// var (len, pv) = PropertyValueParser(data, offset, connection);
|
||||
|
||||
// bagOfBags.Add(pv);// ParsePropertyValueArray(data, offset, cs, connection));
|
||||
// offset += len;
|
||||
// }
|
||||
|
||||
// bagOfBags.Seal();
|
||||
|
||||
// bagOfBags.Then(x =>
|
||||
// {
|
||||
// for (var i = 0; i < list.Count; i++)
|
||||
// list[list.Keys.ElementAt(i)] = x[i];
|
||||
|
||||
// reply.Trigger(list);
|
||||
// });
|
||||
|
||||
// return reply;
|
||||
}
|
||||
}
|
406
lib/src/Data/DataSerializer.dart
Normal file
406
lib/src/Data/DataSerializer.dart
Normal file
@ -0,0 +1,406 @@
|
||||
import 'BinaryList.dart';
|
||||
import 'Codec.dart';
|
||||
import 'IRecord.dart';
|
||||
import '../Net/IIP/DistributedResource.dart';
|
||||
import '../Resource/IResource.dart';
|
||||
import '../Resource/Warehouse.dart';
|
||||
|
||||
import '../Resource/Template/PropertyTemplate.dart';
|
||||
|
||||
import 'PropertyValue.dart';
|
||||
|
||||
import './TransmissionType.dart';
|
||||
import '../Net/IIP/DistributedConnection.dart';
|
||||
|
||||
import 'DC.dart';
|
||||
import 'RepresentationType.dart';
|
||||
import 'IntType.dart';
|
||||
|
||||
class DataSerializerComposeResults {
|
||||
int identifier;
|
||||
DC data;
|
||||
|
||||
DataSerializerComposeResults(this.identifier, this.data);
|
||||
}
|
||||
|
||||
class DataSerializer {
|
||||
//public delegate byte[] Serializer(object value);
|
||||
|
||||
static DC historyComposer(Map<PropertyTemplate, List<PropertyValue>> history,
|
||||
DistributedConnection connection,
|
||||
[bool prependLength = false]) {
|
||||
throw Exception("Not implemented");
|
||||
}
|
||||
|
||||
static DataSerializerComposeResults int32Composer(
|
||||
value, DistributedConnection? connection) {
|
||||
var rt = new DC(4);
|
||||
rt.setInt32(0, (value as Int32).toInt());
|
||||
return DataSerializerComposeResults(TransmissionTypeIdentifier.Int32, rt);
|
||||
}
|
||||
|
||||
static DataSerializerComposeResults uInt32Composer(
|
||||
value, DistributedConnection? connection) {
|
||||
var rt = new DC(4);
|
||||
rt.setUint32(0, (value as UInt32).toInt());
|
||||
return DataSerializerComposeResults(TransmissionTypeIdentifier.UInt32, rt);
|
||||
}
|
||||
|
||||
static DataSerializerComposeResults int16Composer(
|
||||
value, DistributedConnection? connection) {
|
||||
var rt = new DC(2);
|
||||
rt.setInt16(0, (value as Int16).toInt());
|
||||
return DataSerializerComposeResults(TransmissionTypeIdentifier.Int16, rt);
|
||||
}
|
||||
|
||||
static DataSerializerComposeResults uInt16Composer(
|
||||
value, DistributedConnection? connection) {
|
||||
var rt = new DC(2);
|
||||
rt.setUint16(0, (value as UInt16).toInt());
|
||||
return DataSerializerComposeResults(TransmissionTypeIdentifier.UInt16, rt);
|
||||
}
|
||||
|
||||
static DataSerializerComposeResults float32Composer(
|
||||
value, DistributedConnection? connection) {
|
||||
var rt = new DC(4);
|
||||
rt.setFloat32(0, value as double);
|
||||
return DataSerializerComposeResults(TransmissionTypeIdentifier.Float32, rt);
|
||||
}
|
||||
|
||||
static DataSerializerComposeResults float64Composer(
|
||||
value, DistributedConnection? connection) {
|
||||
var rt = new DC(8);
|
||||
rt.setFloat64(0, value as double);
|
||||
return DataSerializerComposeResults(TransmissionTypeIdentifier.Float64, rt);
|
||||
}
|
||||
|
||||
static DataSerializerComposeResults int64Composer(
|
||||
value, DistributedConnection? connection) {
|
||||
var rt = new DC(8);
|
||||
rt.setInt64(0, value as int);
|
||||
return DataSerializerComposeResults(TransmissionTypeIdentifier.Int64, rt);
|
||||
}
|
||||
|
||||
static DataSerializerComposeResults uInt64Composer(
|
||||
value, DistributedConnection? connection) {
|
||||
var rt = new DC(8);
|
||||
rt.setUint64(0, value as int);
|
||||
return DataSerializerComposeResults(TransmissionTypeIdentifier.UInt64, rt);
|
||||
}
|
||||
|
||||
static DataSerializerComposeResults dateTimeComposer(
|
||||
value, DistributedConnection? connection) {
|
||||
var rt = new DC(8);
|
||||
rt.setDateTime(0, value as DateTime);
|
||||
return DataSerializerComposeResults(
|
||||
TransmissionTypeIdentifier.DateTime, rt);
|
||||
}
|
||||
|
||||
static DataSerializerComposeResults float128Composer(
|
||||
value, DistributedConnection? connection) {
|
||||
//@TODO: implement decimal
|
||||
var rt = new DC(16);
|
||||
rt.setFloat64(0, value as double);
|
||||
return DataSerializerComposeResults(TransmissionTypeIdentifier.Float64, rt);
|
||||
}
|
||||
|
||||
static DataSerializerComposeResults stringComposer(
|
||||
value, DistributedConnection? connection) {
|
||||
return DataSerializerComposeResults(
|
||||
TransmissionTypeIdentifier.String, DC.stringToBytes(value as String));
|
||||
}
|
||||
|
||||
static DataSerializerComposeResults enumComposer(
|
||||
value, DistributedConnection? connection) {
|
||||
if (value == null)
|
||||
return DataSerializerComposeResults(
|
||||
TransmissionTypeIdentifier.Null, DC(0));
|
||||
|
||||
var template = Warehouse.getTemplateByType(value.runtimeType);
|
||||
|
||||
if (template == null)
|
||||
return DataSerializerComposeResults(
|
||||
TransmissionTypeIdentifier.Null, DC(0));
|
||||
|
||||
var cts = template.constants.where((x) => x.value == value);
|
||||
|
||||
if (cts.isEmpty)
|
||||
return DataSerializerComposeResults(
|
||||
TransmissionTypeIdentifier.Null, DC(0));
|
||||
|
||||
var rt = BinaryList();
|
||||
|
||||
rt.addGuid(template.classId);
|
||||
rt.addUint8(cts.first.index);
|
||||
|
||||
return DataSerializerComposeResults(
|
||||
TransmissionTypeIdentifier.Enum, rt.toDC());
|
||||
}
|
||||
|
||||
static DataSerializerComposeResults uInt8Composer(
|
||||
value, DistributedConnection? connection) {
|
||||
var rt = new DC(1);
|
||||
rt[0] = (value as UInt8).toInt();
|
||||
return DataSerializerComposeResults(TransmissionTypeIdentifier.UInt8, rt);
|
||||
}
|
||||
|
||||
static DataSerializerComposeResults int8Composer(
|
||||
value, DistributedConnection? connection) {
|
||||
var rt = new DC(1);
|
||||
rt[0] = (value as Int8).toInt();
|
||||
return DataSerializerComposeResults(TransmissionTypeIdentifier.Int8, rt);
|
||||
}
|
||||
|
||||
static DataSerializerComposeResults char8Composer(
|
||||
value, DistributedConnection? connection) {
|
||||
var rt = new DC(1);
|
||||
rt[0] = value as int;
|
||||
return DataSerializerComposeResults(TransmissionTypeIdentifier.Char8, rt);
|
||||
}
|
||||
|
||||
static DataSerializerComposeResults char16Composer(
|
||||
value, DistributedConnection? connection) {
|
||||
var rt = new DC(2);
|
||||
rt.setUint16(0, value as int);
|
||||
return DataSerializerComposeResults(TransmissionTypeIdentifier.Char16, rt);
|
||||
}
|
||||
|
||||
static DataSerializerComposeResults boolComposer(
|
||||
value, DistributedConnection? connection) {
|
||||
return DataSerializerComposeResults(
|
||||
value as bool
|
||||
? TransmissionTypeIdentifier.True
|
||||
: TransmissionTypeIdentifier.False,
|
||||
DC(0));
|
||||
}
|
||||
|
||||
static DataSerializerComposeResults notModifiedComposer(
|
||||
value, DistributedConnection? connection) {
|
||||
return DataSerializerComposeResults(
|
||||
TransmissionTypeIdentifier.NotModified, DC(0));
|
||||
}
|
||||
|
||||
static DataSerializerComposeResults rawDataComposer(
|
||||
value, DistributedConnection? connection) {
|
||||
return DataSerializerComposeResults(
|
||||
TransmissionTypeIdentifier.RawData, value as DC);
|
||||
}
|
||||
|
||||
static DataSerializerComposeResults listComposer(
|
||||
value, DistributedConnection? connection) {
|
||||
if (value == null)
|
||||
return DataSerializerComposeResults(
|
||||
TransmissionTypeIdentifier.Null, DC(0));
|
||||
else
|
||||
return DataSerializerComposeResults(TransmissionTypeIdentifier.List,
|
||||
arrayComposer(value as List, connection));
|
||||
|
||||
//var rt = new List<byte>();
|
||||
//var list = (IEnumerable)value;// ((List<object>)value);
|
||||
|
||||
//foreach (var o in list)
|
||||
// rt.AddRange(Codec.Compose(o, connection));
|
||||
|
||||
//return (TransmissionTypeIdentifier.List, rt.ToArray());
|
||||
}
|
||||
|
||||
static DataSerializerComposeResults typedListComposer(
|
||||
value, Type type, DistributedConnection? connection) {
|
||||
if (value == null)
|
||||
return DataSerializerComposeResults(
|
||||
TransmissionTypeIdentifier.Null, DC(0));
|
||||
|
||||
var composed = arrayComposer(value as List, connection);
|
||||
|
||||
var header =
|
||||
(RepresentationType.fromType(type) ?? RepresentationType.Dynamic)
|
||||
.compose();
|
||||
|
||||
var rt = new BinaryList()
|
||||
..addDC(header)
|
||||
..addDC(composed);
|
||||
|
||||
return DataSerializerComposeResults(
|
||||
TransmissionTypeIdentifier.TypedList, rt.toDC());
|
||||
}
|
||||
|
||||
static DataSerializerComposeResults propertyValueArrayComposer(
|
||||
value, DistributedConnection? connection) {
|
||||
if (value == null)
|
||||
return DataSerializerComposeResults(
|
||||
TransmissionTypeIdentifier.Null, DC(0));
|
||||
|
||||
var rt = BinaryList();
|
||||
var ar = value as List<PropertyValue>;
|
||||
|
||||
for (var pv in ar) {
|
||||
rt.addDC(Codec.compose(pv.age, connection));
|
||||
rt.addDC(Codec.compose(pv.date, connection));
|
||||
rt.addDC(Codec.compose(pv.value, connection));
|
||||
}
|
||||
|
||||
return DataSerializerComposeResults(
|
||||
TransmissionTypeIdentifier.List, rt.toDC());
|
||||
}
|
||||
|
||||
static DataSerializerComposeResults typedMapComposer(
|
||||
value, Type keyType, Type valueType, DistributedConnection? connection) {
|
||||
if (value == null)
|
||||
return DataSerializerComposeResults(
|
||||
TransmissionTypeIdentifier.Null, DC(0));
|
||||
|
||||
var kt =
|
||||
(RepresentationType.fromType(keyType) ?? RepresentationType.Dynamic)
|
||||
.compose();
|
||||
var vt =
|
||||
(RepresentationType.fromType(valueType) ?? RepresentationType.Dynamic)
|
||||
.compose();
|
||||
|
||||
var rt = BinaryList();
|
||||
|
||||
rt.addDC(kt);
|
||||
rt.addDC(vt);
|
||||
|
||||
var map = value as Map;
|
||||
|
||||
for (var el in map.entries) {
|
||||
rt.addDC(Codec.compose(el.key, connection));
|
||||
rt.addDC(Codec.compose(el.value, connection));
|
||||
}
|
||||
|
||||
return DataSerializerComposeResults(
|
||||
TransmissionTypeIdentifier.TypedMap, rt.toDC());
|
||||
}
|
||||
|
||||
static DC arrayComposer(List value, DistributedConnection? connection) {
|
||||
var rt = BinaryList();
|
||||
|
||||
for (var i in value) rt.addDC(Codec.compose(i, connection));
|
||||
|
||||
return rt.toDC();
|
||||
}
|
||||
|
||||
static DataSerializerComposeResults resourceListComposer(
|
||||
value, DistributedConnection? connection) {
|
||||
if (value == null)
|
||||
return DataSerializerComposeResults(
|
||||
TransmissionTypeIdentifier.Null, DC(0));
|
||||
|
||||
return DataSerializerComposeResults(TransmissionTypeIdentifier.ResourceList,
|
||||
arrayComposer(value as List, connection));
|
||||
}
|
||||
|
||||
static DataSerializerComposeResults recordListComposer(
|
||||
value, DistributedConnection? connection) {
|
||||
if (value == null)
|
||||
return DataSerializerComposeResults(
|
||||
TransmissionTypeIdentifier.Null, DC(0));
|
||||
|
||||
return DataSerializerComposeResults(TransmissionTypeIdentifier.RecordList,
|
||||
arrayComposer(value as List, connection));
|
||||
}
|
||||
|
||||
static DataSerializerComposeResults resourceComposer(
|
||||
value, DistributedConnection? connection) {
|
||||
var resource = value as IResource;
|
||||
var rt = new DC(4);
|
||||
|
||||
if (Codec.isLocalResource(resource, connection)) {
|
||||
rt.setUint32(0, (resource as DistributedResource).id ?? 0);
|
||||
return DataSerializerComposeResults(
|
||||
TransmissionTypeIdentifier.ResourceLocal, rt);
|
||||
} else {
|
||||
// @TODO: connection.cache.Add(value as IResource, DateTime.UtcNow);
|
||||
rt.setUint32(0, resource.instance?.id ?? 0);
|
||||
return DataSerializerComposeResults(
|
||||
TransmissionTypeIdentifier.Resource, rt);
|
||||
}
|
||||
}
|
||||
|
||||
static DataSerializerComposeResults mapComposer(
|
||||
value, DistributedConnection? connection) {
|
||||
if (value == null)
|
||||
return DataSerializerComposeResults(
|
||||
TransmissionTypeIdentifier.Null, DC(0));
|
||||
|
||||
var rt = BinaryList();
|
||||
var map = value as Map;
|
||||
|
||||
for (var el in map.entries) {
|
||||
rt.addDC(Codec.compose(el.key, connection));
|
||||
rt.addDC(Codec.compose(el.value, connection));
|
||||
}
|
||||
|
||||
return DataSerializerComposeResults(
|
||||
TransmissionTypeIdentifier.Map, rt.toDC());
|
||||
}
|
||||
|
||||
static DataSerializerComposeResults recordComposer(
|
||||
value, DistributedConnection? connection) {
|
||||
var rt = BinaryList();
|
||||
var record = value as IRecord;
|
||||
|
||||
var template = Warehouse.getTemplateByType(record.runtimeType);
|
||||
|
||||
if (template == null)
|
||||
return DataSerializerComposeResults(
|
||||
TransmissionTypeIdentifier.Null, DC(0));
|
||||
|
||||
rt.addDC(DC.guidToBytes(template.classId));
|
||||
|
||||
var recordData = record.serialize();
|
||||
|
||||
for (var pt in template.properties) {
|
||||
var propValue = recordData[pt.name];
|
||||
rt.addDC(Codec.compose(propValue, connection));
|
||||
}
|
||||
|
||||
return DataSerializerComposeResults(
|
||||
TransmissionTypeIdentifier.Record, rt.toDC());
|
||||
}
|
||||
|
||||
// TODO:
|
||||
// static DataSerializerComposeResults historyComposer(KeyList<PropertyTemplate, PropertyValue[]> history,
|
||||
// DistributedConnection connection, bool prependLength = false)
|
||||
// {
|
||||
// //@TODO:Test
|
||||
// var rt = new BinaryList();
|
||||
|
||||
// for (var i = 0; i < history.Count; i++)
|
||||
// rt.AddUInt8(history.Keys.ElementAt(i).Index)
|
||||
// .AddUInt8Array(Codec.Compose(history.Values.ElementAt(i), connection));
|
||||
|
||||
// if (prependLength)
|
||||
// rt.InsertInt32(0, rt.Length);
|
||||
|
||||
// return rt.ToArray();
|
||||
// }
|
||||
|
||||
static DataSerializerComposeResults TupleComposer(
|
||||
value, DistributedConnection? connection) {
|
||||
//if (value == null)
|
||||
return DataSerializerComposeResults(TransmissionTypeIdentifier.Null, DC(0));
|
||||
|
||||
//@TODO
|
||||
// var rt = BinaryList();
|
||||
|
||||
// var fields = value.GetType().GetFields();
|
||||
// var list = fields.Select(x => x.GetValue(value)).ToArray();
|
||||
// var types = fields.Select(x => RepresentationType.FromType(x.FieldType).Compose()).ToArray();
|
||||
|
||||
// rt.Add((byte)list.Length);
|
||||
|
||||
// foreach (var t in types)
|
||||
// rt.AddRange(t);
|
||||
|
||||
// var composed = ArrayComposer(list, connection);
|
||||
|
||||
// if (composed == null)
|
||||
// return (TransmissionTypeIdentifier.Null, new byte[0]);
|
||||
// else
|
||||
// {
|
||||
// rt.AddRange(composed);
|
||||
// return (TransmissionTypeIdentifier.Tuple, rt.ToArray());
|
||||
// }
|
||||
}
|
||||
}
|
@ -1,116 +0,0 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2019 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.
|
||||
|
||||
*/
|
||||
class DataType
|
||||
{
|
||||
static const int Void = 0x0,
|
||||
//Variant,
|
||||
Bool = 1,
|
||||
Int8 = 2,
|
||||
UInt8 = 3,
|
||||
Char = 4,
|
||||
Int16 = 5,
|
||||
UInt16 = 6,
|
||||
Int32 = 7,
|
||||
UInt32 = 8,
|
||||
Int64 = 9,
|
||||
UInt64 = 0xA,
|
||||
Float32 = 0xB,
|
||||
Float64 = 0xC,
|
||||
Decimal = 0xD,
|
||||
DateTime = 0xE,
|
||||
Resource = 0xF,
|
||||
DistributedResource = 0x10,
|
||||
ResourceLink = 0x11,
|
||||
String = 0x12,
|
||||
Structure = 0x13,
|
||||
Record = 0x14,
|
||||
//Stream,
|
||||
//Array = 0x80,
|
||||
VarArray = 0x80,
|
||||
BoolArray = 0x81,
|
||||
Int8Array = 0x82,
|
||||
UInt8Array = 0x83,
|
||||
CharArray = 0x84,
|
||||
Int16Array = 0x85,
|
||||
UInt16Array = 0x86,
|
||||
Int32Array = 0x87,
|
||||
UInt32Array = 0x88,
|
||||
Int64Array = 0x89,
|
||||
UInt64Array = 0x8A,
|
||||
Float32Array = 0x8B,
|
||||
Float64Array = 0x8C,
|
||||
DecimalArray = 0x8D,
|
||||
DateTimeArray = 0x8E,
|
||||
ResourceArray = 0x8F,
|
||||
DistributedResourceArray = 0x90,
|
||||
ResourceLinkArray = 0x91,
|
||||
StringArray = 0x92,
|
||||
StructureArray = 0x93,
|
||||
RecordArray = 0x94,
|
||||
NotModified = 0x7F,
|
||||
Unspecified = 0xFF;
|
||||
|
||||
static bool isArray(int type)
|
||||
{
|
||||
return ((type & 0x80) == 0x80) && (type != NotModified);
|
||||
}
|
||||
|
||||
static int getElementType(int type)
|
||||
{
|
||||
return type & 0x7F;
|
||||
}
|
||||
|
||||
static int size(int type)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case DataType.Void:
|
||||
case DataType.NotModified:
|
||||
return 0;
|
||||
case DataType.Bool:
|
||||
case DataType.UInt8:
|
||||
case DataType.Int8:
|
||||
return 1;
|
||||
case DataType.Char:
|
||||
case DataType.UInt16:
|
||||
case DataType.Int16:
|
||||
return 2;
|
||||
case DataType.Int32:
|
||||
case DataType.UInt32:
|
||||
case DataType.Float32:
|
||||
case DataType.Resource:
|
||||
return 4;
|
||||
case DataType.Int64:
|
||||
case DataType.UInt64:
|
||||
case DataType.Float64:
|
||||
case DataType.DateTime:
|
||||
return 8;
|
||||
case DataType.DistributedResource:
|
||||
return 4;
|
||||
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,10 +1,12 @@
|
||||
import 'DC.dart';
|
||||
|
||||
class Guid {
|
||||
DC _data;
|
||||
late DC _data;
|
||||
|
||||
Guid(this._data) {
|
||||
|
||||
Guid(this._data) {}
|
||||
|
||||
Guid.fromString(String data) {
|
||||
_data = DC.fromHex(data, '');
|
||||
}
|
||||
|
||||
DC get value => _data;
|
||||
@ -18,7 +20,7 @@ class Guid {
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return _data.getString(0, _data.length);
|
||||
return _data.toHex('');
|
||||
}
|
||||
|
||||
@override
|
||||
|
15
lib/src/Data/IEnum.dart
Normal file
15
lib/src/Data/IEnum.dart
Normal file
@ -0,0 +1,15 @@
|
||||
import '../Resource/Template/TemplateDescriber.dart';
|
||||
|
||||
class IEnum {
|
||||
int index = 0;
|
||||
dynamic value;
|
||||
String name = '';
|
||||
IEnum([this.index = 0, this.value, this.name = ""]);
|
||||
|
||||
TemplateDescriber get template => TemplateDescriber("IEnum");
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return '${name}<$value>';
|
||||
}
|
||||
}
|
@ -24,9 +24,13 @@ SOFTWARE.
|
||||
|
||||
import '../Resource/Template/TemplateDescriber.dart';
|
||||
|
||||
|
||||
abstract class IRecord {
|
||||
Map<String, dynamic> serialize();
|
||||
void deserialize(Map<String, dynamic> value);
|
||||
TemplateDescriber get template;
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return serialize().toString();
|
||||
}
|
||||
}
|
||||
|
69
lib/src/Data/IntType.dart
Normal file
69
lib/src/Data/IntType.dart
Normal file
@ -0,0 +1,69 @@
|
||||
class IntType {
|
||||
int _value = 0;
|
||||
|
||||
bool operator ==(Object other) {
|
||||
if (other is IntType)
|
||||
return this._value == other._value;
|
||||
else if (other is int) return this._value == other;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
IntType(this._value);
|
||||
|
||||
bool operator >(IntType other) {
|
||||
return this._value > other._value;
|
||||
}
|
||||
|
||||
bool operator <(IntType other) {
|
||||
return this._value < other._value;
|
||||
}
|
||||
|
||||
bool operator >=(IntType other) {
|
||||
return this._value >= other._value;
|
||||
}
|
||||
|
||||
bool operator <=(IntType other) {
|
||||
return this._value <= other._value;
|
||||
}
|
||||
|
||||
operator +(IntType other) {
|
||||
this._value += other._value;
|
||||
}
|
||||
|
||||
operator -(IntType other) {
|
||||
this._value -= other._value;
|
||||
}
|
||||
|
||||
int toInt() => _value;
|
||||
|
||||
@override
|
||||
String toString() => _value.toString();
|
||||
|
||||
@override
|
||||
int get hashCode => _value.hashCode;
|
||||
}
|
||||
|
||||
class Int32 extends IntType {
|
||||
Int32(int value) : super(value);
|
||||
}
|
||||
|
||||
class Int16 extends IntType {
|
||||
Int16(int value) : super(value);
|
||||
}
|
||||
|
||||
class Int8 extends IntType {
|
||||
Int8(int value) : super(value);
|
||||
}
|
||||
|
||||
class UInt32 extends IntType {
|
||||
UInt32(int value) : super(value);
|
||||
}
|
||||
|
||||
class UInt16 extends IntType {
|
||||
UInt16(int value) : super(value);
|
||||
}
|
||||
|
||||
class UInt8 extends IntType {
|
||||
UInt8(int value) : super(value);
|
||||
}
|
@ -37,9 +37,12 @@ class KeyList<KT, T> extends IEventHandler with MapMixin<KT, T> {
|
||||
Iterable<KT> get keys => _map.keys;
|
||||
Iterable<T> get values => _map.values;
|
||||
|
||||
operator [](index) => _map[index];
|
||||
//T? operator [](Object? key);
|
||||
//operator []=(KT key, T value);
|
||||
|
||||
operator []=(index, value) => add(index, value);
|
||||
T? operator [](Object? index) => _map[index];
|
||||
|
||||
operator []=(KT index, T value) => add(index, value);
|
||||
|
||||
at(int index) => _map.values.elementAt(index);
|
||||
|
||||
@ -101,8 +104,7 @@ class KeyList<KT, T> extends IEventHandler with MapMixin<KT, T> {
|
||||
|
||||
var value = _map[key];
|
||||
|
||||
if (_removableList)
|
||||
(value as IDestructible).off("destroy", _itemDestroyed);
|
||||
if (_removableList) (value as IDestructible).off("destroy", _itemDestroyed);
|
||||
|
||||
_map.remove(key);
|
||||
|
||||
|
@ -1,9 +1,11 @@
|
||||
import 'dart:collection';
|
||||
|
||||
import '../Resource/Template/TemplateDescriber.dart';
|
||||
|
||||
import 'IRecord.dart';
|
||||
import 'KeyList.dart';
|
||||
|
||||
class Record extends KeyList with IRecord {
|
||||
class Record extends IRecord with MapMixin<String, dynamic> {
|
||||
Map<String, dynamic> _props = Map<String, dynamic>();
|
||||
|
||||
@override
|
||||
@ -17,9 +19,29 @@ class Record extends KeyList with IRecord {
|
||||
}
|
||||
|
||||
operator [](index) => _props[index];
|
||||
operator []=(index, value) => _props[index] = value;
|
||||
operator []=(String index, value) => _props[index] = value;
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return _props.toString();
|
||||
}
|
||||
|
||||
@override
|
||||
// TODO: implement template
|
||||
TemplateDescriber get template => throw UnimplementedError();
|
||||
|
||||
@override
|
||||
void clear() {
|
||||
// TODO: implement clear
|
||||
}
|
||||
|
||||
@override
|
||||
// TODO: implement keys
|
||||
Iterable<String> get keys => _props.keys;
|
||||
|
||||
@override
|
||||
remove(Object? key) {
|
||||
// TODO: implement remove
|
||||
throw UnimplementedError();
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +0,0 @@
|
||||
class RecordComparisonResult {
|
||||
static const Null = 0;
|
||||
static const Record = 1;
|
||||
static const RecordSameType = 2;
|
||||
static const Same = 3;
|
||||
static const Empty = 4;
|
||||
|
||||
}
|
242
lib/src/Data/RepresentationType.dart
Normal file
242
lib/src/Data/RepresentationType.dart
Normal file
@ -0,0 +1,242 @@
|
||||
|
||||
import 'IEnum.dart';
|
||||
import '../Resource/Template/TemplateType.dart';
|
||||
import 'IRecord.dart';
|
||||
import '../Resource/IResource.dart';
|
||||
import '../Resource/Warehouse.dart';
|
||||
|
||||
import 'BinaryList.dart';
|
||||
import 'DC.dart';
|
||||
import 'Guid.dart';
|
||||
import 'package:collection/collection.dart';
|
||||
|
||||
class RepresentationTypeIdentifier {
|
||||
static const int Void = 0x0,
|
||||
Dynamic = 0x1,
|
||||
Bool = 0x2,
|
||||
UInt8 = 0x3,
|
||||
Int8 = 0x4,
|
||||
Char = 0x5,
|
||||
Int16 = 0x6,
|
||||
UInt16 = 0x7,
|
||||
Int32 = 0x8,
|
||||
UInt32 = 0x9,
|
||||
Float32 = 0xA,
|
||||
Int64 = 0xB,
|
||||
UInt64 = 0xC,
|
||||
Float64 = 0xD,
|
||||
DateTime = 0xE,
|
||||
Int128 = 0xF,
|
||||
UInt128 = 0x10,
|
||||
Decimal = 0x11,
|
||||
String = 0x12,
|
||||
RawData = 0x13,
|
||||
Resource = 0x14,
|
||||
Record = 0x15,
|
||||
List = 0x16,
|
||||
Map = 0x17,
|
||||
Enum = 0x18,
|
||||
TypedResource = 0x45, // Followed by UUID
|
||||
TypedRecord = 0x46, // Followed by UUID
|
||||
TypedList = 0x48, // Followed by element type
|
||||
Tuple2 = 0x50, // Followed by element type
|
||||
TypedMap = 0x51, // Followed by key type and value type
|
||||
Tuple3 = 0x58,
|
||||
Tuple4 = 0x60,
|
||||
Tuple5 = 0x68,
|
||||
Tuple6 = 0x70,
|
||||
Tuple7 = 0x78;
|
||||
}
|
||||
|
||||
class DumClass<T> {
|
||||
Type type = T;
|
||||
}
|
||||
|
||||
Type getNullableType<T>() => DumClass<T?>().type;
|
||||
Type getTypeOf<T>() => DumClass<T>().type;
|
||||
|
||||
class RepresentationTypeParseResults {
|
||||
RepresentationType type;
|
||||
int size;
|
||||
|
||||
RepresentationTypeParseResults(this.size, this.type);
|
||||
}
|
||||
|
||||
class RepresentationType {
|
||||
static Type getTypeFromName(String name) {
|
||||
const Map<String, Type> types = {
|
||||
"int": int,
|
||||
"bool": bool,
|
||||
"double": double,
|
||||
"String": String,
|
||||
"IResource": IResource,
|
||||
"IRecord": IRecord,
|
||||
"IEnum": IEnum,
|
||||
"DC": DC,
|
||||
};
|
||||
|
||||
if (types.containsKey(name)) {
|
||||
return types[name]!;
|
||||
} else
|
||||
return Object().runtimeType;
|
||||
}
|
||||
|
||||
RepresentationType toNullable() {
|
||||
return RepresentationType(identifier, true, guid, subTypes);
|
||||
}
|
||||
|
||||
static RepresentationType Void =
|
||||
RepresentationType(RepresentationTypeIdentifier.Void, true, null, null);
|
||||
|
||||
static RepresentationType Dynamic = RepresentationType(
|
||||
RepresentationTypeIdentifier.Dynamic, true, null, null);
|
||||
|
||||
static RepresentationType? fromType(Type type) {
|
||||
return Warehouse.typesFactory[type]?.representationType;
|
||||
|
||||
//Warehouse.typesFactory.values.firstWhereOrNull(x => x.representationType == )
|
||||
//return RepresentationType(
|
||||
// RepresentationTypeIdentifier.Dynamic, true, null, null);
|
||||
}
|
||||
|
||||
// @TODO : complete this;
|
||||
// static RepresentationType? fromType(Type type)
|
||||
// {
|
||||
// var typeName = type.toString();
|
||||
|
||||
// var nullable = typeName.endsWith('?');
|
||||
// if (nullable)
|
||||
// typeName = typeName.substring(0, typeName.length - 1);
|
||||
|
||||
// if (typeName.endsWith('>')) // generic type
|
||||
// {
|
||||
|
||||
// // get args
|
||||
// var argsRex = RegExp(r"(\b[^<>]+)\<(.*)\>$");
|
||||
// var matches = argsRex.allMatches(typeName);
|
||||
|
||||
// var name = matches.elementAt(0).input; // name
|
||||
// var argsStr = matches.elementAt(1).input;
|
||||
|
||||
// var eachArg = RegExp(r"([^,]+\(.+?\))|([^,]+)");
|
||||
// var args = eachArg.allMatches(argsStr);
|
||||
|
||||
// // parse sub types
|
||||
|
||||
// if (name == "List") {
|
||||
// // get sub type
|
||||
// getTypeFromName(args.first.input);
|
||||
// return RepresentationType(RepresentationTypeIdentifier.TypedList, nullable, guid, subTypes)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
Map<int, List<Type>> runtimeTypes = {
|
||||
RepresentationTypeIdentifier.Void: [dynamic, dynamic],
|
||||
RepresentationTypeIdentifier.Dynamic: [dynamic, dynamic],
|
||||
RepresentationTypeIdentifier.Bool: [bool, getNullableType<bool>()],
|
||||
RepresentationTypeIdentifier.Char: [String, getNullableType<String>()],
|
||||
RepresentationTypeIdentifier.UInt8: [int, getNullableType<int>()],
|
||||
RepresentationTypeIdentifier.Int8: [int, getNullableType<int>()],
|
||||
RepresentationTypeIdentifier.Int16: [int, getNullableType<int>()],
|
||||
RepresentationTypeIdentifier.UInt16: [int, getNullableType<int>()],
|
||||
RepresentationTypeIdentifier.Int32: [int, getNullableType<int>()],
|
||||
RepresentationTypeIdentifier.UInt32: [int, getNullableType<int>()],
|
||||
RepresentationTypeIdentifier.Int64: [int, getNullableType<int>()],
|
||||
RepresentationTypeIdentifier.UInt64: [int, getNullableType<int>()],
|
||||
RepresentationTypeIdentifier.Float32: [double, getNullableType<double>()],
|
||||
RepresentationTypeIdentifier.Float64: [double, getNullableType<double>()],
|
||||
RepresentationTypeIdentifier.Decimal: [double, getNullableType<double>()],
|
||||
RepresentationTypeIdentifier.String: [String, getNullableType<String>()],
|
||||
RepresentationTypeIdentifier.DateTime: [
|
||||
DateTime,
|
||||
getNullableType<DateTime>()
|
||||
],
|
||||
RepresentationTypeIdentifier.Resource: [
|
||||
IResource,
|
||||
getNullableType<IResource>()
|
||||
],
|
||||
RepresentationTypeIdentifier.Record: [IRecord, getNullableType<IRecord>()],
|
||||
};
|
||||
|
||||
Type? getRuntimeType() {
|
||||
if (runtimeTypes.containsKey(identifier))
|
||||
return nullable
|
||||
? runtimeTypes[identifier]![1]
|
||||
: runtimeTypes[identifier]![0];
|
||||
if (identifier == RepresentationTypeIdentifier.TypedRecord)
|
||||
return Warehouse.getTemplateByClassId(guid!, TemplateType.Record)
|
||||
?.definedType;
|
||||
else if (identifier == RepresentationTypeIdentifier.TypedResource)
|
||||
return Warehouse.getTemplateByClassId(guid!, TemplateType.Unspecified)
|
||||
?.definedType;
|
||||
else if (identifier == RepresentationTypeIdentifier.Enum)
|
||||
return Warehouse.getTemplateByClassId(guid!, TemplateType.Enum)
|
||||
?.definedType;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
int identifier;
|
||||
bool nullable;
|
||||
Guid? guid;
|
||||
|
||||
List<RepresentationType>? subTypes;
|
||||
|
||||
RepresentationType(this.identifier, this.nullable,
|
||||
[this.guid, this.subTypes]) {}
|
||||
|
||||
DC compose() {
|
||||
var rt = BinaryList();
|
||||
|
||||
if (nullable)
|
||||
rt.addUint8(0x80 | identifier);
|
||||
else
|
||||
rt.addUint8(identifier);
|
||||
|
||||
if (guid != null) rt.addDC(DC.guidToBytes(guid!));
|
||||
|
||||
if (subTypes != null)
|
||||
for (var i = 0; i < subTypes!.length; i++)
|
||||
rt.addDC(subTypes![i].compose());
|
||||
|
||||
return rt.toDC();
|
||||
}
|
||||
|
||||
//public override string ToString() => Identifier.ToString() + (Nullable ? "?" : "")
|
||||
// + TypeTemplate != null ? "<" + TypeTemplate.ClassName + ">" : "";
|
||||
|
||||
static RepresentationTypeParseResults parse(DC data, int offset) {
|
||||
var oOffset = offset;
|
||||
|
||||
var header = data[offset++];
|
||||
bool nullable = (header & 0x80) > 0;
|
||||
var identifier = (header & 0x7F);
|
||||
|
||||
if ((header & 0x40) > 0) {
|
||||
var hasGUID = (header & 0x4) > 0;
|
||||
var subsCount = (header >> 3) & 0x7;
|
||||
|
||||
Guid? guid = null;
|
||||
|
||||
if (hasGUID) {
|
||||
guid = data.getGuid(offset);
|
||||
offset += 16;
|
||||
}
|
||||
|
||||
var subs = <RepresentationType>[];
|
||||
|
||||
for (var i = 0; i < subsCount; i++) {
|
||||
var parsed = RepresentationType.parse(data, offset);
|
||||
subs.add(parsed.type);
|
||||
offset += parsed.size;
|
||||
}
|
||||
|
||||
return RepresentationTypeParseResults(offset - oOffset,
|
||||
RepresentationType(identifier, nullable, guid, subs));
|
||||
} else {
|
||||
return RepresentationTypeParseResults(
|
||||
1, RepresentationType(identifier, nullable, null, null));
|
||||
}
|
||||
}
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
class ResourceComparisonResult {
|
||||
static const Null = 0;
|
||||
static const Distributed = 1;
|
||||
static const Local = 2;
|
||||
static const Same = 3;
|
||||
static const Empty = 4;
|
||||
}
|
@ -1,59 +0,0 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2019 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.
|
||||
|
||||
*/
|
||||
import 'dart:collection';
|
||||
|
||||
class Structure with MapMixin<String, Object>//, IterableMixin<String>
|
||||
{
|
||||
|
||||
Map<String, Object> _map = new Map<String, Object>();
|
||||
|
||||
Iterator<String> get iterator => _map.keys.iterator;
|
||||
|
||||
Iterable<String> get keys => _map.keys;
|
||||
|
||||
operator[](index) => _map[index];
|
||||
|
||||
operator []= (index, value) => _map[index] = value;
|
||||
|
||||
remove(key) => _map.remove(key);
|
||||
|
||||
clear() => _map.clear();
|
||||
|
||||
|
||||
at(int index) => _map.values.elementAt(index);
|
||||
|
||||
List<String> getKeys() => _map.keys.toList();
|
||||
|
||||
|
||||
Structure.fromMap(Map map)
|
||||
{
|
||||
for(var i in map.keys)
|
||||
_map[i.toString()] = map[i];
|
||||
}
|
||||
|
||||
Structure()
|
||||
{
|
||||
// do nothing
|
||||
}
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
class StructureComparisonResult
|
||||
{
|
||||
static const int Null = 0;
|
||||
static const int Structure = 1;
|
||||
static const int StructureSameKeys = 2;
|
||||
static const int StructureSameTypes = 3;
|
||||
static const int Same = 4;
|
||||
}
|
@ -1,6 +0,0 @@
|
||||
class StructureMetadata {
|
||||
List<String>? keys; // = <String>[];
|
||||
List<int>? types;//
|
||||
|
||||
//const StructureMetadata(this.keys, this.types);
|
||||
}
|
189
lib/src/Data/TransmissionType.dart
Normal file
189
lib/src/Data/TransmissionType.dart
Normal file
@ -0,0 +1,189 @@
|
||||
import "DC.dart";
|
||||
|
||||
class TransmissionTypeIdentifier {
|
||||
static const int Null = 0x0,
|
||||
False = 0x1,
|
||||
True = 0x2,
|
||||
NotModified = 0x3,
|
||||
UInt8 = 0x8,
|
||||
Int8 = 0x9,
|
||||
Char8 = 0xA,
|
||||
Int16 = 0x10,
|
||||
UInt16 = 0x11,
|
||||
Char16 = 0x12,
|
||||
Int32 = 0x18,
|
||||
UInt32 = 0x19,
|
||||
Float32 = 0x1A,
|
||||
Resource = 0x1B,
|
||||
ResourceLocal = 0x1C,
|
||||
Int64 = 0x20,
|
||||
UInt64 = 0x21,
|
||||
Float64 = 0x22,
|
||||
DateTime = 0x23,
|
||||
Int128 = 0x28,
|
||||
UInt128 = 0x29,
|
||||
Float128 = 0x2A,
|
||||
RawData = 0x40,
|
||||
String = 0x41,
|
||||
List = 0x42,
|
||||
ResourceList = 0x43,
|
||||
RecordList = 0x44,
|
||||
Map = 0x45,
|
||||
MapList = 0x46,
|
||||
//Tuple = 0x47,
|
||||
|
||||
Record = 0x80,
|
||||
TypedList = 0x81,
|
||||
TypedMap = 0x82,
|
||||
Tuple = 0x83,
|
||||
Enum = 0x84,
|
||||
Constant = 0x85;
|
||||
//TypedResourceList = 0x81,
|
||||
//TypedRecordList = 0x82,
|
||||
|
||||
}
|
||||
|
||||
class TransmissionTypeClass {
|
||||
static const int Fixed = 0, Dynamic = 1, Typed = 2;
|
||||
}
|
||||
|
||||
class TransmissionTypeParseResults {
|
||||
int size;
|
||||
TransmissionType? type;
|
||||
|
||||
TransmissionTypeParseResults(this.size, this.type) {}
|
||||
}
|
||||
|
||||
class TransmissionType {
|
||||
final int identifier;
|
||||
final int index;
|
||||
final int classType;
|
||||
final int offset;
|
||||
final int contentLength;
|
||||
final int exponent;
|
||||
|
||||
static const TransmissionType Null =
|
||||
TransmissionType(TransmissionTypeIdentifier.Null, 0, 0, 0, 0);
|
||||
|
||||
const TransmissionType(this.identifier, this.classType, this.index,
|
||||
this.offset, this.contentLength,
|
||||
[this.exponent = 0]);
|
||||
|
||||
static DC compose(int identifier, DC data) {
|
||||
if (data.length == 0) return DC.fromList([identifier]);
|
||||
|
||||
var cls = identifier >> 6;
|
||||
if (cls == TransmissionTypeClass.Fixed) {
|
||||
return DC.combine([identifier], 0, 1, data, 0, data.length);
|
||||
} else {
|
||||
var len = data.length;
|
||||
|
||||
if (len == 0) {
|
||||
return DC.fromList([identifier]);
|
||||
} else if (len <= 0xFF) {
|
||||
var rt = DC(2 + len);
|
||||
rt[0] = identifier | 0x8;
|
||||
rt[1] = len;
|
||||
rt.set(data, 0, 2, len);
|
||||
return rt;
|
||||
} else if (len <= 0xFFFF) {
|
||||
var rt = DC(3 + len);
|
||||
rt[0] = identifier | 0x10;
|
||||
rt[1] = (len >> 8) & 0xFF;
|
||||
rt[2] = len & 0xFF;
|
||||
rt.set(data, 0, 3, len);
|
||||
return rt;
|
||||
} else if (len <= 0xFFFFFF) {
|
||||
var rt = DC(4 + len);
|
||||
rt[0] = identifier | 0x18;
|
||||
rt[1] = (len >> 16) & 0xFF;
|
||||
rt[2] = (len >> 8) & 0xFF;
|
||||
rt[3] = len & 0xFF;
|
||||
|
||||
rt.set(data, 0, 4, len);
|
||||
return rt;
|
||||
} else if (len <= 0xFFFFFFFF) {
|
||||
var rt = DC(5 + len);
|
||||
rt[0] = (identifier | 0x20);
|
||||
rt[1] = ((len >> 24) & 0xFF);
|
||||
rt[2] = ((len >> 16) & 0xFF);
|
||||
rt[3] = ((len >> 8) & 0xFF);
|
||||
rt[4] = (len & 0xFF);
|
||||
rt.set(data, 0, 5, len);
|
||||
return rt;
|
||||
} else if (len <= 0xFFFFFFFFFF) {
|
||||
var rt = DC(6 + len);
|
||||
|
||||
rt[0] = identifier | 0x28;
|
||||
rt[1] = ((len >> 32) & 0xFF);
|
||||
rt[2] = ((len >> 24) & 0xFF);
|
||||
rt[3] = ((len >> 16) & 0xFF);
|
||||
rt[4] = ((len >> 8) & 0xFF);
|
||||
rt[5] = (len & 0xFF);
|
||||
rt.set(data, 0, 6, len);
|
||||
|
||||
return rt;
|
||||
} else if (len <= 0xFFFFFFFFFFFF) {
|
||||
var rt = DC(7 + len);
|
||||
|
||||
rt[0] = identifier | 0x30;
|
||||
rt[1] = (len >> 40) & 0xFF;
|
||||
rt[2] = (len >> 32) & 0xFF;
|
||||
rt[3] = (len >> 24) & 0xFF;
|
||||
rt[4] = (len >> 16) & 0xFF;
|
||||
rt[5] = (len >> 8) & 0xFF;
|
||||
rt[6] = len & 0xFF;
|
||||
|
||||
rt.set(data, 0, 7, len);
|
||||
return rt;
|
||||
} else //if (len <= 0xFF_FF_FF_FF_FF_FF_FF)
|
||||
{
|
||||
var rt = DC(8 + len);
|
||||
rt[0] = identifier | 0x38;
|
||||
rt[1] = (len >> 48) & 0xFF;
|
||||
rt[2] = (len >> 40) & 0xFF;
|
||||
rt[3] = (len >> 32) & 0xFF;
|
||||
rt[4] = (len >> 24) & 0xFF;
|
||||
rt[5] = (len >> 16) & 0xFF;
|
||||
rt[6] = (len >> 8) & 0xFF;
|
||||
rt[7] = len & 0xFF;
|
||||
data.set(data, 0, 8, len);
|
||||
return rt;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static TransmissionTypeParseResults parse(DC data, int offset, int ends) {
|
||||
var h = data[offset++];
|
||||
|
||||
var cls = h >> 6;
|
||||
|
||||
if (cls == TransmissionTypeClass.Fixed) {
|
||||
var exp = (h & 0x38) >> 3;
|
||||
|
||||
if (exp == 0)
|
||||
return TransmissionTypeParseResults(
|
||||
1, TransmissionType(h, cls, h & 0x7, 0, exp));
|
||||
|
||||
int cl = (1 << (exp - 1));
|
||||
|
||||
if (ends - offset < cl)
|
||||
return TransmissionTypeParseResults(ends - offset - cl, null);
|
||||
|
||||
return TransmissionTypeParseResults(
|
||||
1 + cl, new TransmissionType(h, cls, h & 0x7, offset, cl, exp));
|
||||
} else {
|
||||
int cll = (h >> 3) & 0x7;
|
||||
|
||||
if (ends - offset < cll)
|
||||
return TransmissionTypeParseResults(ends - offset - cll, null);
|
||||
|
||||
int cl = 0;
|
||||
|
||||
for (var i = 0; i < cll; i++) cl = cl << 8 | data[offset++];
|
||||
|
||||
return TransmissionTypeParseResults(
|
||||
1 + cl + cll, TransmissionType((h & 0xC7), cls, h & 0x7, offset, cl));
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user