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