コード例 #1
0
    // Callback function responsible for receiving the acquired data samples from the communication loop started by StartLoopUnity().
    private bool DllCommunicationHandler(int exceptionCode, string exceptionDescription, int nSeq, IntPtr data, int dataInSize)
    {
        lock (callbackPointer)
        {
            try
            {
                // DEBUG
                //Console.WriteLine("Monitoring: " + nSeq + "|" + data + "|" + dataInSize);

                // Check if no exception were raised.
                if (exceptionCode == 0)
                {
                    try
                    {
                        // Convert our data pointer to an array format.
                        int[] dataArray = new int[dataInSize];
                        Marshal.Copy(data, dataArray, 0, dataInSize);

                        callbackPointer.GetCallbackRef()(nSeq, dataArray, dataInSize);
                    }
                    catch (OutOfMemoryException exception)
                    {
                        BufferAcqSamples bufferedSamples = LazyObject.Value;
                        lock (bufferedSamples)
                        {
                            bufferedSamples.actUncaughtException();
                            Debug.Log("Executing preventive approaches to deal with a potential OutOfMemoryException:\n" + exception.StackTrace);
                        }
                    }
                }
                else if (exceptionCode == 777) // Receiving devices found during scan.
                {
                    // Store list of found devices in a global variable shared between threads.
                    Debug.Log("Receiving Device: " + exceptionDescription);
                    List <String> devicesFound = PluxDevsFound.Value;
                    devicesFound.Add(exceptionDescription);
                }
                else
                {
                    Debug.Log("A new C++ exception was found...");
                    // Check if the current exception could be an uncaught one.
                    BufferAcqSamples bufferedSamples = LazyObject.Value;
                    lock (bufferedSamples)
                    {
                        // Activate flag in BufferedSamples object stating that an uncaught exception exists.
                        bufferedSamples.actUncaughtException();
                    }

                    throw new Exception(exceptionCode.ToString() + " | " + exceptionDescription);
                }
            }
            catch (OutOfMemoryException exception)
            {
                BufferAcqSamples bufferedSamples = LazyObject.Value;
                lock (bufferedSamples)
                {
                    bufferedSamples.actUncaughtException();
                    Debug.Log("Executing preventive approaches to deal with a potential OutOfMemoryException:\n" + exception.StackTrace);
                }
            }

            return(true);
        }
    }