OLD | NEW |
(Empty) | |
| 1 #region Copyright notice and license |
| 2 // Copyright 2015-2016, Google Inc. |
| 3 // All rights reserved. |
| 4 // |
| 5 // Redistribution and use in source and binary forms, with or without |
| 6 // modification, are permitted provided that the following conditions are |
| 7 // met: |
| 8 // |
| 9 // * Redistributions of source code must retain the above copyright |
| 10 // notice, this list of conditions and the following disclaimer. |
| 11 // * Redistributions in binary form must reproduce the above |
| 12 // copyright notice, this list of conditions and the following disclaimer |
| 13 // in the documentation and/or other materials provided with the |
| 14 // distribution. |
| 15 // * Neither the name of Google Inc. nor the names of its |
| 16 // contributors may be used to endorse or promote products derived from |
| 17 // this software without specific prior written permission. |
| 18 // |
| 19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 #endregion |
| 31 using System; |
| 32 using System.Runtime.InteropServices; |
| 33 using System.Threading.Tasks; |
| 34 using Grpc.Core.Profiling; |
| 35 |
| 36 namespace Grpc.Core.Internal |
| 37 { |
| 38 /// <summary> |
| 39 /// grpc_metadata_array from <c>grpc/grpc.h</c> |
| 40 /// </summary> |
| 41 internal class MetadataArraySafeHandle : SafeHandleZeroIsInvalid |
| 42 { |
| 43 static readonly NativeMethods Native = NativeMethods.Get(); |
| 44 |
| 45 private MetadataArraySafeHandle() |
| 46 { |
| 47 } |
| 48 |
| 49 public static MetadataArraySafeHandle Create(Metadata metadata) |
| 50 { |
| 51 using (Profilers.ForCurrentThread().NewScope("MetadataArraySafeHandl
e.Create")) |
| 52 { |
| 53 // TODO(jtattermusch): we might wanna check that the metadata is
readonly |
| 54 var metadataArray = Native.grpcsharp_metadata_array_create(new U
IntPtr((ulong)metadata.Count)); |
| 55 for (int i = 0; i < metadata.Count; i++) |
| 56 { |
| 57 var valueBytes = metadata[i].GetSerializedValueUnsafe(); |
| 58 Native.grpcsharp_metadata_array_add(metadataArray, metadata[
i].Key, valueBytes, new UIntPtr((ulong)valueBytes.Length)); |
| 59 } |
| 60 return metadataArray; |
| 61 } |
| 62 } |
| 63 |
| 64 /// <summary> |
| 65 /// Reads metadata from pointer to grpc_metadata_array |
| 66 /// </summary> |
| 67 public static Metadata ReadMetadataFromPtrUnsafe(IntPtr metadataArray) |
| 68 { |
| 69 if (metadataArray == IntPtr.Zero) |
| 70 { |
| 71 return null; |
| 72 } |
| 73 |
| 74 ulong count = Native.grpcsharp_metadata_array_count(metadataArray).T
oUInt64(); |
| 75 |
| 76 var metadata = new Metadata(); |
| 77 for (ulong i = 0; i < count; i++) |
| 78 { |
| 79 var index = new UIntPtr(i); |
| 80 string key = Marshal.PtrToStringAnsi(Native.grpcsharp_metadata_a
rray_get_key(metadataArray, index)); |
| 81 var bytes = new byte[Native.grpcsharp_metadata_array_get_value_l
ength(metadataArray, index).ToUInt64()]; |
| 82 Marshal.Copy(Native.grpcsharp_metadata_array_get_value(metadataA
rray, index), bytes, 0, bytes.Length); |
| 83 metadata.Add(Metadata.Entry.CreateUnsafe(key, bytes)); |
| 84 } |
| 85 return metadata; |
| 86 } |
| 87 |
| 88 internal IntPtr Handle |
| 89 { |
| 90 get |
| 91 { |
| 92 return handle; |
| 93 } |
| 94 } |
| 95 |
| 96 protected override bool ReleaseHandle() |
| 97 { |
| 98 Native.grpcsharp_metadata_array_destroy_full(handle); |
| 99 return true; |
| 100 } |
| 101 } |
| 102 } |
OLD | NEW |