WebGLXHRNativeConnectionLayer.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #if UNITY_WEBGL && !UNITY_EDITOR
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Runtime.InteropServices;
  5. using Best.HTTP.Shared;
  6. using Best.HTTP.Shared.Extensions;
  7. using Best.HTTP.Shared.PlatformSupport.Memory;
  8. namespace Best.HTTP.Hosts.Connections.WebGL
  9. {
  10. internal static class WebGLXHRNativeConnectionLayer
  11. {
  12. static Dictionary<int, WebGLXHRConnection> Connections = new Dictionary<int, WebGLXHRConnection>(1);
  13. public static void Add(int nativeId, WebGLXHRConnection connection) => Connections.Add(nativeId, connection);
  14. public static void Remove(int nativeId) => Connections.Remove(nativeId);
  15. public static void SetupHandlers(int nativeId, HTTPRequest request)
  16. {
  17. WebGLXHRNativeInterface.XHR_SetResponseHandler(nativeId, OnResponse, OnError, OnTimeout, OnAborted, OnBufferCallback, OnAllocArray);
  18. // Setting OnUploadProgress result in an addEventListener("progress", ...) call making the request non-simple.
  19. // https://forum.unity.com/threads/best-http-released.200006/page-49#post-3696220
  20. WebGLXHRNativeInterface.XHR_SetProgressHandler(nativeId,
  21. request.DownloadSettings.OnDownloadProgress == null ? (OnWebGLXHRProgressDelegate)null : OnDownloadProgress,
  22. request.UploadSettings.OnUploadProgress == null ? (OnWebGLXHRProgressDelegate)null : OnUploadProgress);
  23. }
  24. [AOT.MonoPInvokeCallback(typeof(OnWebGLXHRAllocArray))]
  25. static unsafe IntPtr OnAllocArray(int nativeId, int length)
  26. {
  27. byte[] buffer = BufferPool.Get(length, true);
  28. fixed (byte* ptr = buffer)
  29. {
  30. var p = (IntPtr)ptr;
  31. if (HTTPManager.Logger.IsDiagnostic)
  32. HTTPManager.Logger.Verbose(nameof(WebGLXHRConnection), $"({p}) <= OnAllocArray({nativeId}, {length})");
  33. return p;
  34. }
  35. }
  36. [AOT.MonoPInvokeCallback(typeof(OnWebGLXHRRequestHandlerDelegate))]
  37. static void OnResponse(int nativeId, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.U1, SizeParamIndex = 2)] byte[] pBuffer, int length)
  38. {
  39. WebGLXHRConnection conn = null;
  40. if (!Connections.TryGetValue(nativeId, out conn))
  41. {
  42. HTTPManager.Logger.Error("WebGLConnection", $"OnResponse({nativeId}, {pBuffer}, {length}): No WebGL connection found for nativeId({nativeId})!");
  43. return;
  44. }
  45. if (HTTPManager.Logger.IsDiagnostic)
  46. HTTPManager.Logger.Verbose("WebGLConnection", $"OnResponse({nativeId}, {pBuffer}, {length})", conn.Context);
  47. BufferSegment payload = BufferSegment.Empty;
  48. if (length > 0)
  49. payload = pBuffer.AsBuffer(length);
  50. conn.OnResponse(payload);
  51. }
  52. [AOT.MonoPInvokeCallback(typeof(OnWebGLXHRBufferDelegate))]
  53. public static void OnBufferCallback(int nativeId, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.U1, SizeParamIndex = 2)] byte[] pBuffer, int length)
  54. {
  55. WebGLXHRConnection conn = null;
  56. if (!Connections.TryGetValue(nativeId, out conn))
  57. {
  58. HTTPManager.Logger.Error("WebGLConnection - OnBufferCallback", "No WebGL connection found for nativeId: " + nativeId.ToString());
  59. return;
  60. }
  61. BufferSegment payload = BufferSegment.Empty;
  62. if (length > 0)
  63. payload = pBuffer.AsBuffer(length);
  64. conn.OnBuffer(payload);
  65. }
  66. [AOT.MonoPInvokeCallback(typeof(OnWebGLXHRProgressDelegate))]
  67. static void OnDownloadProgress(int nativeId, int downloaded, int total)
  68. {
  69. WebGLXHRConnection conn = null;
  70. if (!Connections.TryGetValue(nativeId, out conn))
  71. {
  72. HTTPManager.Logger.Error("WebGLConnection - OnDownloadProgress", "No WebGL connection found for nativeId: " + nativeId.ToString());
  73. return;
  74. }
  75. HTTPManager.Logger.Information(nativeId + " OnDownloadProgress", downloaded.ToString() + " / " + total.ToString(), conn.Context);
  76. conn.OnDownloadProgress(downloaded, total);
  77. }
  78. [AOT.MonoPInvokeCallback(typeof(OnWebGLXHRProgressDelegate))]
  79. static void OnUploadProgress(int nativeId, int uploaded, int total)
  80. {
  81. WebGLXHRConnection conn = null;
  82. if (!Connections.TryGetValue(nativeId, out conn))
  83. {
  84. HTTPManager.Logger.Error("WebGLConnection - OnUploadProgress", "No WebGL connection found for nativeId: " + nativeId.ToString());
  85. return;
  86. }
  87. HTTPManager.Logger.Information(nativeId + " OnUploadProgress", uploaded.ToString() + " / " + total.ToString(), conn.Context);
  88. conn.OnUploadProgress(uploaded, total);
  89. }
  90. [AOT.MonoPInvokeCallback(typeof(OnWebGLXHRErrorDelegate))]
  91. static void OnError(int nativeId, string error)
  92. {
  93. WebGLXHRConnection conn = null;
  94. if (!Connections.TryGetValue(nativeId, out conn))
  95. {
  96. HTTPManager.Logger.Error("WebGLConnection - OnError", "No WebGL connection found for nativeId: " + nativeId.ToString() + " Error: " + error);
  97. return;
  98. }
  99. conn.OnError(error);
  100. }
  101. [AOT.MonoPInvokeCallback(typeof(OnWebGLXHRTimeoutDelegate))]
  102. static void OnTimeout(int nativeId)
  103. {
  104. WebGLXHRConnection conn = null;
  105. if (!Connections.TryGetValue(nativeId, out conn))
  106. {
  107. HTTPManager.Logger.Error("WebGLConnection - OnTimeout", "No WebGL connection found for nativeId: " + nativeId.ToString());
  108. return;
  109. }
  110. conn.OnTimeout();
  111. }
  112. [AOT.MonoPInvokeCallback(typeof(OnWebGLXHRAbortedDelegate))]
  113. static void OnAborted(int nativeId)
  114. {
  115. WebGLXHRConnection conn = null;
  116. if (!Connections.TryGetValue(nativeId, out conn))
  117. {
  118. HTTPManager.Logger.Error("WebGLConnection - OnAborted", "No WebGL connection found for nativeId: " + nativeId.ToString());
  119. return;
  120. }
  121. conn.OnAborted();
  122. }
  123. }
  124. }
  125. #endif