| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using System.Diagnostics; |
| | | 4 | | |
| | | 5 | | namespace Ice.Internal; |
| | | 6 | | |
| | | 7 | | internal class ConnectRequestHandler : RequestHandler, Reference.GetConnectionCallback, RouterInfo.AddProxyCallback |
| | | 8 | | { |
| | 1 | 9 | | internal ConnectRequestHandler(Reference reference) |
| | | 10 | | { |
| | 1 | 11 | | _reference = reference; |
| | 1 | 12 | | _response = reference.isTwoway; |
| | 1 | 13 | | } |
| | | 14 | | |
| | | 15 | | public int sendAsyncRequest(ProxyOutgoingAsyncBase outAsync) |
| | | 16 | | { |
| | 1 | 17 | | lock (_mutex) |
| | | 18 | | { |
| | 1 | 19 | | if (!_initialized) |
| | | 20 | | { |
| | 1 | 21 | | outAsync.cancelable(this); // This will throw if the request is canceled |
| | | 22 | | } |
| | | 23 | | |
| | 1 | 24 | | if (!initialized()) |
| | | 25 | | { |
| | 1 | 26 | | _requests.AddLast(outAsync); |
| | 1 | 27 | | return OutgoingAsyncBase.AsyncStatusQueued; |
| | | 28 | | } |
| | 1 | 29 | | } |
| | 1 | 30 | | return outAsync.invokeRemote(_connection, _compress, _response); |
| | 1 | 31 | | } |
| | | 32 | | |
| | | 33 | | public void asyncRequestCanceled(OutgoingAsyncBase outAsync, Ice.LocalException ex) |
| | | 34 | | { |
| | 0 | 35 | | lock (_mutex) |
| | | 36 | | { |
| | 0 | 37 | | if (_exception != null) |
| | | 38 | | { |
| | 0 | 39 | | return; // The request has been notified of a failure already. |
| | | 40 | | } |
| | | 41 | | |
| | 0 | 42 | | if (!initialized()) |
| | | 43 | | { |
| | 0 | 44 | | LinkedListNode<ProxyOutgoingAsyncBase> p = _requests.First; |
| | 0 | 45 | | while (p != null) |
| | | 46 | | { |
| | 0 | 47 | | if (p.Value == outAsync) |
| | | 48 | | { |
| | 0 | 49 | | _requests.Remove(p); |
| | 0 | 50 | | if (outAsync.exception(ex)) |
| | | 51 | | { |
| | 0 | 52 | | outAsync.invokeExceptionAsync(); |
| | | 53 | | } |
| | 0 | 54 | | return; |
| | | 55 | | } |
| | 0 | 56 | | p = p.Next; |
| | | 57 | | } |
| | | 58 | | Debug.Assert(false); // The request has to be queued if it timed out and we're not initialized yet. |
| | | 59 | | } |
| | 0 | 60 | | } |
| | 0 | 61 | | _connection.asyncRequestCanceled(outAsync, ex); |
| | 0 | 62 | | } |
| | | 63 | | |
| | | 64 | | public Ice.ConnectionI getConnection() |
| | | 65 | | { |
| | 1 | 66 | | lock (_mutex) |
| | | 67 | | { |
| | | 68 | | // Return the connection in whatever state it is; if the connection establishment failed, return null. |
| | | 69 | | // Like all getConnection implementations, this method never throws: a null return is how the absence of a |
| | | 70 | | // connection is reported. |
| | 1 | 71 | | return _connection; |
| | | 72 | | } |
| | 1 | 73 | | } |
| | | 74 | | |
| | | 75 | | // |
| | | 76 | | // Implementation of Reference.GetConnectionCallback |
| | | 77 | | // |
| | | 78 | | |
| | | 79 | | public void setConnection(Ice.ConnectionI connection, bool compress) |
| | | 80 | | { |
| | 1 | 81 | | lock (_mutex) |
| | | 82 | | { |
| | | 83 | | Debug.Assert(!_flushing && _exception == null && _connection == null); |
| | 1 | 84 | | _connection = connection; |
| | 1 | 85 | | _compress = compress; |
| | 1 | 86 | | } |
| | | 87 | | |
| | | 88 | | // |
| | | 89 | | // If this proxy is for a non-local object, and we are using a router, then |
| | | 90 | | // add this proxy to the router info object. |
| | | 91 | | // |
| | 1 | 92 | | RouterInfo ri = _reference.getRouterInfo(); |
| | 1 | 93 | | if (ri != null && !ri.addProxy(_reference, this)) |
| | | 94 | | { |
| | 1 | 95 | | return; // The request handler will be initialized once addProxy returns. |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | // |
| | | 99 | | // We can now send the queued requests. |
| | | 100 | | // |
| | 1 | 101 | | flushRequests(); |
| | 1 | 102 | | } |
| | | 103 | | |
| | | 104 | | public void setException(Ice.LocalException ex) |
| | | 105 | | { |
| | 1 | 106 | | lock (_mutex) |
| | | 107 | | { |
| | | 108 | | Debug.Assert(!_flushing && !_initialized && _exception == null); |
| | 1 | 109 | | _exception = ex; |
| | 1 | 110 | | _flushing = true; // Ensures request handler is removed before processing new requests. |
| | 1 | 111 | | } |
| | | 112 | | |
| | 1 | 113 | | foreach (ProxyOutgoingAsyncBase outAsync in _requests) |
| | | 114 | | { |
| | 1 | 115 | | if (outAsync.exception(_exception)) |
| | | 116 | | { |
| | 1 | 117 | | outAsync.invokeExceptionAsync(); |
| | | 118 | | } |
| | | 119 | | } |
| | 1 | 120 | | _requests.Clear(); |
| | | 121 | | |
| | 1 | 122 | | lock (_mutex) |
| | | 123 | | { |
| | 1 | 124 | | _flushing = false; |
| | 1 | 125 | | Monitor.PulseAll(_mutex); |
| | 1 | 126 | | } |
| | 1 | 127 | | } |
| | | 128 | | |
| | | 129 | | // |
| | | 130 | | // Implementation of RouterInfo.AddProxyCallback |
| | | 131 | | // |
| | | 132 | | public void addedProxy() => |
| | | 133 | | // |
| | | 134 | | // The proxy was added to the router info, we're now ready to send the |
| | | 135 | | // queued requests. |
| | | 136 | | // |
| | 1 | 137 | | flushRequests(); |
| | | 138 | | |
| | | 139 | | private bool initialized() |
| | | 140 | | { |
| | 1 | 141 | | if (_initialized) |
| | | 142 | | { |
| | | 143 | | Debug.Assert(_connection != null); |
| | 1 | 144 | | return true; |
| | | 145 | | } |
| | | 146 | | else |
| | | 147 | | { |
| | 1 | 148 | | while (_flushing) |
| | | 149 | | { |
| | 1 | 150 | | Monitor.Wait(_mutex); |
| | | 151 | | } |
| | | 152 | | |
| | 1 | 153 | | if (_exception != null) |
| | | 154 | | { |
| | 1 | 155 | | if (_connection != null) |
| | | 156 | | { |
| | | 157 | | // |
| | | 158 | | // Only throw if the connection didn't get established. If |
| | | 159 | | // it died after being established, we allow the caller to |
| | | 160 | | // retry the connection establishment by not throwing here |
| | | 161 | | // (the connection will throw RetryException). |
| | | 162 | | // |
| | 0 | 163 | | return true; |
| | | 164 | | } |
| | 1 | 165 | | throw _exception; |
| | | 166 | | } |
| | | 167 | | else |
| | | 168 | | { |
| | 1 | 169 | | return _initialized; |
| | | 170 | | } |
| | | 171 | | } |
| | | 172 | | } |
| | | 173 | | |
| | | 174 | | private void flushRequests() |
| | | 175 | | { |
| | 1 | 176 | | lock (_mutex) |
| | | 177 | | { |
| | | 178 | | Debug.Assert(_connection != null && !_initialized); |
| | | 179 | | |
| | | 180 | | // |
| | | 181 | | // We set the _flushing flag to true to prevent any additional queuing. Callers |
| | | 182 | | // might block for a little while as the queued requests are being sent but this |
| | | 183 | | // shouldn't be an issue as the request sends are non-blocking. |
| | | 184 | | // |
| | 1 | 185 | | _flushing = true; |
| | 1 | 186 | | } |
| | | 187 | | |
| | 1 | 188 | | Ice.LocalException exception = null; |
| | 1 | 189 | | foreach (ProxyOutgoingAsyncBase outAsync in _requests) |
| | | 190 | | { |
| | | 191 | | try |
| | | 192 | | { |
| | 1 | 193 | | if ( |
| | 1 | 194 | | (outAsync.invokeRemote(_connection, _compress, _response) & |
| | 1 | 195 | | OutgoingAsyncBase.AsyncStatusInvokeSentCallback) != 0) |
| | | 196 | | { |
| | 1 | 197 | | outAsync.invokeSentAsync(); |
| | | 198 | | } |
| | 1 | 199 | | } |
| | 0 | 200 | | catch (RetryException ex) |
| | | 201 | | { |
| | 0 | 202 | | exception = ex.get(); |
| | 0 | 203 | | outAsync.retryException(); |
| | 0 | 204 | | } |
| | 0 | 205 | | catch (Ice.LocalException ex) |
| | | 206 | | { |
| | 0 | 207 | | exception = ex; |
| | 0 | 208 | | if (outAsync.exception(ex)) |
| | | 209 | | { |
| | 0 | 210 | | outAsync.invokeExceptionAsync(); |
| | | 211 | | } |
| | 0 | 212 | | } |
| | | 213 | | } |
| | 1 | 214 | | _requests.Clear(); |
| | | 215 | | |
| | 1 | 216 | | lock (_mutex) |
| | | 217 | | { |
| | | 218 | | Debug.Assert(!_initialized); |
| | 1 | 219 | | _exception = exception; |
| | 1 | 220 | | _initialized = _exception == null; |
| | 1 | 221 | | _flushing = false; |
| | 1 | 222 | | Monitor.PulseAll(_mutex); |
| | 1 | 223 | | } |
| | 1 | 224 | | } |
| | | 225 | | |
| | | 226 | | private readonly Reference _reference; |
| | | 227 | | private readonly bool _response; |
| | | 228 | | |
| | | 229 | | private Ice.ConnectionI _connection; |
| | | 230 | | private bool _compress; |
| | | 231 | | private Ice.LocalException _exception; |
| | | 232 | | private bool _initialized; |
| | | 233 | | private bool _flushing; |
| | | 234 | | |
| | 1 | 235 | | private readonly LinkedList<ProxyOutgoingAsyncBase> _requests = new(); |
| | 1 | 236 | | private readonly object _mutex = new(); |
| | | 237 | | } |