|
| 1 | +// ------------------------------------------------------------------------ |
| 2 | +// Copyright 2021 The Dapr Authors |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// Unless required by applicable law or agreed to in writing, software |
| 8 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +// See the License for the specific language governing permissions and |
| 11 | +// limitations under the License. |
| 12 | +// ------------------------------------------------------------------------ |
| 13 | + |
| 14 | +namespace Dapr.Actors.Communication |
| 15 | +{ |
| 16 | + using System; |
| 17 | + using System.Collections.Generic; |
| 18 | + using System.IO; |
| 19 | + using System.Text.Json; |
| 20 | + using System.Threading.Tasks; |
| 21 | + using System.Xml; |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// This is the implmentation for <see cref="IActorMessageBodySerializationProvider"/>used by remoting service and client during |
| 25 | + /// request/response serialization . It uses request Wrapping and data contract for serialization. |
| 26 | + /// </summary> |
| 27 | + internal class ActorMessageBodyJsonSerializationProvider : IActorMessageBodySerializationProvider |
| 28 | + { |
| 29 | + public JsonSerializerOptions Options { get; } |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Initializes a new instance of the <see cref="ActorMessageBodyJsonSerializationProvider"/> class. |
| 33 | + /// </summary> |
| 34 | + public ActorMessageBodyJsonSerializationProvider(JsonSerializerOptions options) |
| 35 | + { |
| 36 | + Options = options; |
| 37 | + } |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Creates a MessageFactory for Wrapped Message Json Remoting Types. This is used to create Remoting Request/Response objects. |
| 41 | + /// </summary> |
| 42 | + /// <returns> |
| 43 | + /// <see cref="IActorMessageBodyFactory" /> that provides an instance of the factory for creating |
| 44 | + /// remoting request and response message bodies. |
| 45 | + /// </returns> |
| 46 | + public IActorMessageBodyFactory CreateMessageBodyFactory() |
| 47 | + { |
| 48 | + return new WrappedRequestMessageFactory(); |
| 49 | + } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Creates IActorRequestMessageBodySerializer for a serviceInterface using Wrapped Message Json implementation. |
| 53 | + /// </summary> |
| 54 | + /// <param name="serviceInterfaceType">The remoted service interface.</param> |
| 55 | + /// <param name="methodRequestParameterTypes">The union of parameter types of all of the methods of the specified interface.</param> |
| 56 | + /// <param name="wrappedRequestMessageTypes">Wrapped Request Types for all Methods.</param> |
| 57 | + /// <returns> |
| 58 | + /// An instance of the <see cref="IActorRequestMessageBodySerializer" /> that can serialize the service |
| 59 | + /// actor request message body to a messaging body for transferring over the transport. |
| 60 | + /// </returns> |
| 61 | + public IActorRequestMessageBodySerializer CreateRequestMessageBodySerializer( |
| 62 | + Type serviceInterfaceType, |
| 63 | + IEnumerable<Type> methodRequestParameterTypes, |
| 64 | + IEnumerable<Type> wrappedRequestMessageTypes = null) |
| 65 | + { |
| 66 | + return new MemoryStreamMessageBodySerializer<WrappedMessageBody, WrappedMessageBody>(Options, serviceInterfaceType, methodRequestParameterTypes, wrappedRequestMessageTypes); |
| 67 | + } |
| 68 | + |
| 69 | + /// <summary> |
| 70 | + /// Creates IActorResponseMessageBodySerializer for a serviceInterface using Wrapped Message Json implementation. |
| 71 | + /// </summary> |
| 72 | + /// <param name="serviceInterfaceType">The remoted service interface.</param> |
| 73 | + /// <param name="methodReturnTypes">The return types of all of the methods of the specified interface.</param> |
| 74 | + /// <param name="wrappedResponseMessageTypes">Wrapped Response Types for all remoting methods.</param> |
| 75 | + /// <returns> |
| 76 | + /// An instance of the <see cref="IActorResponseMessageBodySerializer" /> that can serialize the service |
| 77 | + /// actor response message body to a messaging body for transferring over the transport. |
| 78 | + /// </returns> |
| 79 | + public IActorResponseMessageBodySerializer CreateResponseMessageBodySerializer( |
| 80 | + Type serviceInterfaceType, |
| 81 | + IEnumerable<Type> methodReturnTypes, |
| 82 | + IEnumerable<Type> wrappedResponseMessageTypes = null) |
| 83 | + { |
| 84 | + return new MemoryStreamMessageBodySerializer<WrappedMessageBody, WrappedMessageBody>(Options, serviceInterfaceType, methodReturnTypes, wrappedResponseMessageTypes); |
| 85 | + } |
| 86 | + |
| 87 | + /// <summary> |
| 88 | + /// Default serializer for service remoting request and response message body that uses the |
| 89 | + /// memory stream to create outgoing message buffers. |
| 90 | + /// </summary> |
| 91 | + private class MemoryStreamMessageBodySerializer<TRequest, TResponse> : |
| 92 | + IActorRequestMessageBodySerializer, |
| 93 | + IActorResponseMessageBodySerializer |
| 94 | + where TRequest : IActorRequestMessageBody |
| 95 | + where TResponse : IActorResponseMessageBody |
| 96 | + { |
| 97 | + private readonly JsonSerializerOptions serializerOptions; |
| 98 | + |
| 99 | + public MemoryStreamMessageBodySerializer( |
| 100 | + JsonSerializerOptions serializerOptions, |
| 101 | + Type serviceInterfaceType, |
| 102 | + IEnumerable<Type> methodRequestParameterTypes, |
| 103 | + IEnumerable<Type> wrappedRequestMessageTypes = null) |
| 104 | + { |
| 105 | + var _methodRequestParameterTypes = new List<Type>(methodRequestParameterTypes); |
| 106 | + var _wrappedRequestMessageTypes = new List<Type>(wrappedRequestMessageTypes); |
| 107 | + |
| 108 | + this.serializerOptions = new(serializerOptions) |
| 109 | + { |
| 110 | + // Workaround since WrappedMessageBody creates an object |
| 111 | + // with parameters as fields |
| 112 | + IncludeFields = true, |
| 113 | + }; |
| 114 | + |
| 115 | + this.serializerOptions.Converters.Add(new ActorMessageBodyJsonConverter<TRequest>(_methodRequestParameterTypes, _wrappedRequestMessageTypes)); |
| 116 | + this.serializerOptions.Converters.Add(new ActorMessageBodyJsonConverter<TResponse>(_methodRequestParameterTypes, _wrappedRequestMessageTypes)); |
| 117 | + } |
| 118 | + |
| 119 | + byte[] IActorRequestMessageBodySerializer.Serialize(IActorRequestMessageBody actorRequestMessageBody) |
| 120 | + { |
| 121 | + if (actorRequestMessageBody == null) |
| 122 | + { |
| 123 | + return null; |
| 124 | + } |
| 125 | + |
| 126 | + return JsonSerializer.SerializeToUtf8Bytes<object>(actorRequestMessageBody, this.serializerOptions); |
| 127 | + } |
| 128 | + |
| 129 | + async ValueTask<IActorRequestMessageBody> IActorRequestMessageBodySerializer.DeserializeAsync(Stream stream) |
| 130 | + { |
| 131 | + if (stream == null) |
| 132 | + { |
| 133 | + return default; |
| 134 | + } |
| 135 | + |
| 136 | + if (stream.Length == 0) |
| 137 | + { |
| 138 | + return default; |
| 139 | + } |
| 140 | + |
| 141 | + stream.Position = 0; |
| 142 | + return await JsonSerializer.DeserializeAsync<TRequest>(stream, this.serializerOptions); |
| 143 | + } |
| 144 | + |
| 145 | + byte[] IActorResponseMessageBodySerializer.Serialize(IActorResponseMessageBody actorResponseMessageBody) |
| 146 | + { |
| 147 | + if (actorResponseMessageBody == null) |
| 148 | + { |
| 149 | + return null; |
| 150 | + } |
| 151 | + |
| 152 | + return JsonSerializer.SerializeToUtf8Bytes<object>(actorResponseMessageBody, this.serializerOptions); |
| 153 | + } |
| 154 | + |
| 155 | + async ValueTask<IActorResponseMessageBody> IActorResponseMessageBodySerializer.DeserializeAsync(Stream messageBody) |
| 156 | + { |
| 157 | + if (messageBody == null) |
| 158 | + { |
| 159 | + return null; |
| 160 | + } |
| 161 | + |
| 162 | + using var stream = new MemoryStream(); |
| 163 | + messageBody.CopyTo(stream); |
| 164 | + stream.Position = 0; |
| 165 | + |
| 166 | + if (stream.Capacity == 0) |
| 167 | + { |
| 168 | + return null; |
| 169 | + } |
| 170 | + |
| 171 | + return await JsonSerializer.DeserializeAsync<TResponse>(stream, this.serializerOptions); |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | +} |
0 commit comments