예제 #1
0
        private static void OnAsyncCallCompleted(IAsyncResult result)
        {
            if (result.CompletedSynchronously)
            {
                return;
            }

            AsyncOperationContext context = (AsyncOperationContext)result.AsyncState;
            Exception             error   = null;

            object[] results = null;
            try
            {
                results = context.EndDelegate(result);
            }
            catch (Exception e)
            {
                if (Fx.IsFatal(e))
                {
                    throw;
                }

                error = e;
            }

            CompleteAsyncCall(context, results, error);
        }
예제 #2
0
 private static void CompleteAsyncCall(AsyncOperationContext context, object[] results, Exception error)
 {
     if (context.CompletionCallback != null)
     {
         InvokeAsyncCompletedEventArgs e = new InvokeAsyncCompletedEventArgs(results, error, false, context.AsyncOperation.UserSuppliedState);
         context.AsyncOperation.PostOperationCompleted(context.CompletionCallback, e);
     }
     else
     {
         context.AsyncOperation.OperationCompleted();
     }
 }
예제 #3
0
        public void EndResolveOperation(IAsyncResult result)
        {
            ResolveMatchesMessageCD1 response = base.Channel.EndResolveOperation(result);
            AsyncOperationContext    context  = (AsyncOperationContext)result.AsyncState;

            if ((response != null) && (response.ResolveMatches != null) && (response.ResolveMatches.ResolveMatch != null))
            {
                this.responseReceiver.ResolveMatchOperation(
                    context.OperationId,
                    DiscoveryUtility.ToDiscoveryMessageSequenceOrNull(response.MessageSequence),
                    response.ResolveMatches.ResolveMatch.ToEndpointDiscoveryMetadata());
            }
            else
            {
                this.responseReceiver.PostResolveCompletedAndRemove(context.OperationId, false, null);
            }
        }
        public void EndProbeOperation(IAsyncResult result)
        {
            ProbeMatchesMessage11 response = base.Channel.EndProbeOperation(result);
            AsyncOperationContext context  = (AsyncOperationContext)result.AsyncState;

            if ((response != null) && (response.ProbeMatches != null))
            {
                this.responseReceiver.ProbeMatchOperation(
                    context.OperationId,
                    DiscoveryUtility.ToDiscoveryMessageSequenceOrNull(response.MessageSequence),
                    DiscoveryUtility.ToEndpointDiscoveryMetadataCollection(response.ProbeMatches),
                    true);
            }
            else
            {
                this.responseReceiver.PostFindCompletedAndRemove(context.OperationId, false, null);
            }
        }
예제 #5
0
        // WARNING: Any changes in the signature/name of the following method ctor must be applied to the
        // ClientClassGenerator.cs as well, otherwise the ClientClassGenerator would generate wrong code.
        protected void InvokeAsync(BeginOperationDelegate beginOperationDelegate, object[] inValues,
                                   EndOperationDelegate endOperationDelegate, SendOrPostCallback operationCompletedCallback, object userState)
        {
            if (beginOperationDelegate == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(beginOperationDelegate));
            }
            if (endOperationDelegate == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(endOperationDelegate));
            }

            AsyncOperation        asyncOperation = AsyncOperationManager.CreateOperation(userState);
            AsyncOperationContext context        = new AsyncOperationContext(asyncOperation, endOperationDelegate, operationCompletedCallback);

            Exception error = null;

            object[]     results = null;
            IAsyncResult result  = null;

            try
            {
                result = beginOperationDelegate(inValues, s_onAsyncCallCompleted, context);
                if (result.CompletedSynchronously)
                {
                    results = endOperationDelegate(result);
                }
            }
            catch (Exception e)
            {
                if (Fx.IsFatal(e))
                {
                    throw;
                }
                error = e;
            }

            if (error != null || result.CompletedSynchronously) /* result cannot be null if error == null */
            {
                CompleteAsyncCall(context, results, error);
            }
        }
 private static void OnAsyncCallCompleted(IAsyncResult result)
 {
     if (!result.CompletedSynchronously)
     {
         AsyncOperationContext <TChannel> asyncState = (AsyncOperationContext <TChannel>)result.AsyncState;
         Exception error   = null;
         object[]  results = null;
         try
         {
             results = asyncState.EndDelegate(result);
         }
         catch (Exception exception2)
         {
             if (Fx.IsFatal(exception2))
             {
                 throw;
             }
             error = exception2;
         }
         ClientBase <TChannel> .CompleteAsyncCall(asyncState, results, error);
     }
 }
        protected void InvokeAsync(BeginOperationDelegate <TChannel> beginOperationDelegate, object[] inValues, EndOperationDelegate <TChannel> endOperationDelegate, SendOrPostCallback operationCompletedCallback, object userState)
        {
            if (beginOperationDelegate == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("beginOperationDelegate");
            }
            if (endOperationDelegate == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("endOperationDelegate");
            }
            AsyncOperationContext <TChannel> state = new AsyncOperationContext <TChannel>(AsyncOperationManager.CreateOperation(userState), endOperationDelegate, operationCompletedCallback);
            Exception error = null;

            object[]     results = null;
            IAsyncResult result  = null;

            try
            {
                result = beginOperationDelegate(inValues, ClientBase <TChannel> .onAsyncCallCompleted, state);
                if (result.CompletedSynchronously)
                {
                    results = endOperationDelegate(result);
                }
            }
            catch (Exception exception2)
            {
                if (Fx.IsFatal(exception2))
                {
                    throw;
                }
                error = exception2;
            }
            if ((error != null) || result.CompletedSynchronously)
            {
                ClientBase <TChannel> .CompleteAsyncCall(state, results, error);
            }
        }
예제 #8
0
        private void DoAsync(SocketAsyncOperation OperationIdentifier, Func <SocketAsyncEventArgs, Boolean> Operation, Func <SocketAsyncEventArgs, Action> ResultToCompleted, Action <Exception> Faulted)
        {
            var Context = new AsyncOperationContext();

            if (!TryLockAsyncOperation(OperationIdentifier, Context))
            {
                Context.Dispose();
                Faulted(new SocketException((int)(SocketError.Shutdown)));
                return;
            }
            var Success = false;

            try
            {
                Context.ResultToCompleted = ResultToCompleted;
                Context.Faulted           = Faulted;
                var TimeoutSeconds = this.TimeoutSeconds;
                if (TimeoutSeconds.HasValue)
                {
                    var IsCompleted = new LockedVariable <Boolean>(false);
                    var Timer       = new Timer(o => { if (!IsCompleted.Check(b => b))
                                                       {
                                                           if (TimedOut != null)
                                                           {
                                                               TimedOut();
                                                           }
                                                       }
                                                }, null, TimeoutSeconds.Value * 1000, Timeout.Infinite);
                    Context.ReleaseAsyncOperation = () =>
                    {
                        IsCompleted.Update(b => true);
                        if (Timer != null)
                        {
                            Timer.Dispose();
                            Timer = null;
                        }
                        ReleaseAsyncOperation(OperationIdentifier);
                    };
                }
                else
                {
                    Context.ReleaseAsyncOperation = () => ReleaseAsyncOperation(OperationIdentifier);
                }
                Success = true;
            }
            finally
            {
                if (!Success)
                {
                    ReleaseAsyncOperation(OperationIdentifier);
                }
            }
            Success = false;
            Exception Exception = null;

            try
            {
                bool willRaiseEvent = Operation(Context.EventArgs);
                if (!willRaiseEvent)
                {
                    QueueUserWorkItem(Context.DoOnCompletion);
                }
                Success = true;
            }
            catch (ObjectDisposedException)
            {
                Exception = new SocketException((int)(SocketError.OperationAborted));
            }
            finally
            {
                if (!Success)
                {
                    Context.ReleaseAsyncOperation();
                }
            }
            if (Exception != null)
            {
                Faulted(Exception);
            }
        }
예제 #9
0
        private Boolean TryLockAsyncOperation(SocketAsyncOperation OperationIdentifier, AsyncOperationContext Context)
        {
            var Success = false;

            while (!Success)
            {
                lock (Lockee)
                {
                    if (IsDisposed)
                    {
                        return(false);
                    }
                    if (AsyncOperations.ContainsKey(OperationIdentifier))
                    {
                        Success = false;
                    }
                    else
                    {
                        AsyncOperations.Add(OperationIdentifier, Context);
                        Success = true;
                    }
                }
                Thread.SpinWait(10);
            }
            return(true);
        }