@@ -139,6 +139,14 @@ public static bool Init(SentryOptions options)
139139 }
140140 }
141141
142+ unsafe
143+ {
144+ var cTransport = sentry_transport_new ( & nativeTransport ) ;
145+ sentry_transport_set_state ( cTransport , GCHandle . ToIntPtr ( GCHandle . Alloc ( options ) ) ) ;
146+ sentry_transport_set_free_func ( cTransport , & nativeTransportFree ) ;
147+ sentry_options_set_transport ( cOptions , cTransport ) ;
148+ }
149+
142150 options . DiagnosticLogger ? . LogDebug ( "Initializing sentry native" ) ;
143151 return 0 == sentry_init ( cOptions ) ;
144152 }
@@ -364,6 +372,66 @@ internal struct sentry_value_t
364372 [ DllImport ( "sentry-native" ) ]
365373 private static extern void sentry_options_set_auto_session_tracking ( IntPtr options , int debug ) ;
366374
375+ [ DllImport ( "sentry-native" ) ]
376+ private static extern void sentry_options_set_transport ( IntPtr options , IntPtr transport ) ;
377+
378+ [ DllImport ( "sentry-native" ) ]
379+ private static extern unsafe IntPtr sentry_transport_new ( delegate * unmanaged/*[Cdecl]*/ < IntPtr , IntPtr , void > sendFunc ) ;
380+
381+ [ DllImport ( "sentry-native" ) ]
382+ private static extern void sentry_transport_set_state ( IntPtr transport , IntPtr state ) ;
383+
384+ [ DllImport ( "sentry-native" ) ]
385+ private static extern unsafe void sentry_transport_set_free_func ( IntPtr transport , delegate * unmanaged/*[Cdecl]*/ < IntPtr , void > freeFunc ) ;
386+
387+ [ DllImport ( "sentry-native" ) ]
388+ private static extern int sentry_envelope_write_to_file ( IntPtr envelope , string path ) ;
389+
390+ [ DllImport ( "sentry-native" ) ]
391+ private static extern void sentry_envelope_free ( IntPtr envelope ) ;
392+
393+ [ DllImport ( "sentry-native" ) ]
394+ private static extern void sentry_free ( IntPtr ptr ) ;
395+
396+ [ UnmanagedCallersOnly ]
397+ private static void nativeTransport ( IntPtr envelope , IntPtr state )
398+ {
399+ try
400+ {
401+ var options = GCHandle . FromIntPtr ( state ) . Target as SentryOptions ;
402+ var cacheDirectoryPath = options ? . TryGetProcessSpecificCacheDirectoryPath ( ) ;
403+ if ( cacheDirectoryPath is null )
404+ {
405+ return ;
406+ }
407+
408+ var envelopePath = Path . Combine ( cacheDirectoryPath , SentryId . Create ( ) + ".envelope" ) ;
409+ if ( sentry_envelope_write_to_file ( envelope , envelopePath ) != 0 )
410+ {
411+ options ? . DiagnosticLogger ? . LogError ( "Failed to write native envelope: {0}" , envelopePath ) ;
412+ }
413+ else
414+ {
415+ options ? . DiagnosticLogger ? . LogDebug ( "Wrote native envelope: {0}" , envelopePath ) ;
416+ }
417+ }
418+ catch
419+ {
420+ // never allow an exception back to native code - it would crash the app
421+ }
422+ finally
423+ {
424+ sentry_envelope_free ( envelope ) ;
425+ }
426+ }
427+
428+ [ UnmanagedCallersOnly ]
429+ private static void nativeTransportFree ( IntPtr state )
430+ {
431+ var handle = GCHandle . FromIntPtr ( state ) ;
432+ handle . Free ( ) ;
433+ }
434+
367435 [ DllImport ( "sentry-native" ) ]
368436 private static extern unsafe void sentry_options_set_logger ( IntPtr options , delegate * unmanaged/*[Cdecl]*/ < int , IntPtr , IntPtr , IntPtr , void > logger , IntPtr userData ) ;
369437
0 commit comments