diff --git a/Esiur/Core/AsyncReply.cs b/Esiur/Core/AsyncReply.cs index b9d5d37..e5d99cf 100644 --- a/Esiur/Core/AsyncReply.cs +++ b/Esiur/Core/AsyncReply.cs @@ -31,6 +31,7 @@ using Esiur.Resource; using System.Reflection; using System.Threading; using System.Runtime.CompilerServices; +using System.Diagnostics; namespace Esiur.Core { @@ -51,19 +52,25 @@ namespace Esiur.Core //List awaiters = new List(); - // object callbacksLock = new object(); + object asyncLock = new object(); + //public Timer timeout;// = new Timer() protected bool resultReady = false; AsyncException exception; - + StackTrace trace; AutoResetEvent mutex = new AutoResetEvent(false); + public static int MaxId; + + public int Id; + public bool Ready { get { return resultReady; } } + public T Wait() { @@ -71,13 +78,14 @@ namespace Esiur.Core return result; if (Debug) - Console.WriteLine($"AsyncReply: {GetHashCode()} Wait"); + Console.WriteLine($"AsyncReply: {Id} Wait"); //mutex = new AutoResetEvent(false); mutex.WaitOne(); if (Debug) - Console.WriteLine($"AsyncReply: {GetHashCode()} Wait ended"); + Console.WriteLine($"AsyncReply: {Id} Wait ended"); + return result; } @@ -88,33 +96,51 @@ namespace Esiur.Core get { return result; } } + public IAsyncReply Then(Action callback) { //lock (callbacksLock) //{ + lock (asyncLock) + { + trace = new StackTrace(); + + if (resultReady) + { + if (Debug) + Console.WriteLine($"AsyncReply: {Id} Then ready"); + + callback(result); + return this; + } + + + //timeout = new Timer(x => + //{ + // // Get calling method name + // Console.WriteLine(trace.GetFrame(1).GetMethod().Name); + + // var tr = String.Join("\r\n", trace.GetFrames().Select(f => f.GetMethod().Name)); + // timeout.Dispose(); + + // tr = trace.ToString(); + // throw new Exception("Request timeout " + Id); + //}, null, 15000, 0); - if (resultReady) - { if (Debug) - Console.WriteLine($"AsyncReply: {GetHashCode()} Then ready"); + Console.WriteLine($"AsyncReply: {Id} Then pending"); + + + + callbacks.Add(callback); - callback(result); return this; } - - - if (Debug) - Console.WriteLine($"AsyncReply: {GetHashCode()} Then pending"); - - - - callbacks.Add(callback); - - return this; - //} } + + public IAsyncReply Error(Action callback) { // lock (callbacksLock) @@ -149,37 +175,37 @@ namespace Esiur.Core public void Trigger(object result) { + lock (asyncLock) + { + //timeout?.Dispose(); - if (Debug) - Console.WriteLine($"AsyncReply: {GetHashCode()} Trigger"); + if (Debug) + Console.WriteLine($"AsyncReply: {Id} Trigger"); + + if (resultReady) + return; + + this.result = (T)result; + + resultReady = true; + + //if (mutex != null) + mutex.Set(); + + foreach (var cb in callbacks) + cb((T)result); - //lock (callbacksLock) - //{ - if (resultReady) - return; - - this.result = (T)result; - - resultReady = true; - - //if (mutex != null) - mutex.Set(); - - foreach (var cb in callbacks) - cb((T)result); - - - if (Debug) - Console.WriteLine($"AsyncReply: {GetHashCode()} Trigger ended"); - - - //} + if (Debug) + Console.WriteLine($"AsyncReply: {Id} Trigger ended"); + } } public void TriggerError(Exception exception) { + //timeout?.Dispose(); + if (resultReady) return; @@ -201,6 +227,8 @@ namespace Esiur.Core public void TriggerProgress(ProgressType type, int value, int max) { + //timeout?.Dispose(); + if (resultReady) return; @@ -215,6 +243,9 @@ namespace Esiur.Core public void TriggerChunk(object value) { + + //timeout?.Dispose(); + if (resultReady) return; @@ -235,14 +266,17 @@ namespace Esiur.Core public AsyncReply() { - //this.Debug = true; + // this.Debug = true; + Id = MaxId++; } public AsyncReply(T result) { - //this.Debug = true; + // this.Debug = true; resultReady = true; this.result = result; + + Id = MaxId++; } /* diff --git a/Esiur/Net/IIP/DistributedConnection.cs b/Esiur/Net/IIP/DistributedConnection.cs index 4d290d8..33b6627 100644 --- a/Esiur/Net/IIP/DistributedConnection.cs +++ b/Esiur/Net/IIP/DistributedConnection.cs @@ -285,13 +285,15 @@ namespace Esiur.Net.IIP void init() { - queue.Then((x) => + var q = queue; + q.Then((x) => { if (x.Type == DistributedResourceQueueItem.DistributedResourceQueueItemType.Event) x.Resource._EmitEventByIndex(x.Index, (object[])x.Value); else x.Resource._UpdatePropertyByIndex(x.Index, x.Value); }); + //q.timeout?.Dispose(); var r = new Random(); localNonce = new byte[32]; diff --git a/Esiur/Net/NetworkServer.cs b/Esiur/Net/NetworkServer.cs index 0011767..d03a2d4 100644 --- a/Esiur/Net/NetworkServer.cs +++ b/Esiur/Net/NetworkServer.cs @@ -179,7 +179,9 @@ namespace Esiur.Net listener = socket; // Start accepting - listener.Accept().Then(NewConnection); + var r = listener.Accept(); + r.Then(NewConnection); + //r.timeout?.Dispose(); //var rt = listener.Accept().Then() //thread = new Thread(new System.Threading.ThreadStart(ListenForConnections)); @@ -303,8 +305,11 @@ namespace Esiur.Net // something wrong with the child. } - // Accept more - listener.Accept().Then(NewConnection); + // Accept more + var l = listener.Accept(); + + l.Then(NewConnection); + //l.timeout?.Dispose(); sock.Begin();