| | 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 | | // |
| | 69 | | // First check for the connection, it's important otherwise the user could first get a connection |
| | 70 | | // and then the exception if he tries to obtain the proxy cached connection multiple times (the |
| | 71 | | // exception can be set after the connection is set if the flush of pending requests fails). |
| | 72 | | // |
| 1 | 73 | | if (_connection != null) |
| | 74 | | { |
| 1 | 75 | | return _connection; |
| | 76 | | } |
| 0 | 77 | | else if (_exception != null) |
| | 78 | | { |
| 0 | 79 | | throw _exception; |
| | 80 | | } |
| 0 | 81 | | return null; |
| | 82 | | } |
| 1 | 83 | | } |
| | 84 | |
|
| | 85 | | // |
| | 86 | | // Implementation of Reference.GetConnectionCallback |
| | 87 | | // |
| | 88 | |
|
| | 89 | | public void setConnection(Ice.ConnectionI connection, bool compress) |
| | 90 | | { |
| 1 | 91 | | lock (_mutex) |
| | 92 | | { |
| | 93 | | Debug.Assert(!_flushing && _exception == null && _connection == null); |
| 1 | 94 | | _connection = connection; |
| 1 | 95 | | _compress = compress; |
| 1 | 96 | | } |
| | 97 | |
|
| | 98 | | // |
| | 99 | | // If this proxy is for a non-local object, and we are using a router, then |
| | 100 | | // add this proxy to the router info object. |
| | 101 | | // |
| 1 | 102 | | RouterInfo ri = _reference.getRouterInfo(); |
| 1 | 103 | | if (ri != null && !ri.addProxy(_reference, this)) |
| | 104 | | { |
| 1 | 105 | | return; // The request handler will be initialized once addProxy returns. |
| | 106 | | } |
| | 107 | |
|
| | 108 | | // |
| | 109 | | // We can now send the queued requests. |
| | 110 | | // |
| 1 | 111 | | flushRequests(); |
| 1 | 112 | | } |
| | 113 | |
|
| | 114 | | public void setException(Ice.LocalException ex) |
| | 115 | | { |
| 1 | 116 | | lock (_mutex) |
| | 117 | | { |
| | 118 | | Debug.Assert(!_flushing && !_initialized && _exception == null); |
| 1 | 119 | | _exception = ex; |
| 1 | 120 | | _flushing = true; // Ensures request handler is removed before processing new requests. |
| 1 | 121 | | } |
| | 122 | |
|
| 1 | 123 | | foreach (ProxyOutgoingAsyncBase outAsync in _requests) |
| | 124 | | { |
| 1 | 125 | | if (outAsync.exception(_exception)) |
| | 126 | | { |
| 1 | 127 | | outAsync.invokeExceptionAsync(); |
| | 128 | | } |
| | 129 | | } |
| 1 | 130 | | _requests.Clear(); |
| | 131 | |
|
| 1 | 132 | | lock (_mutex) |
| | 133 | | { |
| 1 | 134 | | _flushing = false; |
| 1 | 135 | | Monitor.PulseAll(_mutex); |
| 1 | 136 | | } |
| 1 | 137 | | } |
| | 138 | |
|
| | 139 | | // |
| | 140 | | // Implementation of RouterInfo.AddProxyCallback |
| | 141 | | // |
| | 142 | | public void addedProxy() => |
| | 143 | | // |
| | 144 | | // The proxy was added to the router info, we're now ready to send the |
| | 145 | | // queued requests. |
| | 146 | | // |
| 1 | 147 | | flushRequests(); |
| | 148 | |
|
| | 149 | | private bool initialized() |
| | 150 | | { |
| 1 | 151 | | if (_initialized) |
| | 152 | | { |
| | 153 | | Debug.Assert(_connection != null); |
| 1 | 154 | | return true; |
| | 155 | | } |
| | 156 | | else |
| | 157 | | { |
| 1 | 158 | | while (_flushing) |
| | 159 | | { |
| 1 | 160 | | Monitor.Wait(_mutex); |
| | 161 | | } |
| | 162 | |
|
| 1 | 163 | | if (_exception != null) |
| | 164 | | { |
| 1 | 165 | | if (_connection != null) |
| | 166 | | { |
| | 167 | | // |
| | 168 | | // Only throw if the connection didn't get established. If |
| | 169 | | // it died after being established, we allow the caller to |
| | 170 | | // retry the connection establishment by not throwing here |
| | 171 | | // (the connection will throw RetryException). |
| | 172 | | // |
| 0 | 173 | | return true; |
| | 174 | | } |
| 1 | 175 | | throw _exception; |
| | 176 | | } |
| | 177 | | else |
| | 178 | | { |
| 1 | 179 | | return _initialized; |
| | 180 | | } |
| | 181 | | } |
| | 182 | | } |
| | 183 | |
|
| | 184 | | private void flushRequests() |
| | 185 | | { |
| 1 | 186 | | lock (_mutex) |
| | 187 | | { |
| | 188 | | Debug.Assert(_connection != null && !_initialized); |
| | 189 | |
|
| | 190 | | // |
| | 191 | | // We set the _flushing flag to true to prevent any additional queuing. Callers |
| | 192 | | // might block for a little while as the queued requests are being sent but this |
| | 193 | | // shouldn't be an issue as the request sends are non-blocking. |
| | 194 | | // |
| 1 | 195 | | _flushing = true; |
| 1 | 196 | | } |
| | 197 | |
|
| 1 | 198 | | Ice.LocalException exception = null; |
| 1 | 199 | | foreach (ProxyOutgoingAsyncBase outAsync in _requests) |
| | 200 | | { |
| | 201 | | try |
| | 202 | | { |
| 1 | 203 | | if ( |
| 1 | 204 | | (outAsync.invokeRemote(_connection, _compress, _response) & |
| 1 | 205 | | OutgoingAsyncBase.AsyncStatusInvokeSentCallback) != 0) |
| | 206 | | { |
| 1 | 207 | | outAsync.invokeSentAsync(); |
| | 208 | | } |
| 1 | 209 | | } |
| 0 | 210 | | catch (RetryException ex) |
| | 211 | | { |
| 0 | 212 | | exception = ex.get(); |
| 0 | 213 | | outAsync.retryException(); |
| 0 | 214 | | } |
| 0 | 215 | | catch (Ice.LocalException ex) |
| | 216 | | { |
| 0 | 217 | | exception = ex; |
| 0 | 218 | | if (outAsync.exception(ex)) |
| | 219 | | { |
| 0 | 220 | | outAsync.invokeExceptionAsync(); |
| | 221 | | } |
| 0 | 222 | | } |
| | 223 | | } |
| 1 | 224 | | _requests.Clear(); |
| | 225 | |
|
| 1 | 226 | | lock (_mutex) |
| | 227 | | { |
| | 228 | | Debug.Assert(!_initialized); |
| 1 | 229 | | _exception = exception; |
| 1 | 230 | | _initialized = _exception == null; |
| 1 | 231 | | _flushing = false; |
| 1 | 232 | | Monitor.PulseAll(_mutex); |
| 1 | 233 | | } |
| 1 | 234 | | } |
| | 235 | |
|
| | 236 | | private readonly Reference _reference; |
| | 237 | | private readonly bool _response; |
| | 238 | |
|
| | 239 | | private Ice.ConnectionI _connection; |
| | 240 | | private bool _compress; |
| | 241 | | private Ice.LocalException _exception; |
| | 242 | | private bool _initialized; |
| | 243 | | private bool _flushing; |
| | 244 | |
|
| 1 | 245 | | private readonly LinkedList<ProxyOutgoingAsyncBase> _requests = new(); |
| 1 | 246 | | private readonly object _mutex = new(); |
| | 247 | | } |