| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | #nullable enable |
| | 4 | |
|
| | 5 | | using Ice.Internal; |
| | 6 | | using System.Diagnostics; |
| | 7 | |
|
| | 8 | | namespace Ice; |
| | 9 | |
|
| | 10 | | /// <summary> |
| | 11 | | /// Provides extension methods for <see cref="Current"/>. |
| | 12 | | /// </summary> |
| | 13 | | public static class CurrentExtensions |
| | 14 | | { |
| | 15 | | /// <summary>Makes sure the operation mode of an incoming request is not idempotent.</summary> |
| | 16 | | /// <param name="current">The current object of the corresponding incoming request.</param> |
| | 17 | | /// <exception cref="MarshalException">Thrown when the request's operation mode is |
| | 18 | | /// <see cref="OperationMode.Idempotent" /> or <see cref="OperationMode.Nonmutating" />.</exception> |
| | 19 | | /// <remarks>The generated code calls this method to ensure that when an operation's mode is not idempotent |
| | 20 | | /// (locally), the incoming request's operation mode is not idempotent.</remarks> |
| | 21 | | public static void checkNonIdempotent(this Current current) |
| | 22 | | { |
| 1 | 23 | | if (current.mode != OperationMode.Normal) |
| | 24 | | { |
| 0 | 25 | | throw new MarshalException( |
| 0 | 26 | | $"Operation mode mismatch for operation '{current.operation}': received {current.mode} for non-idempoten |
| | 27 | | } |
| 1 | 28 | | } |
| | 29 | |
|
| | 30 | | /// <summary> |
| | 31 | | /// Creates an outgoing response with reply status <see cref="ReplyStatus.Ok"/>. |
| | 32 | | /// </summary> |
| | 33 | | /// <typeparam name="TResult">The type of result.</typeparam> |
| | 34 | | /// <param name="current">The current object of the corresponding incoming request.</param> |
| | 35 | | /// <param name="result">The result to marshal into the response payload.</param> |
| | 36 | | /// <param name="marshal">The action that marshals result into an output stream.</param> |
| | 37 | | /// <param name="formatType">The class format.</param> |
| | 38 | | /// <returns>A new outgoing response.</returns> |
| | 39 | | public static OutgoingResponse createOutgoingResponse<TResult>( |
| | 40 | | this Current current, |
| | 41 | | TResult result, |
| | 42 | | Action<OutputStream, TResult> marshal, |
| | 43 | | FormatType? formatType = null) |
| | 44 | | { |
| 1 | 45 | | OutputStream ostr = current.startReplyStream(); |
| 1 | 46 | | if (current.requestId != 0) |
| | 47 | | { |
| | 48 | | try |
| | 49 | | { |
| 1 | 50 | | ostr.startEncapsulation(current.encoding, formatType); |
| 1 | 51 | | marshal(ostr, result); |
| 1 | 52 | | ostr.endEncapsulation(); |
| 1 | 53 | | return new OutgoingResponse(ostr); |
| | 54 | | } |
| 0 | 55 | | catch (System.Exception exception) |
| | 56 | | { |
| 0 | 57 | | return current.createOutgoingResponse(exception); |
| | 58 | | } |
| | 59 | | } |
| | 60 | | else |
| | 61 | | { |
| | 62 | | Debug.Fail("A one-way request cannot return a response"); |
| 0 | 63 | | return new OutgoingResponse(ostr); |
| | 64 | | } |
| 1 | 65 | | } |
| | 66 | |
|
| | 67 | | /// <summary> |
| | 68 | | /// Creates an empty outgoing response with reply status <see cref="ReplyStatus.Ok"/>. |
| | 69 | | /// </summary> |
| | 70 | | /// <param name="current">The current object of the corresponding incoming request.</param> |
| | 71 | | /// <returns>An outgoing response with an empty payload.</returns> |
| | 72 | | public static OutgoingResponse createEmptyOutgoingResponse(this Current current) |
| | 73 | | { |
| 1 | 74 | | OutputStream ostr = current.startReplyStream(); |
| 1 | 75 | | if (current.requestId != 0) |
| | 76 | | { |
| | 77 | | try |
| | 78 | | { |
| 1 | 79 | | ostr.writeEmptyEncapsulation(current.encoding); |
| 1 | 80 | | } |
| 0 | 81 | | catch (System.Exception ex) |
| | 82 | | { |
| 0 | 83 | | return current.createOutgoingResponse(ex); |
| | 84 | | } |
| | 85 | | } |
| 1 | 86 | | return new OutgoingResponse(ostr); |
| 0 | 87 | | } |
| | 88 | |
|
| | 89 | | /// <summary> |
| | 90 | | /// Creates an outgoing response with the specified payload. |
| | 91 | | /// </summary> |
| | 92 | | /// <param name="current">The current object of the corresponding incoming request.</param> |
| | 93 | | /// <param name="ok">When true, the reply status of the response is <see cref="ReplyStatus.Ok" />; otherwise, it's |
| | 94 | | /// <see cref="ReplyStatus.UserException" />.</param> |
| | 95 | | /// <param name="encapsulation">The payload of the response.</param> |
| | 96 | | /// <returns>A new outgoing response.</returns> |
| | 97 | | public static OutgoingResponse createOutgoingResponse(this Current current, bool ok, byte[] encapsulation) |
| | 98 | | { |
| | 99 | | // For compatibility with the Ice 3.7 and earlier. |
| 1 | 100 | | encapsulation ??= []; |
| | 101 | |
|
| 1 | 102 | | OutputStream ostr = current.startReplyStream(ok ? ReplyStatus.Ok : ReplyStatus.UserException); |
| | 103 | |
|
| 1 | 104 | | if (current.requestId != 0) |
| | 105 | | { |
| | 106 | | try |
| | 107 | | { |
| 1 | 108 | | if (encapsulation.Length > 0) |
| | 109 | | { |
| 1 | 110 | | ostr.writeEncapsulation(encapsulation); |
| | 111 | | } |
| | 112 | | else |
| | 113 | | { |
| 1 | 114 | | ostr.writeEmptyEncapsulation(current.encoding); |
| | 115 | | } |
| 1 | 116 | | } |
| 0 | 117 | | catch (System.Exception ex) |
| | 118 | | { |
| 0 | 119 | | return current.createOutgoingResponse(ex); |
| | 120 | | } |
| | 121 | | } |
| 1 | 122 | | return new OutgoingResponse( |
| 1 | 123 | | replyStatus: ok ? ReplyStatus.Ok : ReplyStatus.UserException, |
| 1 | 124 | | exceptionId: null, |
| 1 | 125 | | exceptionDetails: null, |
| 1 | 126 | | ostr); |
| 0 | 127 | | } |
| | 128 | |
|
| | 129 | | /// <summary> |
| | 130 | | /// Creates an outgoing response that marshals an exception. |
| | 131 | | /// </summary> |
| | 132 | | /// <param name="current">The current object of the corresponding incoming request.</param> |
| | 133 | | /// <param name="exception">The exception to marshal into the response payload.</param> |
| | 134 | | /// <returns>A new outgoing response.</returns> |
| | 135 | | public static OutgoingResponse createOutgoingResponse(this Current current, System.Exception exception) |
| | 136 | | { |
| | 137 | | try |
| | 138 | | { |
| 1 | 139 | | return createOutgoingResponseCore(exception); |
| | 140 | | } |
| 0 | 141 | | catch (System.Exception ex) |
| | 142 | | { |
| | 143 | | // Try a second time with the marshal exception. This should not fail. |
| 0 | 144 | | return createOutgoingResponseCore(ex); |
| | 145 | | } |
| | 146 | |
|
| | 147 | | OutgoingResponse createOutgoingResponseCore(System.Exception exc) |
| | 148 | | { |
| | 149 | | OutputStream ostr; |
| | 150 | |
|
| 1 | 151 | | if (current.requestId != 0) |
| | 152 | | { |
| | 153 | | // The default class format doesn't matter since we always encode user exceptions in Sliced format. |
| 1 | 154 | | ostr = new OutputStream(Util.currentProtocolEncoding); |
| 1 | 155 | | ostr.writeBlob(Protocol.replyHdr); |
| 1 | 156 | | ostr.writeInt(current.requestId); |
| | 157 | | } |
| | 158 | | else |
| | 159 | | { |
| 1 | 160 | | ostr = new OutputStream(); |
| | 161 | | } |
| | 162 | |
|
| | 163 | | ReplyStatus replyStatus; |
| | 164 | | string? exceptionId; |
| 1 | 165 | | string? dispatchExceptionMessage = null; |
| | 166 | |
|
| | 167 | | switch (exc) |
| | 168 | | { |
| | 169 | | case UserException ex: |
| 1 | 170 | | exceptionId = null; |
| 1 | 171 | | replyStatus = ReplyStatus.UserException; |
| | 172 | |
|
| 1 | 173 | | if (current.requestId != 0) |
| | 174 | | { |
| 1 | 175 | | ReplyStatusHelper.write(ostr, replyStatus); |
| 1 | 176 | | ostr.startEncapsulation(current.encoding, FormatType.SlicedFormat); |
| 1 | 177 | | ostr.writeException(ex); |
| 1 | 178 | | ostr.endEncapsulation(); |
| | 179 | | } |
| 1 | 180 | | break; |
| | 181 | |
|
| | 182 | | case DispatchException ex: |
| 1 | 183 | | exceptionId = ex.ice_id(); |
| 1 | 184 | | replyStatus = ex.replyStatus; |
| 1 | 185 | | dispatchExceptionMessage = ex.Message; |
| 1 | 186 | | break; |
| | 187 | |
|
| | 188 | | case LocalException ex: |
| 1 | 189 | | exceptionId = ex.ice_id(); |
| 1 | 190 | | replyStatus = ReplyStatus.UnknownLocalException; |
| 1 | 191 | | break; |
| | 192 | |
|
| | 193 | | default: |
| 1 | 194 | | replyStatus = ReplyStatus.UnknownException; |
| 1 | 195 | | exceptionId = exc.GetType().FullName ?? "System.Exception"; |
| | 196 | | break; |
| | 197 | | } |
| | 198 | |
|
| 1 | 199 | | if (replyStatus > ReplyStatus.UserException && current.requestId != 0) // two-way, so we marshal a reply |
| | 200 | | { |
| | 201 | | // We can't use ReplyStatusHelper to marshal a possibly unknown reply status value. |
| 1 | 202 | | ostr.writeByte((byte)replyStatus); |
| | 203 | |
|
| 1 | 204 | | if (replyStatus is ReplyStatus.ObjectNotExist or |
| 1 | 205 | | ReplyStatus.FacetNotExist or |
| 1 | 206 | | ReplyStatus.OperationNotExist) |
| | 207 | | { |
| 1 | 208 | | var rfe = exc as RequestFailedException; // can be null |
| | 209 | |
|
| 1 | 210 | | Identity id = rfe?.id ?? new Identity(); |
| 1 | 211 | | string facet = rfe?.facet ?? ""; |
| 1 | 212 | | if (id.name.Length == 0) |
| | 213 | | { |
| 1 | 214 | | id = current.id; |
| 1 | 215 | | facet = current.facet; |
| | 216 | | } |
| 1 | 217 | | string operation = rfe?.operation ?? ""; |
| 1 | 218 | | if (operation.Length == 0) |
| | 219 | | { |
| 1 | 220 | | operation = current.operation; |
| | 221 | | } |
| | 222 | |
|
| 1 | 223 | | Identity.ice_write(ostr, id); |
| | 224 | |
|
| 1 | 225 | | if (facet.Length == 0) |
| | 226 | | { |
| 1 | 227 | | ostr.writeStringSeq([]); |
| | 228 | | } |
| | 229 | | else |
| | 230 | | { |
| 1 | 231 | | ostr.writeStringSeq([facet]); |
| | 232 | | } |
| 1 | 233 | | ostr.writeString(operation); |
| | 234 | | // and we don't use the dispatchExceptionMessage. |
| | 235 | | } |
| | 236 | | else |
| | 237 | | { |
| | 238 | | // If the exception is a DispatchException, we keep its message as-is; otherwise, we create a custom |
| | 239 | | // message. This message doesn't include the stack trace. |
| 1 | 240 | | dispatchExceptionMessage ??= $"Dispatch failed with {exceptionId}: {exc.Message}"; |
| 1 | 241 | | ostr.writeString(dispatchExceptionMessage); |
| | 242 | | } |
| | 243 | | } |
| | 244 | |
|
| 1 | 245 | | return new OutgoingResponse( |
| 1 | 246 | | replyStatus, |
| 1 | 247 | | exceptionId, |
| 1 | 248 | | replyStatus > ReplyStatus.UserException ? exc.ToString() : null, |
| 1 | 249 | | ostr); |
| | 250 | | } |
| 1 | 251 | | } |
| | 252 | |
|
| | 253 | | /// <summary> |
| | 254 | | /// Starts the output stream for a reply, with everything up to and including the reply status. When the request ID |
| | 255 | | /// is 0 (one-way request), the returned output stream is empty. |
| | 256 | | /// </summary> |
| | 257 | | /// <param name="current">The current object of the corresponding incoming request.</param> |
| | 258 | | /// <param name="replyStatus">The reply status.</param> |
| | 259 | | /// <returns>The new output stream.</returns> |
| | 260 | | public static OutputStream startReplyStream(this Current current, ReplyStatus replyStatus = ReplyStatus.Ok) |
| | 261 | | { |
| 1 | 262 | | if (current.requestId == 0) |
| | 263 | | { |
| 1 | 264 | | return new OutputStream(); |
| | 265 | | } |
| | 266 | | else |
| | 267 | | { |
| 1 | 268 | | var ostr = new OutputStream( |
| 1 | 269 | | Util.currentProtocolEncoding, |
| 1 | 270 | | current.adapter.getCommunicator().instance.defaultsAndOverrides().defaultFormat); |
| | 271 | |
|
| 1 | 272 | | ostr.writeBlob(Protocol.replyHdr); |
| 1 | 273 | | ostr.writeInt(current.requestId); |
| 1 | 274 | | ReplyStatusHelper.write(ostr, replyStatus); |
| 1 | 275 | | return ostr; |
| | 276 | | } |
| | 277 | | } |
| | 278 | | } |