Giter Site home page Giter Site logo

MakeHttpRequest from lua about urho3d HOT 14 CLOSED

cartuchogl avatar cartuchogl commented on May 2, 2024
MakeHttpRequest from lua

from urho3d.

Comments (14)

cadaver avatar cadaver commented on May 2, 2024

Anything that returns a SharedPtr would require correct reference counting between C++ and the script language. This is natural to AngelScript but to my knowledge tolua++ does not support it. Therefore anything that can't return just a raw pointer has not been exposed to Lua as of now.

@aster2013 may have further insight on the matter.

from urho3d.

cartuchogl avatar cartuchogl commented on May 2, 2024

Thanks for the quick answer.

I will try to watch syntax for AngelScript.

from urho3d.

Canardian avatar Canardian commented on May 2, 2024

I thought you can pass a raw pointer as int between C++ and tolua++?

from urho3d.

cadaver avatar cadaver commented on May 2, 2024

Raw pointers are being passed OK, that's not the problem. The problem is that MakeHttpRequest expects the receiver of the SharedPtr to properly reference count the object and release it when done. As far as I understand, unmodified tolua++ has no facility for this. You could cheat and increment the reference count, let the original SharedPtr expire, and return the raw pointer to Lua, but this would leak memory for the remainder of the program.

from urho3d.

friesencr avatar friesencr commented on May 2, 2024

there is __gc method on lua metatables that gets called on destruction.

from urho3d.

aster2013 avatar aster2013 commented on May 2, 2024

Tolua++ can bind template to lua, But I have not test is fully.

from urho3d.

aster2013 avatar aster2013 commented on May 2, 2024

Sorry, as I know tolua++ can not support shared pointer, you'd better use raw pointer with it.

from urho3d.

cadaver avatar cadaver commented on May 2, 2024

I just realized that MakeHttpRequest() is pretty much only a redundant wrapper. In Lua one could just simply create a new HttpRequest object, as it is self-sufficient. It doesn't even need a Context pointer.

from urho3d.

aster2013 avatar aster2013 commented on May 2, 2024

I have add SharedPtr support in Lua, Please checkout the code for test.

from urho3d.

cadaver avatar cadaver commented on May 2, 2024

Nice! I'll test; the solution seems good at least for this case. What we're missing is a general robust solution that would understand the relationship between RefCounted subclasses and their SharedPtr's or WeakPtr's, but that's a lot harder to encode into the bindings.

from urho3d.

cartuchogl avatar cartuchogl commented on May 2, 2024

Hello,

I pull last code in master and I try to use MakeHttpRequest from lua, but I don't know how to read the response. The example I tried are:

require "LuaScripts/Utilities/Sample"

local http

function Start()
    -- Execute the common startup for samples
    SampleStart()

    SubscribeToEvents()

    http = GetNetwork():MakeHttpRequest('http://google.com','GET',{},"")
end

function SubscribeToEvents()
    -- Subscribe HandleUpdate() function for processing update events
    SubscribeToEvent("Update", "HandleUpdate")
end

function HandleUpdate(eventType, eventData)
    print(http:GetState(),http:GetAvailableSize(),"\n")
    if http:GetAvailableSize()>0 then
        -- how read?
    end
end

thanks in advance

from urho3d.

cadaver avatar cadaver commented on May 2, 2024

The bulk read function ie. plain Read() is not exposed to Lua (yet) but you can use the rest of the Deserializer API, like ReadUByte() to read one byte at a time, or ReadLine() for text.

from urho3d.

aster2013 avatar aster2013 commented on May 2, 2024

Now SharedPtr may have problem to convert to Deserializer.

from urho3d.

cartuchogl avatar cartuchogl commented on May 2, 2024

I tried with ReadUByte() at previous sample

-- same as previous sample
function HandleUpdate(eventType, eventData)
    if http:GetAvailableSize()>0 then
        print(http:ReadUByte())
    end
end

This sample crash with Bus error: 10 on OSX 10.8.5.

With ReadLine()

function HandleUpdate(eventType, eventData)
    if http:GetAvailableSize()>0 then
        print(http:ReadLine())
    end
end

Nothing is readed

For last, I try to add a function ReadByteP() that use Read() and It works

diff --git a/Source/Engine/Network/HttpRequest.cpp b/Source/Engine/Network/HttpRequest.cpp
--- a/Source/Engine/Network/HttpRequest.cpp
+++ b/Source/Engine/Network/HttpRequest.cpp
@@ -193,6 +193,13 @@ void HttpRequest::ThreadFunction()
     }
 }

+unsigned char HttpRequest::ReadByteP()
+{
+    unsigned char b;
+    this->Read(&b,1);
+    return b;
+}
+
 unsigned HttpRequest::Read(void* dest, unsigned size)
 {
     mutex_.Acquire();
diff --git a/Source/Engine/Network/HttpRequest.h b/Source/Engine/Network/HttpRequest.h
--- a/Source/Engine/Network/HttpRequest.h
+++ b/Source/Engine/Network/HttpRequest.h
@@ -56,6 +56,8 @@ public:
     virtual unsigned Read(void* dest, unsigned size);
     /// Set position from the beginning of the stream. Not supported.
     virtual unsigned Seek(unsigned position) { return position_; }
+
+    unsigned char ReadByteP();

     /// Return URL used in the request.
     const String& GetURL() const { return url_; }
diff --git a/Source/Extras/LuaScript/pkgs/Network/HttpRequest.pkg b/Source/Extras/LuaScript/pkgs/Network/HttpRequest.pkg
--- a/Source/Extras/LuaScript/pkgs/Network/HttpRequest.pkg
+++ b/Source/Extras/LuaScript/pkgs/Network/HttpRequest.pkg
@@ -16,6 +16,8 @@ class HttpRequest : public Deserializer
     HttpRequestState GetState() const;
     unsigned GetAvailableSize() const;
     bool IsOpen() const;
+
+    unsigned char ReadByteP();

     tolua_readonly tolua_property__get_set String URL;
     tolua_readonly tolua_property__get_set String verb;

And the modified handle

-- ...
local str = ""

function HandleUpdate(eventType, eventData)
    while http:GetAvailableSize()>0 do
        str = str .. string.char(http:ReadByteP())
    end
    print(str)
end

Thanks for your support.

from urho3d.

Related Issues (20)

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.