From def7512bf80184f50c4c9822f38686a235b41a62 Mon Sep 17 00:00:00 2001 From: hubera01 Date: Fri, 29 Apr 2016 07:51:41 +0200 Subject: [PATCH] Enable automatic children retrieval References #21 --- Lawo.EmberPlusSharp/Model/Consumer.cs | 37 +++++++++++++++------------ Lawo.EmberPlusSharp/Model/Element.cs | 8 ++---- Lawo.EmberPlusSharp/Model/Root.cs | 24 ++++++++++++----- 3 files changed, 40 insertions(+), 29 deletions(-) diff --git a/Lawo.EmberPlusSharp/Model/Consumer.cs b/Lawo.EmberPlusSharp/Model/Consumer.cs index 43e9fc7f..77c06631 100644 --- a/Lawo.EmberPlusSharp/Model/Consumer.cs +++ b/Lawo.EmberPlusSharp/Model/Consumer.cs @@ -97,21 +97,7 @@ public Task SendChangesAsync() /// . public async Task SendAsync() { - if (this.root.HasChanges) - { - MemoryStream stream; - - // TODO: Reuse MemoryStream and EmberWriter for all outgoing messages. - using (stream = new MemoryStream()) - using (var writer = new EmberWriter(stream)) - { - this.root.WriteChanges(writer, this.pendingInvocations); - } - - await this.client.SendMessageAsync(this.emberDataMessage, stream.ToArray()); - } - - if (await this.SendRequestAsync()) + if (await this.SendCoreAsync()) { await this.isVerifiedSource.Task; } @@ -257,6 +243,25 @@ private Consumer(S101Client client, int timeout, ChildrenRetrievalPolicy childre this.client.ConnectionLost += this.receiveQueue.OnConnectionLost; } + private async Task SendCoreAsync() + { + if (this.root.HasChanges) + { + MemoryStream stream; + + // TODO: Reuse MemoryStream and EmberWriter for all outgoing messages. + using (stream = new MemoryStream()) + using (var writer = new EmberWriter(stream)) + { + this.root.WriteChanges(writer, this.pendingInvocations); + } + + await this.client.SendMessageAsync(this.emberDataMessage, stream.ToArray()); + } + + return await this.SendRequestAsync(); + } + private void CancelAutoSendDelay() { if (!this.autoSendDelayCancellationSource.IsCancellationRequested) @@ -307,7 +312,7 @@ private async void SendReceiveLoop() if (await Task.WhenAny(localTask, providerTask) == localTask) { await localTask; - await this.SendAsync(); + await this.SendCoreAsync(); localTask = this.WaitForLocalChangesAsync(); } else diff --git a/Lawo.EmberPlusSharp/Model/Element.cs b/Lawo.EmberPlusSharp/Model/Element.cs index ab11b309..3853dd50 100644 --- a/Lawo.EmberPlusSharp/Model/Element.cs +++ b/Lawo.EmberPlusSharp/Model/Element.cs @@ -154,14 +154,10 @@ internal virtual RetrievalState RetrievalState set { } // Intentionally empty } - internal void ResetRetrievalState() + internal virtual void ResetRetrievalState() { this.RetrievalState = RetrievalState.None; - - if (this.parent != null) - { - this.parent.ResetRetrievalState(); - } + this.parent.ResetRetrievalState(); } internal virtual void SetContext(Context context) diff --git a/Lawo.EmberPlusSharp/Model/Root.cs b/Lawo.EmberPlusSharp/Model/Root.cs index bd417f5c..74cb0538 100644 --- a/Lawo.EmberPlusSharp/Model/Root.cs +++ b/Lawo.EmberPlusSharp/Model/Root.cs @@ -43,13 +43,7 @@ void IParent.SetHasChanges() if (!this.HasChanges) { this.HasChanges = true; - - var handler = this.HasChangesSet; - - if (handler != null) - { - handler(this, EventArgs.Empty); - } + this.RaiseHasChangesSet(); } } @@ -153,6 +147,12 @@ internal sealed override void WriteChanges(EmberWriter writer, IInvocationCollec } } + internal sealed override void ResetRetrievalState() + { + this.RetrievalState = RetrievalState.None; + this.RaiseHasChangesSet(); + } + //////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// Initializes a new instance of the class. @@ -166,6 +166,16 @@ protected Root() //////////////////////////////////////////////////////////////////////////////////////////////////////////////// + private void RaiseHasChangesSet() + { + var handler = this.HasChangesSet; + + if (handler != null) + { + handler(this, EventArgs.Empty); + } + } + private void ReadQualifiedChild(EmberReader reader, ElementType actualType) { reader.ReadAndAssertOuter(GlowQualifiedNode.Path.OuterId);