Search Unity

custom DownloadHandlerScript not working properly, even though DownloadHandlerBuffer does

Discussion in 'Scripting' started by spatial_io, Apr 30, 2021.

  1. spatial_io

    spatial_io

    Joined:
    Jul 3, 2017
    Posts:
    21
    We have a custom DownloadHandlerScript; I've reduced it here to the simplest example. It's been working fine for us for over a year. The problem is, when we create a request to download some addresses on https://ifps.io, the download handler never even gets any callbacks— it fails with errors like
    Curl error 23: Failed writing body (0 != 4096).

    This only happens for some addresses on https://ifps.io . Please email me at josh@spatial.io or mike@spatial.io for a specific address that can repro this.

    Using Unity 2020.1.17f1, and reproducing this in editor on a mac (as well as on other platforms).

    Code (CSharp):
    1. public class SampleDownloadHandler : DownloadHandlerScript, System.IDisposable
    2.     {
    3.         public NativeArray<byte> buffer
    4.         {
    5.             get;
    6.             private set;
    7.         }
    8.  
    9.         private int _writeIndex = 0;
    10.  
    11.         protected override void ReceiveContentLengthHeader(ulong contentLength)
    12.         {
    13.             buffer = new NativeArray<byte>((int)contentLength, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
    14.         }
    15.  
    16.         protected override bool ReceiveData(byte[] data, int dataLength)
    17.         {
    18.             if (_writeIndex + dataLength > buffer.Length)
    19.             {
    20.                 return false;
    21.             }
    22.  
    23.             NativeArray<byte>.Copy(data, 0, buffer, _writeIndex, dataLength);
    24.             _writeIndex += dataLength;
    25.  
    26.             return _writeIndex < buffer.Length;
    27.         }
    28.  
    29.         protected override byte[] GetData()
    30.         {
    31.             Log.Warning($"{nameof(GLTFDownloadHandler)}: using this may cause memory leaks");
    32.             return buffer.ToArray();
    33.         }
    34.  
    35.         protected override string GetText()
    36.         {
    37.             Log.Warning($"{nameof(GLTFDownloadHandler)}: using this may cause memory leaks");
    38.             return buffer.GetText();
    39.         }
    40.  
    41.     }
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
    This line should always return true, otherwise you abort the request when receiving the very last bytes.
     
    spatial_io likes this.
  3. spatial_io

    spatial_io

    Joined:
    Jul 3, 2017
    Posts:
    21
    I see, thank you!
    The problem was we were expecting to receive the call to ReceiveContentLengthHeader before receiving data, but never are receiving the content length header. We adjusted it to work without that expectation and it works fine now
     
  4. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
    In case of chunked transfer there is no Content-Length header.