private bool HandlePackage(IntPtr data, int dataSize)
        {
            NativeArray nativeArray = new NativeArray(data, dataSize);
            bool        result      = _managedDataHandler.HandlePackage(nativeArray);

            if (result)
            {
                //We should override data pointer, otherwise when GC will call destructor, we will get access violation
                //because data was already removed by native code
                nativeArray.Data = IntPtr.Zero;
            }
            return(result);
        }
Example #2
0
 unsafe private void Read(Stream stream)
 {
     byte[] dataReadBuffer   = new byte[DataBufferMaxSize];
     byte[] headerReadBuffer = new byte[sizeof(byte) + sizeof(int)];
     while (!_disposed)
     {
         try
         {
             int  read = stream.Read(headerReadBuffer, 0, headerReadBuffer.Length);
             byte dataMarker;
             int  dataSize;
             if (read == 0)
             {
                 continue;
             }
             fixed(byte *headerReadBufferPointer = headerReadBuffer)
             {
                 dataMarker = *(headerReadBufferPointer);
                 dataSize   = *(int *)(headerReadBufferPointer + 1);
             }
             if (dataSize > dataReadBuffer.Length)
             {
                 LoggingProvider.Current.Log(TraceEventType.Error, string.Format("Attempt to read data bigger than buffer. Buffer size: {0}. Data size: {1}", dataReadBuffer.Length, dataSize));
                 continue;
             }
             stream.Read(dataReadBuffer, 0, dataSize);
             NativeArray         array   = NativeArray.Copy(dataReadBuffer, dataSize);
             IManagedDataHandler handler = _handlers[dataMarker];
             if (handler == null || handler.HandlePackage(array))
             {
                 array.Dispose();
             }
         }
         catch (ObjectDisposedException objectDisposedException)
         {
             LoggingProvider.Current.Log(TraceEventType.Information, objectDisposedException);
             return;
         }
         catch (Exception exception)
         {
             LoggingProvider.Current.Log(TraceEventType.Warning, exception);
             break;
         }
     }
 }