From a112f2bd1f3283763f62013a32c1999bfb971a6a Mon Sep 17 00:00:00 2001 From: Jose Date: Thu, 1 Feb 2024 10:21:32 +0100 Subject: [PATCH] Review fixes --- csharp/src/Ice/AsyncResult.cs | 480 -------------------------------- csharp/src/Ice/CommunicatorI.cs | 30 -- csharp/src/Ice/ConnectionI.cs | 77 ----- csharp/src/Ice/OutgoingAsync.cs | 189 ------------- csharp/src/Ice/Proxy.cs | 111 -------- csharp/test/Ice/ami/AllTests.cs | 55 ++-- 6 files changed, 20 insertions(+), 922 deletions(-) delete mode 100644 csharp/src/Ice/AsyncResult.cs diff --git a/csharp/src/Ice/AsyncResult.cs b/csharp/src/Ice/AsyncResult.cs deleted file mode 100644 index ed83e4025d2..00000000000 --- a/csharp/src/Ice/AsyncResult.cs +++ /dev/null @@ -1,480 +0,0 @@ -// -// Copyright (c) ZeroC, Inc. All rights reserved. -// - -namespace Ice -{ - /// - /// - /// Callback that requires the application to down-cast the proxy. - /// - /// - public delegate void AsyncCallback(AsyncResult r); - - /// - /// - /// Callback for the successful completion of an operation - /// that returns no data. - /// - /// - public delegate void OnewayCallback(); - - /// - /// - /// Callback to inform when a call has been passed to the local - /// transport. - /// - /// - public delegate void SentCallback(bool sentSynchronously); - - /// - /// - /// Called when an invocation raises an exception. - /// - /// - public delegate void ExceptionCallback(Ice.Exception ex); - - /// - /// - /// - /// - public interface AsyncResult : System.IAsyncResult - { - void cancel(); - - Communicator getCommunicator(); - - Connection getConnection(); - - ObjectPrx getProxy(); - - bool isCompleted_(); - - void waitForCompleted(); - - bool isSent(); - void waitForSent(); - - void throwLocalException(); - - bool sentSynchronously(); - - string getOperation(); - - AsyncResult whenSent(AsyncCallback cb); - AsyncResult whenSent(SentCallback cb); - AsyncResult whenCompleted(ExceptionCallback excb); - } - - public interface AsyncResult : AsyncResult - { - AsyncResult whenCompleted(T cb, ExceptionCallback excb); - - new AsyncResult whenCompleted(ExceptionCallback excb); - new AsyncResult whenSent(SentCallback cb); - } -} - -namespace IceInternal -{ - using System.Diagnostics; - using System.Threading; - - abstract public class AsyncResultI : Ice.AsyncResult - { - public virtual void cancel() - { - Debug.Assert(outgoing_ != null); - outgoing_.cancel(); - } - - public Ice.Communicator getCommunicator() - { - return _communicator; - } - - public virtual Ice.Connection getConnection() - { - return null; - } - - public virtual Ice.ObjectPrx getProxy() - { - return null; - } - - public bool isCompleted_() - { - lock(this) - { - return (state_ & StateDone) != 0; - } - } - - public void waitForCompleted() - { - lock(this) - { - while((state_ & StateDone) == 0) - { - Monitor.Wait(this); - } - } - } - - public bool isSent() - { - lock(this) - { - return (state_ & StateSent) != 0; - } - } - - public void waitForSent() - { - lock(this) - { - while((state_ & StateSent) == 0 && exception_ == null) - { - Monitor.Wait(this); - } - } - } - - public void throwLocalException() - { - lock(this) - { - if(exception_ != null) - { - throw exception_; - } - } - } - - public bool sentSynchronously() - { - Debug.Assert(outgoing_ != null); - return outgoing_.sentSynchronously(); // No lock needed - } - - // - // Implementation of System.IAsyncResult properties - // - public bool IsCompleted - { - get - { - return isCompleted_(); - } - } - - public bool CompletedSynchronously - { - get - { - Debug.Assert(outgoing_ != null); - if(getProxy() != null && getProxy().ice_isTwoway()) - { - return false; - } - return outgoing_.sentSynchronously(); - } - } - - public object AsyncState - { - get - { - return _cookie; // No lock needed, cookie is immutable. - } - } - - public WaitHandle AsyncWaitHandle - { - get - { - lock(this) - { - if(waitHandle_ == null) - { - waitHandle_ = new EventWaitHandle(false, EventResetMode.ManualReset); - } - if((state_ & StateDone) != 0) - { - waitHandle_.Set(); - } - return waitHandle_; - } - } - } - - public OutgoingAsyncBase OutgoingAsync - { - get - { - return outgoing_; - } - } - - public Ice.AsyncResult whenSent(Ice.AsyncCallback cb) - { - lock(this) - { - if(cb == null) - { - throw new System.ArgumentException("callback is null"); - } - if(sentCallback_ != null) - { - throw new System.ArgumentException("sent callback already set"); - } - sentCallback_ = cb; - if((state_ & StateSent) == 0) - { - return this; - } - } - - if(outgoing_.sentSynchronously()) - { - try - { - sentCallback_(this); - } - catch(System.Exception ex) - { - warning(ex); - } - } - else - { - instance_.clientThreadPool().dispatch(() => - { - try - { - sentCallback_(this); - } - catch(System.Exception ex) - { - warning(ex); - } - }, cachedConnection_); - } - return this; - } - - public Ice.AsyncResult whenSent(Ice.SentCallback cb) - { - lock(this) - { - if(cb == null) - { - throw new System.ArgumentException("callback is null"); - } - if(sentCallback_ != null) - { - throw new System.ArgumentException("sent callback already set"); - } - sentCallback_ = (Ice.AsyncResult r) => - { - cb(r.sentSynchronously()); - }; - if((state_ & StateSent) == 0) - { - return this; - } - } - - if(outgoing_.sentSynchronously()) - { - try - { - cb(true); - } - catch(System.Exception ex) - { - warning(ex); - } - } - else - { - instance_.clientThreadPool().dispatch(() => - { - try - { - cb(false); - } - catch(System.Exception ex) - { - warning(ex); - } - }, cachedConnection_); - } - return this; - } - - public Ice.AsyncResult whenCompleted(Ice.ExceptionCallback cb) - { - if(cb == null) - { - throw new System.ArgumentException("callback is null"); - } - lock(this) - { - if(exceptionCallback_ != null) - { - throw new System.ArgumentException("callback already set"); - } - exceptionCallback_ = cb; - } - setCompletedCallback(getCompletedCallback()); - return this; - } - - public string getOperation() - { - return _operation; - } - - public bool wait() - { - lock(this) - { - if((state_ & StateEndCalled) != 0) - { - throw new System.ArgumentException("end_ method called more than once"); - } - state_ |= StateEndCalled; - while((state_ & StateDone) == 0) - { - Monitor.Wait(this); - } - if(exception_ != null) - { - throw exception_; - } - return (state_ & StateOK) != 0; - } - } - - protected AsyncResultI(Ice.Communicator communicator, - Instance instance, - string op, - object cookie, - Ice.AsyncCallback cb) - { - instance_ = instance; - state_ = 0; - - _communicator = communicator; - _operation = op; - exception_ = null; - _cookie = cookie; - completedCallback_ = cb; - } - - protected void setCompletedCallback(Ice.AsyncCallback cb) - { - lock(this) - { - if(cb == null) - { - throw new System.ArgumentException("callback is null"); - } - if(completedCallback_ != null) - { - throw new System.ArgumentException("callback already set"); - } - completedCallback_ = cb; - if((state_ & StateDone) == 0) - { - return; - } - else if((getProxy() == null || !getProxy().ice_isTwoway()) && exception_ == null) - { - return; - } - } - - instance_.clientThreadPool().dispatch(() => - { - try - { - try - { - cb(this); - } - catch(System.AggregateException ex) - { - throw ex.InnerException; - } - } - catch(System.Exception ex) - { - warning(ex); - } - }, cachedConnection_); - } - - abstract protected Ice.AsyncCallback getCompletedCallback(); - - public static AsyncResultI check(Ice.AsyncResult r, Ice.ObjectPrx prx, string operation) - { - if(r != null && r.getProxy() != prx) - { - throw new System.ArgumentException("Proxy for call to end_" + operation + - " does not match proxy that was used to call corresponding begin_" + - operation + " method"); - } - return check(r, operation); - } - - public static AsyncResultI check(Ice.AsyncResult r, string operation) - { - if(r == null) - { - throw new System.ArgumentException("AsyncResult == null"); - } - if(r.getOperation() != operation) - { - throw new System.ArgumentException("Incorrect operation for end_" + operation + " method: " + - r.getOperation()); - } - if(!(r is AsyncResultI)) - { - throw new System.ArgumentException("Incorrect AsyncResult object for end_" + operation + " method"); - } - return (AsyncResultI)r; - } - - protected void warning(System.Exception ex) - { - if(instance_.initializationData().properties.getPropertyAsIntWithDefault("Ice.Warn.AMICallback", 1) > 0) - { - instance_.initializationData().logger.warning("exception raised by AMI callback:\n" + ex); - } - } - - protected Instance instance_; - protected Ice.Instrumentation.InvocationObserver observer_; - protected Ice.Connection cachedConnection_; - - private readonly Ice.Communicator _communicator; - private readonly string _operation; - private readonly object _cookie; - protected Ice.Exception exception_; - protected EventWaitHandle waitHandle_; - - protected Ice.AsyncCallback completedCallback_; - protected Ice.AsyncCallback sentCallback_; - protected Ice.ExceptionCallback exceptionCallback_; - - protected const int StateOK = 0x1; - protected const int StateDone = 0x2; - protected const int StateSent = 0x4; - protected const int StateEndCalled = 0x8; - protected int state_; - protected OutgoingAsyncBase outgoing_; - } -} diff --git a/csharp/src/Ice/CommunicatorI.cs b/csharp/src/Ice/CommunicatorI.cs index c4ea2046387..3259399aad9 100644 --- a/csharp/src/Ice/CommunicatorI.cs +++ b/csharp/src/Ice/CommunicatorI.cs @@ -191,36 +191,6 @@ public Task flushBatchRequestsAsync(Ice.CompressBatch compressBatch, private const string _flushBatchRequests_name = "flushBatchRequests"; - private class CommunicatorFlushBatchCompletionCallback : AsyncResultCompletionCallback - { - public CommunicatorFlushBatchCompletionCallback(Communicator communicator, - Instance instance, - string op, - object cookie, - AsyncCallback callback) - : base(communicator, instance, op, cookie, callback) - { - } - - protected override AsyncCallback getCompletedCallback() - { - return (AsyncResult result) => - { - try - { - result.throwLocalException(); - } - catch(Exception ex) - { - if(exceptionCallback_ != null) - { - exceptionCallback_.Invoke(ex); - } - } - }; - } - }; - public ObjectPrx createAdmin(ObjectAdapter adminAdapter, Identity adminIdentity) { return _instance.createAdmin(adminAdapter, adminIdentity); diff --git a/csharp/src/Ice/ConnectionI.cs b/csharp/src/Ice/ConnectionI.cs index ec1876c206c..5101a1398a7 100644 --- a/csharp/src/Ice/ConnectionI.cs +++ b/csharp/src/Ice/ConnectionI.cs @@ -483,45 +483,6 @@ public void flushBatchRequests(CompressBatch compressBatch) } } - private class ConnectionFlushBatchCompletionCallback : AsyncResultCompletionCallback - { - public ConnectionFlushBatchCompletionCallback(Connection connection, - Communicator communicator, - Instance instance, - string op, - object cookie, - AsyncCallback callback) - : base(communicator, instance, op, cookie, callback) - { - _connection = connection; - } - - public override Connection getConnection() - { - return _connection; - } - - protected override AsyncCallback getCompletedCallback() - { - return (AsyncResult result) => - { - try - { - result.throwLocalException(); - } - catch(Exception ex) - { - if(exceptionCallback_ != null) - { - exceptionCallback_.Invoke(ex); - } - } - }; - } - - private Connection _connection; - } - public Task flushBatchRequestsAsync(CompressBatch compressBatch, IProgress progress = null, CancellationToken cancel = new CancellationToken()) @@ -579,44 +540,6 @@ public void heartbeat() heartbeatAsync().Wait(); } - private class HeartbeatCompletionCallback : AsyncResultCompletionCallback - { - public HeartbeatCompletionCallback(Ice.Connection connection, - Ice.Communicator communicator, - Instance instance, - object cookie, - Ice.AsyncCallback callback) - : base(communicator, instance, "heartbeat", cookie, callback) - { - _connection = connection; - } - - public override Ice.Connection getConnection() - { - return _connection; - } - - protected override Ice.AsyncCallback getCompletedCallback() - { - return (Ice.AsyncResult result) => - { - try - { - result.throwLocalException(); - } - catch(Ice.Exception ex) - { - if(exceptionCallback_ != null) - { - exceptionCallback_.Invoke(ex); - } - } - }; - } - - private Ice.Connection _connection; - } - private class HeartbeatTaskCompletionCallback : TaskCompletionCallback { public HeartbeatTaskCompletionCallback(System.IProgress progress, diff --git a/csharp/src/Ice/OutgoingAsync.cs b/csharp/src/Ice/OutgoingAsync.cs index de5b71f14a0..15ed5d2abce 100644 --- a/csharp/src/Ice/OutgoingAsync.cs +++ b/csharp/src/Ice/OutgoingAsync.cs @@ -1524,193 +1524,4 @@ public override void handleInvokeResponse(bool ok, OutgoingAsyncBase og) SetResult(null); } } - - abstract public class AsyncResultCompletionCallback : AsyncResultI, OutgoingAsyncCompletionCallback - { - public AsyncResultCompletionCallback(Ice.Communicator com, Instance instance, string op, object cookie, - Ice.AsyncCallback cb) : - base(com, instance, op, cookie, cb) - { - } - - public void init(OutgoingAsyncBase outgoing) - { - outgoing_ = outgoing; - } - - public bool handleSent(bool done, bool alreadySent, OutgoingAsyncBase og) - { - lock(this) - { - state_ |= StateSent; - if(done) - { - state_ |= StateDone | StateOK; - } - if(waitHandle_ != null) - { - waitHandle_.Set(); - } - Monitor.PulseAll(this); - - // - // Invoke the sent callback only if not already invoked. - // - return !alreadySent && sentCallback_ != null; - } - } - - public bool handleException(Ice.Exception ex, OutgoingAsyncBase og) - { - lock(this) - { - state_ |= StateDone; - exception_ = ex; - if(waitHandle_ != null) - { - waitHandle_.Set(); - } - Monitor.PulseAll(this); - return completedCallback_ != null; - } - } - - public bool handleResponse(bool userThread, bool ok, OutgoingAsyncBase og) - { - lock(this) - { - state_ |= StateDone; - if(ok) - { - state_ |= StateOK; - } - if(waitHandle_ != null) - { - waitHandle_.Set(); - } - Monitor.PulseAll(this); - return completedCallback_ != null; - } - } - - public void handleInvokeSent(bool sentSynchronously, bool done, bool alreadySent, OutgoingAsyncBase og) - { - sentCallback_(this); - } - - public void handleInvokeException(Ice.Exception ex, OutgoingAsyncBase og) - { - try - { - completedCallback_(this); - } - catch(Ice.Exception e) - { - throw new System.AggregateException(e); - } - } - - public void handleInvokeResponse(bool ok, OutgoingAsyncBase og) - { - try - { - completedCallback_(this); - } - catch(Ice.Exception e) - { - throw new System.AggregateException(e); - } - } - } - - abstract public class ProxyAsyncResultCompletionCallback : AsyncResultCompletionCallback, Ice.AsyncResult - { - public ProxyAsyncResultCompletionCallback(Ice.ObjectPrxHelperBase proxy, string operation, object cookie, - Ice.AsyncCallback cb) : - base(proxy.ice_getCommunicator(), proxy.iceReference().getInstance(), operation, cookie, cb) - { - _proxy = proxy; - } - - public override Ice.ObjectPrx getProxy() - { - return _proxy; - } - - new public Ice.AsyncResult whenCompleted(Ice.ExceptionCallback excb) - { - base.whenCompleted(excb); - return this; - } - - virtual public Ice.AsyncResult whenCompleted(T cb, Ice.ExceptionCallback excb) - { - if(cb == null && excb == null) - { - throw new System.ArgumentException("callback is null"); - } - lock(this) - { - if(responseCallback_ != null || exceptionCallback_ != null) - { - throw new System.ArgumentException("callback already set"); - } - responseCallback_ = cb; - exceptionCallback_ = excb; - } - setCompletedCallback(getCompletedCallback()); - return this; - } - - new public Ice.AsyncResult whenSent(Ice.SentCallback cb) - { - base.whenSent(cb); - return this; - } - - protected T responseCallback_; - private Ice.ObjectPrx _proxy; - } - - public class OperationAsyncResultCompletionCallback : ProxyAsyncResultCompletionCallback - { - public OperationAsyncResultCompletionCallback(System.Action completed, - Ice.ObjectPrxHelperBase proxy, - string operation, - object cookie, - Ice.AsyncCallback callback) : - base(proxy, operation, cookie, callback) - { - _completed = completed; - } - - override protected Ice.AsyncCallback getCompletedCallback() - { - return (Ice.AsyncResult r) => - { - Debug.Assert(r == this); - try - { - R result = ((OutgoingAsyncT)outgoing_).getResult(wait()); - try - { - _completed(responseCallback_, result); - } - catch(Ice.Exception ex) - { - throw new System.AggregateException(ex); - } - } - catch(Ice.Exception ex) - { - if(exceptionCallback_ != null) - { - exceptionCallback_.Invoke(ex); - } - } - }; - } - - private System.Action _completed; - } } diff --git a/csharp/src/Ice/Proxy.cs b/csharp/src/Ice/Proxy.cs index 95357d6880e..266f9462945 100644 --- a/csharp/src/Ice/Proxy.cs +++ b/csharp/src/Ice/Proxy.cs @@ -1622,38 +1622,6 @@ public bool ice_isFixed() return _reference is IceInternal.FixedReference; } - private class ProxyGetConnectionAsyncCallback : ProxyAsyncResultCompletionCallback - { - public ProxyGetConnectionAsyncCallback(ObjectPrxHelperBase proxy, string operation, object cookie, - AsyncCallback cb) : - base(proxy, operation, cookie, cb) - { - } - - protected override AsyncCallback getCompletedCallback() - { - return (AsyncResult result) => - { - try - { - result.throwLocalException(); - if(responseCallback_ != null) - { - responseCallback_.Invoke(((ProxyGetConnection)OutgoingAsync).getConnection()); - } - - } - catch(Exception ex) - { - if(exceptionCallback_ != null) - { - exceptionCallback_.Invoke(ex); - } - } - }; - } - } - public class GetConnectionTaskCompletionCallback : TaskCompletionCallback { public GetConnectionTaskCompletionCallback(ObjectPrx proxy, @@ -1745,44 +1713,6 @@ public Connection ice_getCachedConnection() return null; } - private class ProxyFlushBatchRequestsAsyncCallback : AsyncResultCompletionCallback - { - public ProxyFlushBatchRequestsAsyncCallback(ObjectPrx proxy, - string operation, - object cookie, - AsyncCallback callback) : - base(proxy.ice_getCommunicator(), ((ObjectPrxHelperBase)proxy).iceReference().getInstance(), - operation, cookie, callback) - { - _proxy = proxy; - } - - public override ObjectPrx getProxy() - { - return _proxy; - } - - protected override AsyncCallback getCompletedCallback() - { - return (AsyncResult result) => - { - try - { - result.throwLocalException(); - } - catch(Exception ex) - { - if(exceptionCallback_ != null) - { - exceptionCallback_.Invoke(ex); - } - } - }; - } - - private ObjectPrx _proxy; - } - /// /// Flushes any pending batched requests for this communicator. The call blocks until the flush is complete. /// @@ -2131,47 +2061,6 @@ public Object_Ice_invokeResult } } - public class InvokeAsyncResultCompletionCallback : ProxyAsyncResultCompletionCallback - { - public InvokeAsyncResultCompletionCallback(ObjectPrxHelperBase proxy, - string operation, - object cookie, - AsyncCallback callback) : - base(proxy, operation, cookie, callback) - { - } - - override protected AsyncCallback getCompletedCallback() - { - return (AsyncResult r) => - { - Debug.Assert(r == this); - try - { - Object_Ice_invokeResult result = ((InvokeOutgoingAsyncT)outgoing_).getResult(wait()); - try - { - if(responseCallback_ != null) - { - responseCallback_.Invoke(result.returnValue, result.outEncaps); - } - } - catch(Exception ex) - { - throw new AggregateException(ex); - } - } - catch(Exception ex) - { - if(exceptionCallback_ != null) - { - exceptionCallback_.Invoke(ex); - } - } - }; - } - }; - private class InvokeTaskCompletionCallback : TaskCompletionCallback { public InvokeTaskCompletionCallback(IProgress progress, CancellationToken cancellationToken) : diff --git a/csharp/test/Ice/ami/AllTests.cs b/csharp/test/Ice/ami/AllTests.cs index 2c7c8ad91ca..d98a5a9c4f8 100644 --- a/csharp/test/Ice/ami/AllTests.cs +++ b/csharp/test/Ice/ami/AllTests.cs @@ -665,40 +665,29 @@ public static async Task allTestsAsync(global::Test.TestHelper helper, bool coll cs2.Cancel(); try { - t1.Wait(); + await t1; test(false); } - catch(AggregateException ae) + catch(InvocationCanceledException) { - ae.Handle(ex => - { - return ex is Ice.InvocationCanceledException; - }); } + try { - t2.Wait(); + await t2; test(false); } - catch(AggregateException ae) + catch (InvocationCanceledException) { - ae.Handle(ex => - { - return ex is Ice.InvocationCanceledException; - }); } try { - t3.Wait(); + await t3; test(false); } - catch(AggregateException ae) + catch (InvocationCanceledException) { - ae.Handle(ex => - { - return ex is Ice.InvocationCanceledException; - }); } } @@ -725,7 +714,7 @@ public static async Task allTestsAsync(global::Test.TestHelper helper, bool coll con.setCloseCallback(_ => tcs.SetResult()); Task t = p.sleepAsync(100); con.close(ConnectionClose.GracefullyWithWait); - t.Wait(); // Should complete successfully. + await t; // Should complete successfully. await tcs.Task; } { @@ -777,9 +766,10 @@ public static async Task allTestsAsync(global::Test.TestHelper helper, bool coll maxQueue *= 2; done = false; } + foreach(Task q in results) { - q.Wait(); + await q; } } } @@ -801,13 +791,12 @@ public static async Task allTestsAsync(global::Test.TestHelper helper, bool coll con.close(ConnectionClose.Gracefully); try { - t.Wait(); + await t; test(false); } - catch(System.AggregateException ex) + catch(ConnectionManuallyClosedException ex) { - test(ex.InnerException is Ice.ConnectionManuallyClosedException); - test((ex.InnerException as Ice.ConnectionManuallyClosedException).graceful); + test(ex.graceful); } p.finishDispatch(); @@ -821,7 +810,7 @@ public static async Task allTestsAsync(global::Test.TestHelper helper, bool coll t = p.sleepAsync(100); p.close(Test.CloseMode.Gracefully); // Close is delayed until sleep completes. await tcs.Task; - t.Wait(); + await t; } output.WriteLine("ok"); @@ -841,13 +830,12 @@ public static async Task allTestsAsync(global::Test.TestHelper helper, bool coll con.close(ConnectionClose.Forcefully); try { - t.Wait(); + await t; test(false); } - catch(AggregateException ex) + catch(ConnectionManuallyClosedException ex) { - test(ex.InnerException is ConnectionManuallyClosedException); - test(!(ex.InnerException as ConnectionManuallyClosedException).graceful); + test(!ex.graceful); } p.finishDispatch(); @@ -946,12 +934,9 @@ public static async Task allTestsAsync(global::Test.TestHelper helper, bool coll { var q = Test.Outer.Inner.TestIntfPrxHelper.uncheckedCast( communicator.stringToProxy("test2:" + helper.getTestEndpoint(0))); - q.opAsync(1).ContinueWith(t => - { - var r = t.Result; - test(r.returnValue == 1); - test(r.j == 1); - }).Wait(); + var r = await q.opAsync(1); + test(r.returnValue == 1); + test(r.j == 1); } output.WriteLine("ok");