aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.PMR/NativePMR.cs
blob: 5666fbeb0693e4a60b5fea93d19ea42b03012ad8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using Google.Protobuf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Runtime.ExceptionServices;

namespace Tango.PMR
{
    /// <summary>
    /// Represents a standard C++ protobuf method delegate.
    /// </summary>
    /// <param name="requestArray">The request array.</param>
    /// <param name="requestArraySize">Size of the request array.</param>
    /// <param name="resultArray">The result array.</param>
    /// <returns>The size of the result array.</returns>
    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    public delegate int NativeMethodDelegate(IntPtr requestArray, int requestArraySize, ref IntPtr resultArray);

    /// <summary>
    /// Represents a PMR wrapper for invoking native protobuf supported methods.
    /// </summary>
    /// <typeparam name="Request">The type of the Request.</typeparam>
    /// <typeparam name="Response">The type of the Response.</typeparam>
    public class NativePMR<Request, Response> where Request : IMessage<Request> where Response : class, IMessage<Response>
    {
        private NativeMethodDelegate _nativeMethod;

        /// <summary>
        /// Initializes a new instance of the <see cref="NativePMR{Request, Response}"/> class.
        /// </summary>
        /// <param name="nativeMethod">The native method.</param>
        public NativePMR(NativeMethodDelegate nativeMethod)
        {
            _nativeMethod = nativeMethod;
        }

        /// <summary>
        /// Invokes the native method with the specified request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="nativeMethod">The native method.</param>
        /// <returns></returns>
        [HandleProcessCorruptedStateExceptions]
        public Response Invoke(Request request)
        {
            try
            {
                //Serialize the request to byte array.
                byte[] messageData = request.ToByteArray();

                //Allocate unmanaged array on memory.
                IntPtr unmanagedArr = Marshal.AllocHGlobal(messageData.Length);

                //Copy the request data to the unmanaged array.
                Marshal.Copy(messageData, 0, unmanagedArr, messageData.Length);

                //Initialize pointer for pointing to the result array.
                IntPtr output = IntPtr.Zero;

                //Invoke the native method.
                int size = _nativeMethod(unmanagedArr, messageData.Length, ref output);

                if (size == 0) return null;

                //Initialize a new byte array for holding the native result.
                byte[] responseData = new byte[size];

                //Copy the unmanaged byte array result to the managed result array.
                Marshal.Copy(output, responseData, 0, size);

                //Generate response parser.
                MessageParser<Response> parser = new MessageParser<Response>(() => Activator.CreateInstance<Response>());

                //Parse the response
                Response response = parser.ParseFrom(responseData);

                //Free unmanaged memory.
                Marshal.FreeHGlobal(unmanagedArr);

                return response;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}