/src/Network/NWDataTransferReport.cs

https://github.com/xamarin/xamarin-macios · C# · 173 lines · 118 code · 47 blank · 8 comment · 8 complexity · 4ab794b50ea8ab9793e661014500d9e7 MD5 · raw file

  1. //
  2. // NWDataTransferReport.cs: Bindings the Network nw_data_transfer_report_t API.
  3. //
  4. // Authors:
  5. // Manuel de la Pena (mandel@microsoft.com)
  6. //
  7. // Copyright 2019 Microsoft Inc
  8. //
  9. #nullable enable
  10. using System;
  11. using System.Runtime.InteropServices;
  12. using System.Runtime.CompilerServices;
  13. using ObjCRuntime;
  14. using Foundation;
  15. using CoreFoundation;
  16. using OS_nw_data_transfer_report=System.IntPtr;
  17. using OS_nw_connection=System.IntPtr;
  18. using OS_nw_interface=System.IntPtr;
  19. namespace Network {
  20. [TV (13,0), Mac (10,15), iOS (13,0), Watch (6,0)]
  21. public enum NWDataTransferReportState {
  22. Collecting = 1,
  23. Collected = 2,
  24. }
  25. [TV (13,0), Mac (10,15), iOS (13,0), Watch (6,0)]
  26. public class NWDataTransferReport : NativeObject {
  27. internal NWDataTransferReport (IntPtr handle, bool owns) : base (handle, owns) {}
  28. [DllImport (Constants.NetworkLibrary)]
  29. static extern OS_nw_data_transfer_report nw_connection_create_new_data_transfer_report (OS_nw_connection connection);
  30. public NWDataTransferReport (NWConnection connection)
  31. {
  32. if (connection == null)
  33. throw new ArgumentNullException (nameof (connection));
  34. InitializeHandle (nw_connection_create_new_data_transfer_report (connection.Handle));
  35. }
  36. [DllImport (Constants.NetworkLibrary)]
  37. static extern uint nw_data_transfer_report_get_path_count (OS_nw_data_transfer_report report);
  38. public uint PathCount => nw_data_transfer_report_get_path_count (GetCheckedHandle ());
  39. [DllImport (Constants.NetworkLibrary)]
  40. static extern ulong nw_data_transfer_report_get_duration_milliseconds (OS_nw_data_transfer_report report);
  41. public TimeSpan Duration => TimeSpan.FromMilliseconds (nw_data_transfer_report_get_duration_milliseconds (GetCheckedHandle ()));
  42. [DllImport (Constants.NetworkLibrary)]
  43. static extern OS_nw_interface nw_data_transfer_report_copy_path_interface (OS_nw_data_transfer_report report, uint path_index);
  44. public NWInterface GetInterface (uint pathIndex)
  45. => new NWInterface (nw_data_transfer_report_copy_path_interface (GetCheckedHandle (), pathIndex), owns: true);
  46. [DllImport (Constants.NetworkLibrary)]
  47. static extern ulong nw_data_transfer_report_get_received_application_byte_count (OS_nw_data_transfer_report report, uint path_index);
  48. public ulong GetApplicationReceivedByteCount (uint pathIndex)
  49. => nw_data_transfer_report_get_received_application_byte_count (GetCheckedHandle (), pathIndex);
  50. [DllImport (Constants.NetworkLibrary)]
  51. static extern ulong nw_data_transfer_report_get_sent_application_byte_count (OS_nw_data_transfer_report report, uint path_index);
  52. public ulong GetApplicationSentByteCount (uint pathIndex)
  53. => nw_data_transfer_report_get_sent_application_byte_count (GetCheckedHandle (), pathIndex);
  54. [DllImport (Constants.NetworkLibrary)]
  55. static extern ulong nw_data_transfer_report_get_received_transport_byte_count (OS_nw_data_transfer_report report, uint path_index);
  56. public ulong GetTransportReceivedByteCount (uint pathIndex)
  57. => nw_data_transfer_report_get_received_transport_byte_count (GetCheckedHandle (), pathIndex);
  58. [DllImport (Constants.NetworkLibrary)]
  59. static extern ulong nw_data_transfer_report_get_received_transport_duplicate_byte_count (OS_nw_data_transfer_report report, uint path_index);
  60. public ulong GetTransportReceivedDuplicateByteCount (uint pathIndex)
  61. => nw_data_transfer_report_get_received_transport_duplicate_byte_count (GetCheckedHandle (), pathIndex);
  62. [DllImport (Constants.NetworkLibrary)]
  63. static extern ulong nw_data_transfer_report_get_received_transport_out_of_order_byte_count (OS_nw_data_transfer_report report, uint path_index);
  64. public ulong GetTransportReceivedOutOfOrderByteCount (uint pathIndex)
  65. => nw_data_transfer_report_get_received_transport_duplicate_byte_count (GetCheckedHandle (), pathIndex);
  66. [DllImport (Constants.NetworkLibrary)]
  67. static extern ulong nw_data_transfer_report_get_sent_transport_byte_count (OS_nw_data_transfer_report report, uint path_index);
  68. public ulong GetTransportSentByteCount (uint pathIndex)
  69. => nw_data_transfer_report_get_sent_transport_byte_count (GetCheckedHandle (), pathIndex);
  70. [DllImport (Constants.NetworkLibrary)]
  71. static extern ulong nw_data_transfer_report_get_sent_transport_retransmitted_byte_count (OS_nw_data_transfer_report report, uint path_index);
  72. public ulong GetTransportRetransmittedByteCount (uint pathIndex)
  73. => nw_data_transfer_report_get_sent_transport_retransmitted_byte_count (GetCheckedHandle (), pathIndex);
  74. [DllImport (Constants.NetworkLibrary)]
  75. static extern ulong nw_data_transfer_report_get_transport_smoothed_rtt_milliseconds (OS_nw_data_transfer_report report, uint path_index);
  76. public TimeSpan GetTransportSmoothedRoundTripTime (uint pathIndex)
  77. => TimeSpan.FromMilliseconds (nw_data_transfer_report_get_transport_smoothed_rtt_milliseconds (GetCheckedHandle (), pathIndex));
  78. [DllImport (Constants.NetworkLibrary)]
  79. static extern ulong nw_data_transfer_report_get_transport_minimum_rtt_milliseconds (OS_nw_data_transfer_report report, uint path_index);
  80. public TimeSpan GetTransportMinimumRoundTripTime (uint pathIndex)
  81. => TimeSpan.FromMilliseconds (nw_data_transfer_report_get_transport_minimum_rtt_milliseconds (GetCheckedHandle (), pathIndex));
  82. [DllImport (Constants.NetworkLibrary)]
  83. static extern ulong nw_data_transfer_report_get_transport_rtt_variance (OS_nw_data_transfer_report report, uint path_index);
  84. public ulong GetTransportRoundTripTimeVariance (uint pathIndex)
  85. => nw_data_transfer_report_get_transport_rtt_variance (GetCheckedHandle (), pathIndex);
  86. [DllImport (Constants.NetworkLibrary)]
  87. static extern ulong nw_data_transfer_report_get_received_ip_packet_count (OS_nw_data_transfer_report report, uint path_index);
  88. public ulong GetTransportReceivedIPPackageCount (uint pathIndex)
  89. => nw_data_transfer_report_get_received_ip_packet_count (GetCheckedHandle (), pathIndex);
  90. [DllImport (Constants.NetworkLibrary)]
  91. static extern ulong nw_data_transfer_report_get_sent_ip_packet_count (OS_nw_data_transfer_report report, uint path_index);
  92. public ulong GetTransportSentIPPackageCount (uint pathIndex)
  93. => nw_data_transfer_report_get_sent_ip_packet_count (GetCheckedHandle (), pathIndex);
  94. [DllImport (Constants.NetworkLibrary)]
  95. unsafe static extern void nw_data_transfer_report_collect (OS_nw_data_transfer_report report, IntPtr queue, ref BlockLiteral collect_block);
  96. delegate void nw_data_transfer_report_collect_t (IntPtr block, IntPtr report);
  97. static nw_data_transfer_report_collect_t static_CollectHandler = TrampolineCollectHandler;
  98. [MonoPInvokeCallback (typeof (nw_data_transfer_report_collect_t))]
  99. static void TrampolineCollectHandler (IntPtr block, IntPtr report)
  100. {
  101. var del = BlockLiteral.GetTarget<Action<NWDataTransferReport>> (block);
  102. if (del != null) {
  103. using (var nwReport = new NWDataTransferReport (report, owns: false))
  104. del (nwReport);
  105. }
  106. }
  107. [BindingImpl (BindingImplOptions.Optimizable)]
  108. public void Collect (DispatchQueue queue, Action<NWDataTransferReport> handler)
  109. {
  110. if (queue == null)
  111. throw new ArgumentNullException (nameof (queue));
  112. if (handler == null)
  113. throw new ArgumentNullException (nameof (handler));
  114. BlockLiteral block_handler = new BlockLiteral ();
  115. block_handler.SetupBlockUnsafe (static_CollectHandler, handler);
  116. try {
  117. nw_data_transfer_report_collect (GetCheckedHandle (), queue.Handle, ref block_handler);
  118. } finally {
  119. block_handler.CleanupBlock ();
  120. }
  121. }
  122. [DllImport (Constants.NetworkLibrary)]
  123. static extern NWDataTransferReportState nw_data_transfer_report_get_state (OS_nw_data_transfer_report report);
  124. public NWDataTransferReportState State => nw_data_transfer_report_get_state (GetCheckedHandle ());
  125. }
  126. }