| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | #nullable enable |
| | | 4 | | |
| | | 5 | | using System.ComponentModel; |
| | | 6 | | |
| | | 7 | | namespace Ice; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// The base interface for servants. |
| | | 11 | | /// </summary> |
| | | 12 | | [SliceTypeId("::Ice::Object")] |
| | | 13 | | public interface Object |
| | | 14 | | { |
| | | 15 | | /// <summary> |
| | | 16 | | /// Dispatches an incoming request and returns the corresponding outgoing response. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <param name="request">The incoming request.</param> |
| | | 19 | | /// <returns>A value task that holds the outgoing response.</returns> |
| | | 20 | | /// <remarks>If an exception is thrown, Ice will marshal it as the response payload.</remarks> |
| | | 21 | | public ValueTask<OutgoingResponse> dispatchAsync(IncomingRequest request) => |
| | 0 | 22 | | request.current.operation switch |
| | 0 | 23 | | { |
| | 0 | 24 | | "ice_id" => iceD_ice_idAsync(this, request), |
| | 0 | 25 | | "ice_ids" => iceD_ice_idsAsync(this, request), |
| | 0 | 26 | | "ice_isA" => iceD_ice_isAAsync(this, request), |
| | 0 | 27 | | "ice_ping" => iceD_ice_pingAsync(this, request), |
| | 0 | 28 | | _ => throw new OperationNotExistException() |
| | 0 | 29 | | }; |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Tests whether this object supports a specific Slice interface. |
| | | 33 | | /// </summary> |
| | | 34 | | /// |
| | | 35 | | /// <param name="s">The type ID of the Slice interface to test against.</param> |
| | | 36 | | /// <param name="current">The Current object of the incoming request.</param> |
| | | 37 | | /// <returns><see langword="true"/> if this object implements the Slice interface specified by <paramref name="s"/> |
| | | 38 | | /// or implements a derived interface, <see langword="false"/> otherwise.</returns> |
| | | 39 | | public bool ice_isA(string s, Current current) |
| | | 40 | | { |
| | 1 | 41 | | foreach (Type type in GetType().GetInterfaces()) |
| | | 42 | | { |
| | 1 | 43 | | if (type.GetSliceTypeId() is string typeId && typeId == s) |
| | | 44 | | { |
| | 1 | 45 | | return true; |
| | | 46 | | } |
| | | 47 | | } |
| | 1 | 48 | | return false; |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Tests whether this object can be reached. |
| | | 53 | | /// </summary> |
| | | 54 | | /// <param name="current">The Current object of the incoming request.</param> |
| | | 55 | | public void ice_ping(Current current) |
| | | 56 | | { |
| | | 57 | | // does nothing |
| | 1 | 58 | | } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Returns the Slice interfaces supported by this object as a list of Slice type IDs. |
| | | 62 | | /// </summary> |
| | | 63 | | /// <param name="current">The Current object of the incoming request.</param> |
| | | 64 | | /// <returns>The Slice type IDs of the interfaces supported by this object, in alphabetical order.</returns> |
| | | 65 | | public string[] ice_ids(Current current) |
| | | 66 | | { |
| | 1 | 67 | | var sortedSet = new SortedSet<string>(StringComparer.Ordinal); |
| | 1 | 68 | | foreach (Type type in GetType().GetInterfaces()) |
| | | 69 | | { |
| | 1 | 70 | | if (type.GetSliceTypeId() is string typeId) |
| | | 71 | | { |
| | 1 | 72 | | sortedSet.Add(typeId); |
| | | 73 | | } |
| | | 74 | | } |
| | 1 | 75 | | return sortedSet.ToArray(); |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Returns the type ID of the most-derived Slice interface supported by this object. |
| | | 80 | | /// </summary> |
| | | 81 | | /// <param name="current">The Current object of the incoming request.</param> |
| | | 82 | | /// <returns>The Slice type ID of the most-derived interface.</returns> |
| | 0 | 83 | | public string ice_id(Current current) => ice_staticId(); |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Returns the type ID of the associated Slice interface. |
| | | 87 | | /// </summary> |
| | | 88 | | /// <returns>The return value is always <c>"::Ice::Object"</c>.</returns> |
| | 1 | 89 | | public static string ice_staticId() => "::Ice::Object"; |
| | | 90 | | |
| | | 91 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| | | 92 | | protected static ValueTask<OutgoingResponse> iceD_ice_isAAsync(Object obj, IncomingRequest request) |
| | | 93 | | { |
| | 1 | 94 | | InputStream istr = request.inputStream; |
| | 1 | 95 | | istr.startEncapsulation(); |
| | 1 | 96 | | string iceP_id = istr.readString(); |
| | 1 | 97 | | istr.endEncapsulation(); |
| | 1 | 98 | | bool ret = obj.ice_isA(iceP_id, request.current); |
| | 1 | 99 | | return new(request.current.createOutgoingResponse(ret, static (ostr, ret) => ostr.writeBool(ret))); |
| | | 100 | | } |
| | | 101 | | |
| | | 102 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| | | 103 | | protected static ValueTask<OutgoingResponse> iceD_ice_pingAsync(Object obj, IncomingRequest request) |
| | | 104 | | { |
| | 1 | 105 | | InputStream istr = request.inputStream; |
| | 1 | 106 | | istr.skipEmptyEncapsulation(); |
| | 1 | 107 | | obj.ice_ping(request.current); |
| | 1 | 108 | | return new(request.current.createEmptyOutgoingResponse()); |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| | | 112 | | protected static ValueTask<OutgoingResponse> iceD_ice_idsAsync(Object obj, IncomingRequest request) |
| | | 113 | | { |
| | 1 | 114 | | InputStream istr = request.inputStream; |
| | 1 | 115 | | istr.skipEmptyEncapsulation(); |
| | 1 | 116 | | string[] ret = obj.ice_ids(request.current); |
| | 1 | 117 | | return new(request.current.createOutgoingResponse(ret, static (ostr, ret) => ostr.writeStringSeq(ret))); |
| | | 118 | | } |
| | | 119 | | |
| | | 120 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| | | 121 | | protected static ValueTask<OutgoingResponse> iceD_ice_idAsync(Object obj, IncomingRequest request) |
| | | 122 | | { |
| | 1 | 123 | | InputStream istr = request.inputStream; |
| | 1 | 124 | | istr.skipEmptyEncapsulation(); |
| | 1 | 125 | | string ret = obj.ice_id(request.current); |
| | 1 | 126 | | return new(request.current.createOutgoingResponse(ret, static (ostr, ret) => ostr.writeString(ret))); |
| | | 127 | | } |
| | | 128 | | } |
| | | 129 | | |
| | | 130 | | /// <summary> |
| | | 131 | | /// Base class for dynamic dispatch servants. |
| | | 132 | | /// </summary> |
| | | 133 | | /// <remarks>This class is provided for backward compatibility. You should consider deriving directly from |
| | | 134 | | /// <see cref="Object"/> and overriding the <see cref="Object.dispatchAsync"/> method.</remarks> |
| | | 135 | | public abstract class Blobject : Object |
| | | 136 | | { |
| | | 137 | | /// <summary> |
| | | 138 | | /// Dispatches an incoming request. |
| | | 139 | | /// </summary> |
| | | 140 | | /// <param name="inParams">An encapsulation containing the encoded in-parameters for the operation.</param> |
| | | 141 | | /// <param name="outParams">An encapsulation containing the encoded result for the operation.</param> |
| | | 142 | | /// <param name="current">The Current object of the incoming request.</param> |
| | | 143 | | /// <returns><see langword="true"/> if the dispatch completes successfully, <see langword="false"/> if the dispatch |
| | | 144 | | /// completes with a user exception encoded in <paramref name="outParams"/>.</returns> |
| | | 145 | | public abstract bool ice_invoke(byte[] inParams, out byte[] outParams, Current current); |
| | | 146 | | |
| | | 147 | | public ValueTask<OutgoingResponse> dispatchAsync(IncomingRequest request) |
| | | 148 | | { |
| | | 149 | | byte[] inEncaps = request.inputStream.readEncapsulation(out _); |
| | | 150 | | bool ok = ice_invoke(inEncaps, out byte[] outEncaps, request.current); |
| | | 151 | | return new(request.current.createOutgoingResponse(ok, outEncaps)); |
| | | 152 | | } |
| | | 153 | | } |
| | | 154 | | |
| | | 155 | | /// <summary> |
| | | 156 | | /// Base class for asynchronous dynamic dispatch servants. |
| | | 157 | | /// </summary> |
| | | 158 | | /// <remarks>This class is provided for backward compatibility. You should consider deriving directly from |
| | | 159 | | /// <see cref="Object"/> and overriding the <see cref="Object.dispatchAsync"/> method.</remarks> |
| | | 160 | | public abstract class BlobjectAsync : Object |
| | | 161 | | { |
| | | 162 | | /// <summary> |
| | | 163 | | /// Dispatches an incoming request. |
| | | 164 | | /// </summary> |
| | | 165 | | /// <param name="inEncaps">An encapsulation containing the encoded in-parameters for the operation.</param> |
| | | 166 | | /// <param name="current">The Current object of the incoming request.</param> |
| | | 167 | | /// <returns>A task that will complete with an instance of <see cref="Object_Ice_invokeResult"/>.</returns> |
| | | 168 | | public abstract Task<Object_Ice_invokeResult> ice_invokeAsync(byte[] inEncaps, Current current); |
| | | 169 | | |
| | | 170 | | public async ValueTask<OutgoingResponse> dispatchAsync(IncomingRequest request) |
| | | 171 | | { |
| | | 172 | | byte[] inEncaps = request.inputStream.readEncapsulation(out _); |
| | | 173 | | Object_Ice_invokeResult result = await ice_invokeAsync(inEncaps, request.current).ConfigureAwait(false); |
| | | 174 | | return request.current.createOutgoingResponse(result.returnValue, result.outEncaps); |
| | | 175 | | } |
| | | 176 | | } |