diff --git a/Esiur.Stores.MongoDB/Esiur.Stores.MongoDB.csproj b/Esiur.Stores.MongoDB/Esiur.Stores.MongoDB.csproj index 979d441..55baa8f 100644 --- a/Esiur.Stores.MongoDB/Esiur.Stores.MongoDB.csproj +++ b/Esiur.Stores.MongoDB/Esiur.Stores.MongoDB.csproj @@ -11,7 +11,7 @@ http://www.esiur.com https://github.com/esiur/esiur-dotnet/ True - 1.2.3 + 1.2.5 diff --git a/Esiur.Stores.MongoDB/MongoDBStore.cs b/Esiur.Stores.MongoDB/MongoDBStore.cs index 451871e..aab580f 100644 --- a/Esiur.Stores.MongoDB/MongoDBStore.cs +++ b/Esiur.Stores.MongoDB/MongoDBStore.cs @@ -1,6 +1,6 @@ using Esiur.Resource; using System; -using Esiur.Engine; +using Esiur.Core; using MongoDB.Driver.Core; using MongoDB.Driver; using MongoDB.Bson; @@ -530,12 +530,14 @@ namespace Esiur.Stores.MongoDB var attrsDoc = ComposeStructure(attrs); + var type = ResourceProxy.GetBaseType(resource); + var document = new BsonDocument { { "parents", parents }, { "children", children }, {"attributes", attrsDoc }, - { "classname", resource.GetType().FullName + "," + resource.GetType().GetTypeInfo().Assembly.GetName().Name }, + { "classname", type.FullName + "," + type.GetTypeInfo().Assembly.GetName().Name }, { "name", resource.Instance.Name }, { "_id", new BsonObjectId(new ObjectId(resource.Instance.Attributes["objectId"].ToString())) }, {"values", values } diff --git a/Esiur/Engine/AsyncAwaiter.cs b/Esiur/Core/AsyncAwaiter.cs similarity index 67% rename from Esiur/Engine/AsyncAwaiter.cs rename to Esiur/Core/AsyncAwaiter.cs index 2c1af2e..cead3c4 100644 --- a/Esiur/Engine/AsyncAwaiter.cs +++ b/Esiur/Core/AsyncAwaiter.cs @@ -3,21 +3,21 @@ using System.Collections.Generic; using System.Runtime.CompilerServices; using System.Text; -namespace Esiur.Engine +namespace Esiur.Core { public class AsyncAwaiter : INotifyCompletion { - Action callback = null; - T result; + public Action callback = null; + public T result; private bool completed; public AsyncAwaiter(AsyncReply reply) { reply.Then(x => { - completed = true; - result = x; - callback?.Invoke(); + this.completed = true; + this.result = x; + this.callback?.Invoke(); }); } @@ -28,10 +28,10 @@ namespace Esiur.Engine public bool IsCompleted => completed; - //From INotifyCompletion public void OnCompleted(Action continuation) { - Console.WriteLine("Continue...."); + // Continue.... + callback = continuation; } diff --git a/Esiur/Engine/AsyncBag.cs b/Esiur/Core/AsyncBag.cs similarity index 99% rename from Esiur/Engine/AsyncBag.cs rename to Esiur/Core/AsyncBag.cs index e277df5..7109eda 100644 --- a/Esiur/Engine/AsyncBag.cs +++ b/Esiur/Core/AsyncBag.cs @@ -28,7 +28,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace Esiur.Engine +namespace Esiur.Core { public class AsyncBag: AsyncReply { diff --git a/Esiur/Engine/AsyncException.cs b/Esiur/Core/AsyncException.cs similarity index 68% rename from Esiur/Engine/AsyncException.cs rename to Esiur/Core/AsyncException.cs index fea9a4b..eb52267 100644 --- a/Esiur/Engine/AsyncException.cs +++ b/Esiur/Core/AsyncException.cs @@ -26,39 +26,8 @@ using System; using System.Collections.Generic; using System.Text; -namespace Esiur.Engine +namespace Esiur.Core { - public enum ExceptionCode : ushort - { - HostNotReachable, - AccessDenied, - ResourceNotFound, - AttachDenied, - InvalidMethod, - InvokeDenied, - CreateDenied, - AddParentDenied, - AddChildDenied, - ViewAttributeDenied, - UpdateAttributeDenied, - StoreNotFound, - ParentNotFound, - ChildNotFound, - ResourceIsNotStore, - DeleteDenied, - DeleteFailed, - UpdateAttributeFailed, - GetAttributesFailed, - ClearAttributesFailed, - TemplateNotFound, - RenameDenied, - ClassNotFound, - MethodNotFound, - PropertyNotFound, - SetPropertyDenied, - ReadOnlyProperty - } - public class AsyncException: Exception { diff --git a/Esiur/Engine/AsyncQueue.cs b/Esiur/Core/AsyncQueue.cs similarity index 99% rename from Esiur/Engine/AsyncQueue.cs rename to Esiur/Core/AsyncQueue.cs index 4ad2c0e..227df86 100644 --- a/Esiur/Engine/AsyncQueue.cs +++ b/Esiur/Core/AsyncQueue.cs @@ -28,7 +28,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace Esiur.Engine +namespace Esiur.Core { public class AsyncQueue : AsyncReply { diff --git a/Esiur/Engine/AsyncReplyGeneric.cs b/Esiur/Core/AsyncReply.cs similarity index 90% rename from Esiur/Engine/AsyncReplyGeneric.cs rename to Esiur/Core/AsyncReply.cs index c32c0b2..54f35c3 100644 --- a/Esiur/Engine/AsyncReplyGeneric.cs +++ b/Esiur/Core/AsyncReply.cs @@ -32,7 +32,7 @@ using System.Reflection; using System.Threading; using System.Runtime.CompilerServices; -namespace Esiur.Engine +namespace Esiur.Core { public class AsyncReply: IAsyncReply { @@ -53,8 +53,6 @@ namespace Esiur.Engine protected bool resultReady = false; AsyncException exception; - TaskCompletionSource tcs = new TaskCompletionSource(); - public bool Ready { @@ -84,10 +82,7 @@ namespace Esiur.Engine errorCallbacks.Add(callback); if (exception != null) - { callback(exception); - tcs.SetException(exception); - } return this; } @@ -119,27 +114,28 @@ namespace Esiur.Engine foreach (var cb in callbacks) cb((T)result); - tcs.TrySetResult(result); } } - public void TriggerError(AsyncException exception) + public void TriggerError(Exception exception) { if (resultReady) return; - this.exception = exception; - + if (exception is AsyncException) + this.exception = exception as AsyncException; + else + this.exception = new AsyncException(ErrorType.Management, 0, exception.Message); + lock (callbacksLock) { foreach (var cb in errorCallbacks) - cb(exception); + cb(this.exception); } - tcs.TrySetException(exception); } public void TriggerProgress(ProgressType type, int value, int max) @@ -174,13 +170,7 @@ namespace Esiur.Engine return new AsyncAwaiter(this); } - public Task Task - { - get - { - return tcs.Task; - } - } + @@ -192,7 +182,6 @@ namespace Esiur.Engine public AsyncReply(T result) { resultReady = true; - tcs.SetResult(result); this.result = result; } diff --git a/Esiur/Engine/AsyncReply.cs b/Esiur/Core/AsyncReplyNon.cs similarity index 99% rename from Esiur/Engine/AsyncReply.cs rename to Esiur/Core/AsyncReplyNon.cs index 5105337..4bb5493 100644 --- a/Esiur/Engine/AsyncReply.cs +++ b/Esiur/Core/AsyncReplyNon.cs @@ -28,7 +28,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace Esiur.Engine +namespace Esiur.Core { public class AsyncReply { diff --git a/Esiur/Engine/ErrorType.cs b/Esiur/Core/ErrorType.cs similarity index 86% rename from Esiur/Engine/ErrorType.cs rename to Esiur/Core/ErrorType.cs index f6b55f3..4ec2271 100644 --- a/Esiur/Engine/ErrorType.cs +++ b/Esiur/Core/ErrorType.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using System.Text; -namespace Esiur.Engine +namespace Esiur.Core { public enum ErrorType { diff --git a/Esiur/Core/ExceptionCode.cs b/Esiur/Core/ExceptionCode.cs new file mode 100644 index 0000000..bf390e6 --- /dev/null +++ b/Esiur/Core/ExceptionCode.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Esiur.Core +{ + public enum ExceptionCode : ushort + { + HostNotReachable, + AccessDenied, + ResourceNotFound, + AttachDenied, + InvalidMethod, + InvokeDenied, + CreateDenied, + AddParentDenied, + AddChildDenied, + ViewAttributeDenied, + UpdateAttributeDenied, + StoreNotFound, + ParentNotFound, + ChildNotFound, + ResourceIsNotStore, + DeleteDenied, + DeleteFailed, + UpdateAttributeFailed, + GetAttributesFailed, + ClearAttributesFailed, + TemplateNotFound, + RenameDenied, + ClassNotFound, + MethodNotFound, + PropertyNotFound, + SetPropertyDenied, + ReadOnlyProperty + } +} diff --git a/Esiur/Engine/IAsyncReply.cs b/Esiur/Core/IAsyncReply.cs similarity index 88% rename from Esiur/Engine/IAsyncReply.cs rename to Esiur/Core/IAsyncReply.cs index d302561..bf1a01d 100644 --- a/Esiur/Engine/IAsyncReply.cs +++ b/Esiur/Core/IAsyncReply.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.Runtime.CompilerServices; using System.Text; -namespace Esiur.Engine +namespace Esiur.Core { public interface IAsyncReply//IAsyncEnumerator { @@ -12,7 +12,7 @@ namespace Esiur.Engine IAsyncReply Progress(Action callback); IAsyncReply Chunk(Action callback); void Trigger(object result); - void TriggerError(AsyncException exception); + void TriggerError(Exception exception); void TriggerProgress(ProgressType type, int value, int max); void TriggerChunk(object value); diff --git a/Esiur/Engine/IDestructible.cs b/Esiur/Core/IDestructible.cs similarity index 98% rename from Esiur/Engine/IDestructible.cs rename to Esiur/Core/IDestructible.cs index 01c531d..26ef401 100644 --- a/Esiur/Engine/IDestructible.cs +++ b/Esiur/Core/IDestructible.cs @@ -27,7 +27,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace Esiur.Engine +namespace Esiur.Core { public delegate void DestroyedEvent(object sender); diff --git a/Esiur/Engine/LogType.cs b/Esiur/Core/LogType.cs similarity index 98% rename from Esiur/Engine/LogType.cs rename to Esiur/Core/LogType.cs index 7c60bd3..eafb8c0 100644 --- a/Esiur/Engine/LogType.cs +++ b/Esiur/Core/LogType.cs @@ -29,7 +29,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace Esiur.Engine +namespace Esiur.Core { public enum LogType { diff --git a/Esiur/Engine/ProgressType.cs b/Esiur/Core/ProgressType.cs similarity index 86% rename from Esiur/Engine/ProgressType.cs rename to Esiur/Core/ProgressType.cs index a72cad7..5ee724a 100644 --- a/Esiur/Engine/ProgressType.cs +++ b/Esiur/Core/ProgressType.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using System.Text; -namespace Esiur.Engine +namespace Esiur.Core { public enum ProgressType { diff --git a/Esiur/Data/AutoList.cs b/Esiur/Data/AutoList.cs index e479adc..cb5dd14 100644 --- a/Esiur/Data/AutoList.cs +++ b/Esiur/Data/AutoList.cs @@ -27,7 +27,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; -using Esiur.Engine; +using Esiur.Core; using System.Reflection; namespace Esiur.Data diff --git a/Esiur/Data/BinaryList.cs b/Esiur/Data/BinaryList.cs index 86e2f51..21ad2ff 100644 --- a/Esiur/Data/BinaryList.cs +++ b/Esiur/Data/BinaryList.cs @@ -28,6 +28,7 @@ using System.Linq; using System.Text; using Esiur.Misc; using System.Reflection; +using Esiur.Core; namespace Esiur.Data { @@ -36,7 +37,7 @@ namespace Esiur.Data /// public class BinaryList { - private List held = new List(); + private List list = new List(); /// /// Create an instance of BinaryList @@ -46,18 +47,19 @@ namespace Esiur.Data } + /* /// /// Converts parameters to binary in same order /// /// Variables to convert public static byte[] ToBytes(params object[] values) { - var held = new List(); + var list = new List(); foreach (var i in values) { if (i is byte) - held.Add((byte)i); + list.Add((byte)i); else { #if NETSTANDARD1_5 @@ -68,13 +70,14 @@ namespace Esiur.Data if (mi != null) { var b = (byte[])mi.Invoke(null, new object[] { i }); - held.AddRange(b); + list.AddRange(b); } } } - return held.ToArray(); + return list.ToArray(); } + /// /// Create a new instance of BinaryList @@ -94,7 +97,7 @@ namespace Esiur.Data foreach (var i in values) { if (i is byte) - held.Add((byte)i); + list.Add((byte)i); else { #if NETSTANDARD1_5 @@ -105,7 +108,7 @@ namespace Esiur.Data if (mi != null) { var b = (byte[])mi.Invoke(null, new object[] {i}); - held.AddRange(b); + list.AddRange(b); } } } @@ -131,7 +134,7 @@ namespace Esiur.Data { if (i is byte) { - held.Insert(offset++, (byte)i); + list.Insert(offset++, (byte)i); } else { @@ -143,7 +146,7 @@ namespace Esiur.Data if (mi != null) { var b = (byte[])mi.Invoke(null, new object[] { i }); - held.InsertRange(offset, b); + list.InsertRange(offset, b); offset += b.Length; } } @@ -157,64 +160,555 @@ namespace Esiur.Data { get { - return held.Count; + return list.Count; } } /* public void Append(byte data) { - held.Add(data); + list.Add(data); } public void Append(byte[] data) { - held.AddRange(data); + list.AddRange(data); } public void Append(int data) { - held.AddRange(DC.ToBytes(data)); + list.AddRange(DC.ToBytes(data)); } public void Append(uint data) { - held.AddRange(DC.ToBytes(data)); + list.AddRange(DC.ToBytes(data)); } public void Append(float data) { - held.AddRange(DC.ToBytes(data)); + list.AddRange(DC.ToBytes(data)); } public void Append(short data) { - held.AddRange(DC.ToBytes(data)); + list.AddRange(DC.ToBytes(data)); } public void Append(ushort data) { - held.AddRange(DC.ToBytes(data)); + list.AddRange(DC.ToBytes(data)); } public void Append(double data) { - held.AddRange(DC.ToBytes(data)); + list.AddRange(DC.ToBytes(data)); } public void Append(sbyte data) { - held.Add((byte)data); + list.Add((byte)data); } */ + + public int Length => list.Count; + + public BinaryList AddDateTime(DateTime value) + { + list.AddRange(DC.ToBytes(value)); + return this; + } + + public BinaryList InsertDateTime(int position, DateTime value) + { + list.InsertRange(position, DC.ToBytes(value)); + return this; + } + + + public BinaryList AddDateTimeArray(DateTime[] value) + { + list.AddRange(DC.ToBytes(value)); + return this; + } + + public BinaryList InsertDateTimeArray(int position, DateTime[] value) + { + list.InsertRange(position, DC.ToBytes(value)); + return this; + } + + public BinaryList AddGuid(Guid value) + { + list.AddRange(DC.ToBytes(value)); + return this; + } + + public BinaryList InsertGuid(int position, Guid value) + { + list.InsertRange(position, DC.ToBytes(value)); + return this; + } + + public BinaryList AddGuidArray(Guid[] value) + { + list.AddRange(DC.ToBytes(value)); + return this; + } + + public BinaryList InsertGuidArray(int position, Guid[] value) + { + list.InsertRange(position, DC.ToBytes(value)); + return this; + } + + + public BinaryList AddUInt8Array(byte[] value) + { + list.AddRange(value); + return this; + } + + + public BinaryList InsertUInt8Array(int position, byte[] value) + { + list.InsertRange(position, value); + return this; + } + + + public BinaryList AddHex(string value) + { + return this.AddUInt8Array(DC.FromHex(value, null)); + } + + public BinaryList InsertHex(int position, string value) + { + return this.InsertUInt8Array(position, DC.FromHex(value, null)); + } + + + + public BinaryList AddString(string value) + { + list.AddRange(DC.ToBytes(value)); + return this; + } + + public BinaryList InsertString(int position, string value) + { + list.InsertRange(position, DC.ToBytes(value)); + return this; + } + + public BinaryList AddStringArray(string[] value) + { + list.AddRange(DC.ToBytes(value)); + return this; + } + + public BinaryList InsertStringArray(int position, string[] value) + { + list.InsertRange(position, DC.ToBytes(value)); + return this; + } + + public BinaryList InsertUInt8(int position, byte value) + { + list.Insert(position, value); + return this; + } + + public BinaryList AddUInt8(byte value) + { + list.Add(value); + return this; + } + + public BinaryList AddInt8(sbyte value) + { + list.Add((byte)value); + return this; + } + + public BinaryList InsertInt8(int position, sbyte value) + { + list.Insert(position, (byte)value); + return this; + } + + public BinaryList AddInt8Array(sbyte[] value) + { + list.AddRange(DC.ToBytes(value)); + return this; + } + + public BinaryList InsertInt8Array(int position, sbyte[] value) + { + list.InsertRange(position, DC.ToBytes(value)); + return this; + } + + + public BinaryList AddChar(char value) + { + list.AddRange(DC.ToBytes(value)); + return this; + } + + public BinaryList InsertChar(int position, char value) + { + list.InsertRange(position, DC.ToBytes(value)); + return this; + } + + public BinaryList AddCharArray(char[] value) + { + list.AddRange(DC.ToBytes(value)); + return this; + } + + public BinaryList InsertCharArray(int position, char[] value) + { + list.InsertRange(position, DC.ToBytes(value)); + return this; + } + + + public BinaryList AddBoolean(bool value) + { + list.AddRange(DC.ToBytes(value)); + return this; + } + + public BinaryList InsertBoolean(int position, bool value) + { + list.InsertRange(position, DC.ToBytes(value)); + return this; + } + + public BinaryList AddBooleanArray(bool[] value) + { + list.AddRange(DC.ToBytes(value)); + return this; + } + + public BinaryList InsertBooleanArray(int position, bool[] value) + { + list.InsertRange(position, DC.ToBytes(value)); + return this; + } + + + public BinaryList AddUInt16(ushort value) + { + list.AddRange(DC.ToBytes(value)); + return this; + } + public BinaryList InsertUInt16(int position, ushort value) + { + list.InsertRange(position, DC.ToBytes(value)); + return this; + } + + public BinaryList AddUInt16Array(ushort[] value) + { + list.AddRange(DC.ToBytes(value)); + return this; + } + + public BinaryList InsertUInt16Array(int position, ushort[] value) + { + list.InsertRange(position, DC.ToBytes(value)); + return this; + } + + + public BinaryList AddInt16(short value) + { + list.AddRange(DC.ToBytes(value)); + return this; + } + + public BinaryList InsertInt16(int position, short value) + { + list.InsertRange(position, DC.ToBytes(value)); + return this; + } + + + public BinaryList AddInt16Array(short[] value) + { + list.AddRange(DC.ToBytes(value)); + return this; + } + + public BinaryList InsertInt16Array(int position, short[] value) + { + list.InsertRange(position, DC.ToBytes(value)); + return this; + } + + public BinaryList AddUInt32(uint value) + { + list.AddRange(DC.ToBytes(value)); + return this; + } + public BinaryList InsertUInt32(int position, uint value) + { + list.InsertRange(position, DC.ToBytes(value)); + return this; + } + + public BinaryList AddUInt32Array(uint[] value) + { + list.AddRange(DC.ToBytes(value)); + return this; + } + public BinaryList InsertUInt32Array(int position, uint[] value) + { + list.InsertRange(position, DC.ToBytes(value)); + return this; + } + + + public BinaryList AddInt32(int value) + { + list.AddRange(DC.ToBytes(value)); + return this; + } + public BinaryList InsertInt32(int position, int value) + { + list.InsertRange(position, DC.ToBytes(value)); + return this; + } + + public BinaryList AddInt32Array(int[] value) + { + list.AddRange(DC.ToBytes(value)); + return this; + } + public BinaryList InsertInt32Array(int position, int[] value) + { + list.InsertRange(position, DC.ToBytes(value)); + return this; + } + + + public BinaryList AddUInt64(ulong value) + { + list.AddRange(DC.ToBytes(value)); + return this; + } + public BinaryList InsertUInt64(int position, ulong value) + { + list.InsertRange(position, DC.ToBytes(value)); + return this; + } + + public BinaryList AddUInt64Array(ulong[] value) + { + list.AddRange(DC.ToBytes(value)); + return this; + } + + public BinaryList InsertUInt64Array(int position, ulong[] value) + { + list.InsertRange(position, DC.ToBytes(value)); + return this; + } + + + + + public BinaryList AddInt64(long value) + { + list.AddRange(DC.ToBytes(value)); + return this; + } + + public BinaryList InsertInt64(int position, long value) + { + list.InsertRange(position, DC.ToBytes(value)); + return this; + } + + public BinaryList AddInt64Array(long[] value) + { + list.AddRange(DC.ToBytes(value)); + return this; + } + + public BinaryList InsertInt64Array(int position, long[] value) + { + list.InsertRange(position, DC.ToBytes(value)); + return this; + } + + + public BinaryList AddFloat32(float value) + { + list.AddRange(DC.ToBytes(value)); + return this; + } + + public BinaryList InsertFloat32(int position, float value) + { + list.InsertRange(position, DC.ToBytes(value)); + return this; + } + + public BinaryList AddFloat32Array(float[] value) + { + list.AddRange(DC.ToBytes(value)); + return this; + } + + public BinaryList InsertFloat32Array(int position, float[] value) + { + list.InsertRange(position, DC.ToBytes(value)); + return this; + } + + + public BinaryList AddFloat64(double value) + { + list.AddRange(DC.ToBytes(value)); + return this; + } + + public BinaryList InsertFloat64(int position, double value) + { + list.InsertRange(position, DC.ToBytes(value)); + return this; + } + + public BinaryList AddFloat64Array(double[] value) + { + list.AddRange(DC.ToBytes(value)); + return this; + } + + public BinaryList InsertFloat64Array(int position, double[] value) + { + list.InsertRange(position, DC.ToBytes(value)); + return this; + } + + + + public BinaryList Add(DataType type, object value) + { + switch (type) + { + case DataType.Bool: + AddBoolean((bool)value); + return this; + case DataType.BoolArray: + AddBooleanArray((bool[])value); + return this; + case DataType.UInt8: + AddUInt8((byte)value); + return this; + case DataType.UInt8Array: + AddUInt8Array((byte[])value); + return this; + case DataType.Int8: + AddInt8((sbyte)value); + return this; + case DataType.Int8Array: + AddInt8Array((sbyte[])value); + return this; + case DataType.Char: + AddChar((char)value); + return this; + case DataType.CharArray: + AddCharArray((char[])value); + return this; + case DataType.UInt16: + AddUInt16((ushort)value); + return this; + case DataType.UInt16Array: + AddUInt16Array((ushort[])value); + return this; + case DataType.Int16: + AddInt16((short)value); + return this; + case DataType.Int16Array: + AddInt16Array((short[])value); + return this; + case DataType.UInt32: + AddUInt32((uint)value); + return this; + case DataType.UInt32Array: + AddUInt32Array((uint[])value); + return this; + case DataType.Int32: + AddInt32((int)value); + return this; + case DataType.Int32Array: + AddInt32Array((int[])value); + return this; + case DataType.UInt64: + AddUInt64((ulong)value); + return this; + case DataType.UInt64Array: + AddUInt64Array((ulong[])value); + return this; + case DataType.Int64: + AddInt64((long)value); + return this; + case DataType.Int64Array: + AddInt64Array((long[])value); + return this; + + case DataType.Float32: + AddFloat32((float)value); + return this; + case DataType.Float32Array: + AddFloat32Array((float[])value); + return this; + + case DataType.Float64: + AddFloat64((double)value); + return this; + case DataType.Float64Array: + AddFloat64Array((double[])value); + return this; + + case DataType.String: + AddString((string)value); + return this; + case DataType.StringArray: + AddStringArray((string[])value); + return this; + + case DataType.DateTime: + AddDateTime((DateTime)value); + return this; + case DataType.DateTimeArray: + AddDateTimeArray((DateTime[])value); + return this; + + default: + throw new Exception("Not Implemented " + type.ToString()); + //return this; + } + } /// /// Convert the list to an array of bytes /// /// Bytes array public byte[] ToArray() { - return held.ToArray(); + return list.ToArray(); + } + + public virtual IAsyncReply Done() + { + return null; + // } } } diff --git a/Esiur/Data/Codec.cs b/Esiur/Data/Codec.cs index cc79fc9..9b45275 100644 --- a/Esiur/Data/Codec.cs +++ b/Esiur/Data/Codec.cs @@ -28,7 +28,7 @@ using System.Text; using Esiur.Misc; using System.ComponentModel; using Esiur.Data; -using Esiur.Engine; +using Esiur.Core; using Esiur.Net.IIP; using Esiur.Resource; using System.Linq; @@ -128,24 +128,24 @@ namespace Esiur.Data var rt = new BinaryList(); var comparsion = StructureComparisonResult.Structure; - rt.Append((byte)comparsion); - rt.Append(ComposeStructure(structures[0], connection, true, true, true)); + rt.AddUInt8((byte)comparsion) + .AddUInt8Array(ComposeStructure(structures[0], connection, true, true, true)); for (var i = 1; i < structures.Length; i++) { comparsion = Compare(structures[i - 1], structures[i], connection); - rt.Append((byte)comparsion); + rt.AddUInt8((byte)comparsion); if (comparsion == StructureComparisonResult.Structure) - rt.Append(ComposeStructure(structures[i], connection, true, true, true)); + rt.AddUInt8Array(ComposeStructure(structures[i], connection, true, true, true)); else if (comparsion == StructureComparisonResult.StructureSameKeys) - rt.Append(ComposeStructure(structures[i], connection, false, true, true)); + rt.AddUInt8Array(ComposeStructure(structures[i], connection, false, true, true)); else if (comparsion == StructureComparisonResult.StructureSameTypes) - rt.Append(ComposeStructure(structures[i], connection, false, false, true)); + rt.AddUInt8Array(ComposeStructure(structures[i], connection, false, false, true)); } if (prependLength) - rt.Insert(0, rt.Length); + rt.InsertInt32(0, rt.Length); return rt.ToArray(); } @@ -244,17 +244,19 @@ namespace Esiur.Data foreach (var i in value) { var key = DC.ToBytes(i.Key); - rt.Append((byte)key.Length, key, Compose(i.Value, connection)); + rt.AddUInt8((byte)key.Length) + .AddUInt8Array(key) + .AddUInt8Array(Compose(i.Value, connection)); } } else { foreach (var i in value) - rt.Append(Compose(i.Value, connection, includeTypes)); + rt.AddUInt8Array(Compose(i.Value, connection, includeTypes)); } if (prependLength) - rt.Insert(0, rt.Length); + rt.InsertInt32(0, rt.Length); return rt.ToArray(); } @@ -587,7 +589,7 @@ namespace Esiur.Data /// Resource to check. /// DistributedConnection to check if the resource is local to it. /// True, if the resource owner is the given connection, otherwise False. - static bool IsLocalResource(IResource resource, DistributedConnection connection) + public static bool IsLocalResource(IResource resource, DistributedConnection connection) { if (resource is DistributedResource) if ((resource as DistributedResource).Connection == connection) @@ -628,7 +630,8 @@ namespace Esiur.Data return DC.ToBytes((resource as DistributedResource).Id); else { - return BinaryList.ToBytes(resource.Instance.Template.ClassId, resource.Instance.Id); + return new BinaryList().AddGuid(resource.Instance.Template.ClassId).AddUInt32(resource.Instance.Id).ToArray(); + //return BinaryList.ToBytes(resource.Instance.Template.ClassId, resource.Instance.Id); } } @@ -648,25 +651,25 @@ namespace Esiur.Data var rt = new BinaryList(); var comparsion = Compare(null, resources[0], connection); - rt.Append((byte)comparsion); + rt.AddUInt8((byte)comparsion); if (comparsion == ResourceComparisonResult.Local) - rt.Append((resources[0] as DistributedResource).Id); + rt.AddUInt32((resources[0] as DistributedResource).Id); else if (comparsion == ResourceComparisonResult.Distributed) - rt.Append(resources[0].Instance.Id); + rt.AddUInt32(resources[0].Instance.Id); for (var i = 1; i < resources.Length; i++) { comparsion = Compare(resources[i - 1], resources[i], connection); - rt.Append((byte)comparsion); + rt.AddUInt8((byte)comparsion); if (comparsion == ResourceComparisonResult.Local) - rt.Append((resources[i] as DistributedResource).Id); + rt.AddUInt32((resources[i] as DistributedResource).Id); else if (comparsion == ResourceComparisonResult.Distributed) - rt.Append(resources[i].Instance.Id); + rt.AddUInt32(resources[i].Instance.Id); } if (prependLength) - rt.Insert(0, rt.Length); + rt.InsertInt32(0, rt.Length); return rt.ToArray(); } @@ -831,9 +834,16 @@ namespace Esiur.Data /// Array of bytes in the network byte order. public static byte[] ComposePropertyValue(PropertyValue propertyValue, DistributedConnection connection)//, bool includeAge = true) { + + return new BinaryList() + .AddUInt64(propertyValue.Age) + .AddDateTime(propertyValue.Date) + .AddUInt8Array(Compose(propertyValue.Value, connection)) + .ToArray(); + // age, date, value //if (includeAge) - return BinaryList.ToBytes(propertyValue.Age, propertyValue.Date, Compose(propertyValue.Value, connection)); + // return BinaryList.ToBytes(propertyValue.Age, propertyValue.Date, Compose(propertyValue.Value, connection)); //else // return BinaryList.ToBytes(propertyValue.Date, Compose(propertyValue.Value, connection)); @@ -905,7 +915,7 @@ namespace Esiur.Data while (offset < ends) { var index = data[offset++]; - var pt = resource.Instance.Template.GetPropertyTemplate(index); + var pt = resource.Instance.Template.GetPropertyTemplateByIndex(index); list.Add(pt, null); var cs = DC.GetUInt32(data, offset); offset += 4; @@ -933,16 +943,20 @@ namespace Esiur.Data /// History /// DistributedConnection is required to fetch resources. /// - public static byte[] ComposeHistory(KeyList history, DistributedConnection connection, bool prependLength = false) + public static byte[] ComposeHistory(KeyList history, + DistributedConnection connection, bool prependLength = false) { var rt = new BinaryList(); for (var i = 0; i < history.Count; i++) - rt.Append((byte)history.Keys.ElementAt(i).Index, - ComposePropertyValueArray(history.Values.ElementAt(i), connection, true)); + rt.AddUInt8(history.Keys.ElementAt(i).Index) + .AddUInt8Array(ComposePropertyValueArray(history.Values.ElementAt(i), connection, true)); + + // rt.Append((byte)history.Keys.ElementAt(i).Index, + // ComposePropertyValueArray(history.Values.ElementAt(i), connection, true)); if (prependLength) - rt.Insert(0, (uint)rt.Length); + rt.InsertInt32(0, rt.Length); return rt.ToArray(); } @@ -1005,47 +1019,47 @@ namespace Esiur.Data case DataType.String: var st = DC.ToBytes((string)value); - rt.Append(st.Length, st); + rt.AddInt32(st.Length).AddUInt8Array(st); break; case DataType.Resource: - rt.Append((value as DistributedResource).Id); + rt.AddUInt32((value as DistributedResource).Id); break; case DataType.DistributedResource: //rt.Append((value as IResource).Instance.Template.ClassId, (value as IResource).Instance.Id); - rt.Append((value as IResource).Instance.Id); + rt.AddUInt32((value as IResource).Instance.Id); break; case DataType.Structure: - rt.Append(ComposeStructure((Structure)value, connection, true, true, true)); + rt.AddUInt8Array(ComposeStructure((Structure)value, connection, true, true, true)); break; case DataType.VarArray: - rt.Append(ComposeVarArray((object[])value, connection, true)); + rt.AddUInt8Array(ComposeVarArray((object[])value, connection, true)); break; case DataType.ResourceArray: if (value is IResource[]) - rt.Append(ComposeResourceArray((IResource[])value, connection, true)); + rt.AddUInt8Array(ComposeResourceArray((IResource[])value, connection, true)); else - rt.Append(ComposeResourceArray((IResource[])DC.CastConvert(value, typeof(IResource[])), connection, true)); + rt.AddUInt8Array(ComposeResourceArray((IResource[])DC.CastConvert(value, typeof(IResource[])), connection, true)); break; case DataType.StructureArray: - rt.Append(ComposeStructureArray((Structure[])value, connection, true)); + rt.AddUInt8Array(ComposeStructureArray((Structure[])value, connection, true)); break; default: - rt.Append(value); + rt.Add(type, value); if (type.IsArray()) - rt.Insert(0, rt.Length); + rt.InsertInt32(0, rt.Length); break; } if (prependType) - rt.Insert(0, (byte)type); + rt.InsertUInt8(0, (byte)type); return rt.ToArray(); } @@ -1058,23 +1072,56 @@ namespace Esiur.Data /// True, if implements . public static bool ImplementsInterface(Type type, Type iface) { - - - while (type != null) + /* + if (iface.GetTypeInfo().IsGenericType) { - if (type == iface) - return true; + //var x = (type.GetTypeInfo().GetInterfaces().Any(x => x.GetTypeInfo().IsGenericType Contains(iface)) + + iface = iface.GetTypeInfo().GetGenericTypeDefinition(); + + //if (type.GetTypeInfo().IsGenericType) + // type = + while (type != null) + { + if (type == iface) + return true; #if NETSTANDARD1_5 - if (type.GetTypeInfo().GetInterfaces().Contains(iface)) - return true; - type = type.GetTypeInfo().BaseType; + if (type.GetTypeInfo().GetInterfaces().Contains(iface))// (x=>x.GetTypeInfo().IsGenericType (iface)) + return true; + + type = type.GetTypeInfo().BaseType; #else if (type.GetInterfaces().Contains(iface)) return true; type = type.BaseType; #endif + } + + } + else + */ + //{ + + while (type != null) + { + if (type == iface) + return true; + +#if NETSTANDARD1_5 + if (type.GetTypeInfo().GetInterfaces().Contains(iface)) + return true; + + type = type.GetTypeInfo().BaseType; +#else + if (type.GetInterfaces().Contains(iface)) + return true; + type = type.BaseType; +#endif + } + + //} return false; } diff --git a/Esiur/Data/DataConverter.cs b/Esiur/Data/DataConverter.cs index 8b477a4..c23d5ed 100644 --- a/Esiur/Data/DataConverter.cs +++ b/Esiur/Data/DataConverter.cs @@ -34,7 +34,7 @@ using System.Net; using System.Net.NetworkInformation; using System.Reflection; using Esiur.Data; -using Esiur.Engine; +using Esiur.Core; using Esiur.Resource; namespace Esiur.Data @@ -88,6 +88,15 @@ namespace Esiur.Data { try { + var underType = Nullable.GetUnderlyingType(destinationType); + if (underType != null) + { + if (value == null) + return null; + else + destinationType = underType; + } + #if NETSTANDARD1_5 if (destinationType.GetTypeInfo().IsInstanceOfType(value)) #else @@ -105,64 +114,523 @@ namespace Esiur.Data } } - /* - private static byte[] ReverseArray(byte[] data, uint offset, uint count) + + + + public static byte[] ToBytes(sbyte value) { - if (offset + count > data.Length) + return new byte[1] { (byte)value }; + } + + public static byte[] ToBytes(byte value) + { + return new byte[1] { value }; + } + + public static byte[] ToBytes(IPAddress ip) + { + return ip.GetAddressBytes(); + } + + public static byte[] ToBytes(PhysicalAddress mac) + { + return mac.GetAddressBytes(); + } + + public static byte[] ToBytes(bool value) + { + return new byte[1] { value ? (byte)1 : (byte)0 }; + } + + public static byte ToByte(bool value) + { + return value ? (byte)1 : (byte)0; + } + + public static byte ToByte(sbyte value) + { + return (byte)value; + } + + public static byte[] ToBytes(byte[] value) + { + return value; + } + + + public static byte[] ToBytes(bool[] value) + { + + byte[] ba = new byte[value.Length]; + + for (int i = 0; i < ba.Length; i++) + ba[i] = DC.ToByte(value[i]); + + return ba; + } + + public static byte[] ToBytes(sbyte[] value) + { + + byte[] ba = new byte[value.Length]; + + for (int i = 0; i < ba.Length; i++) + ba[i] = DC.ToByte(value[i]); + + return ba; + } + + public static byte[] ToBytes(char value) + { + byte[] ret = BitConverter.GetBytes(value); + Array.Reverse(ret); + return ret; + } + + public static byte[] ToBytes(Guid value) + { + return value.ToByteArray(); + } + + public static byte[] ToBytes(Guid[] value) + { + var rt = new List(); + foreach (var g in value) + rt.AddRange(g.ToByteArray()); + return rt.ToArray(); + } + + public static byte[] ToBytes(char[] value) + { + List rt = new List(); + + for (int i = 0; i < value.Length; i++) + rt.AddRange(ToBytes(value[i])); + + return rt.ToArray(); + } + + public static byte[] ToBytes(short[] value) + { + List rt = new List(); + + for (int i = 0; i < value.Length; i++) + rt.AddRange(ToBytes(value[i])); + + return rt.ToArray(); + } + + + + public static byte[] ToBytes(ushort[] value) + { + List rt = new List(); + + for (int i = 0; i < value.Length; i++) + rt.AddRange(ToBytes(value[i])); + + return rt.ToArray(); + } + + public static void Append(ref byte[] dst, byte[] src) + { + Append(ref dst, src, (uint)0, (uint)src.Length); + } + + public static void Append(ref byte[] dst, byte[] src, uint srcOffset, uint length) + { + var dstOffset = dst.Length; + Array.Resize(ref dst, dstOffset + (int)length); + Buffer.BlockCopy(src, (int)srcOffset, dst, dstOffset, (int)length); + } + + public static byte[] Combine(byte[] src1, uint src1Offset, uint src1Length, byte[] src2, uint src2Offset, uint src2Length) + { + var rt = new byte[src1Length + src2Length]; + Buffer.BlockCopy(src1, (int)src1Offset, rt, 0, (int)src1Length); + Buffer.BlockCopy(src2, (int)src2Offset, rt, (int)src1Length, (int)src2Length); + return rt; + } + + public static byte[] Merge(params byte[][] arrays) + { + var s = arrays.Sum(x => x.Length); + var r = new byte[s]; + var offset = 0; + foreach (var array in arrays) { - Console.WriteLine("ReverseArray: Bad offset " + data.Length + " " + offset + " " + count); - - StackTrace st = new StackTrace(); - Console.WriteLine(st.ToString()); - - return null; + Buffer.BlockCopy(array, 0, r, offset, array.Length); + offset += array.Length; } - byte[] rt = new byte[count]; + return r; + } - uint b = count; - for (var i = offset; i < (offset + count); i++) - rt[--b] = data[i]; + public static byte[] ToBytes(this int[] value) + { + List rt = new List(); + + for (int i = 0; i < value.Length; i++) + rt.AddRange(ToBytes(value[i])); + + return rt.ToArray(); + } + + public static byte[] ToBytes(this uint[] value) + { + List rt = new List(); + + for (int i = 0; i < value.Length; i++) + rt.AddRange(ToBytes(value[i])); + + return rt.ToArray(); + } + + public static byte[] ToBytes(this long[] value) + { + List rt = new List(); + + for (int i = 0; i < value.Length; i++) + rt.AddRange(ToBytes(value[i])); + + return rt.ToArray(); + } + + public static byte[] ToBytes(this ulong[] value) + { + List rt = new List(); + + for (int i = 0; i < value.Length; i++) + rt.AddRange(ToBytes(value[i])); + + return rt.ToArray(); + } + + public static byte[] ToBytes(this float[] value) + { + List rt = new List(); + + for (int i = 0; i < value.Length; i++) + rt.AddRange(ToBytes(value[i])); + + return rt.ToArray(); + } + + public static byte[] ToBytes(this double[] value) + { + List rt = new List(); + + for (int i = 0; i < value.Length; i++) + rt.AddRange(ToBytes(value[i])); + + return rt.ToArray(); + } + + + public static byte[] ToBytes(this decimal[] value) + { + List rt = new List(); + + for (int i = 0; i < value.Length; i++) + rt.AddRange(ToBytes(value[i])); + + return rt.ToArray(); + } + + + public static byte[] ToBytes(this DateTime[] value) + { + List rt = new List(); + + for (int i = 0; i < value.Length; i++) + rt.AddRange(ToBytes(value[i])); + + return rt.ToArray(); + } + + + public static byte[] ToBytes(this string[] value) + { + List rt = new List(); + + for (int i = 0; i < value.Length; i++) + { + byte[] ba = ToBytes(value[i]); + // add string length + rt.AddRange(ToBytes(ba.Length)); + // add encoded string + rt.AddRange(ba); + } + + return rt.ToArray(); + } + + public static unsafe byte[] ToBytes(this int value) + { + var rt = new byte[4]; + byte* p = (byte*)&value; + + rt[0] = *(p + 3); + rt[1] = *(p + 2); + rt[2] = *(p + 1); + rt[3] = *(p + 0); return rt; } - */ - /* - public static T[] ArrayFromBytes(byte[] data, uint offset, uint length) + public static unsafe byte[] ToBytes(this short value) + { + var rt = new byte[2]; + byte* p = (byte*)&value; + + rt[0] = *(p + 1); + rt[1] = *(p + 0); + + return rt; + } + + public static unsafe byte[] ToBytes(this float value) + + { + var rt = new byte[4]; + + //float rt = 0; + byte* p = (byte*)&value; + rt[0] = *(p + 3); + rt[1] = *(p + 2); + rt[2] = *(p + 1); + rt[3] = *(p); + + return rt; + } + + + + + public static byte[] ToBytes(this string value) + { + return Encoding.UTF8.GetBytes(value); + } + + public unsafe static byte[] ToBytes(this double value) + { + var rt = new byte[8]; + + byte* p = (byte*)&value; + + rt[0] = *(p + 7); + rt[1] = *(p + 6); + rt[2] = *(p + 5); + rt[3] = *(p + 4); + rt[4] = *(p + 3); + rt[5] = *(p + 2); + rt[6] = *(p + 1); + rt[7] = *(p + 0); + + return rt; + } + + public static unsafe byte[] ToBytes(this long value) + { + var rt = new byte[8]; + + byte* p = (byte*)&value; + + rt[0] = *(p + 7); + rt[1] = *(p + 6); + rt[2] = *(p + 5); + rt[3] = *(p + 4); + rt[4] = *(p + 3); + rt[5] = *(p + 2); + rt[6] = *(p + 1); + rt[7] = *(p + 0); + + return rt; + + } + + public static unsafe byte[] ToBytes(this DateTime value) { - if (typeof(T) == typeof(string)) + var rt = new byte[8]; + var v = value.ToUniversalTime().Ticks; + + byte* p = (byte*)&v; + + rt[0] = *(p + 7); + rt[1] = *(p + 6); + rt[2] = *(p + 5); + rt[3] = *(p + 4); + rt[4] = *(p + 3); + rt[5] = *(p + 2); + rt[6] = *(p + 1); + rt[7] = *(p + 0); + + return rt; + + } + + + public static unsafe byte[] ToBytes(this ulong value) + { + var rt = new byte[8]; + + byte* p = (byte*)&value; + + rt[0] = *(p + 7); + rt[1] = *(p + 6); + rt[2] = *(p + 5); + rt[3] = *(p + 4); + rt[4] = *(p + 3); + rt[5] = *(p + 2); + rt[6] = *(p + 1); + rt[7] = *(p + 0); + + return rt; + } + + public static unsafe byte[] ToBytes(this uint value) + { + + var rt = new byte[4]; + + byte* p = (byte*)&value; + + rt[0] = *(p + 3); + rt[1] = *(p + 2); + rt[2] = *(p + 1); + rt[3] = *(p + 0); + + return rt; + } + + public static unsafe byte[] ToBytes(this ushort value) + { + var rt = new byte[2]; + + byte* p = (byte*)&value; + + rt[0] = *(p + 1); + rt[1] = *(p); + + return rt; + } + + public static byte[] ToBytes(this decimal value) + { + byte[] ret = new byte[0];// BitConverter.GetBytes(value); + + Array.Reverse(ret); + + return ret; + } + + public static string ToHex(this byte[] ba) + { + if (ba == null) + return "NULL"; + return ToHex(ba, 0, (uint)ba.Length); + } + + public static string ToHex(this byte[] ba, uint offset, uint length, string separator = " ") + { + StringBuilder hex = new StringBuilder((int)length * 2); + + for (var i = offset; i < offset + length; i++) { - List ar = new List(); + hex.AppendFormat("{0:x2}", ba[i]); + if (separator != null) + hex.Append(separator); + } - uint i = 0; + return hex.ToString(); + } - while (i < length) - { - var cl = GetUInt32(data, 0); - i += 4; - ar.Add(Encoding.UTF8.GetString(data, (int)(offset + i), (int)cl)); - i += cl; - } - - return (T[])(object)ar.ToArray(); + public static byte[] FromHex(string hexString, string separator = " ") + { + var rt = new List(); + if (separator == null) + { + for (var i = 0; i < hexString.Length; i += 2) + rt.Add(Convert.ToByte(hexString.Substring(i, 2), 16)); } else { - uint blockSize = (uint)Marshal.SizeOf(typeof(T)); + var hexes = hexString.Split(new string[] { separator }, StringSplitOptions.RemoveEmptyEntries); + foreach (var h in hexes) + rt.Add(Convert.ToByte(h, 16)); + } - T[] ar = new T[length / blockSize]; + return rt.ToArray(); + } - for (uint i = 0; i < ar.Length; i += blockSize) - ar[i] = FromBytes(data, offset + i); + public static string FlagsEnumToString(ulong value) + { - return ar; + string rt = typeof(T).Name + ":"; + + for (int i = 0; i < 64; i++) + { + ulong bit = (ulong)(Convert.ToUInt64(Math.Pow(2, i)) & value); + if (bit != 0) + { + rt += " " + Enum.GetName(typeof(T), bit); + } + } + + return rt; + } + + + + public static bool TryParse(object Input, out T Results) + { + try + { +#if NETSTANDARD1_5 + var tryParse = typeof(T).GetTypeInfo().GetDeclaredMethod("TryParse"); + if ((bool)tryParse.Invoke(null, new object[] { Input, null })) + { + var parse = typeof(T).GetTypeInfo().GetDeclaredMethod("Parse"); + + Results = (T)parse.Invoke(null, new object[] { Input }); + return true; + } +#else + if ((bool)typeof(T).InvokeMember("TryParse", BindingFlags.Public | BindingFlags.Static | BindingFlags.InvokeMethod, null, null, new object[] { Input, null })) + { + Results = (T)typeof(T).InvokeMember("Parse", BindingFlags.Public | BindingFlags.Static | BindingFlags.InvokeMethod, null, null, new object[] { Input }); + return true; + } + +#endif + else + { + Results = default(T); + return false; + } + } + catch //Exception ex) + { + Results = default(T); + return false; } } - */ + + + + public static DateTime FromUnixTime(uint seconds) + { + return new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds((double)seconds); + } + + public static DateTime FromUnixTime(ulong milliseconds) + { + return new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds((double)milliseconds); + } + public static sbyte GetInt8(this byte[] data, uint offset) { @@ -183,7 +651,7 @@ namespace Esiur.Data public static byte[] GetUInt8Array(this byte[] data, uint offset, uint length) { - var rt = new byte[length]; + var rt = new byte[length]; Buffer.BlockCopy(rt, (int)offset, rt, 0, (int)length); return rt; } @@ -195,7 +663,7 @@ namespace Esiur.Data public static Int16[] GetInt16Array(this byte[] data, uint offset, uint length) { - var rt = new Int16[length/2]; + var rt = new Int16[length / 2]; for (var i = 0; i < length; i += 2) rt[i] = GetInt16(data, (uint)(offset + i)); @@ -225,7 +693,7 @@ namespace Esiur.Data var rt = new Int32[length / 4]; for (var i = 0; i < length; i += 4) rt[i] = GetInt32(data, (uint)(offset + i)); - + return rt; } @@ -376,15 +844,15 @@ namespace Esiur.Data public static char[] GetCharArray(this byte[] data, uint offset, uint length) { - var rt = new char[length/2]; - for (var i = 0; i < length; i+=2) + var rt = new char[length / 2]; + for (var i = 0; i < length; i += 2) rt[i] = GetChar(data, (uint)(offset + i)); return rt; } public static string GetString(this byte[] data, uint offset, uint length) { - return Encoding.UTF8.GetString(data, (int)offset, (int)length); + return Encoding.UTF8.GetString(data, (int)offset, (int)length); } public static string[] GetStringArray(this byte[] data, uint offset, uint length) @@ -406,7 +874,7 @@ namespace Esiur.Data public static Guid GetGuid(this byte[] data, uint offset) { - return new Guid(DC.Clip(data, offset, 16)); + return new Guid(Clip(data, offset, 16)); } public static Guid[] GetGuidArray(this byte[] data, uint offset, uint length) @@ -549,161 +1017,6 @@ namespace Esiur.Data } */ - public static byte[] ToBytes(sbyte value) - { - return new byte[1] { (byte)value }; - } - - public static byte[] ToBytes(byte value) - { - return new byte[1] { value }; - } - - public static byte[] ToBytes(IPAddress ip) - { - return ip.GetAddressBytes(); - } - - public static byte[] ToBytes(PhysicalAddress mac) - { - return mac.GetAddressBytes(); - } - - public static byte[] ToBytes(bool value) - { - return new byte[1] { value ? (byte)1 : (byte)0 }; - } - - public static byte ToByte(bool value) - { - return value ? (byte)1 : (byte)0; - } - - public static byte[] ToBytes(byte[] value) - { - return value; - } - - //public static byte[] ToBytes(Codec value) - //{ - // return value.ToStructuredValue(); - //} - - public static byte[] ToBytes(bool[] value) - { - - byte[] ba = new byte[value.Length]; - - for (int i = 0; i < ba.Length; i++) - ba[i] = DC.ToByte(value[i]); - - return ba; - } - - - /* - public static byte[] ToBytes(IResource value) - { - - if (value is DistributedResource) - return DC.ToBytes((value as DistributedResource).Id); - else - { - List rt = new List(); - // Add GUID - rt.AddRange(value.Instance.Template.ClassId.ToByteArray()); - // Add Instance ID - rt.AddRange(DC.ToBytes(value.Instance.Id)); - - return rt.ToArray(); - } - } - */ - - /* - public static byte[] ToBytes(Structure value) - { - return value.Compose(); - }*/ - - public static byte[] ToBytes(char value) - { - byte[] ret = BitConverter.GetBytes(value); - Array.Reverse(ret); - return ret; - } - - public static byte[] ToBytes(Guid value) - { - return value.ToByteArray(); - } - - public static byte[] ToBytes(char[] value) - { - List rt = new List(); - - for (int i = 0; i < value.Length; i++) - rt.AddRange(ToBytes(value[i])); - - return rt.ToArray(); - } - - public static byte[] ToBytes(short[] value) - { - List rt = new List(); - - for (int i = 0; i < value.Length; i++) - rt.AddRange(ToBytes(value[i])); - - return rt.ToArray(); - } - - - - public static byte[] ToBytes(ushort[] value) - { - List rt = new List(); - - for (int i = 0; i < value.Length; i++) - rt.AddRange(ToBytes(value[i])); - - return rt.ToArray(); - } - - public static void Append(ref byte[] dst, byte[] src) - { - Append(ref dst, src, (uint)0, (uint)src.Length); - } - - public static void Append(ref byte[] dst, byte[] src, uint srcOffset, uint length) - { - var dstOffset = dst.Length; - Array.Resize(ref dst, dstOffset + (int)length); - Buffer.BlockCopy(src, (int)srcOffset, dst, dstOffset, (int)length); - } - - public static byte[] Combine(byte[] src1, uint src1Offset, uint src1Length, byte[] src2, uint src2Offset, uint src2Length) - { - var rt = new byte[src1Length + src2Length]; - Buffer.BlockCopy(src1, (int)src1Offset, rt, 0, (int)src1Length); - Buffer.BlockCopy(src2, (int)src2Offset, rt, (int)src1Length, (int)src2Length); - return rt; - } - - public static byte[] Merge(params byte[][] arrays) - { - var s = arrays.Sum(x => x.Length); - var r = new byte[s]; - var offset = 0; - foreach (var array in arrays) - { - Buffer.BlockCopy(array, 0, r, offset, array.Length); - offset += array.Length; - } - - return r; - } - public static byte[] Clip(this byte[] data, uint offset, uint length) { if (data.Length < offset + length) @@ -717,441 +1030,14 @@ namespace Esiur.Data return b; } - public static byte[] ToBytes(int[] value) - { - List rt = new List(); - - for (int i = 0; i < value.Length; i++) - rt.AddRange(ToBytes(value[i])); - - return rt.ToArray(); - } - - public static byte[] ToBytes(uint[] value) - { - List rt = new List(); - - for (int i = 0; i < value.Length; i++) - rt.AddRange(ToBytes(value[i])); - - return rt.ToArray(); - } - - public static byte[] ToBytes(long[] value) - { - List rt = new List(); - - for (int i = 0; i < value.Length; i++) - rt.AddRange(ToBytes(value[i])); - - return rt.ToArray(); - } - - public static byte[] ToBytes(ulong[] value) - { - List rt = new List(); - - for (int i = 0; i < value.Length; i++) - rt.AddRange(ToBytes(value[i])); - - return rt.ToArray(); - } - - public static byte[] ToBytes(float[] value) - { - List rt = new List(); - - for (int i = 0; i < value.Length; i++) - rt.AddRange(ToBytes(value[i])); - - return rt.ToArray(); - } - - public static byte[] ToBytes(double[] value) - { - List rt = new List(); - - for (int i = 0; i < value.Length; i++) - rt.AddRange(ToBytes(value[i])); - - return rt.ToArray(); - } - - - public static byte[] ToBytes(decimal[] value) - { - List rt = new List(); - - for (int i = 0; i < value.Length; i++) - rt.AddRange(ToBytes(value[i])); - - return rt.ToArray(); - } - - - public static byte[] ToBytes(DateTime[] value) - { - List rt = new List(); - - for (int i = 0; i < value.Length; i++) - rt.AddRange(ToBytes(value[i])); - - return rt.ToArray(); - } - - - public static byte[] ToBytes(string[] value) - { - List rt = new List(); - - for (int i = 0; i < value.Length; i++) - { - byte[] ba = ToBytes(value[i]); - // add string length - rt.AddRange(ToBytes(ba.Length)); - // add encoded string - rt.AddRange(ba); - } - - return rt.ToArray(); - } - - public static unsafe byte[] ToBytes(int value) - { - var rt = new byte[4]; - byte* p = (byte*)&value; - - rt[0] = *(p + 3); - rt[1] = *(p + 2); - rt[2] = *(p + 1); - rt[3] = *(p + 0); - - return rt; - } - - public static unsafe byte[] ToBytes(short value) - { - var rt = new byte[2]; - byte* p = (byte*)&value; - - rt[0] = *(p + 1); - rt[1] = *(p + 0); - - return rt; - } - - public static unsafe byte[] ToBytes(float value) - - { - var rt = new byte[4]; - - //float rt = 0; - byte* p = (byte*)&value; - rt[0] = *(p + 3); - rt[1] = *(p + 2); - rt[2] = *(p + 1); - rt[3] = *(p); - - return rt; - } - - - - - /* - public static byte[] ToBytes(Structure[] values) - { - if (values == null || values.Length == 0) - return new byte[0]; - - var rt = new BinaryList(); - var previous = values[0]; - - // include first one - if (previous == null) - rt.Append((byte)Structure.ComparisonResult.Null); - else - rt.Append((byte)Structure.ComparisonResult.DifferentStructure, previous.Compose(true,true,true)); - - for (var i = 1; i < values.Length; i++) - { - var current = values[i]; - var results = Structure.Compare(previous, current); - - rt.Append((byte)results); - - if (results == Structure.ComparisonResult.DifferentStructure) - rt.Append(current.Compose(true, true, true)); - else if (results == Structure.ComparisonResult.SameStructureDifferentValueTypes) - rt.Append(current.Compose(false)); - else if (results == Structure.ComparisonResult.SameStructureDifferentValues) - rt.Append(current.Compose(false, false)); - } - - return rt.ToArray(); - } - - - public static byte[] ToBytes(IIPObject[] values) - { - if (values == null || values.Length == 0) - return new byte[0]; - - var rt = new BinaryList(); - var previous = values[0]; - - // include first one - if (previous == null) - rt.Append((byte)IIPObject.ComparisonResult.Null); - else - rt.Append((byte)IIPObject.ComparisonResult.DifferentObject, previous.GUID.ToByteArray(), previous.InstanceID); - - for (var i = 1; i < values.Length; i++) - { - var current = values[i]; - var results = IIPObject.Compare(previous, current); - - rt.Append((byte)results); - - if (results == IIPObject.ComparisonResult.DifferentObject) - rt.Append(current.GUID.ToByteArray(), current.InstanceID); - else if (results == IIPObject.ComparisonResult.SameTypeDifferentInstance) - rt.Append(current.InstanceID); - } - - return rt.ToArray(); - } - */ - public static byte[] ToBytes(string value) - { - return Encoding.UTF8.GetBytes(value); - } - - public unsafe static byte[] ToBytes(double value) - { - var rt = new byte[8]; - - byte* p = (byte*)&value; - - rt[0] = *(p + 7); - rt[1] = *(p + 6); - rt[2] = *(p + 5); - rt[3] = *(p + 4); - rt[4] = *(p + 3); - rt[5] = *(p + 2); - rt[6] = *(p + 1); - rt[7] = *(p + 0); - - return rt; - } - - public static unsafe byte[] ToBytes(long value) - { - var rt = new byte[8]; - - byte* p = (byte*)&value; - - rt[0] = *(p + 7); - rt[1] = *(p + 6); - rt[2] = *(p + 5); - rt[3] = *(p + 4); - rt[4] = *(p + 3); - rt[5] = *(p + 2); - rt[6] = *(p + 1); - rt[7] = *(p + 0); - - return rt; - - } - - public static unsafe byte[] ToBytes(DateTime value) - { - - var rt = new byte[8]; - var v = value.ToUniversalTime().Ticks; - - byte* p = (byte*)&v; - - rt[0] = *(p + 7); - rt[1] = *(p + 6); - rt[2] = *(p + 5); - rt[3] = *(p + 4); - rt[4] = *(p + 3); - rt[5] = *(p + 2); - rt[6] = *(p + 1); - rt[7] = *(p + 0); - - return rt; - - } - - - public static unsafe byte[] ToBytes(ulong value) - { - var rt = new byte[8]; - - byte* p = (byte*)&value; - - rt[0] = *(p + 7); - rt[1] = *(p + 6); - rt[2] = *(p + 5); - rt[3] = *(p + 4); - rt[4] = *(p + 3); - rt[5] = *(p + 2); - rt[6] = *(p + 1); - rt[7] = *(p + 0); - - return rt; - } - - public static unsafe byte[] ToBytes(uint value) - { - - var rt = new byte[4]; - - byte* p = (byte*)&value; - - rt[0] = *(p + 3); - rt[1] = *(p + 2); - rt[2] = *(p + 1); - rt[3] = *(p + 0); - - return rt; - } - - public static unsafe byte[] ToBytes(ushort value) - { - var rt = new byte[2]; - - byte* p = (byte*)&value; - - rt[0] = *(p + 1); - rt[1] = *(p); - - return rt; - } - - public static byte[] ToBytes(decimal value) - { - byte[] ret = new byte[0];// BitConverter.GetBytes(value); - - Array.Reverse(ret); - - return ret; - } - - public static string ToHex(byte[] ba) - { - if (ba == null) - return "NULL"; - return ToHex(ba, 0, (uint)ba.Length); - } - - public static string ToHex(byte[] ba, uint offset, uint length, string separator = " ") - { - StringBuilder hex = new StringBuilder((int)length * 2); - - for (var i = offset; i < offset + length; i++) - { - hex.AppendFormat("{0:x2}", ba[i]); - if (separator != null) - hex.Append(separator); - } - - return hex.ToString(); - } - - public static byte[] FromHex(string hexString, string separator = " ") - { - var rt = new List(); - - if (separator == null) - { - for (var i = 0; i < hexString.Length; i += 2) - rt.Add(Convert.ToByte(hexString.Substring(i, 2), 16)); - } - else - { - var hexes = hexString.Split(new string[] { separator }, StringSplitOptions.RemoveEmptyEntries); - foreach (var h in hexes) - rt.Add(Convert.ToByte(h, 16)); - } - - return rt.ToArray(); - } - - public static string FlagsEnumToString(ulong value) - { - - string rt = typeof(T).Name + ":"; - - for (int i = 0; i < 64; i++) - { - ulong bit = (ulong)(Convert.ToUInt64(Math.Pow(2, i)) & value); - if (bit != 0) - { - rt += " " + Enum.GetName(typeof(T), bit); - } - } - - return rt; - } - public static string ToISODateTime(this DateTime date) { return date.ToString("yyyy-MM-dd HH:mm:ss"); } - - - public static bool TryParse(object Input, out T Results) - { - try - { -#if NETSTANDARD1_5 - var tryParse = typeof(T).GetTypeInfo().GetDeclaredMethod("TryParse"); - if ((bool)tryParse.Invoke(null, new object[] { Input, null })) - { - var parse = typeof(T).GetTypeInfo().GetDeclaredMethod("Parse"); - - Results = (T)parse.Invoke(null, new object[] { Input }); - return true; - } -#else - if ((bool)typeof(T).InvokeMember("TryParse", BindingFlags.Public | BindingFlags.Static | BindingFlags.InvokeMethod, null, null, new object[] { Input, null })) - { - Results = (T)typeof(T).InvokeMember("Parse", BindingFlags.Public | BindingFlags.Static | BindingFlags.InvokeMethod, null, null, new object[] { Input }); - return true; - } - -#endif - else - { - Results = default(T); - return false; - } - } - catch //Exception ex) - { - Results = default(T); - return false; - } - } - - public static uint ToUnixTime(this DateTime date) { return (uint)(date - new DateTime(1970, 1, 1)).TotalSeconds; } - - public static DateTime FromUnixTime(uint seconds) - { - return new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds((double)seconds); - } - - public static DateTime FromUnixTime(ulong milliseconds) - { - return new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds((double)milliseconds); - } } diff --git a/Esiur/Data/KeyList.cs b/Esiur/Data/KeyList.cs index b91696b..eaac428 100644 --- a/Esiur/Data/KeyList.cs +++ b/Esiur/Data/KeyList.cs @@ -31,7 +31,7 @@ using System.Reflection; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; -using Esiur.Engine; +using Esiur.Core; namespace Esiur.Data { diff --git a/Esiur/Data/Structure.cs b/Esiur/Data/Structure.cs index dee4f71..b90f762 100644 --- a/Esiur/Data/Structure.cs +++ b/Esiur/Data/Structure.cs @@ -30,7 +30,7 @@ using System.Text; using System.Threading.Tasks; using Esiur.Data; using Esiur.Misc; -using Esiur.Engine; +using Esiur.Core; namespace Esiur.Data { diff --git a/Esiur/Esiur.csproj b/Esiur/Esiur.csproj index f0eb67c..9117ea0 100644 --- a/Esiur/Esiur.csproj +++ b/Esiur/Esiur.csproj @@ -7,11 +7,12 @@ https://github.com/esiur/esiur-dotnet/blob/master/LICENSE http://www.esiur.com true - 1.2.6 + 1.2.7 https://github.com/esiur/esiur-dotnet Ahmed Kh. Zamil - 1.2.5.0 + 1.2.7.0 Esiur Foundation + 1.2.7.0 @@ -24,7 +25,7 @@ - + @@ -33,7 +34,7 @@ - + diff --git a/Esiur/Misc/Global.cs b/Esiur/Misc/Global.cs index da593e8..20c7848 100644 --- a/Esiur/Misc/Global.cs +++ b/Esiur/Misc/Global.cs @@ -35,7 +35,7 @@ using System.Collections.Generic; using System.Text.RegularExpressions; using System.Net.NetworkInformation; using System.Linq; -using Esiur.Engine; +using Esiur.Core; using System.Diagnostics; using System.Runtime.InteropServices; diff --git a/Esiur/Net/DataLink/PacketFilter.cs b/Esiur/Net/DataLink/PacketFilter.cs index 9acb2b2..3c6da41 100644 --- a/Esiur/Net/DataLink/PacketFilter.cs +++ b/Esiur/Net/DataLink/PacketFilter.cs @@ -26,7 +26,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Text; -using Esiur.Engine; +using Esiur.Core; using Esiur.Data; using Esiur.Net.Packets; using Esiur.Resource; diff --git a/Esiur/Net/DataLink/PacketServer.cs b/Esiur/Net/DataLink/PacketServer.cs index 072bff5..8eac7da 100644 --- a/Esiur/Net/DataLink/PacketServer.cs +++ b/Esiur/Net/DataLink/PacketServer.cs @@ -26,7 +26,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Text; -using Esiur.Engine; +using Esiur.Core; using Esiur.Data; using System.Runtime.InteropServices; using Esiur.Net.Packets; diff --git a/Esiur/Net/DataLink/PacketSource.cs b/Esiur/Net/DataLink/PacketSource.cs index 30c3bd3..b2568a5 100644 --- a/Esiur/Net/DataLink/PacketSource.cs +++ b/Esiur/Net/DataLink/PacketSource.cs @@ -27,7 +27,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Text; -using Esiur.Engine; +using Esiur.Core; using Esiur.Resource; namespace Esiur.Net.DataLink diff --git a/Esiur/Net/HTTP/HTTPFilter.cs b/Esiur/Net/HTTP/HTTPFilter.cs index 64d6fa9..2c8df44 100644 --- a/Esiur/Net/HTTP/HTTPFilter.cs +++ b/Esiur/Net/HTTP/HTTPFilter.cs @@ -32,7 +32,7 @@ using System.Net; using System.Collections; using System.Collections.Generic; using Esiur.Data; -using Esiur.Engine; +using Esiur.Core; using Esiur.Resource; namespace Esiur.Net.HTTP diff --git a/Esiur/Net/HTTP/HTTPServer.cs b/Esiur/Net/HTTP/HTTPServer.cs index 53d3e4e..5b43499 100644 --- a/Esiur/Net/HTTP/HTTPServer.cs +++ b/Esiur/Net/HTTP/HTTPServer.cs @@ -34,7 +34,7 @@ using System.Collections.Generic; using Esiur.Net.Sockets; using Esiur.Data; using Esiur.Misc; -using Esiur.Engine; +using Esiur.Core; using Esiur.Net.Packets; using System.Security.Cryptography.X509Certificates; using Esiur.Resource; diff --git a/Esiur/Net/HTTP/HTTPSession.cs b/Esiur/Net/HTTP/HTTPSession.cs index 3b56eb8..c2ea05a 100644 --- a/Esiur/Net/HTTP/HTTPSession.cs +++ b/Esiur/Net/HTTP/HTTPSession.cs @@ -33,7 +33,7 @@ using System.Collections; using System.Collections.Generic; using Esiur.Data; using Esiur.Misc; -using Esiur.Engine; +using Esiur.Core; namespace Esiur.Net.HTTP { diff --git a/Esiur/Net/HTTP/IIPoWS.cs b/Esiur/Net/HTTP/IIPoWS.cs index 1957e7c..f9cd2cd 100644 --- a/Esiur/Net/HTTP/IIPoWS.cs +++ b/Esiur/Net/HTTP/IIPoWS.cs @@ -30,7 +30,7 @@ using System.Threading.Tasks; using Esiur.Resource; using Esiur.Net.IIP; using Esiur.Net.Sockets; -using Esiur.Engine; +using Esiur.Core; namespace Esiur.Net.HTTP { diff --git a/Esiur/Net/IIP/DistributedConnection.cs b/Esiur/Net/IIP/DistributedConnection.cs index 027d743..cd5a3dd 100644 --- a/Esiur/Net/IIP/DistributedConnection.cs +++ b/Esiur/Net/IIP/DistributedConnection.cs @@ -31,7 +31,7 @@ using System.Security.Cryptography; using Esiur.Net.Sockets; using Esiur.Data; using Esiur.Misc; -using Esiur.Engine; +using Esiur.Core; using Esiur.Net.Packets; using Esiur.Resource; using Esiur.Security.Authority; @@ -62,6 +62,8 @@ namespace Esiur.Net.IIP Session session; + AsyncReply openReply; + byte[] localPassword; byte[] localNonce, remoteNonce; @@ -109,8 +111,11 @@ namespace Esiur.Net.IIP /// Send data to the other end as parameters /// /// Values will be converted to bytes then sent. - internal void SendParams(params object[] values) + internal SendList SendParams(IAsyncReply reply = null)//params object[] values) { + return new SendList(this, reply); + + /* var data = BinaryList.ToBytes(values); if (ready) @@ -149,6 +154,7 @@ namespace Esiur.Net.IIP // Get calling method name //Console.WriteLine("TX " + hostType + " " + ar.Length + " " + stackTrace.GetFrame(1).GetMethod().ToString()); + */ } /// @@ -199,13 +205,28 @@ namespace Esiur.Net.IIP if (socket.State == SocketState.Established) { - SendParams((byte)0x60, (byte)dmn.Length, dmn, localNonce, (byte)un.Length, un); + SendParams() + .AddUInt8(0x60) + .AddUInt8((byte)dmn.Length) + .AddUInt8Array(dmn) + .AddUInt8Array(localNonce) + .AddUInt8((byte)un.Length) + .AddUInt8Array(un) + .Done();//, dmn, localNonce, (byte)un.Length, un); } else { socket.OnConnect += () => { // declare (Credentials -> No Auth, No Enctypt) - SendParams((byte)0x60, (byte)dmn.Length, dmn, localNonce, (byte)un.Length, un); + //SendParams((byte)0x60, (byte)dmn.Length, dmn, localNonce, (byte)un.Length, un); + SendParams() + .AddUInt8(0x60) + .AddUInt8((byte)dmn.Length) + .AddUInt8Array(dmn) + .AddUInt8Array(localNonce) + .AddUInt8((byte)un.Length) + .AddUInt8Array(un) + .Done(); }; } } @@ -569,12 +590,17 @@ namespace Esiur.Net.IIP session.RemoteAuthentication.Username = authPacket.RemoteUsername; remoteNonce = authPacket.RemoteNonce; session.RemoteAuthentication.Domain = authPacket.Domain; - SendParams((byte)0xa0, localNonce); + SendParams() + .AddUInt8(0xa0) + .AddUInt8Array(localNonce) + .Done(); + //SendParams((byte)0xa0, localNonce); } else { //Console.WriteLine("User not found"); - SendParams((byte)0xc0, (byte)1, (ushort)14, DC.ToBytes("User not found")); + //SendParams((byte)0xc0, (byte)1, (ushort)14, DC.ToBytes("User not found")); + SendParams().AddUInt8(0xc0).AddUInt8(1).AddUInt16(14).AddString("User not found").Done(); } }); @@ -592,21 +618,29 @@ namespace Esiur.Net.IIP if (pw != null) { var hashFunc = SHA256.Create(); - var hash = hashFunc.ComputeHash(BinaryList.ToBytes(pw, remoteNonce, localNonce)); + //var hash = hashFunc.ComputeHash(BinaryList.ToBytes(pw, remoteNonce, localNonce)); + var hash = hashFunc.ComputeHash((new BinaryList()) + .AddUInt8Array(pw) + .AddUInt8Array( remoteNonce) + .AddUInt8Array(localNonce) + .ToArray()); if (hash.SequenceEqual(remoteHash)) { - // send our hash - var localHash = hashFunc.ComputeHash(BinaryList.ToBytes(localNonce, remoteNonce, pw)); + // send our hash + //var localHash = hashFunc.ComputeHash(BinaryList.ToBytes(localNonce, remoteNonce, pw)); + //SendParams((byte)0, localHash); - SendParams((byte)0, localHash); + var localHash = hashFunc.ComputeHash((new BinaryList()).AddUInt8Array(localNonce).AddUInt8Array(remoteNonce).AddUInt8Array(pw).ToArray()); + SendParams().AddUInt8(0).AddUInt8Array(localHash).Done(); readyToEstablish = true; } else { - Global.Log("auth", LogType.Warning, "U:" + RemoteUsername + " IP:" + Socket.RemoteEndPoint.Address.ToString() + " S:DENIED"); + //Global.Log("auth", LogType.Warning, "U:" + RemoteUsername + " IP:" + Socket.RemoteEndPoint.Address.ToString() + " S:DENIED"); //Console.WriteLine("Incorrect password"); - SendParams((byte)0xc0, (byte)1, (ushort)5, DC.ToBytes("Error")); + //SendParams((byte)0xc0, (byte)1, (ushort)5, DC.ToBytes("Error")); + SendParams().AddUInt8(0xc0).AddUInt8(1).AddUInt16(5).AddString("Error").Done(); } } }); @@ -618,12 +652,18 @@ namespace Esiur.Net.IIP var r = new Random(); session.Id = new byte[32]; r.NextBytes(session.Id); - SendParams((byte)0x28, session.Id); + //SendParams((byte)0x28, session.Id); + SendParams() + .AddUInt8(0x28) + .AddUInt8Array(session.Id) + .Done(); + ready = true; + openReply?.Trigger(true); OnReady?.Invoke(this); Server.Membership.Login(session); - Global.Log("auth", LogType.Warning, "U:" + RemoteUsername + " IP:" + Socket.RemoteEndPoint.Address.ToString() + " S:AUTH"); + //Global.Log("auth", LogType.Warning, "U:" + RemoteUsername + " IP:" + Socket.RemoteEndPoint.Address.ToString() + " S:AUTH"); } } @@ -637,9 +677,19 @@ namespace Esiur.Net.IIP // send our hash var hashFunc = SHA256.Create(); - var localHash = hashFunc.ComputeHash(BinaryList.ToBytes(localPassword, localNonce, remoteNonce)); + //var localHash = hashFunc.ComputeHash(BinaryList.ToBytes(localPassword, localNonce, remoteNonce)); + var localHash = hashFunc.ComputeHash(new BinaryList() + .AddUInt8Array(localPassword) + .AddUInt8Array(localNonce) + .AddUInt8Array(remoteNonce) + .ToArray()); - SendParams((byte)0, localHash); + SendParams() + .AddUInt8(0) + .AddUInt8Array(localHash) + .Done(); + + //SendParams((byte)0, localHash); } else if (authPacket.Command == IIPAuthPacket.IIPAuthPacketCommand.Action) { @@ -647,16 +697,33 @@ namespace Esiur.Net.IIP { // check if the server knows my password var hashFunc = SHA256.Create(); - var remoteHash = hashFunc.ComputeHash(BinaryList.ToBytes(remoteNonce, localNonce, localPassword)); + //var remoteHash = hashFunc.ComputeHash(BinaryList.ToBytes(remoteNonce, localNonce, localPassword)); + var remoteHash = hashFunc.ComputeHash(new BinaryList() + .AddUInt8Array(remoteNonce) + .AddUInt8Array(localNonce) + .AddUInt8Array(localPassword) + .ToArray()); + if (remoteHash.SequenceEqual(authPacket.Hash)) { // send establish request - SendParams((byte)0x20, (ushort)0); + //SendParams((byte)0x20, (ushort)0); + SendParams() + .AddUInt8(0x20) + .AddUInt16(0) + .Done(); } else { - SendParams((byte)0xc0, 1, (ushort)5, DC.ToBytes("Error")); + SendParams() + .AddUInt8(0xc0) + .AddUInt8(1) + .AddUInt16(5) + .AddString("Error") + .Done(); + + //SendParams((byte)0xc0, 1, (ushort)5, DC.ToBytes("Error")); } } else if (authPacket.Action == IIPAuthPacket.IIPAuthPacketAction.ConnectionEstablished) @@ -664,12 +731,14 @@ namespace Esiur.Net.IIP session.Id = authPacket.SessionId; ready = true; + openReply?.Trigger(true); OnReady?.Invoke(this); } } else if (authPacket.Command == IIPAuthPacket.IIPAuthPacketCommand.Error) { + openReply?.TriggerError(new AsyncException(ErrorType.Management, authPacket.ErrorCode, authPacket.ErrorMessage)); OnError?.Invoke(this, authPacket.ErrorCode, authPacket.ErrorMessage); Close(); } @@ -710,6 +779,40 @@ namespace Esiur.Net.IIP /// public AsyncReply Trigger(ResourceTrigger trigger) { + if (trigger == ResourceTrigger.Open) + { + if (Instance.Attributes.ContainsKey("username") + && Instance.Attributes.ContainsKey("password")) + { + var hostname = String.Join("://", Instance.Name.Split(new string[] { "://" }, StringSplitOptions.None).Skip(1)).Split('/')[0]; + // assign domain from hostname if not provided + + var address = hostname.Split(':')[0]; + var port = ushort.Parse(hostname.Split(':')[1]); + var username = Instance.Attributes["username"].ToString(); + + var domain = Instance.Attributes.ContainsKey("domain") ? Instance.Attributes["domain"].ToString() : address; + + session = new Session(new ClientAuthentication() + , new HostAuthentication()); + + session.LocalAuthentication.Domain = domain; + session.LocalAuthentication.Username = username; + localPassword = DC.ToBytes(Instance.Attributes["password"].ToString()); + + openReply = new AsyncReply(); + var sock = new TCPSocket(); + + + sock.Connect(domain, port).Then((x)=> { + Assign(sock); + //rt.trigger(true); + }).Error((x) => openReply.TriggerError(x)); + + return openReply; + } + } + return new AsyncReply(); } @@ -720,7 +823,9 @@ namespace Esiur.Net.IIP /// public bool Put(IResource resource) { - resources.Add(Convert.ToUInt32(resource.Instance.Name), (DistributedResource)resource); + if (Codec.IsLocalResource(resource, this)) + resources.Add((resource as DistributedResource).Id, (DistributedResource)resource); + // else ... send it to the peer return true; } diff --git a/Esiur/Net/IIP/DistributedConnectionProtocol.cs b/Esiur/Net/IIP/DistributedConnectionProtocol.cs index 43861c5..a58401c 100644 --- a/Esiur/Net/IIP/DistributedConnectionProtocol.cs +++ b/Esiur/Net/IIP/DistributedConnectionProtocol.cs @@ -23,7 +23,7 @@ SOFTWARE. */ using Esiur.Data; -using Esiur.Engine; +using Esiur.Core; using Esiur.Net.Packets; using Esiur.Resource; using Esiur.Resource.Template; @@ -52,7 +52,7 @@ namespace Esiur.Net.IIP KeyList> requests = new KeyList>(); - uint callbackCounter = 0; + volatile uint callbackCounter = 0; AsyncQueue queue = new AsyncQueue(); @@ -62,6 +62,16 @@ namespace Esiur.Net.IIP /// Packet action. /// Arguments to send. /// + internal SendList SendRequest(IIPPacket.IIPPacketAction action) + { + var reply = new AsyncReply(); + var c = callbackCounter++; // avoid thread racing + requests.Add(c, reply); + + return (SendList)SendParams(reply).AddUInt8((byte)(0x40 | (byte)action)).AddUInt32(c); + } + + /* internal IAsyncReply SendRequest(IIPPacket.IIPPacketAction action, params object[] args) { var reply = new AsyncReply(); @@ -72,11 +82,13 @@ namespace Esiur.Net.IIP requests.Add(callbackCounter, reply); return reply; } + */ - uint maxcallerid = 0; + //uint maxcallerid = 0; - internal void SendReply(IIPPacket.IIPPacketAction action, uint callbackId, params object[] args) + internal SendList SendReply(IIPPacket.IIPPacketAction action, uint callbackId) { + /* if (callbackId > maxcallerid) { maxcallerid = callbackId; @@ -86,18 +98,18 @@ namespace Esiur.Net.IIP Console.Beep(); } + */ - var bl = new BinaryList((byte)(0x80 | (byte)action), callbackId); - bl.AddRange(args); - Send(bl.ToArray()); - //Console.WriteLine("SendReply " + action.ToString() + " " + callbackId); + return (SendList)SendParams().AddUInt8((byte)(0x80 | (byte)action)).AddUInt32(callbackId); } - internal void SendEvent(IIPPacket.IIPPacketEvent evt, params object[] args) + internal SendList SendEvent(IIPPacket.IIPPacketEvent evt) { - var bl = new BinaryList((byte)(evt)); - bl.AddRange(args); - Send(bl.ToArray()); + //var bl = new BinaryList((byte)(evt)); + //bl.AddRange(args); + //Send(bl.ToArray()); + + return (SendList)SendParams().AddUInt8((byte)(evt)); } internal AsyncReply SendInvokeByArrayArguments(uint instanceId, byte index, object[] parameters) @@ -105,11 +117,19 @@ namespace Esiur.Net.IIP var pb = Codec.ComposeVarArray(parameters, this, true); var reply = new AsyncReply(); - callbackCounter++; - var bl = new BinaryList((byte)(0x40 | (byte)Packets.IIPPacket.IIPPacketAction.InvokeFunctionArrayArguments), - callbackCounter, instanceId, index, pb); - Send(bl.ToArray()); - requests.Add(callbackCounter, reply); + var c = callbackCounter++; + requests.Add(c, reply); + + SendParams().AddUInt8((byte)(0x40 | (byte)Packets.IIPPacket.IIPPacketAction.InvokeFunctionArrayArguments)) + .AddUInt32(c) + .AddUInt32(instanceId) + .AddUInt8(index) + .AddUInt8Array(pb) + .Done(); + + //var bl = new BinaryList((byte)(0x40 | (byte)Packets.IIPPacket.IIPPacketAction.InvokeFunctionArrayArguments), + // callbackCounter, instanceId, index, pb); + //Send(bl.ToArray()); return reply; } @@ -118,6 +138,7 @@ namespace Esiur.Net.IIP { var pb = Codec.ComposeStructure(parameters, this, true, true, true); + /* var reply = new AsyncReply(); callbackCounter++; var bl = new BinaryList((byte)(0x40 | (byte)Packets.IIPPacket.IIPPacketAction.InvokeFunctionNamedArguments), @@ -126,6 +147,19 @@ namespace Esiur.Net.IIP requests.Add(callbackCounter, reply); return reply; + */ + + var reply = new AsyncReply(); + var c = callbackCounter++; + requests.Add(c, reply); + + SendParams().AddUInt8((byte)(0x40 | (byte)Packets.IIPPacket.IIPPacketAction.InvokeFunctionNamedArguments)) + .AddUInt32(c) + .AddUInt32(instanceId) + .AddUInt8(index) + .AddUInt8Array(pb) + .Done(); + return reply; } @@ -133,20 +167,40 @@ namespace Esiur.Net.IIP { var msg = DC.ToBytes(errorMessage); if (type == ErrorType.Management) - SendParams((byte)(0xC0 | (byte)IIPPacket.IIPPacketReport.ManagementError), callbackId, errorCode); + SendParams() + .AddUInt8((byte)(0xC0 | (byte)IIPPacket.IIPPacketReport.ManagementError)) + .AddUInt32(callbackId) + .AddUInt16(errorCode) + .Done(); else if (type == ErrorType.Exception) - SendParams((byte)(0xC0 | (byte)IIPPacket.IIPPacketReport.ExecutionError), callbackId, errorCode, (ushort)msg.Length, msg); + SendParams() + .AddUInt8((byte)(0xC0 | (byte)IIPPacket.IIPPacketReport.ExecutionError)) + .AddUInt32(callbackId) + .AddUInt16(errorCode) + .AddUInt16((ushort)msg.Length) + .AddUInt8Array(msg) + .Done(); } void SendProgress(uint callbackId, int value, int max) { - SendParams((byte)(0xC0 | (byte)IIPPacket.IIPPacketReport.ProgressReport), callbackId, value, max); + SendParams() + .AddUInt8((byte)(0xC0 | (byte)IIPPacket.IIPPacketReport.ProgressReport)) + .AddUInt32(callbackId) + .AddInt32(value) + .AddInt32(max) + .Done(); + //SendParams(, callbackId, value, max); } void SendChunk(uint callbackId, object chunk) { var c = Codec.Compose(chunk, this, true); - SendParams((byte)(0xC0 | (byte)IIPPacket.IIPPacketReport.ChunkStream), callbackId, c); + SendParams() + .AddUInt8((byte)(0xC0 | (byte)IIPPacket.IIPPacketReport.ChunkStream)) + .AddUInt32(callbackId) + .AddUInt8Array(c) + .Done(); } void IIPReply(uint callbackId, params object[] results) @@ -214,7 +268,7 @@ namespace Esiur.Net.IIP Codec.Parse(content, 0, this).Then((arguments) => { - var pt = r.Instance.Template.GetPropertyTemplate(index); + var pt = r.Instance.Template.GetPropertyTemplateByIndex(index); if (pt != null) { item.Trigger(new DistributedResourceQueueItem((DistributedResource)r, @@ -277,7 +331,7 @@ namespace Esiur.Net.IIP Codec.ParseVarArray(content, this).Then((arguments) => { - var et = r.Instance.Template.GetEventTemplate(index); + var et = r.Instance.Template.GetEventTemplateByIndex(index); if (et != null) { item.Trigger(new DistributedResourceQueueItem((DistributedResource)r, @@ -384,10 +438,19 @@ namespace Esiur.Net.IIP } var r = res as IResource; + + // unsubscribe + r.Instance.ResourceEventOccurred -= Instance_EventOccurred; + r.Instance.ResourceModified -= Instance_PropertyModified; + r.Instance.ResourceDestroyed -= Instance_ResourceDestroyed; + r.Instance.Children.OnAdd -= Children_OnAdd; + r.Instance.Children.OnRemoved -= Children_OnRemoved; + r.Instance.Attributes.OnModified -= Attributes_OnModified; + + // subscribe r.Instance.ResourceEventOccurred += Instance_EventOccurred; r.Instance.ResourceModified += Instance_PropertyModified; r.Instance.ResourceDestroyed += Instance_ResourceDestroyed; - r.Instance.Children.OnAdd += Children_OnAdd; r.Instance.Children.OnRemoved += Children_OnRemoved; r.Instance.Attributes.OnModified += Attributes_OnModified; @@ -397,18 +460,24 @@ namespace Esiur.Net.IIP if (r is DistributedResource) { // reply ok - SendReply(IIPPacket.IIPPacketAction.AttachResource, - callback, r.Instance.Template.ClassId, - r.Instance.Age, (ushort)link.Length, link, - Codec.ComposePropertyValueArray((r as DistributedResource)._Serialize(), this, true)); + SendReply(IIPPacket.IIPPacketAction.AttachResource, callback) + .AddGuid(r.Instance.Template.ClassId) + .AddUInt64(r.Instance.Age) + .AddUInt16((ushort)link.Length) + .AddUInt8Array(link) + .AddUInt8Array(Codec.ComposePropertyValueArray((r as DistributedResource)._Serialize(), this, true)) + .Done(); } else { // reply ok - SendReply(IIPPacket.IIPPacketAction.AttachResource, - callback, r.Instance.Template.ClassId, - r.Instance.Age, (ushort)link.Length, link, - Codec.ComposePropertyValueArray(r.Instance.Serialize(), this, true)); + SendReply(IIPPacket.IIPPacketAction.AttachResource, callback) + .AddGuid(r.Instance.Template.ClassId) + .AddUInt64(r.Instance.Age) + .AddUInt16((ushort)link.Length) + .AddUInt8Array(link) + .AddUInt8Array(Codec.ComposePropertyValueArray(r.Instance.Serialize(), this, true)) + .Done(); } } else @@ -426,19 +495,29 @@ namespace Esiur.Net.IIP { var instance = (sender.Owner as Instance); var name = DC.ToBytes(newValue.ToString()); - SendEvent(IIPPacket.IIPPacketEvent.ChildRemoved, instance.Id, (ushort)name.Length, name); + SendEvent(IIPPacket.IIPPacketEvent.ChildRemoved) + .AddUInt32(instance.Id) + .AddUInt16((ushort)name.Length) + .AddUInt8Array(name) + .Done(); } } private void Children_OnRemoved(Instance sender, IResource value) { - SendEvent(IIPPacket.IIPPacketEvent.ChildRemoved, sender.Id, value.Instance.Id); + SendEvent(IIPPacket.IIPPacketEvent.ChildRemoved) + .AddUInt32(sender.Id) + .AddUInt32(value.Instance.Id) + .Done(); } private void Children_OnAdd(Instance sender, IResource value) { //if (sender.Applicable(sender.Resource, this.session, ActionType.)) - SendEvent(IIPPacket.IIPPacketEvent.ChildAdded, sender.Id, value.Instance.Id); + SendEvent(IIPPacket.IIPPacketEvent.ChildAdded) + .AddUInt32(sender.Id) + .AddUInt32(value.Instance.Id) + .Done(); } void IIPRequestReattachResource(uint callback, uint resourceId, ulong resourceAge) @@ -448,13 +527,27 @@ namespace Esiur.Net.IIP if (res != null) { var r = res as IResource; + // unsubscribe + r.Instance.ResourceEventOccurred -= Instance_EventOccurred; + r.Instance.ResourceModified -= Instance_PropertyModified; + r.Instance.ResourceDestroyed -= Instance_ResourceDestroyed; + r.Instance.Children.OnAdd -= Children_OnAdd; + r.Instance.Children.OnRemoved -= Children_OnRemoved; + r.Instance.Attributes.OnModified -= Attributes_OnModified; + + // subscribe r.Instance.ResourceEventOccurred += Instance_EventOccurred; r.Instance.ResourceModified += Instance_PropertyModified; r.Instance.ResourceDestroyed += Instance_ResourceDestroyed; + r.Instance.Children.OnAdd += Children_OnAdd; + r.Instance.Children.OnRemoved += Children_OnRemoved; + r.Instance.Attributes.OnModified += Attributes_OnModified; + // reply ok - SendReply(IIPPacket.IIPPacketAction.ReattachResource, - callback, r.Instance.Age, - Codec.ComposePropertyValueArray(r.Instance.Serialize(), this, true)); + SendReply(IIPPacket.IIPPacketAction.ReattachResource, callback) + .AddUInt64(r.Instance.Age) + .AddUInt8Array(Codec.ComposePropertyValueArray(r.Instance.Serialize(), this, true)) + .Done(); } else { @@ -475,7 +568,7 @@ namespace Esiur.Net.IIP r.Instance.ResourceModified -= Instance_PropertyModified; r.Instance.ResourceDestroyed -= Instance_ResourceDestroyed; // reply ok - SendReply(IIPPacket.IIPPacketAction.DetachResource, callback); + SendReply(IIPPacket.IIPPacketAction.DetachResource, callback).Done(); } else { @@ -599,7 +692,9 @@ namespace Esiur.Net.IIP Warehouse.Put(resource, name, store as IStore, parent); - SendReply(IIPPacket.IIPPacketAction.CreateResource, callback, resource.Instance.Id); + SendReply(IIPPacket.IIPPacketAction.CreateResource, callback) + .AddUInt32(resource.Instance.Id) + .Done(); }); }); @@ -625,7 +720,7 @@ namespace Esiur.Net.IIP } if (Warehouse.Remove(r)) - SendReply(IIPPacket.IIPPacketAction.DeleteResource, callback); + SendReply(IIPPacket.IIPPacketAction.DeleteResource, callback).Done(); //SendParams((byte)0x84, callback); else SendError(ErrorType.Management, callback, (ushort)ExceptionCode.DeleteFailed); @@ -657,8 +752,9 @@ namespace Esiur.Net.IIP var st = r.Instance.GetAttributes(attrs); if (st != null) - SendReply(all ? IIPPacket.IIPPacketAction.GetAllAttributes : IIPPacket.IIPPacketAction.GetAttributes, callback, - Codec.ComposeStructure(st, this, true, true, true)); + SendReply(all ? IIPPacket.IIPPacketAction.GetAllAttributes : IIPPacket.IIPPacketAction.GetAttributes, callback) + .AddUInt8Array(Codec.ComposeStructure(st, this, true, true, true)) + .Done(); else SendError(ErrorType.Management, callback, (ushort)ExceptionCode.GetAttributesFailed); @@ -697,7 +793,7 @@ namespace Esiur.Net.IIP parent.Instance.Children.Add(child); - SendReply(IIPPacket.IIPPacketAction.AddChild, callback); + SendReply(IIPPacket.IIPPacketAction.AddChild, callback).Done(); //child.Instance.Parents }); @@ -736,7 +832,7 @@ namespace Esiur.Net.IIP parent.Instance.Children.Remove(child); - SendReply(IIPPacket.IIPPacketAction.RemoveChild, callback); + SendReply(IIPPacket.IIPPacketAction.RemoveChild, callback).Done(); //child.Instance.Parents }); @@ -761,7 +857,7 @@ namespace Esiur.Net.IIP resource.Instance.Name = name.GetString(0, (uint)name.Length); - SendReply(IIPPacket.IIPPacketAction.RenameResource, callback); + SendReply(IIPPacket.IIPPacketAction.RenameResource, callback).Done(); }); } @@ -775,10 +871,9 @@ namespace Esiur.Net.IIP return; } - SendReply(IIPPacket.IIPPacketAction.ResourceChildren, callback, - - Codec.ComposeResourceArray(resource.Instance.Children.ToArray(), this, true) - ); + SendReply(IIPPacket.IIPPacketAction.ResourceChildren, callback) + .AddUInt8Array(Codec.ComposeResourceArray(resource.Instance.Children.ToArray(), this, true)) + .Done(); }); } @@ -793,11 +888,9 @@ namespace Esiur.Net.IIP return; } - SendReply(IIPPacket.IIPPacketAction.ResourceParents, callback, - - Codec.ComposeResourceArray(resource.Instance.Parents.ToArray(), this, true) - ); - + SendReply(IIPPacket.IIPPacketAction.ResourceParents, callback) + .AddUInt8Array(Codec.ComposeResourceArray(resource.Instance.Parents.ToArray(), this, true)) + .Done(); }); } @@ -823,7 +916,7 @@ namespace Esiur.Net.IIP attrs = attributes.GetStringArray(0, (uint)attributes.Length); if (r.Instance.RemoveAttributes(attrs)) - SendReply(all ? IIPPacket.IIPPacketAction.ClearAllAttributes : IIPPacket.IIPPacketAction.ClearAttributes, callback); + SendReply(all ? IIPPacket.IIPPacketAction.ClearAllAttributes : IIPPacket.IIPPacketAction.ClearAttributes, callback).Done(); else SendError(ErrorType.Management, callback, (ushort)ExceptionCode.UpdateAttributeFailed); @@ -850,7 +943,7 @@ namespace Esiur.Net.IIP { if (r.Instance.SetAttributes(attrs, clearAttributes)) SendReply(clearAttributes ? IIPPacket.IIPPacketAction.ClearAllAttributes : IIPPacket.IIPPacketAction.ClearAttributes, - callback); + callback).Done(); else SendError(ErrorType.Management, callback, (ushort)ExceptionCode.UpdateAttributeFailed); }); @@ -864,7 +957,10 @@ namespace Esiur.Net.IIP Warehouse.GetTemplate(className).Then((t) => { if (t != null) - SendReply(IIPPacket.IIPPacketAction.TemplateFromClassName, callback, (uint)t.Content.Length, t.Content); + SendReply(IIPPacket.IIPPacketAction.TemplateFromClassName, callback) + .AddInt32(t.Content.Length) + .AddUInt8Array(t.Content) + .Done(); else { // reply failed @@ -878,7 +974,10 @@ namespace Esiur.Net.IIP Warehouse.GetTemplate(classId).Then((t) => { if (t != null) - SendReply(IIPPacket.IIPPacketAction.TemplateFromClassId, callback, (uint)t.Content.Length, t.Content); + SendReply(IIPPacket.IIPPacketAction.TemplateFromClassId, callback) + .AddInt32(t.Content.Length) + .AddUInt8Array(t.Content) + .Done(); else { // reply failed @@ -894,8 +993,10 @@ namespace Esiur.Net.IIP Warehouse.Get(resourceId).Then((r) => { if (r != null) - SendReply(IIPPacket.IIPPacketAction.TemplateFromResourceId, callback, - (uint)r.Instance.Template.Content.Length, r.Instance.Template.Content); + SendReply(IIPPacket.IIPPacketAction.TemplateFromResourceId, callback) + .AddInt32(r.Instance.Template.Content.Length) + .AddUInt8Array(r.Instance.Template.Content) + .Done(); else { // reply failed @@ -918,7 +1019,9 @@ namespace Esiur.Net.IIP if (list.Length == 0) SendError(ErrorType.Management, callback, (ushort)ExceptionCode.ResourceNotFound); else - SendReply(IIPPacket.IIPPacketAction.QueryLink, callback, Codec.ComposeResourceArray(list, this, true)); + SendReply(IIPPacket.IIPPacketAction.QueryLink, callback) + .AddUInt8Array(Codec.ComposeResourceArray(list, this, true)) + .Done(); //} //else //{ @@ -942,7 +1045,7 @@ namespace Esiur.Net.IIP { Codec.ParseVarArray(content, this).Then((arguments) => { - var ft = r.Instance.Template.GetFunctionTemplate(index); + var ft = r.Instance.Template.GetFunctionTemplateByIndex(index); if (ft != null) { if (r is DistributedResource) @@ -952,7 +1055,9 @@ namespace Esiur.Net.IIP { rt.Then(res => { - SendReply(IIPPacket.IIPPacketAction.InvokeFunctionArrayArguments, callback, Codec.Compose(res, this)); + SendReply(IIPPacket.IIPPacketAction.InvokeFunctionArrayArguments, callback) + .AddUInt8Array(Codec.Compose(res, this)) + .Done(); }); } else @@ -1007,7 +1112,7 @@ namespace Esiur.Net.IIP { rt = fi.Invoke(r, args); } - catch(Exception ex) + catch (Exception ex) { SendError(ErrorType.Exception, callback, 0, ex.ToString()); return; @@ -1020,7 +1125,9 @@ namespace Esiur.Net.IIP foreach (var v in enu) SendChunk(callback, v); - SendReply(IIPPacket.IIPPacketAction.InvokeFunctionArrayArguments, callback, (byte)DataType.Void); + SendReply(IIPPacket.IIPPacketAction.InvokeFunctionArrayArguments, callback) + .AddUInt8((byte)DataType.Void) + .Done(); } else if (rt is Task) @@ -1032,18 +1139,22 @@ namespace Esiur.Net.IIP #else var res = t.GetType().GetProperty("Result").GetValue(t); #endif - SendReply(IIPPacket.IIPPacketAction.InvokeFunctionArrayArguments, callback, Codec.Compose(res, this)); + SendReply(IIPPacket.IIPPacketAction.InvokeFunctionArrayArguments, callback) + .AddUInt8Array(Codec.Compose(res, this)) + .Done(); }); //await t; //SendParams((byte)0x90, callback, Codec.Compose(res, this)); } - else if (rt.GetType().GetTypeInfo().IsGenericType - && rt.GetType().GetGenericTypeDefinition() == typeof(IAsyncReply<>)) + else if (rt is IAsyncReply)// Codec.ImplementsInterface(rt.GetType(), typeof(IAsyncReply<>)))// rt.GetType().GetTypeInfo().IsGenericType + //&& rt.GetType().GetGenericTypeDefinition() == typeof(IAsyncReply<>)) { (rt as IAsyncReply).Then(res => { - SendReply(IIPPacket.IIPPacketAction.InvokeFunctionArrayArguments, callback, Codec.Compose(res, this)); + SendReply(IIPPacket.IIPPacketAction.InvokeFunctionArrayArguments, callback) + .AddUInt8Array(Codec.Compose(res, this)) + .Done(); }).Error(ex => { SendError(ErrorType.Exception, callback, (ushort)ex.Code, ex.Message); @@ -1057,7 +1168,9 @@ namespace Esiur.Net.IIP } else { - SendReply(IIPPacket.IIPPacketAction.InvokeFunctionArrayArguments, callback, Codec.Compose(rt, this)); + SendReply(IIPPacket.IIPPacketAction.InvokeFunctionArrayArguments, callback) + .AddUInt8Array(Codec.Compose(rt, this)) + .Done(); } } else @@ -1087,29 +1200,31 @@ namespace Esiur.Net.IIP { if (r != null) { - Codec.ParseStructure(content, 0, (uint)content.Length, this).Then((namedArgs) => - { - var ft = r.Instance.Template.GetFunctionTemplate(index); - if (ft != null) - { - if (r is DistributedResource) - { - var rt = (r as DistributedResource)._InvokeByNamedArguments(index, namedArgs); - if (rt != null) - { - rt.Then(res => - { - SendReply(IIPPacket.IIPPacketAction.InvokeFunctionNamedArguments, callback, Codec.Compose(res, this)); - }); - } - else - { + Codec.ParseStructure(content, 0, (uint)content.Length, this).Then((namedArgs) => + { + var ft = r.Instance.Template.GetFunctionTemplateByIndex(index); + if (ft != null) + { + if (r is DistributedResource) + { + var rt = (r as DistributedResource)._InvokeByNamedArguments(index, namedArgs); + if (rt != null) + { + rt.Then(res => + { + SendReply(IIPPacket.IIPPacketAction.InvokeFunctionNamedArguments, callback) + .AddUInt8Array(Codec.Compose(res, this)) + .Done(); + }); + } + else + { // function not found on a distributed object } - } - else - { + } + else + { #if NETSTANDARD1_5 var fi = r.GetType().GetTypeInfo().GetMethod(ft.Name); #else @@ -1117,101 +1232,107 @@ namespace Esiur.Net.IIP #endif if (fi != null) - { - if (r.Instance.Applicable(session, ActionType.Execute, ft) == Ruling.Denied) - { - SendError(ErrorType.Management, callback, - (ushort)ExceptionCode.InvokeDenied); - return; - } + { + if (r.Instance.Applicable(session, ActionType.Execute, ft) == Ruling.Denied) + { + SendError(ErrorType.Management, callback, + (ushort)ExceptionCode.InvokeDenied); + return; + } // cast arguments ParameterInfo[] pi = fi.GetParameters(); - object[] args = new object[pi.Length]; + object[] args = new object[pi.Length]; - for (var i = 0; i < pi.Length; i++) - { - if (pi[i].ParameterType == typeof(DistributedConnection)) - { - args[i] = this; - } - else if (namedArgs.ContainsKey(pi[i].Name)) - { - args[i] = DC.CastConvert(namedArgs[pi[i].Name], pi[i].ParameterType); - } - } + for (var i = 0; i < pi.Length; i++) + { + if (pi[i].ParameterType == typeof(DistributedConnection)) + { + args[i] = this; + } + else if (namedArgs.ContainsKey(pi[i].Name)) + { + args[i] = DC.CastConvert(namedArgs[pi[i].Name], pi[i].ParameterType); + } + } - - object rt; - try - { - rt = fi.Invoke(r, args); - } - catch (Exception ex) - { - SendError(ErrorType.Exception, callback, 0, ex.ToString()); - return; - } + object rt; - if (rt is System.Collections.IEnumerable && !(rt is Array)) - { - var enu = rt as System.Collections.IEnumerable; + try + { + rt = fi.Invoke(r, args); + } + catch (Exception ex) + { + SendError(ErrorType.Exception, callback, 0, ex.ToString()); + return; + } - foreach (var v in enu) - SendChunk(callback, v); + if (rt is System.Collections.IEnumerable && !(rt is Array)) + { + var enu = rt as System.Collections.IEnumerable; - SendReply(IIPPacket.IIPPacketAction.InvokeFunctionNamedArguments, callback, (byte)DataType.Void); + foreach (var v in enu) + SendChunk(callback, v); - } - else if (rt is Task) - { - (rt as Task).ContinueWith(t => - { + SendReply(IIPPacket.IIPPacketAction.InvokeFunctionNamedArguments, callback) + .AddUInt8((byte)DataType.Void) + .Done(); + } + else if (rt is Task) + { + (rt as Task).ContinueWith(t => + { #if NETSTANDARD1_5 var res = t.GetType().GetTypeInfo().GetProperty("Result").GetValue(t); #else var res = t.GetType().GetProperty("Result").GetValue(t); #endif - SendReply(IIPPacket.IIPPacketAction.InvokeFunctionNamedArguments, callback, Codec.Compose(res, this)); - }); + SendReply(IIPPacket.IIPPacketAction.InvokeFunctionNamedArguments, callback) + .AddUInt8Array(Codec.Compose(res, this)) + .Done(); + }); - } -// else if (rt is AsyncReply) - else if (rt.GetType().GetTypeInfo().IsGenericType - && rt.GetType().GetGenericTypeDefinition() == typeof(IAsyncReply<>)) - { - (rt as IAsyncReply).Then(res => - { - SendReply(IIPPacket.IIPPacketAction.InvokeFunctionNamedArguments, callback, Codec.Compose(res, this)); - }).Error(ex => - { - SendError(ErrorType.Exception, callback, (ushort)ex.Code, ex.Message); - }).Progress((pt, pv, pm) => - { - SendProgress(callback, pv, pm); - }).Chunk(v => - { - SendChunk(callback, v); - }); - } - else - { - SendReply(IIPPacket.IIPPacketAction.InvokeFunctionNamedArguments, callback, Codec.Compose(rt, this)); - } - } - else + } + else if (rt is IAsyncReply) + { + (rt as IAsyncReply).Then(res => { + SendReply(IIPPacket.IIPPacketAction.InvokeFunctionNamedArguments, callback) + .AddUInt8Array(Codec.Compose(res, this)) + .Done(); + + }).Error(ex => + { + SendError(ErrorType.Exception, callback, (ushort)ex.Code, ex.Message); + }).Progress((pt, pv, pm) => + { + SendProgress(callback, pv, pm); + }).Chunk(v => + { + SendChunk(callback, v); + }); + } + else + { + SendReply(IIPPacket.IIPPacketAction.InvokeFunctionNamedArguments, callback) + .AddUInt8Array(Codec.Compose(rt, this)) + .Done(); + } + } + else + { // ft found, fi not found, this should never happen } - } - } - else - { + } + } + else + { // no function at this index } - }); + }); } else { @@ -1226,13 +1347,14 @@ namespace Esiur.Net.IIP { if (r != null) { - var pt = r.Instance.Template.GetFunctionTemplate(index); + var pt = r.Instance.Template.GetFunctionTemplateByIndex(index); if (pt != null) { if (r is DistributedResource) { - SendReply(IIPPacket.IIPPacketAction.GetProperty, callback, - Codec.Compose((r as DistributedResource)._Get(pt.Index), this)); + SendReply(IIPPacket.IIPPacketAction.GetProperty, callback) + .AddUInt8Array(Codec.Compose((r as DistributedResource)._Get(pt.Index), this)) + .Done(); } else { @@ -1244,8 +1366,9 @@ namespace Esiur.Net.IIP if (pi != null) { - SendReply(IIPPacket.IIPPacketAction.GetProperty, - callback, Codec.Compose(pi.GetValue(r), this)); + SendReply(IIPPacket.IIPPacketAction.GetProperty, callback) + .AddUInt8Array(Codec.Compose(pi.GetValue(r), this)) + .Done(); } else { @@ -1292,7 +1415,9 @@ namespace Esiur.Net.IIP }*/ - SendReply(IIPPacket.IIPPacketAction.ResourceHistory, callback, history); + SendReply(IIPPacket.IIPPacketAction.ResourceHistory, callback) + .AddUInt8Array(history) + .Done(); }); } @@ -1305,7 +1430,7 @@ namespace Esiur.Net.IIP { if (r != null) { - var pt = r.Instance.Template.GetFunctionTemplate(index); + var pt = r.Instance.Template.GetFunctionTemplateByIndex(index); if (pt != null) { if (r.Instance.GetAge(index) > age) @@ -1317,8 +1442,9 @@ namespace Esiur.Net.IIP #endif if (pi != null) { - SendReply(IIPPacket.IIPPacketAction.GetPropertyIfModified, callback, - Codec.Compose(pi.GetValue(r), this)); + SendReply(IIPPacket.IIPPacketAction.GetPropertyIfModified, callback) + .AddUInt8Array(Codec.Compose(pi.GetValue(r), this)) + .Done(); } else { @@ -1327,8 +1453,9 @@ namespace Esiur.Net.IIP } else { - SendReply(IIPPacket.IIPPacketAction.GetPropertyIfModified, callback, - (byte)DataType.NotModified); + SendReply(IIPPacket.IIPPacketAction.GetPropertyIfModified, callback) + .AddUInt8((byte)DataType.NotModified) + .Done(); } } else @@ -1351,7 +1478,7 @@ namespace Esiur.Net.IIP { - var pt = r.Instance.Template.GetPropertyTemplate(index); + var pt = r.Instance.Template.GetPropertyTemplateByIndex(index); if (pt != null) { Codec.Parse(content, 0, this).Then((value) => @@ -1361,7 +1488,7 @@ namespace Esiur.Net.IIP // propagation (r as DistributedResource)._Set(index, value).Then((x) => { - SendReply(IIPPacket.IIPPacketAction.SetProperty, callback); + SendReply(IIPPacket.IIPPacketAction.SetProperty, callback).Done(); }).Error(x => { SendError(x.Type, callback, (ushort)x.Code, x.Message); @@ -1378,7 +1505,7 @@ namespace Esiur.Net.IIP #endif*/ var pi = pt.Info; - + if (pi != null) { @@ -1405,17 +1532,17 @@ namespace Esiur.Net.IIP value = DC.CastConvert(value, pi.PropertyType); } - + try { pi.SetValue(r, value); - SendReply(IIPPacket.IIPPacketAction.SetProperty, callback); + SendReply(IIPPacket.IIPPacketAction.SetProperty, callback).Done(); } - catch(Exception ex) + catch (Exception ex) { SendError(ErrorType.Exception, callback, 0, ex.Message); } - + } else { @@ -1591,16 +1718,19 @@ namespace Esiur.Net.IIP var reply = new AsyncReply(); templateRequests.Add(classId, reply); - SendRequest(IIPPacket.IIPPacketAction.TemplateFromClassId, classId).Then((rt) => - { - templateRequests.Remove(classId); - templates.Add(((ResourceTemplate)rt[0]).ClassId, (ResourceTemplate)rt[0]); - Warehouse.PutTemplate(rt[0] as ResourceTemplate); - reply.Trigger(rt[0]); - }).Error((ex) => - { - reply.TriggerError(ex); - }); + SendRequest(IIPPacket.IIPPacketAction.TemplateFromClassId) + .AddGuid(classId) + .Done() + .Then((rt) => + { + templateRequests.Remove(classId); + templates.Add(((ResourceTemplate)rt[0]).ClassId, (ResourceTemplate)rt[0]); + Warehouse.PutTemplate(rt[0] as ResourceTemplate); + reply.Trigger(rt[0]); + }).Error((ex) => + { + reply.TriggerError(ex); + }); return reply; } @@ -1692,29 +1822,32 @@ namespace Esiur.Net.IIP var reply = new AsyncReply(); resourceRequests.Add(id, reply); - SendRequest(IIPPacket.IIPPacketAction.AttachResource, id).Then((rt) => - { - var dr = new DistributedResource(this, id, (ulong)rt[1], (string)rt[2]); + SendRequest(IIPPacket.IIPPacketAction.AttachResource) + .AddUInt32(id) + .Done() + .Then((rt) => + { + var dr = new DistributedResource(this, id, (ulong)rt[1], (string)rt[2]); - GetTemplate((Guid)rt[0]).Then((tmp) => - { - // ClassId, ResourceAge, ResourceLink, Content - Warehouse.Put(dr, id.ToString(), this, null, tmp); + GetTemplate((Guid)rt[0]).Then((tmp) => + { + // ClassId, ResourceAge, ResourceLink, Content + Warehouse.Put(dr, id.ToString(), this, null, tmp); - Codec.ParsePropertyValueArray((byte[])rt[3], this).Then((ar) => - { - dr._Attached(ar); - resourceRequests.Remove(id); - reply.Trigger(dr); - }); - }).Error((ex) => - { - reply.TriggerError(ex); - }); - }).Error((ex) => - { - reply.TriggerError(ex); - }); + Codec.ParsePropertyValueArray((byte[])rt[3], this).Then((ar) => + { + dr._Attached(ar); + resourceRequests.Remove(id); + reply.Trigger(dr); + }); + }).Error((ex) => + { + reply.TriggerError(ex); + }); + }).Error((ex) => + { + reply.TriggerError(ex); + }); return reply; } @@ -1724,14 +1857,17 @@ namespace Esiur.Net.IIP { var rt = new AsyncReply(); - SendRequest(IIPPacket.IIPPacketAction.ResourceChildren, resource.Instance.Id).Then(ar => - { - var d = (byte[])ar[0]; - Codec.ParseResourceArray(d, 0, (uint)d.Length, this).Then(resources => - { - rt.Trigger(resources); - }).Error(ex => rt.TriggerError(ex)); - }); + SendRequest(IIPPacket.IIPPacketAction.ResourceChildren) + .AddUInt32(resource.Instance.Id) + .Done() + .Then(ar => + { + var d = (byte[])ar[0]; + Codec.ParseResourceArray(d, 0, (uint)d.Length, this).Then(resources => + { + rt.Trigger(resources); + }).Error(ex => rt.TriggerError(ex)); + }); return rt; } @@ -1740,14 +1876,17 @@ namespace Esiur.Net.IIP { var rt = new AsyncReply(); - SendRequest(IIPPacket.IIPPacketAction.ResourceParents, resource.Instance.Id).Then(ar => - { - var d = (byte[])ar[0]; - Codec.ParseResourceArray(d, 0, (uint)d.Length, this).Then(resources => + SendRequest(IIPPacket.IIPPacketAction.ResourceParents) + .AddUInt32(resource.Instance.Id) + .Done() + .Then(ar => { - rt.Trigger(resources); - }).Error(ex => rt.TriggerError(ex)); - }); + var d = (byte[])ar[0]; + Codec.ParseResourceArray(d, 0, (uint)d.Length, this).Then(resources => + { + rt.Trigger(resources); + }).Error(ex => rt.TriggerError(ex)); + }); return rt; } @@ -1757,17 +1896,21 @@ namespace Esiur.Net.IIP var rt = new AsyncReply(); if (attributes == null) - SendRequest(IIPPacket.IIPPacketAction.ClearAllAttributes, resource.Instance.Id).Then(ar => - { - rt.Trigger(true); - }).Error(ex => rt.TriggerError(ex)); + SendRequest(IIPPacket.IIPPacketAction.ClearAllAttributes) + .AddUInt32(resource.Instance.Id) + .Done() + .Then(ar => rt.Trigger(true)) + .Error(ex => rt.TriggerError(ex)); else { var attrs = DC.ToBytes(attributes); - SendRequest(IIPPacket.IIPPacketAction.ClearAttributes, resource.Instance.Id, (uint)attrs.Length, attrs).Then(ar => - { - rt.Trigger(true); - }).Error(ex => rt.TriggerChunk(ex)); + SendRequest(IIPPacket.IIPPacketAction.ClearAttributes) + .AddUInt32(resource.Instance.Id) + .AddInt32(attrs.Length) + .AddUInt8Array(attrs) + .Done() + .Then(ar => rt.Trigger(true)) + .Error(ex => rt.TriggerError(ex)); } return rt; @@ -1777,12 +1920,12 @@ namespace Esiur.Net.IIP { var rt = new AsyncReply(); - SendRequest(clearAttributes ? IIPPacket.IIPPacketAction.UpdateAllAttributes : IIPPacket.IIPPacketAction.UpdateAttributes - , resource.Instance.Id, - Codec.ComposeStructure(attributes, this, true, true, true)).Then(ar => - { - rt.Trigger(true); - }).Error(ex => rt.TriggerError(ex)); + SendRequest(clearAttributes ? IIPPacket.IIPPacketAction.UpdateAllAttributes : IIPPacket.IIPPacketAction.UpdateAttributes) + .AddUInt32(resource.Instance.Id) + .AddUInt8Array(Codec.ComposeStructure(attributes, this, true, true, true)) + .Done() + .Then(ar => rt.Trigger(true)) + .Error(ex => rt.TriggerError(ex)); return rt; } @@ -1793,32 +1936,40 @@ namespace Esiur.Net.IIP if (attributes == null) { - SendRequest(IIPPacket.IIPPacketAction.GetAllAttributes, resource.Instance.Id).Then(ar => - { - var d = ar[0] as byte[]; - Codec.ParseStructure(d, 0, (uint)d.Length, this).Then(st => + SendRequest(IIPPacket.IIPPacketAction.GetAllAttributes) + .AddUInt32(resource.Instance.Id) + .Done() + .Then(ar => { + var d = ar[0] as byte[]; + Codec.ParseStructure(d, 0, (uint)d.Length, this).Then(st => + { - resource.Instance.SetAttributes(st); + resource.Instance.SetAttributes(st); - rt.Trigger(st); - }).Error(ex => rt.TriggerError(ex)); - }); + rt.Trigger(st); + }).Error(ex => rt.TriggerError(ex)); + }); } else { var attrs = DC.ToBytes(attributes); - SendRequest(IIPPacket.IIPPacketAction.GetAttributes, resource.Instance.Id, (uint)attrs.Length, attrs).Then(ar => - { - var d = ar[0] as byte[]; - Codec.ParseStructure(d, 0, (uint)d.Length, this).Then(st => + SendRequest(IIPPacket.IIPPacketAction.GetAttributes) + .AddUInt32(resource.Instance.Id) + .AddInt32(attrs.Length) + .AddUInt8Array(attrs) + .Done() + .Then(ar => { + var d = ar[0] as byte[]; + Codec.ParseStructure(d, 0, (uint)d.Length, this).Then(st => + { - resource.Instance.SetAttributes(st); + resource.Instance.SetAttributes(st); - rt.Trigger(st); - }).Error(ex => rt.TriggerError(ex)); - }); + rt.Trigger(st); + }).Error(ex => rt.TriggerError(ex)); + }); } return rt; @@ -1842,18 +1993,19 @@ namespace Esiur.Net.IIP var reply = new AsyncReply>(); - SendRequest(IIPPacket.IIPPacketAction.ResourceHistory, dr.Id, fromDate, toDate).Then(rt => - { - var content = (byte[])rt[0]; - - Codec.ParseHistory(content, 0, (uint)content.Length, resource, this).Then((history) => + SendRequest(IIPPacket.IIPPacketAction.ResourceHistory) + .AddUInt32(dr.Id) + .AddDateTime(fromDate) + .AddDateTime(toDate) + .Done() + .Then(rt => { - reply.Trigger(history); - }); - }).Error((ex) => - { - reply.TriggerError(ex); - }); ; + var content = (byte[])rt[0]; + + Codec.ParseHistory(content, 0, (uint)content.Length, resource, this) + .Then((history) => reply.Trigger(history)); + + }).Error((ex) => reply.TriggerError(ex)); return reply; } @@ -1871,15 +2023,18 @@ namespace Esiur.Net.IIP var str = DC.ToBytes(path); var reply = new AsyncReply(); - SendRequest(IIPPacket.IIPPacketAction.QueryLink, (ushort)str.Length, str).Then(args => - { - var content = args[0] as byte[]; + SendRequest(IIPPacket.IIPPacketAction.QueryLink) + .AddUInt16((ushort)str.Length) + .AddUInt8Array(str) + .Done() + .Then(args => + { + var content = args[0] as byte[]; - Codec.ParseResourceArray(content, 0, (uint)content.Length, this).Then(resources => - { - reply.Trigger(resources); - }); - }); + Codec.ParseResourceArray(content, 0, (uint)content.Length, this) + .Then(resources => reply.Trigger(resources)); + + }).Error(ex=>reply.TriggerError(ex)); return reply; } @@ -1897,25 +2052,30 @@ namespace Esiur.Net.IIP public AsyncReply Create(IStore store, IResource parent, string className, object[] parameters, Structure attributes, Structure values) { var reply = new AsyncReply(); - var pkt = new BinaryList(store.Instance.Id, - parent.Instance.Id, - className.Length, Encoding.ASCII.GetBytes(className), - Codec.ComposeVarArray(parameters, this, true), - Codec.ComposeStructure(attributes, this, true, true, true), - Codec.ComposeStructure(values, this)); + var pkt = new BinaryList() + .AddUInt32(store.Instance.Id) + .AddUInt32(parent.Instance.Id) + .AddUInt8((byte)className.Length) + .AddString(className) + .AddUInt8Array(Codec.ComposeVarArray(parameters, this, true)) + .AddUInt8Array(Codec.ComposeStructure(attributes, this, true, true, true)) + .AddUInt8Array(Codec.ComposeStructure(values, this)); - pkt.Insert(8, pkt.Length); + pkt.InsertInt32(8, pkt.Length); - SendRequest(IIPPacket.IIPPacketAction.CreateResource, pkt.ToArray()).Then(args => - { - var rid = (uint)args[0]; - - Fetch(rid).Then((r) => + SendRequest(IIPPacket.IIPPacketAction.CreateResource) + .AddUInt8Array(pkt.ToArray()) + .Done() + .Then(args => { - reply.Trigger(r); - }); + var rid = (uint)args[0]; - }); + Fetch(rid).Then((r) => + { + reply.Trigger(r); + }); + + }); return reply; } @@ -1923,17 +2083,23 @@ namespace Esiur.Net.IIP private void Instance_ResourceDestroyed(IResource resource) { // compose the packet - SendEvent(IIPPacket.IIPPacketEvent.ResourceDestroyed, resource.Instance.Id); + SendEvent(IIPPacket.IIPPacketEvent.ResourceDestroyed) + .AddUInt32(resource.Instance.Id) + .Done(); } private void Instance_PropertyModified(IResource resource, string name, object newValue) { - var pt = resource.Instance.Template.GetPropertyTemplate(name); + var pt = resource.Instance.Template.GetPropertyTemplateByName(name); if (pt == null) return; - SendEvent(IIPPacket.IIPPacketEvent.PropertyUpdated, resource.Instance.Id, pt.Index, Codec.Compose(newValue, this)); + SendEvent(IIPPacket.IIPPacketEvent.PropertyUpdated) + .AddUInt32(resource.Instance.Id) + .AddUInt8(pt.Index) + .AddUInt8Array(Codec.Compose(newValue, this)) + .Done(); } @@ -1941,7 +2107,7 @@ namespace Esiur.Net.IIP private void Instance_EventOccurred(IResource resource, object issuer, Session[] receivers, string name, object[] args) { - var et = resource.Instance.Template.GetEventTemplate(name); + var et = resource.Instance.Template.GetEventTemplateByName(name); if (et == null) return; @@ -1964,8 +2130,11 @@ namespace Esiur.Net.IIP return; // compose the packet - SendEvent(IIPPacket.IIPPacketEvent.EventOccurred, - resource.Instance.Id, (byte)et.Index, Codec.ComposeVarArray(args, this, true)); + SendEvent(IIPPacket.IIPPacketEvent.EventOccurred) + .AddUInt32(resource.Instance.Id) + .AddUInt8((byte)et.Index) + .AddUInt8Array(Codec.ComposeVarArray(args, this, true)) + .Done(); } } } diff --git a/Esiur/Net/IIP/DistributedResource.cs b/Esiur/Net/IIP/DistributedResource.cs index 140ae9f..dbd6519 100644 --- a/Esiur/Net/IIP/DistributedResource.cs +++ b/Esiur/Net/IIP/DistributedResource.cs @@ -33,7 +33,7 @@ using Esiur.Misc; using Esiur.Data; using System.Dynamic; using System.Security.Cryptography; -using Esiur.Engine; +using Esiur.Core; using System.Runtime.CompilerServices; using System.Reflection.Emit; using System.Linq; @@ -229,7 +229,7 @@ namespace Esiur.Net.IIP internal void _EmitEventByIndex(byte index, object[] args) { - var et = Instance.Template.GetEventTemplate(index); + var et = Instance.Template.GetEventTemplateByIndex(index); events[index]?.Invoke(this, args); Instance.EmitResourceEvent(null, null, et.Name, args); } @@ -261,7 +261,7 @@ namespace Esiur.Net.IIP public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result) { - var ft = Instance.Template.GetFunctionTemplate(binder.Name); + var ft = Instance.Template.GetFunctionTemplateByName(binder.Name); var reply = new AsyncReply(); @@ -325,7 +325,7 @@ namespace Esiur.Net.IIP if (!isAttached) return false; - var pt = Instance.Template.GetPropertyTemplate(binder.Name); + var pt = Instance.Template.GetPropertyTemplateByName(binder.Name); if (pt != null) { @@ -334,7 +334,7 @@ namespace Esiur.Net.IIP } else { - var et = Instance.Template.GetEventTemplate(binder.Name); + var et = Instance.Template.GetEventTemplateByName(binder.Name); if (et == null) return false; @@ -347,7 +347,7 @@ namespace Esiur.Net.IIP internal void _UpdatePropertyByIndex(byte index, object value) { - var pt = Instance.Template.GetPropertyTemplate(index); + var pt = Instance.Template.GetPropertyTemplateByIndex(index); properties[index] = value; Instance.EmitModification(pt, value); } @@ -366,12 +366,18 @@ namespace Esiur.Net.IIP var reply = new AsyncReply(); var parameters = Codec.Compose(value, connection); - connection.SendRequest(Packets.IIPPacket.IIPPacketAction.SetProperty, instanceId, index, parameters).Then((res) => - { - // not really needed, server will always send property modified, this only happens if the programmer forgot to emit in property setter - properties[index] = value; - reply.Trigger(null); - }); + connection.SendRequest(Packets.IIPPacket.IIPPacketAction.SetProperty) + .AddUInt32(instanceId) + .AddUInt8(index) + .AddUInt8Array(parameters) + .Done() + .Then((res) => + { + // not really needed, server will always send property modified, + // this only happens if the programmer forgot to emit in property setter + properties[index] = value; + reply.Trigger(null); + }); return reply; } @@ -384,7 +390,7 @@ namespace Esiur.Net.IIP if (!isAttached) return false; - var pt = Instance.Template.GetPropertyTemplate(binder.Name); + var pt = Instance.Template.GetPropertyTemplateByName(binder.Name); if (pt != null) { @@ -393,7 +399,7 @@ namespace Esiur.Net.IIP } else { - var et = Instance.Template.GetEventTemplate(binder.Name); + var et = Instance.Template.GetEventTemplateByName(binder.Name); if (et == null) return false; diff --git a/Esiur/Net/IIP/DistributedServer.cs b/Esiur/Net/IIP/DistributedServer.cs index 0cec3fe..b3611c9 100644 --- a/Esiur/Net/IIP/DistributedServer.cs +++ b/Esiur/Net/IIP/DistributedServer.cs @@ -30,7 +30,7 @@ using Esiur.Net.Sockets; using Esiur.Misc; using System.Threading; using Esiur.Data; -using Esiur.Engine; +using Esiur.Core; using System.Net; using Esiur.Resource; using Esiur.Security.Membership; @@ -128,7 +128,7 @@ namespace Esiur.Net.IIP protected override void ClientConnected(DistributedConnection sender) { - //Console.WriteLine("DistributedConnection Client Connected"); + Console.WriteLine("DistributedConnection Client Connected"); } diff --git a/Esiur/Net/NetworkConnection.cs b/Esiur/Net/NetworkConnection.cs index 1802cef..bf81c23 100644 --- a/Esiur/Net/NetworkConnection.cs +++ b/Esiur/Net/NetworkConnection.cs @@ -31,7 +31,7 @@ using System.Net; using System.Collections; using System.Collections.Generic; using Esiur.Misc; -using Esiur.Engine; +using Esiur.Core; using Esiur.Data; using Esiur.Net.Sockets; using Esiur.Resource; diff --git a/Esiur/Net/NetworkServer.cs b/Esiur/Net/NetworkServer.cs index ac62098..b5c118b 100644 --- a/Esiur/Net/NetworkServer.cs +++ b/Esiur/Net/NetworkServer.cs @@ -27,7 +27,7 @@ using System.Threading; using System.Collections.Generic; using Esiur.Data; using Esiur.Misc; -using Esiur.Engine; +using Esiur.Core; using Esiur.Net.Sockets; using Esiur.Resource; using System.Threading.Tasks; diff --git a/Esiur/Net/NetworkSession.cs b/Esiur/Net/NetworkSession.cs index 2626734..ea974c8 100644 --- a/Esiur/Net/NetworkSession.cs +++ b/Esiur/Net/NetworkSession.cs @@ -33,7 +33,7 @@ using System.Collections; using System.Collections.Generic; using Esiur.Data; using Esiur.Misc; -using Esiur.Engine; +using Esiur.Core; namespace Esiur.Net { diff --git a/Esiur/Net/Packets/IIPPacket.cs b/Esiur/Net/Packets/IIPPacket.cs index 714c913..5cfb1aa 100644 --- a/Esiur/Net/Packets/IIPPacket.cs +++ b/Esiur/Net/Packets/IIPPacket.cs @@ -23,7 +23,7 @@ SOFTWARE. */ using Esiur.Data; -using Esiur.Engine; +using Esiur.Core; using Esiur.Misc; using Esiur.Net.Packets; using System; diff --git a/Esiur/Net/SendList.cs b/Esiur/Net/SendList.cs new file mode 100644 index 0000000..2513d87 --- /dev/null +++ b/Esiur/Net/SendList.cs @@ -0,0 +1,26 @@ +using Esiur.Core; +using Esiur.Data; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Esiur.Net +{ + public class SendList : BinaryList + { + NetworkConnection connection; + IAsyncReply reply; + + public SendList(NetworkConnection connection, IAsyncReply reply) + { + this.reply = reply; + this.connection = connection; + } + + public override IAsyncReply Done() + { + connection.Send(this.ToArray()); + return reply; + } + } +} diff --git a/Esiur/Net/Sockets/ISocket.cs b/Esiur/Net/Sockets/ISocket.cs index 26e9274..25c20cf 100644 --- a/Esiur/Net/Sockets/ISocket.cs +++ b/Esiur/Net/Sockets/ISocket.cs @@ -34,7 +34,7 @@ using Esiur.Data; using Esiur.Misc; using System.Collections.Concurrent; using Esiur.Resource; -using Esiur.Engine; +using Esiur.Core; namespace Esiur.Net.Sockets { @@ -53,7 +53,7 @@ namespace Esiur.Net.Sockets void Send(byte[] message); void Send(byte[] message, int offset, int size); void Close(); - bool Connect(string hostname, ushort port); + AsyncReply Connect(string hostname, ushort port); bool Begin(); //ISocket Accept(); AsyncReply Accept(); diff --git a/Esiur/Net/Sockets/SSLSocket.cs b/Esiur/Net/Sockets/SSLSocket.cs index 58eb7e1..c98836b 100644 --- a/Esiur/Net/Sockets/SSLSocket.cs +++ b/Esiur/Net/Sockets/SSLSocket.cs @@ -29,7 +29,7 @@ using System.Text; using System.Net.Sockets; using System.Net; using Esiur.Misc; -using Esiur.Engine; +using Esiur.Core; using System.Threading; using System.Net.Security; using System.Security.Cryptography.X509Certificates; @@ -77,20 +77,29 @@ namespace Esiur.Net.Sockets } } - public bool Connect(string hostname, ushort port) + public AsyncReply Connect(string hostname, ushort port) { + var rt = new AsyncReply(); + try { - this.hostname = hostname; - server = false; state = SocketState.Connecting; - sock.ConnectAsync(hostname, port).ContinueWith(Connected); - return true; + sock.ConnectAsync(hostname, port).ContinueWith((x) => + { + if (x.IsFaulted) + rt.TriggerError(x.Exception); + else + rt.Trigger(true); + + Connected(x); + }); } - catch + catch (Exception ex) { - return false; + rt.TriggerError(ex); } + + return rt; } private void DataSent(Task task) diff --git a/Esiur/Net/Sockets/TCPSocket.cs b/Esiur/Net/Sockets/TCPSocket.cs index 62b6345..9cb77fd 100644 --- a/Esiur/Net/Sockets/TCPSocket.cs +++ b/Esiur/Net/Sockets/TCPSocket.cs @@ -29,7 +29,7 @@ using System.Text; using System.Net.Sockets; using System.Net; using Esiur.Misc; -using Esiur.Engine; +using Esiur.Core; using System.Threading; using Esiur.Resource; using System.Threading.Tasks; @@ -62,13 +62,6 @@ namespace Esiur.Net.Sockets SocketAsyncEventArgs socketArgs = new SocketAsyncEventArgs(); - private void Connected(Task t) - { - state = SocketState.Established; - OnConnect?.Invoke(); - Begin(); - } - public bool Begin() { if (began) @@ -86,18 +79,34 @@ namespace Esiur.Net.Sockets return true; } - public bool Connect(string hostname, ushort port) + public AsyncReply Connect(string hostname, ushort port) { + var rt = new AsyncReply(); + try { state = SocketState.Connecting; - sock.ConnectAsync(hostname, port).ContinueWith(Connected); - return true; + sock.ConnectAsync(hostname, port).ContinueWith((x) => + { + + if (x.IsFaulted) + rt.TriggerError(x.Exception); + else + { + + state = SocketState.Established; + OnConnect?.Invoke(); + Begin(); + rt.Trigger(true); + } + }); } - catch + catch (Exception ex) { - return false; + rt.TriggerError(ex); } + + return rt; } @@ -117,7 +126,7 @@ namespace Esiur.Net.Sockets } //if (receiveNetworkBuffer.Protected) - // Console.WriteLine(); + // Console.WriteLine(); //lock (receiveNetworkBuffer.SyncLock) receiveNetworkBuffer.Write(receiveBuffer, 0, (uint)task.Result); @@ -128,7 +137,7 @@ namespace Esiur.Net.Sockets if (state == SocketState.Established) { sock.ReceiveAsync(receiveBufferSegment, SocketFlags.None).ContinueWith(DataReceived); - + } } @@ -149,7 +158,7 @@ namespace Esiur.Net.Sockets try { // SocketError err; - + if (state == SocketState.Closed || state == SocketState.Terminated) return; @@ -162,7 +171,7 @@ namespace Esiur.Net.Sockets //if (receiveNetworkBuffer.Protected) // Console.WriteLine(); - + //lock (receiveNetworkBuffer.SyncLock) receiveNetworkBuffer.Write(receiveBuffer, 0, (uint)e.BytesTransferred); diff --git a/Esiur/Net/Sockets/WSSocket.cs b/Esiur/Net/Sockets/WSSocket.cs index b913aa5..b462a7c 100644 --- a/Esiur/Net/Sockets/WSSocket.cs +++ b/Esiur/Net/Sockets/WSSocket.cs @@ -31,7 +31,7 @@ using System.Net; using Esiur.Net.Packets; using Esiur.Misc; using System.IO; -using Esiur.Engine; +using Esiur.Core; using Esiur.Resource; using Esiur.Data; @@ -236,7 +236,7 @@ namespace Esiur.Net.Sockets sock.Close(); } - public bool Connect(string hostname, ushort port) + public AsyncReply Connect(string hostname, ushort port) { throw new NotImplementedException(); } diff --git a/Esiur/Net/TCP/TCPFilter.cs b/Esiur/Net/TCP/TCPFilter.cs index df86ebb..c39ad7f 100644 --- a/Esiur/Net/TCP/TCPFilter.cs +++ b/Esiur/Net/TCP/TCPFilter.cs @@ -29,7 +29,7 @@ using System.Text; using System.Collections; using Esiur.Data; using Esiur.Net.Sockets; -using Esiur.Engine; +using Esiur.Core; using Esiur.Resource; namespace Esiur.Net.TCP diff --git a/Esiur/Net/TCP/TCPServer.cs b/Esiur/Net/TCP/TCPServer.cs index b5b739b..3c2ccd5 100644 --- a/Esiur/Net/TCP/TCPServer.cs +++ b/Esiur/Net/TCP/TCPServer.cs @@ -30,7 +30,7 @@ using Esiur.Net.Sockets; using Esiur.Misc; using System.Threading; using Esiur.Data; -using Esiur.Engine; +using Esiur.Core; using System.Net; using Esiur.Resource; diff --git a/Esiur/Net/UDP/UDPFilter.cs b/Esiur/Net/UDP/UDPFilter.cs index 01633f9..2ae4298 100644 --- a/Esiur/Net/UDP/UDPFilter.cs +++ b/Esiur/Net/UDP/UDPFilter.cs @@ -29,7 +29,7 @@ using System.Text; using System.Collections; using System.Net; using Esiur.Data; -using Esiur.Engine; +using Esiur.Core; using Esiur.Resource; namespace Esiur.Net.UDP diff --git a/Esiur/Resource/IResource.cs b/Esiur/Resource/IResource.cs index 95a6230..3b640e6 100644 --- a/Esiur/Resource/IResource.cs +++ b/Esiur/Resource/IResource.cs @@ -27,7 +27,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using Esiur.Data; -using Esiur.Engine; +using Esiur.Core; namespace Esiur.Resource { diff --git a/Esiur/Resource/IStore.cs b/Esiur/Resource/IStore.cs index 96df8af..577a252 100644 --- a/Esiur/Resource/IStore.cs +++ b/Esiur/Resource/IStore.cs @@ -23,7 +23,7 @@ SOFTWARE. */ using Esiur.Data; -using Esiur.Engine; +using Esiur.Core; using Esiur.Resource.Template; using System; using System.Collections.Generic; diff --git a/Esiur/Resource/Instance.cs b/Esiur/Resource/Instance.cs index 473e7fe..1524b92 100644 --- a/Esiur/Resource/Instance.cs +++ b/Esiur/Resource/Instance.cs @@ -11,6 +11,7 @@ using Esiur.Misc; using Esiur.Security.Permissions; using Esiur.Resource.Template; using Esiur.Security.Authority; +using Esiur.Proxy; namespace Esiur.Resource { @@ -265,7 +266,7 @@ namespace Esiur.Resource /// public bool LoadProperty(string name, ulong age, DateTime modificationDate, object value) { - var pt = template.GetPropertyTemplate(name); + var pt = template.GetPropertyTemplateByName(name); if (pt == null) return false; @@ -336,7 +337,7 @@ namespace Esiur.Resource { for (byte i = 0; i < properties.Length; i++) { - var pt = this.template.GetPropertyTemplate(i); + var pt = this.template.GetPropertyTemplateByIndex(i); if (pt != null) { var pv = properties[i]; @@ -486,7 +487,7 @@ namespace Esiur.Resource object value; if (GetPropertyValue(propertyName, out value)) { - var pt = template.GetPropertyTemplate(propertyName); + var pt = template.GetPropertyTemplateByName(propertyName); EmitModification(pt, value); } } @@ -515,7 +516,7 @@ namespace Esiur.Resource #endif */ - var pt = template.GetPropertyTemplate(name); + var pt = template.GetPropertyTemplateByName(name); if (pt != null && pt.Info != null) { @@ -712,7 +713,7 @@ namespace Esiur.Resource } // connect events - Type t = resource.GetType(); + Type t = ResourceProxy.GetBaseType(resource); #if NETSTANDARD1_5 var events = t.GetTypeInfo().GetEvents(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly); diff --git a/Esiur/Resource/Resource.cs b/Esiur/Resource/Resource.cs index 5b5474e..0b31c8f 100644 --- a/Esiur/Resource/Resource.cs +++ b/Esiur/Resource/Resource.cs @@ -24,7 +24,7 @@ SOFTWARE. using System; using System.Collections.Generic; using System.Text; -using Esiur.Engine; +using Esiur.Core; namespace Esiur.Resource { diff --git a/Esiur/Resource/ResourceEventHandler.cs b/Esiur/Resource/ResourceEventHandler.cs index 18954e1..662d013 100644 --- a/Esiur/Resource/ResourceEventHandler.cs +++ b/Esiur/Resource/ResourceEventHandler.cs @@ -22,7 +22,7 @@ SOFTWARE. */ using Esiur.Data; -using Esiur.Engine; +using Esiur.Core; using Esiur.Net.IIP; using Esiur.Security.Authority; using System; diff --git a/Esiur/Resource/ResourceProperty.cs b/Esiur/Resource/ResourceProperty.cs index aef6567..82fb3f4 100644 --- a/Esiur/Resource/ResourceProperty.cs +++ b/Esiur/Resource/ResourceProperty.cs @@ -63,7 +63,7 @@ namespace Esiur.Resource } } - public ResourceProperty(StorageMode storage = StorageMode.Volatile, string readExpansion = null, string writeExpansion = null) + public ResourceProperty(StorageMode storage = StorageMode.NonVolatile, string readExpansion = null, string writeExpansion = null) { this.readExpansion = readExpansion; this.writeExpansion = writeExpansion; diff --git a/Esiur/Resource/ResourceTrigger.cs b/Esiur/Resource/ResourceTrigger.cs index 837d75e..237392f 100644 --- a/Esiur/Resource/ResourceTrigger.cs +++ b/Esiur/Resource/ResourceTrigger.cs @@ -32,7 +32,7 @@ namespace Esiur.Resource { public enum ResourceTrigger : int { - Loaded = 0, + Open = 0, Initialize, Terminate, Configure, diff --git a/Esiur/Resource/Storable.cs b/Esiur/Resource/Storable.cs index 76184cc..8701c4f 100644 --- a/Esiur/Resource/Storable.cs +++ b/Esiur/Resource/Storable.cs @@ -23,7 +23,7 @@ SOFTWARE. */ using Esiur.Data; -using Esiur.Engine; +using Esiur.Core; using System; using System.Collections.Generic; using System.Linq; diff --git a/Esiur/Resource/StorageMode.cs b/Esiur/Resource/StorageMode.cs index 5513838..5f557f5 100644 --- a/Esiur/Resource/StorageMode.cs +++ b/Esiur/Resource/StorageMode.cs @@ -6,8 +6,8 @@ namespace Esiur.Resource { public enum StorageMode : byte { - Volatile = 0, NonVolatile, + Volatile, Recordable } } diff --git a/Esiur/Resource/Template/EventTemplate.cs b/Esiur/Resource/Template/EventTemplate.cs index e132e97..dd95868 100644 --- a/Esiur/Resource/Template/EventTemplate.cs +++ b/Esiur/Resource/Template/EventTemplate.cs @@ -22,10 +22,20 @@ namespace Esiur.Resource.Template if (Expansion != null) { var exp = DC.ToBytes(Expansion); - return BinaryList.ToBytes((byte)0x50, exp.Length, exp, (byte)name.Length, name); + return new BinaryList() + .AddUInt8(0x50) + .AddInt32(exp.Length) + .AddUInt8Array(exp) + .AddUInt8((byte)name.Length) + .AddUInt8Array(name) + .ToArray(); } else - return BinaryList.ToBytes((byte)0x40, (byte)name.Length, name); + return new BinaryList() + .AddUInt8(0x40) + .AddUInt8((byte)name.Length) + .AddUInt8Array(name) + .ToArray(); } diff --git a/Esiur/Resource/Template/FunctionTemplate.cs b/Esiur/Resource/Template/FunctionTemplate.cs index 694df5a..0ecae96 100644 --- a/Esiur/Resource/Template/FunctionTemplate.cs +++ b/Esiur/Resource/Template/FunctionTemplate.cs @@ -30,10 +30,18 @@ namespace Esiur.Resource.Template if (Expansion != null) { var exp = DC.ToBytes(Expansion); - return BinaryList.ToBytes((byte)(0x10 | (IsVoid ? 0x8 : 0x0)), (byte)name.Length, name, exp.Length, exp); + return new BinaryList().AddUInt8((byte)(0x10 | (IsVoid ? 0x8 : 0x0))) + .AddUInt8((byte)name.Length) + .AddUInt8Array(name) + .AddInt32(exp.Length) + .AddUInt8Array(exp) + .ToArray(); } else - return BinaryList.ToBytes((byte)(IsVoid ? 0x8 : 0x0), (byte)name.Length, name); + return new BinaryList().AddUInt8((byte)(IsVoid ? 0x8 : 0x0)) + .AddUInt8((byte)name.Length) + .AddUInt8Array(name) + .ToArray(); } diff --git a/Esiur/Resource/Template/PropertyTemplate.cs b/Esiur/Resource/Template/PropertyTemplate.cs index ed77f92..99876c4 100644 --- a/Esiur/Resource/Template/PropertyTemplate.cs +++ b/Esiur/Resource/Template/PropertyTemplate.cs @@ -73,20 +73,44 @@ namespace Esiur.Resource.Template { var rexp = DC.ToBytes(ReadExpansion); var wexp = DC.ToBytes(WriteExpansion); - return BinaryList.ToBytes((byte)(0x38 | pv), (byte)name.Length, name, wexp.Length, wexp, rexp.Length, rexp); + return new BinaryList() + .AddUInt8((byte)(0x38 | pv)) + .AddUInt8((byte)name.Length) + .AddUInt8Array(name) + .AddInt32(wexp.Length) + .AddUInt8Array(wexp) + .AddInt32(rexp.Length) + .AddUInt8Array(rexp) + .ToArray(); } else if (WriteExpansion != null) { var wexp = DC.ToBytes(WriteExpansion); - return BinaryList.ToBytes((byte)(0x30 | pv), (byte)name.Length, name, wexp.Length, wexp); + return new BinaryList() + .AddUInt8((byte)(0x30 | pv)) + .AddUInt8((byte)name.Length) + .AddUInt8Array(name) + .AddInt32(wexp.Length) + .AddUInt8Array(wexp) + .ToArray(); } else if (ReadExpansion != null) { var rexp = DC.ToBytes(ReadExpansion); - return BinaryList.ToBytes((byte)(0x28 | pv), (byte)name.Length, name, rexp.Length, rexp); + return new BinaryList() + .AddUInt8((byte)(0x28 | pv)) + .AddUInt8((byte)name.Length) + .AddUInt8Array(name) + .AddInt32(rexp.Length) + .AddUInt8Array(rexp) + .ToArray(); } else - return BinaryList.ToBytes((byte)(0x20 | pv), (byte)name.Length, name); + return new BinaryList() + .AddUInt8((byte)(0x20 | pv)) + .AddUInt8((byte)name.Length) + .AddUInt8Array(name) + .ToArray(); } public PropertyTemplate(ResourceTemplate template, byte index, string name, string read, string write, StorageMode storage) diff --git a/Esiur/Resource/Template/ResourceTemplate.cs b/Esiur/Resource/Template/ResourceTemplate.cs index dceb89d..6e05d58 100644 --- a/Esiur/Resource/Template/ResourceTemplate.cs +++ b/Esiur/Resource/Template/ResourceTemplate.cs @@ -5,7 +5,7 @@ using System.Text; using System.Reflection; using Esiur.Misc; using Esiur.Data; -using Esiur.Engine; +using Esiur.Core; using System.Security.Cryptography; using Esiur.Proxy; @@ -32,16 +32,16 @@ namespace Esiur.Resource.Template public MemberTemplate GetMemberTemplate(MemberInfo member) { if (member is MethodInfo) - return GetFunctionTemplate(member.Name); + return GetFunctionTemplateByName(member.Name); else if (member is EventInfo) - return GetEventTemplate(member.Name); + return GetEventTemplateByName(member.Name); else if (member is PropertyInfo) - return GetPropertyTemplate(member.Name); + return GetPropertyTemplateByName(member.Name); else return null; } - public EventTemplate GetEventTemplate(string eventName) + public EventTemplate GetEventTemplateByName(string eventName) { foreach (var i in events) if (i.Name == eventName) @@ -49,7 +49,7 @@ namespace Esiur.Resource.Template return null; } - public EventTemplate GetEventTemplate(byte index) + public EventTemplate GetEventTemplateByIndex(byte index) { foreach (var i in events) if (i.Index == index) @@ -57,14 +57,14 @@ namespace Esiur.Resource.Template return null; } - public FunctionTemplate GetFunctionTemplate(string functionName) + public FunctionTemplate GetFunctionTemplateByName(string functionName) { foreach (var i in functions) if (i.Name == functionName) return i; return null; } - public FunctionTemplate GetFunctionTemplate(byte index) + public FunctionTemplate GetFunctionTemplateByIndex(byte index) { foreach (var i in functions) if (i.Index == index) @@ -72,7 +72,7 @@ namespace Esiur.Resource.Template return null; } - public PropertyTemplate GetPropertyTemplate(byte index) + public PropertyTemplate GetPropertyTemplateByIndex(byte index) { foreach (var i in properties) if (i.Index == index) @@ -80,7 +80,7 @@ namespace Esiur.Resource.Template return null; } - public PropertyTemplate GetPropertyTemplate(string propertyName) + public PropertyTemplate GetPropertyTemplateByName(string propertyName) { foreach (var i in properties) if (i.Name == propertyName) @@ -200,16 +200,20 @@ namespace Esiur.Resource.Template // bake it binarily var b = new BinaryList(); - b.Append(classId); - b.Append((byte)className.Length, className); - b.Append(version); - b.Append((ushort)members.Count); + b.AddGuid(classId) + .AddUInt8((byte)className.Length) + .AddString(className) + .AddInt32(version) + .AddUInt16((ushort)members.Count); + + foreach (var ft in functions) - b.Append(ft.Compose()); + b.AddUInt8Array(ft.Compose()); foreach (var pt in properties) - b.Append(pt.Compose()); + b.AddUInt8Array(pt.Compose()); foreach (var et in events) - b.Append(et.Compose()); + b.AddUInt8Array(et.Compose()); + content = b.ToArray(); } diff --git a/Esiur/Resource/Warehouse.cs b/Esiur/Resource/Warehouse.cs index eb0b931..45ea9e6 100644 --- a/Esiur/Resource/Warehouse.cs +++ b/Esiur/Resource/Warehouse.cs @@ -23,7 +23,7 @@ SOFTWARE. */ using Esiur.Data; -using Esiur.Engine; +using Esiur.Core; using Esiur.Proxy; using Esiur.Resource.Template; using Esiur.Security.Permissions; @@ -54,7 +54,7 @@ namespace Esiur.Resource public static event StoreConnectedEvent StoreConnected; public static event StoreDisconnectedEvent StoreDisconnected; - static KeyList protocols = new KeyList(); + public static KeyList> Protocols { get; } = new KeyList>(); /// /// Get a store by its name. @@ -277,15 +277,15 @@ namespace Esiur.Resource var rt = new AsyncReply(); - if (protocols.ContainsKey(url[0])) + if (Protocols.ContainsKey(url[0])) { - var handler = protocols[url[0]]; + var handler = Protocols[url[0]]; - var store = Activator.CreateInstance(handler.GetType()) as IStore; + var store = handler();// Activator.CreateInstance(handler.GetType()) as IStore; Put(store, url[0] + "://" + hostname, null, parent, null, 0, manager, attributes); - store.Trigger(ResourceTrigger.Initialize).Then(x => { + store.Trigger(ResourceTrigger.Open).Then(x => { if (pathname.Length > 0 && pathname != "") store.Get(pathname).Then(r => { rt.Trigger(r); @@ -345,7 +345,7 @@ namespace Esiur.Resource resources.Add(resource.Instance.Id, resource); - if (!storeIsOpen) + if (storeIsOpen) resource.Trigger(ResourceTrigger.Initialize); } diff --git a/Esiur/Security/Authority/CACertificate.cs b/Esiur/Security/Authority/CACertificate.cs index 28dcfb3..3dac45c 100644 --- a/Esiur/Security/Authority/CACertificate.cs +++ b/Esiur/Security/Authority/CACertificate.cs @@ -23,7 +23,7 @@ SOFTWARE. */ using Esiur.Data; -using Esiur.Engine; +using Esiur.Core; using Esiur.Misc; using Esiur.Security.Cryptography; using Esiur.Security.Integrity; @@ -131,16 +131,20 @@ namespace Esiur.Security.Authority BinaryList cr = new BinaryList(); // make header - - cr.Append(id, issueDate, expireDate); + + cr.AddUInt64(id) + .AddDateTime(issueDate) + .AddDateTime(expireDate); + // hash function - cr.Append((byte)((byte)hashFunction << 4)); + cr.AddUInt8((byte)((byte)hashFunction << 4)); this.hashFunction = hashFunction; // CA Name this.name = authorityName; - cr.Append((byte)(authorityName.Length), Encoding.ASCII.GetBytes(authorityName)); + cr.AddUInt8((byte)(authorityName.Length)) + .AddUInt8Array(Encoding.ASCII.GetBytes(authorityName)); // public key rsa = RSA.Create();// new RSACryptoServiceProvider(2048); @@ -148,14 +152,16 @@ namespace Esiur.Security.Authority RSAParameters dRSAKey = rsa.ExportParameters(true); - cr.Append((byte)dRSAKey.Exponent.Length, dRSAKey.Exponent, (ushort)dRSAKey.Modulus.Length, dRSAKey.Modulus); + cr.AddUInt8((byte)dRSAKey.Exponent.Length) + .AddUInt8Array(dRSAKey.Exponent) + .AddUInt16((ushort)dRSAKey.Modulus.Length) + .AddUInt8Array(dRSAKey.Modulus); publicRawData = cr.ToArray(); privateRawData = DC.Merge(dRSAKey.D, dRSAKey.DP, dRSAKey.DQ, dRSAKey.InverseQ, dRSAKey.P, dRSAKey.Q); - } public override bool Save(string filename, bool includePrivate = false) @@ -163,9 +169,15 @@ namespace Esiur.Security.Authority try { if (includePrivate) - File.WriteAllBytes(filename, BinaryList.ToBytes((byte)CertificateType.CAPrivate, publicRawData, privateRawData)); + File.WriteAllBytes(filename, new BinaryList() + .AddUInt8((byte)CertificateType.CAPrivate) + .AddUInt8Array(publicRawData) + .AddUInt8Array(privateRawData) + .ToArray()); else - File.WriteAllBytes(filename, BinaryList.ToBytes((byte)CertificateType.CAPublic, publicRawData)); + File.WriteAllBytes(filename, new BinaryList() + .AddUInt8((byte)CertificateType.CAPublic) + .AddUInt8Array(publicRawData).ToArray()); return true; } @@ -178,7 +190,10 @@ namespace Esiur.Security.Authority public override byte[] Serialize(bool includePrivate = false) { if (includePrivate) - return BinaryList.ToBytes(publicRawData, privateRawData); + return new BinaryList() + .AddUInt8Array(publicRawData) + .AddUInt8Array(privateRawData) + .ToArray(); else return publicRawData; } diff --git a/Esiur/Security/Authority/Certificate.cs b/Esiur/Security/Authority/Certificate.cs index cc91da8..f23ed87 100644 --- a/Esiur/Security/Authority/Certificate.cs +++ b/Esiur/Security/Authority/Certificate.cs @@ -23,7 +23,7 @@ SOFTWARE. */ using Esiur.Data; -using Esiur.Engine; +using Esiur.Core; using Esiur.Misc; using Esiur.Security.Cryptography; using Esiur.Security.Integrity; diff --git a/Esiur/Security/Authority/DomainCertificate.cs b/Esiur/Security/Authority/DomainCertificate.cs index 5f294fa..ca034d2 100644 --- a/Esiur/Security/Authority/DomainCertificate.cs +++ b/Esiur/Security/Authority/DomainCertificate.cs @@ -166,44 +166,50 @@ namespace Esiur.Security.Authority var cr = new BinaryList(); // id - cr.Append(id); + cr.AddUInt64(id); // ip this.ip = ip; this.ip6 = ip6; - cr.Append(ip); + cr.AddUInt32(ip); if (ip6?.Length == 16) - cr.Append(ip6); + cr.AddUInt8Array(ip6); else - cr.Append(new byte[16]); + cr.AddUInt8Array(new byte[16]); - cr.Append(issueDate, expireDate); + cr.AddDateTime(issueDate) + .AddDateTime(expireDate); // domain this.domain = domain; - cr.Append((byte)(domain.Length), Encoding.ASCII.GetBytes(domain)); + cr.AddUInt8((byte)(domain.Length)) + .AddUInt8Array(Encoding.ASCII.GetBytes(domain)); // CA this.caName = authority.Name; - cr.Append((byte)(authority.Name.Length), Encoding.ASCII.GetBytes(authority.Name)); + cr.AddUInt8((byte)(authority.Name.Length)) + .AddUInt8Array(Encoding.ASCII.GetBytes(authority.Name)); this.authorityName = authority.Name; // CA Index //co.KeyIndex = authority.KeyIndex; this.caId = authority.Id; - cr.Append(caId); + cr.AddUInt64(caId); // public key rsa = RSA.Create();// new RSACryptoServiceProvider(2048); rsa.KeySize = 2048; RSAParameters dRSAKey = rsa.ExportParameters(true); - cr.Append((byte)dRSAKey.Exponent.Length, dRSAKey.Exponent, (ushort)dRSAKey.Modulus.Length, dRSAKey.Modulus, AsymetricEncryptionAlgorithmType.RSA); + cr.AddUInt8((byte)dRSAKey.Exponent.Length) + .AddUInt8Array(dRSAKey.Exponent) + .AddUInt16((ushort)dRSAKey.Modulus.Length) + .AddUInt8Array(dRSAKey.Modulus); publicRawData = cr.ToArray(); @@ -220,9 +226,9 @@ namespace Esiur.Security.Authority try { if (includePrivate) - File.WriteAllBytes(filename, BinaryList.ToBytes((byte)CertificateType.DomainPrivate, publicRawData, signature, privateRawData)); + File.WriteAllBytes(filename, DC.Merge(new byte[] { (byte)CertificateType.DomainPrivate }, publicRawData, signature, privateRawData)); else - File.WriteAllBytes(filename, BinaryList.ToBytes((byte)CertificateType.DomainPublic, publicRawData, signature)); + File.WriteAllBytes(filename, DC.Merge(new byte[] { (byte)CertificateType.DomainPublic }, publicRawData, signature)); return true; } @@ -235,9 +241,9 @@ namespace Esiur.Security.Authority public override byte[] Serialize(bool includePrivate = false) { if (includePrivate) - return BinaryList.ToBytes(publicRawData, signature, privateRawData); + return DC.Merge(publicRawData, signature, privateRawData); else - return BinaryList.ToBytes(publicRawData, signature); + return DC.Merge(publicRawData, signature); } } diff --git a/Esiur/Security/Authority/Session.cs b/Esiur/Security/Authority/Session.cs index 29e8608..c502aeb 100644 --- a/Esiur/Security/Authority/Session.cs +++ b/Esiur/Security/Authority/Session.cs @@ -22,7 +22,7 @@ SOFTWARE. */ using Esiur.Data; -using Esiur.Engine; +using Esiur.Core; using Esiur.Net; using Esiur.Resource; using System; diff --git a/Esiur/Security/Authority/UserCertificate.cs b/Esiur/Security/Authority/UserCertificate.cs index d538bcb..6e62678 100644 --- a/Esiur/Security/Authority/UserCertificate.cs +++ b/Esiur/Security/Authority/UserCertificate.cs @@ -170,42 +170,45 @@ namespace Esiur.Security.Authority var cr = new BinaryList(); //id - cr.Append(id); + cr.AddUInt64(id); // ip this.ip = ip; this.ip6 = ip6; - cr.Append(ip); + cr.AddUInt32(ip); if (ip6?.Length == 16) - cr.Append(ip6); + cr.AddUInt8Array(ip6); else - cr.Append(new byte[16]); + cr.AddUInt8Array(new byte[16]); // dates this.issueDate = DateTime.UtcNow; this.expireDate = expireDate; - cr.Append(issueDate, expireDate); + cr.AddDateTime(issueDate) + .AddDateTime(expireDate); // domain this.domainId = domainCertificate.Id; - cr.Append(domainCertificate.Id); + cr.AddUInt64(domainCertificate.Id); this.domain = domainCertificate.Domain; - cr.Append((byte)domainCertificate.Domain.Length, Encoding.ASCII.GetBytes(domainCertificate.Domain)); + cr.AddUInt8((byte)domainCertificate.Domain.Length) + .AddUInt8Array(Encoding.ASCII.GetBytes(domainCertificate.Domain)); // username this.username = username; - cr.Append((byte)(username.Length), Encoding.ASCII.GetBytes(username)); + cr.AddUInt8((byte)(username.Length)) + .AddUInt8Array(Encoding.ASCII.GetBytes(username)); // hash function (SHA1) - cr.Append((byte)((byte)hashFunction << 4));// (byte)0x10); + cr.AddUInt8((byte)((byte)hashFunction << 4));// (byte)0x10); // public key @@ -214,7 +217,10 @@ namespace Esiur.Security.Authority // write public certificate file var key = rsa.ExportParameters(true); - publicRawData = BinaryList.ToBytes((byte)key.Exponent.Length, key.Exponent, (ushort)key.Modulus.Length, key.Modulus); + publicRawData = new BinaryList().AddUInt8((byte)key.Exponent.Length) + .AddUInt8Array(key.Exponent) + .AddUInt16((ushort)key.Modulus.Length) + .AddUInt8Array(key.Modulus).ToArray(); // sign it @@ -231,9 +237,9 @@ namespace Esiur.Security.Authority try { if (includePrivate) - File.WriteAllBytes(filename, BinaryList.ToBytes((byte)CertificateType.DomainPrivate, publicRawData, signature, privateRawData)); + File.WriteAllBytes(filename, DC.Merge(new byte[] { (byte)CertificateType.DomainPrivate }, publicRawData, signature, privateRawData)); else - File.WriteAllBytes(filename, BinaryList.ToBytes((byte)CertificateType.DomainPublic, publicRawData, signature)); + File.WriteAllBytes(filename, DC.Merge(new byte[] { (byte)CertificateType.DomainPublic }, publicRawData, signature)); return true; } @@ -246,9 +252,9 @@ namespace Esiur.Security.Authority public override byte[] Serialize(bool includePrivate = false) { if (includePrivate) - return BinaryList.ToBytes(publicRawData, signature, privateRawData); + return DC.Merge(publicRawData, signature, privateRawData); else - return BinaryList.ToBytes(publicRawData, signature); + return DC.Merge(publicRawData, signature); } } } diff --git a/Esiur/Security/Integrity/SHA256.cs b/Esiur/Security/Integrity/SHA256.cs new file mode 100644 index 0000000..3e2541e --- /dev/null +++ b/Esiur/Security/Integrity/SHA256.cs @@ -0,0 +1,157 @@ +using Esiur.Data; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Esiur.Security.Integrity +{ + public static class SHA256 + { + + static uint RROT(uint n, int d) + { + return (n >> d) | (n << (32 - d)); + } + + public static byte[] Compute(byte[] msg) + { + /* + Note 1: All variables are 32 bit unsigned integers and addition is calculated modulo 2^32 + Note 2: For each round, there is one round constant k[i] and one entry in the message schedule array w[i], 0 ≤ i ≤ 63 + Note 3: The compression function uses 8 working variables, a through h + Note 4: Big-endian convention is used when expressing the constants in this pseudocode, + and when parsing message block data from bytes to words, for example, + the first word of the input message "abc" after padding is 0x61626380 + */ + + // Initialize hash values: + // (first 32 bits of the fractional parts of the square roots of the first 8 primes 2..19): + + var hash = new uint[] { 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 }; + + // Initialize array of round constants: + // (first 32 bits of the fractional parts of the cube roots of the first 64 primes 2..311): + var k = new uint[] { + 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, + 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, + 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, + 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, + 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, + 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, + 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, + 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 }; + + + + // Pre-processing: + // begin with the original message of length L bits + ulong L = (ulong)msg.Length * 8; + + // append a single '1' bit + // append K '0' bits, where K is the minimum number >= 0 such that L + 1 + K + 64 is a multiple of 512 + + var K = 512 - ((L + 1 + 64) % 512); + + if (K == 512) + K = 0; + + var paddingLength = (K + 1) / 8; + var paddingBytes = new byte[paddingLength]; + paddingBytes[0] = 0x80; + + var data = new BinaryList().AddUInt8Array(msg).AddUInt8Array(paddingBytes).AddUInt64(L).ToArray(); + + + + // append L as a 64-bit big-endian integer, making the total post-processed length a multiple of 512 bits + + // Process the message in successive 512-bit chunks: + // break message into 512-bit chunks + // for each chunk + + for (var chunk = 0; chunk < data.Length; chunk += 64) + { + // create a 64-entry message schedule array w[0..63] of 32-bit words + // (The initial values in w[0..63] don't matter, so many implementations zero them here) + // copy chunk into first 16 words w[0..15] of the message schedule array + + var w = new uint[64]; + for (var i = 0; i < 16; i++) + w[i] = data.GetUInt32((uint)(chunk + (i * 4))); + + //for(var i = 16; i < 64; i++) + // w[i] = 0; + + // Extend the first 16 words into the remaining 48 words w[16..63] of the message schedule array: + // for i from 16 to 63 + // s0 := (w[i-15] rightrotate 7) xor (w[i-15] rightrotate 18) xor (w[i-15] rightshift 3) + // s1 := (w[i-2] rightrotate 17) xor (w[i-2] rightrotate 19) xor (w[i-2] rightshift 10) + // w[i] := w[i-16] + s0 + w[i-7] + s1 + + for (var i = 16; i < 64; i++) + { + var s0 = SHA256.RROT(w[i - 15], 7) ^ SHA256.RROT(w[i - 15], 18) ^ (w[i - 15] >> 3); + var s1 = SHA256.RROT(w[i - 2], 17) ^ SHA256.RROT(w[i - 2], 19) ^ (w[i - 2] >> 10); + w[i] = w[i - 16] + s0 + w[i - 7] + s1; + } + + // Initialize working variables to current hash value: + var a = hash[0]; + var b = hash[1]; + var c = hash[2]; + var d = hash[3]; + var e = hash[4]; + var f = hash[5]; + var g = hash[6]; + var h = hash[7]; + + + // Compression function main loop: + for (var i = 0; i < 64; i++) + { + var S1 = SHA256.RROT(e, 6) ^ SHA256.RROT(e, 11) ^ SHA256.RROT(e, 25); + var ch = (e & f) ^ ((~e) & g); + var temp1 = h + S1 + ch + k[i] + w[i]; + var S0 = SHA256.RROT(a, 2) ^ SHA256.RROT(a, 13) ^ SHA256.RROT(a, 22); + var maj = (a & b) ^ (a & c) ^ (b & c); + uint temp2 = S0 + maj; + + h = g; + g = f; + f = e; + e = (d + temp1) >> 0; + d = c; + c = b; + b = a; + a = (temp1 + temp2) >> 0; + } + + // Add the compressed chunk to the current hash value: + + hash[0] = (hash[0] + a) >> 0; + hash[1] = (hash[1] + b) >> 0; + hash[2] = (hash[2] + c) >> 0; + hash[3] = (hash[3] + d) >> 0; + hash[4] = (hash[4] + e) >> 0; + hash[5] = (hash[5] + f) >> 0; + hash[6] = (hash[6] + g) >> 0; + hash[7] = (hash[7] + h) >> 0; + + + } + + + + + // Produce the final hash value (big-endian): + //digest := hash := h0 append h1 append h2 append h3 append h4 append h5 append h6 append h7 + + var results = new BinaryList(); + for (var i = 0; i < 8; i++) + results.AddUInt32(hash[i]); + + + return results.ToArray(); + } + } +} diff --git a/Esiur/Security/Membership/IMembership.cs b/Esiur/Security/Membership/IMembership.cs index cf2988c..c6d4799 100644 --- a/Esiur/Security/Membership/IMembership.cs +++ b/Esiur/Security/Membership/IMembership.cs @@ -29,7 +29,7 @@ using System.Text; using System.Threading.Tasks; using Esiur.Data; using Esiur.Net.IIP; -using Esiur.Engine; +using Esiur.Core; using Esiur.Security.Authority; using Esiur.Resource; diff --git a/Esiur/Security/Membership/IUser.cs b/Esiur/Security/Membership/IUser.cs index 11f7cd2..e354baf 100644 --- a/Esiur/Security/Membership/IUser.cs +++ b/Esiur/Security/Membership/IUser.cs @@ -22,7 +22,7 @@ SOFTWARE. */ -using Esiur.Engine; +using Esiur.Core; using System; using System.Collections.Generic; using System.Linq; diff --git a/Esiur/Security/Permissions/IPermissionsManager.cs b/Esiur/Security/Permissions/IPermissionsManager.cs index 9e94fea..fca2d65 100644 --- a/Esiur/Security/Permissions/IPermissionsManager.cs +++ b/Esiur/Security/Permissions/IPermissionsManager.cs @@ -23,7 +23,7 @@ SOFTWARE. */ using Esiur.Data; -using Esiur.Engine; +using Esiur.Core; using Esiur.Net; using Esiur.Resource; using Esiur.Resource.Template; diff --git a/Esiur/Security/Permissions/ParentalPermissionsManager.cs b/Esiur/Security/Permissions/ParentalPermissionsManager.cs index dff3bde..fb878d6 100644 --- a/Esiur/Security/Permissions/ParentalPermissionsManager.cs +++ b/Esiur/Security/Permissions/ParentalPermissionsManager.cs @@ -26,7 +26,7 @@ using System; using System.Collections.Generic; using System.Text; using Esiur.Data; -using Esiur.Engine; +using Esiur.Core; using Esiur.Resource; using Esiur.Resource.Template; using Esiur.Security.Authority; diff --git a/Esiur/Security/Permissions/UserPermissionsManager.cs b/Esiur/Security/Permissions/UserPermissionsManager.cs index c2f58d5..e318bb0 100644 --- a/Esiur/Security/Permissions/UserPermissionsManager.cs +++ b/Esiur/Security/Permissions/UserPermissionsManager.cs @@ -26,7 +26,7 @@ using System; using System.Collections.Generic; using System.Text; using Esiur.Data; -using Esiur.Engine; +using Esiur.Core; using Esiur.Resource; using Esiur.Resource.Template; using Esiur.Security.Authority; diff --git a/Esiur/Stores/MemoryStore.cs b/Esiur/Stores/MemoryStore.cs index 30a2ea7..f80d048 100644 --- a/Esiur/Stores/MemoryStore.cs +++ b/Esiur/Stores/MemoryStore.cs @@ -4,7 +4,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -using Esiur.Engine; +using Esiur.Core; using Esiur.Data; using Esiur.Resource.Template; diff --git a/Test/MyMembership.cs b/Test/MyMembership.cs index 0a72bd5..2c78096 100644 --- a/Test/MyMembership.cs +++ b/Test/MyMembership.cs @@ -1,5 +1,5 @@ using Esiur.Data; -using Esiur.Engine; +using Esiur.Core; using Esiur.Resource; using Esiur.Security.Authority; using Esiur.Security.Membership; diff --git a/Test/MyObject.cs b/Test/MyObject.cs index 1317bb7..9e6f44d 100644 --- a/Test/MyObject.cs +++ b/Test/MyObject.cs @@ -1,5 +1,5 @@ using Esiur.Data; -using Esiur.Engine; +using Esiur.Core; using Esiur.Net.IIP; using Esiur.Resource; using System; @@ -29,9 +29,9 @@ namespace Test [ResourceFunction] - public int Add(int value) + public int Add(int? value) { - Level += value; + Level += (int)value; LevelUp?.Invoke(null, "going up", value); return Level; } diff --git a/Test/Program.cs b/Test/Program.cs index b5d70c7..2b53bf6 100644 --- a/Test/Program.cs +++ b/Test/Program.cs @@ -23,7 +23,7 @@ SOFTWARE. */ using Esiur.Data; -using Esiur.Engine; +using Esiur.Core; using Esiur.Net.HTTP; using Esiur.Net.IIP; using Esiur.Net.Sockets; @@ -35,61 +35,29 @@ using System; using System.Threading; using System.Threading.Tasks; +using Esiur.Security.Integrity; +using System.Linq; + namespace Test { class Program { - static MyObject myObject; - static DistributedResource remoteObject; - + static MyObject localObject; + static IResource remoteObject; + static async Task Main(string[] args) { - - //AsyncContext.Run(() => ()); + + Warehouse.Protocols.Add("iip", () => new DistributedConnection()); // Create stores to keep objects. var system = Warehouse.New("system"); var remote = Warehouse.New("remote"); var mongo = Warehouse.New("db"); - /* - var system = await Warehouse.Get("mem://system").Task; - var remote = await Warehouse.Get("mem://remote").Task; - var mongo = await Warehouse.Get("mongo://db").Task; - var iip = await Warehouse.Get("iip://:5000").Task; - var iws = await Warehouse.Get("iipows://:5001", new Structure() { ["iip"] = iip }).Task; - */ - - var ok = await Warehouse.Open(); - - - // Open the warehouse - - - // Create new object if the store is empty - if (mongo.Count == 0) - myObject = Warehouse.New("my", mongo, null, - new UserPermissionsManager(new Structure() - { - ["demo@localhost"] = new Structure() - { - ["Subtract"] = new Structure { ["Execute"] = "yes" }, - ["Stream"] = new Structure { ["Execute"] = "yes" }, - ["_attach"] = "yes", - ["_get_attributes"] = "yes", - ["_set_attributes"] = "yes", - } - })); - else - myObject =(MyObject) (await Warehouse.Get("db/my"));//.Then((o) => { myObject = (MyObject)o; }); - - - //var obj = ProxyObject.(); - //Warehouse.Put(obj, "dd", system); - //obj.Level2= 33; // Create new distributed server object var iip = Warehouse.New("iip", system); @@ -108,6 +76,44 @@ namespace Test wsOverHttp.DistributedServer = iip; + + /* + var system = await Warehouse.Get("mem://system").Task; + var remote = await Warehouse.Get("mem://remote").Task; + var mongo = await Warehouse.Get("mongo://db").Task; + var iip = await Warehouse.Get("iip://:5000").Task; + var iws = await Warehouse.Get("iipows://:5001", new Structure() { ["iip"] = iip }).Task; + */ + + var ok = await Warehouse.Open(); + + + // Open the warehouse + + + // Create new object if the store is empty + if (mongo.Count == 0) + localObject = Warehouse.New("my", mongo, null, + new UserPermissionsManager(new Structure() + { + ["demo@localhost"] = new Structure() + { + ["Subtract"] = new Structure { ["Execute"] = "yes" }, + ["Stream"] = new Structure { ["Execute"] = "yes" }, + ["_attach"] = "yes", + ["_get_attributes"] = "yes", + ["_set_attributes"] = "yes", + } + })); + else + localObject = (MyObject)(await Warehouse.Get("db/my"));//.Then((o) => { myObject = (MyObject)o; }); + + + //var obj = ProxyObject.(); + //Warehouse.Put(obj, "dd", system); + //obj.Level2= 33; + + Warehouse.StoreConnected += (store, name) => { if (store.Instance.Parents.Contains(iip)) @@ -124,7 +130,7 @@ namespace Test }; // Start testing - // TestClient(); + TestClient(); var running = true; @@ -143,83 +149,63 @@ namespace Test }); else { - myObject.Level = 88; - Console.WriteLine(myObject.Name + " " + myObject.Level ); + localObject.Level = 88; + Console.WriteLine(localObject.Name + " " + localObject.Level); } } } - private static void TestClient() + private static async void TestClient() { - //return; - // Create a new client - var client = new DistributedConnection(new TCPSocket("localhost", 5000), "localhost", "demo", "1234"); - // Put the client in our memory store - var remote = Warehouse.GetStore("remote"); - Warehouse.Put(client, "Endpoint", remote); + remoteObject = await Warehouse.Get("iip://localhost:5000/db/my", new Structure() { ["username"] = "demo", ["password"] = "1234" }); + dynamic x = remoteObject; - client.OnReady += async (c) => + + Console.WriteLine("My Name is: " + x.Name); + x.Name = "Hamoo"; + x.LevelUp += new DistributedResourceEvent((sender, parameters) => { - // Get remote object from the server. - //remoteObject = await client.Get("db/my").Task as DistributedResource; + Console.WriteLine("LevelUp " + parameters[0] + " " + parameters[1]); + }); - dynamic x = remoteObject; + x.LevelDown += new DistributedResourceEvent((sender, parameters) => + { + Console.WriteLine("LevelUp " + parameters[0] + " " + parameters[1]); + }); - Console.WriteLine("My Name is: " + x.Name); - x.Name = "Hamoo"; - x.LevelUp += new DistributedResourceEvent((sender, parameters) => - { - Console.WriteLine("LevelUp " + parameters[0] + " " + parameters[1]); - }); + + (x.Stream(10) as AsyncReply).Then(r => + { + Console.WriteLine("Stream ended: " + r); + }).Chunk(r => + { + Console.WriteLine("Chunk..." + r); + }).Progress((t, v, m) => Console.WriteLine("Processing {0}/{1}", v, m)); - x.LevelDown += new DistributedResourceEvent((sender, parameters) => - { - Console.WriteLine("LevelUp " + parameters[0] + " " + parameters[1]); - }); + var rt = await x.Subtract(10); - (x.Stream(10) as AsyncReply).Then(r => - { - Console.WriteLine("Stream ended: " + r); - }).Chunk(r => - { - Console.WriteLine("Chunk..." + r); - }).Progress((t, v, m) => Console.WriteLine("Processing {0}/{1}", v, m)); - var rt = await x.Subtract(10).Task; + Console.WriteLine(rt); + // Getting object record + (remoteObject.Instance.Store as DistributedConnection).GetRecord(remoteObject, DateTime.Now - TimeSpan.FromDays(1), DateTime.Now).Then(record => + { + Console.WriteLine("Records received: " + record.Count); + }); - //var rt2 = await x.Add(10).Task; + //var timer = new Timer(T_Elapsed, null, 5000, 5000); + - Console.WriteLine(rt); - /* - (x.Subtract(10) as AsyncReply).Then((r) => - { - Console.WriteLine("Subtracted: " + r + " " + x.Level); - }).Error((ex) => - { - Console.WriteLine("Exception " + ex.Code + " " + ex.Message); - }); - - // Getting object record - client.GetRecord(remoteObject, DateTime.Now - TimeSpan.FromDays(1), DateTime.Now).Then(record => - { - Console.WriteLine("Records received: " + record.Count); - }); - - var t = new Timer(T_Elapsed, null, 5000, 5000); - */ - }; } private static void T_Elapsed(object state) { - myObject.Level++; + localObject.Level++; dynamic o = remoteObject; - Console.WriteLine(myObject.Level + " " + o.Level + o.Me.Me.Level); - Console.WriteLine(o.Info.ToString()); + Console.WriteLine(localObject.Level + " " + o.Level + o.Me.Me.Level); } } }