aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Utilities/Tango.RemoteRunner.UI/App.xaml.cs
blob: 9bfeb819224cb0866a7632a80b73664ab9a28fd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace Tango.RemoteRunner.UI
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
    }
}
/span>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 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> /// 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> public delegate int NativeMethodDelegate(IntPtr requestArray, int requestArraySize, ref IntPtr resultArray); /// <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; } } } }