Giter Site home page Giter Site logo

Comments (8)

Jayatubi avatar Jayatubi commented on June 23, 2024 1

The OnPerformCallback will be called when the request is finished which also means the Lib.curl_multi_info_read returns DONE underhood.

If the link you request never ends, such as a living stream, then the OnPerformCallback might not have a chance to be called. If so you could consider to use ProgressCallback instead.

Beside, when the CurlEasy reqeust was going to callback it would put it into a TaskScheduler which belongs to the thread where the Perform was called. That means if you didn't perform this request on the Unity main thread the callback might not be called. If so I will mark this issue as a bug and find a way to handle it.

BTW, if you could provide a simple project to reproduce this issue would be much great helpful.

from curl-unity.

lavarith avatar lavarith commented on June 23, 2024

Thanks for the quick reply!

Carving out a 'simple' project is a bit hard since I need LoginWithAmazon and even a bunch more stood up to support creating a DownChannel with AVS.

Using the ProgressCallback makes sense. When I added that to my Easy declaration, the function is basically called continuously, so I created this function:

public void DownChannelProgressCallback(long dltotal, long dlnow, long ultotal, long ulnow, CurlEasy easy)
    {
        // We're in downchannel callback, meaning we've either received a directive or a disconnect
        // A directive?
        if(easy.inText != null || easy.message != null || easy.outText != null)
        {
            Debug.Log("=========== In downchannel progress callback. ===========");
            Debug.Log(easy.inData);
            Debug.Log(easy.outData);
            Debug.Log(easy.inText);
            Debug.Log(easy.outText);
            Debug.Log(easy.contentType);
            Debug.Log(easy.message);
            Debug.Log($"Download - Total: {dltotal}; Now: {dlnow};");
            Debug.Log($"Upload - Total: {ultotal}; Now: {ulnow};");

            easy.Abort();
        }
    }

However, this never prints anything. Any thoughts there? Am I looking in the right place? Am I handling the response correctly?

from curl-unity.

Jayatubi avatar Jayatubi commented on June 23, 2024

The inText will be available after the request is finised so does the message. The outText was the data you put into the request and in the code you provided it seems to be empty.

If you just want the response headers, without any response body, you may try to set the method to HEAD such as:

var easy = new CurlEasy();
easy.method = "HEAD";

Then this request will auto abort after all the headers retrieved.

If the server doesn't accept the HEAD method then you could try to use the default GET instead and set the option CURLOPT.NOBODY to true:

easy.SetOpt(CURLOPT.NOBODY, true);

from curl-unity.

lavarith avatar lavarith commented on June 23, 2024

The directives endpoint has no body, so it makes sense that it's empty.

But I want the response body as well. Let me try the CURLOPT setting.

from curl-unity.

Jayatubi avatar Jayatubi commented on June 23, 2024

If you want the response body you should wait the request to finish.

If you really want to access incoming data before it finishes you could take a look at the private member responseBodyStream of CurlEasy which holds the unfinished incoming data. Just be careful this stream is being writing on other thread than the Unity main thread.

from curl-unity.

lavarith avatar lavarith commented on June 23, 2024

That makes sense, but the directives endpoint is designed to never close (unless you close it). I'll look at the responseBodyStream.

from curl-unity.

lavarith avatar lavarith commented on June 23, 2024

That did it!

I changed responseBodyStream to public and then do this in my ProgressCallback:

public void DownChannelProgressCallback(long dltotal, long dlnow, long ultotal, long ulnow, CurlEasy easy)
    {
        // We're in downchannel callback, meaning we've either received a directive or a disconnect
        // A directive?
        var ms = easy.responseBodyStream as MemoryStream;
        // Parse data in the stream
        byte[] inData = ms.ToArray();
        if (inData.Length > 0)
        {
            Debug.Log("========== In the DownChannel! ==========");
            Debug.Log(Encoding.UTF8.GetString(inData));

            // Clear out the stream
            easy.responseBodyStream = new MemoryStream();
        }
    }

Any idea if there's a better way to implement this? I'm worried a bit about obliterating a message when I clear responseBodyStream. I'm also wondering if I should use a Coroutine or task to periodically query the responseBodyStream rather than the progressCallback, which seems to fire continuously.

from curl-unity.

Jayatubi avatar Jayatubi commented on June 23, 2024

Yes you could access the stream in a coroutine if you make it thread safe, such as by adding some necessary locks.
In my opinion the most handy way is to make your own duplex steam which support reading and writing at the same time. And since you are reading an endless stream it is not wise to use a memory stream because it may cause an out of memory issue.
You should consider to use a file stream or a ring buffer.

from curl-unity.

Related Issues (13)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.