mirror of
				https://github.com/esiur/esiur-dotnet.git
				synced 2025-10-29 07:10:29 +00:00 
			
		
		
		
	1.2.7
This commit is contained in:
		| @@ -3,21 +3,21 @@ using System.Collections.Generic; | ||||
| using System.Runtime.CompilerServices; | ||||
| using System.Text; | ||||
| 
 | ||||
| namespace Esiur.Engine | ||||
| namespace Esiur.Core | ||||
| { | ||||
|     public class AsyncAwaiter<T> : INotifyCompletion | ||||
|     { | ||||
|         Action callback = null; | ||||
|         T result; | ||||
|         public Action callback = null; | ||||
|         public T result; | ||||
|         private bool completed; | ||||
| 
 | ||||
|         public AsyncAwaiter(AsyncReply<T> 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; | ||||
|         } | ||||
| 
 | ||||
| 
 | ||||
| @@ -28,7 +28,7 @@ using System.Linq; | ||||
| using System.Text; | ||||
| using System.Threading.Tasks; | ||||
| 
 | ||||
| namespace Esiur.Engine | ||||
| namespace Esiur.Core | ||||
| { | ||||
|     public class AsyncBag<T>: AsyncReply<T[]> | ||||
|     { | ||||
| @@ -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 | ||||
|     { | ||||
| 
 | ||||
| @@ -28,7 +28,7 @@ using System.Linq; | ||||
| using System.Text; | ||||
| using System.Threading.Tasks; | ||||
| 
 | ||||
| namespace Esiur.Engine | ||||
| namespace Esiur.Core | ||||
| { | ||||
|     public class AsyncQueue<T> : AsyncReply<T> | ||||
|     { | ||||
| @@ -32,7 +32,7 @@ using System.Reflection; | ||||
| using System.Threading; | ||||
| using System.Runtime.CompilerServices; | ||||
| 
 | ||||
| namespace Esiur.Engine | ||||
| namespace Esiur.Core | ||||
| { | ||||
|     public class AsyncReply<T>: IAsyncReply<T> | ||||
|     { | ||||
| @@ -53,8 +53,6 @@ namespace Esiur.Engine | ||||
|         protected bool resultReady = false; | ||||
|         AsyncException exception; | ||||
| 
 | ||||
|         TaskCompletionSource<object> tcs = new TaskCompletionSource<object>(); | ||||
| 
 | ||||
| 
 | ||||
|         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<T>(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; | ||||
|         } | ||||
|      | ||||
| @@ -28,7 +28,7 @@ using System.Linq; | ||||
| using System.Text; | ||||
| using System.Threading.Tasks; | ||||
| 
 | ||||
| namespace Esiur.Engine | ||||
| namespace Esiur.Core | ||||
| { | ||||
|     public class AsyncReply | ||||
|     { | ||||
| @@ -2,7 +2,7 @@ | ||||
| using System.Collections.Generic; | ||||
| using System.Text; | ||||
| 
 | ||||
| namespace Esiur.Engine | ||||
| namespace Esiur.Core | ||||
| { | ||||
|     public enum ErrorType | ||||
|     { | ||||
							
								
								
									
										37
									
								
								Esiur/Core/ExceptionCode.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								Esiur/Core/ExceptionCode.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -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 | ||||
|     } | ||||
| } | ||||
| @@ -3,7 +3,7 @@ using System.Collections.Generic; | ||||
| using System.Runtime.CompilerServices; | ||||
| using System.Text; | ||||
| 
 | ||||
| namespace Esiur.Engine | ||||
| namespace Esiur.Core | ||||
| { | ||||
|     public interface IAsyncReply<out T>//IAsyncEnumerator<T>  | ||||
|     {    | ||||
| @@ -12,7 +12,7 @@ namespace Esiur.Engine | ||||
|         IAsyncReply<T> Progress(Action<ProgressType, int, int> callback); | ||||
|         IAsyncReply<T> Chunk(Action<T> 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); | ||||
| 
 | ||||
| @@ -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); | ||||
| 
 | ||||
| @@ -29,7 +29,7 @@ using System.Linq; | ||||
| using System.Text; | ||||
| using System.Threading.Tasks; | ||||
| 
 | ||||
| namespace Esiur.Engine | ||||
| namespace Esiur.Core | ||||
| { | ||||
|     public enum LogType | ||||
|     { | ||||
| @@ -2,7 +2,7 @@ | ||||
| using System.Collections.Generic; | ||||
| using System.Text; | ||||
| 
 | ||||
| namespace Esiur.Engine | ||||
| namespace Esiur.Core | ||||
| { | ||||
|     public enum ProgressType | ||||
|     { | ||||
| @@ -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 | ||||
|   | ||||
| @@ -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 | ||||
|     /// </summary> | ||||
|     public class BinaryList | ||||
|     { | ||||
|         private List<byte> held = new List<byte>(); | ||||
|         private List<byte> list = new List<byte>(); | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Create an instance of BinaryList | ||||
| @@ -46,18 +47,19 @@ namespace Esiur.Data | ||||
|  | ||||
|         } | ||||
|  | ||||
|         /* | ||||
|         /// <summary> | ||||
|         /// Converts parameters to binary in same order | ||||
|         /// </summary> | ||||
|         /// <param name="values">Variables to convert</param> | ||||
|         public static byte[] ToBytes(params object[] values) | ||||
|         { | ||||
|             var held = new List<byte>(); | ||||
|             var list = new List<byte>(); | ||||
|  | ||||
|             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(); | ||||
|         } | ||||
|         | ||||
|  | ||||
|         /// <summary> | ||||
|         /// 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; | ||||
|             } | ||||
|         } | ||||
|         /// <summary> | ||||
|         /// Convert the list to an array of bytes | ||||
|         /// </summary> | ||||
|         /// <returns>Bytes array</returns> | ||||
|         public byte[] ToArray() | ||||
|         { | ||||
|             return held.ToArray(); | ||||
|             return list.ToArray(); | ||||
|         } | ||||
|  | ||||
|         public virtual IAsyncReply<object[]> Done() | ||||
|         { | ||||
|             return null; | ||||
|             //  | ||||
|         } | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -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 | ||||
|         /// <param name="resource">Resource to check.</param> | ||||
|         /// <param name="connection">DistributedConnection to check if the resource is local to it.</param> | ||||
|         /// <returns>True, if the resource owner is the given connection, otherwise False.</returns> | ||||
|         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 | ||||
|         /// <returns>Array of bytes in the network byte order.</returns> | ||||
|         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 | ||||
|         /// <param name="history">History</param> | ||||
|         /// <param name="connection">DistributedConnection is required to fetch resources.</param> | ||||
|         /// <returns></returns> | ||||
|         public static byte[] ComposeHistory(KeyList<PropertyTemplate, PropertyValue[]> history, DistributedConnection  connection, bool prependLength = false) | ||||
|         public static byte[] ComposeHistory(KeyList<PropertyTemplate, PropertyValue[]> 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 | ||||
|         /// <returns>True, if <paramref name="type"/> implements <paramref name="iface"/>.</returns> | ||||
|         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; | ||||
|         } | ||||
|  | ||||
|   | ||||
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							| @@ -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 | ||||
| { | ||||
|   | ||||
| @@ -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 | ||||
| { | ||||
|   | ||||
| @@ -7,11 +7,12 @@ | ||||
|     <PackageLicenseUrl>https://github.com/esiur/esiur-dotnet/blob/master/LICENSE</PackageLicenseUrl> | ||||
|     <PackageProjectUrl>http://www.esiur.com</PackageProjectUrl> | ||||
|     <GeneratePackageOnBuild>true</GeneratePackageOnBuild> | ||||
|     <Version>1.2.6</Version> | ||||
|     <Version>1.2.7</Version> | ||||
|     <RepositoryUrl>https://github.com/esiur/esiur-dotnet</RepositoryUrl> | ||||
|     <Authors>Ahmed Kh. Zamil</Authors> | ||||
|     <AssemblyVersion>1.2.5.0</AssemblyVersion> | ||||
|     <AssemblyVersion>1.2.7.0</AssemblyVersion> | ||||
|     <Company>Esiur Foundation</Company> | ||||
|     <FileVersion>1.2.7.0</FileVersion> | ||||
|   </PropertyGroup> | ||||
|  | ||||
|   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> | ||||
| @@ -24,7 +25,7 @@ | ||||
|   </PropertyGroup> | ||||
|  | ||||
|   <ItemGroup> | ||||
|     <Compile Remove="Engine\AsyncReply.cs" /> | ||||
|     <Compile Remove="Core\AsyncReplyNon.cs" /> | ||||
|     <Compile Remove="Net\UDP\UDPServer.cs" /> | ||||
|   </ItemGroup> | ||||
|  | ||||
| @@ -33,7 +34,7 @@ | ||||
|   </ItemGroup> | ||||
|  | ||||
|   <ItemGroup> | ||||
|     <None Include="Engine\AsyncReply.cs" /> | ||||
|     <None Include="Core\AsyncReplyNon.cs" /> | ||||
|     <None Include="Net\UDP\UDPServer.cs" /> | ||||
|   </ItemGroup> | ||||
|  | ||||
|   | ||||
| @@ -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; | ||||
|  | ||||
|   | ||||
| @@ -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; | ||||
|   | ||||
| @@ -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; | ||||
|   | ||||
| @@ -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 | ||||
|   | ||||
| @@ -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 | ||||
|   | ||||
| @@ -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; | ||||
|   | ||||
| @@ -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 | ||||
| { | ||||
|   | ||||
| @@ -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 | ||||
| { | ||||
|   | ||||
| @@ -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<bool> openReply; | ||||
|  | ||||
|         byte[] localPassword; | ||||
|         byte[] localNonce, remoteNonce; | ||||
|  | ||||
| @@ -109,8 +111,11 @@ namespace Esiur.Net.IIP | ||||
|         /// Send data to the other end as parameters | ||||
|         /// </summary> | ||||
|         /// <param name="values">Values will be converted to bytes then sent.</param> | ||||
|         internal void SendParams(params object[] values) | ||||
|         internal SendList SendParams(IAsyncReply<object[]> 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()); | ||||
|             */ | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
| @@ -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 | ||||
|         /// <returns></returns> | ||||
|         public AsyncReply<bool> 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<bool>(); | ||||
|                     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<bool>(); | ||||
|         } | ||||
|  | ||||
| @@ -720,7 +823,9 @@ namespace Esiur.Net.IIP | ||||
|         /// <returns></returns> | ||||
|         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; | ||||
|         } | ||||
|  | ||||
|   | ||||
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							| @@ -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<object>(); | ||||
|  | ||||
| @@ -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<object>(); | ||||
|  | ||||
|             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; | ||||
|  | ||||
|   | ||||
| @@ -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"); | ||||
|              | ||||
|         } | ||||
|  | ||||
|   | ||||
| @@ -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; | ||||
|   | ||||
| @@ -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; | ||||
|   | ||||
| @@ -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 | ||||
| { | ||||
|   | ||||
| @@ -23,7 +23,7 @@ SOFTWARE. | ||||
| */ | ||||
|  | ||||
| using Esiur.Data; | ||||
| using Esiur.Engine; | ||||
| using Esiur.Core; | ||||
| using Esiur.Misc; | ||||
| using Esiur.Net.Packets; | ||||
| using System; | ||||
|   | ||||
							
								
								
									
										26
									
								
								Esiur/Net/SendList.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								Esiur/Net/SendList.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -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<object[]> reply; | ||||
|  | ||||
|         public SendList(NetworkConnection connection, IAsyncReply<object[]> reply) | ||||
|         { | ||||
|             this.reply = reply; | ||||
|             this.connection = connection; | ||||
|         } | ||||
|  | ||||
|         public override IAsyncReply<object[]> Done() | ||||
|         { | ||||
|             connection.Send(this.ToArray()); | ||||
|             return reply; | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -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<bool> Connect(string hostname, ushort port); | ||||
|         bool Begin(); | ||||
|         //ISocket Accept(); | ||||
|         AsyncReply<ISocket> Accept(); | ||||
|   | ||||
| @@ -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<bool> Connect(string hostname, ushort port) | ||||
|         { | ||||
|             var rt = new AsyncReply<bool>(); | ||||
|  | ||||
|             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) | ||||
|   | ||||
| @@ -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<bool> Connect(string hostname, ushort port) | ||||
|         { | ||||
|             var rt = new AsyncReply<bool>(); | ||||
|  | ||||
|             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); | ||||
|  | ||||
|   | ||||
| @@ -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<bool> Connect(string hostname, ushort port) | ||||
|         { | ||||
|             throw new NotImplementedException(); | ||||
|         } | ||||
|   | ||||
| @@ -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 | ||||
|   | ||||
| @@ -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; | ||||
|  | ||||
|   | ||||
| @@ -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 | ||||
|   | ||||
| @@ -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 | ||||
| { | ||||
|   | ||||
| @@ -23,7 +23,7 @@ SOFTWARE. | ||||
| */ | ||||
|  | ||||
| using Esiur.Data; | ||||
| using Esiur.Engine; | ||||
| using Esiur.Core; | ||||
| using Esiur.Resource.Template; | ||||
| using System; | ||||
| using System.Collections.Generic; | ||||
|   | ||||
| @@ -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 | ||||
|         /// <returns></returns> | ||||
|         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); | ||||
|   | ||||
| @@ -24,7 +24,7 @@ SOFTWARE. | ||||
| using System; | ||||
| using System.Collections.Generic; | ||||
| using System.Text; | ||||
| using Esiur.Engine; | ||||
| using Esiur.Core; | ||||
|  | ||||
| namespace Esiur.Resource | ||||
| { | ||||
|   | ||||
| @@ -22,7 +22,7 @@ SOFTWARE. | ||||
|  | ||||
| */ | ||||
| using Esiur.Data; | ||||
| using Esiur.Engine; | ||||
| using Esiur.Core; | ||||
| using Esiur.Net.IIP; | ||||
| using Esiur.Security.Authority; | ||||
| using System; | ||||
|   | ||||
| @@ -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; | ||||
|   | ||||
| @@ -32,7 +32,7 @@ namespace Esiur.Resource | ||||
| { | ||||
|     public enum ResourceTrigger : int | ||||
|     { | ||||
|         Loaded = 0, | ||||
|         Open = 0, | ||||
|         Initialize, | ||||
|         Terminate, | ||||
|         Configure, | ||||
|   | ||||
| @@ -23,7 +23,7 @@ SOFTWARE. | ||||
| */ | ||||
|  | ||||
| using Esiur.Data; | ||||
| using Esiur.Engine; | ||||
| using Esiur.Core; | ||||
| using System; | ||||
| using System.Collections.Generic; | ||||
| using System.Linq; | ||||
|   | ||||
| @@ -6,8 +6,8 @@ namespace Esiur.Resource | ||||
| { | ||||
|     public enum StorageMode : byte | ||||
|     { | ||||
|         Volatile = 0, | ||||
|         NonVolatile, | ||||
|         Volatile, | ||||
|         Recordable | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -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(); | ||||
|         } | ||||
|  | ||||
|  | ||||
|   | ||||
| @@ -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(); | ||||
|         } | ||||
|  | ||||
|  | ||||
|   | ||||
| @@ -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) | ||||
|   | ||||
| @@ -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(); | ||||
|         } | ||||
|  | ||||
|   | ||||
| @@ -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<string, IStore> protocols = new KeyList<string, IStore>(); | ||||
|         public static KeyList<string, Func<IStore>> Protocols { get; } = new KeyList<string, Func<IStore>>(); | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Get a store by its name. | ||||
| @@ -277,15 +277,15 @@ namespace Esiur.Resource | ||||
|  | ||||
|                 var rt = new AsyncReply<IResource>(); | ||||
|  | ||||
|                 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); | ||||
|  | ||||
|         } | ||||
|   | ||||
| @@ -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; | ||||
|         } | ||||
|   | ||||
| @@ -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; | ||||
|   | ||||
| @@ -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); | ||||
|         } | ||||
|  | ||||
|     } | ||||
|   | ||||
| @@ -22,7 +22,7 @@ SOFTWARE. | ||||
|  | ||||
| */ | ||||
| using Esiur.Data; | ||||
| using Esiur.Engine; | ||||
| using Esiur.Core; | ||||
| using Esiur.Net; | ||||
| using Esiur.Resource; | ||||
| using System; | ||||
|   | ||||
| @@ -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); | ||||
|         } | ||||
|     } | ||||
| } | ||||
|   | ||||
							
								
								
									
										157
									
								
								Esiur/Security/Integrity/SHA256.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										157
									
								
								Esiur/Security/Integrity/SHA256.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -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(); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -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; | ||||
|  | ||||
|   | ||||
| @@ -22,7 +22,7 @@ SOFTWARE. | ||||
|  | ||||
| */ | ||||
|  | ||||
| using Esiur.Engine; | ||||
| using Esiur.Core; | ||||
| using System; | ||||
| using System.Collections.Generic; | ||||
| using System.Linq; | ||||
|   | ||||
| @@ -23,7 +23,7 @@ SOFTWARE. | ||||
| */ | ||||
|  | ||||
| using Esiur.Data; | ||||
| using Esiur.Engine; | ||||
| using Esiur.Core; | ||||
| using Esiur.Net; | ||||
| using Esiur.Resource; | ||||
| using Esiur.Resource.Template; | ||||
|   | ||||
| @@ -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; | ||||
|   | ||||
| @@ -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; | ||||
|   | ||||
| @@ -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; | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user