[Applications.RPCPort] Add new constructor and method for Parcel (#3549)
authorhjhun <36876573+hjhun@users.noreply.github.com>
Thu, 16 Sep 2021 07:20:20 +0000 (16:20 +0900)
committerGitHub <noreply@github.com>
Thu, 16 Sep 2021 07:20:20 +0000 (16:20 +0900)
* Add new ctor and method for Parcel

Adds:
 - public Parcel(byte[] bytes);
 - public byte[] Marshall();

Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
* Fix variable name and remove unnecessary type casting

Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
* Change method name to 'ToBytes()'

Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
src/Tizen.Applications.Common/Interop/Interop.RPCPort.cs
src/Tizen.Applications.Common/Tizen.Applications.RPCPort/Parcel.cs

index e4d2728..4957f22 100755 (executable)
@@ -169,6 +169,14 @@ internal static partial class Interop
             //int rpc_port_parcel_header_get_timestamp(rpc_port_parcel_header_h header, struct timespec *timestamp);
             [DllImport(Libraries.RpcPort, EntryPoint = "rpc_port_parcel_header_get_timestamp")]
             internal static extern ErrorCode GetTimeStamp(IntPtr parcelHeaderHandle, ref Libc.TimeStamp time);
+
+            //int rpc_port_parcel_get_raw(rpc_port_parcel_h h, void **raw, unsigned int *size);
+            [DllImport(Libraries.RpcPort, EntryPoint = "rpc_port_parcel_get_raw")]
+            internal static extern ErrorCode GetRaw(IntPtr parcelHandle, out IntPtr raw, out uint size);
+
+            //int rpc_port_parcel_create_from_raw(rpc_port_parcel_h *h, const void *raw, unsigned int size);
+            [DllImport(Libraries.RpcPort, EntryPoint = "rpc_port_parcel_create_from_raw")]
+            internal static extern ErrorCode CreateFromRaw(out IntPtr parcelHandle, byte[] raw, uint size);
         }
 
         internal static partial class Proxy
index bdb3f47..a5f88f5 100755 (executable)
@@ -15,6 +15,7 @@
  */
 
 using System;
+using System.Runtime.InteropServices;
 
 namespace Tizen.Applications.RPCPort
 {
@@ -182,6 +183,37 @@ namespace Tizen.Applications.RPCPort
         }
 
         /// <summary>
+        /// Constructor with the raw bytes.
+        /// </summary>
+        /// <param name="bytes">The raw bytes.</param>
+        /// <exception cref="InvalidIOException">Thrown when an internal IO error occurs.</exception>
+        /// <since_tizen> 9 </since_tizen>
+        public Parcel(byte[] bytes)
+        {
+            if (bytes == null)
+                throw new InvalidIOException();
+            var r = Interop.LibRPCPort.Parcel.CreateFromRaw(out _handle, bytes, (uint)bytes.Length);
+            if (r != Interop.LibRPCPort.ErrorCode.None)
+                throw new InvalidIOException();
+        }
+
+        /// <summary>
+        /// Gets the raw bytes of the parcel.
+        /// </summary>
+        /// <returns>The raw bytes of the parcel.</returns>
+        /// <exception cref="InvalidIOException">Thrown when an internal IO error occurs.</exception>
+        /// <since_tizen> 9 </since_tizen>
+        public byte[] ToBytes()
+        {
+            var r = Interop.LibRPCPort.Parcel.GetRaw(_handle, out IntPtr raw, out uint size);
+            if (r != Interop.LibRPCPort.ErrorCode.None)
+                throw new InvalidIOException();
+            byte[] bytes = new byte[size];
+            Marshal.Copy(raw, bytes, 0, (int)size);
+            return bytes;
+        }
+
+        /// <summary>
         /// Sends parcel data through the port.
         /// </summary>
         /// <param name="p">The RPC port object for writing data.</param>