English 中文(简体)
Can t lua_resume after async_wait?
原标题:
  • 时间:2009-11-23 08:10:39
  •  标签:
  • c++
  • lua

I have some lua script that have some long running task like getting a web page so I make it yield then the C code handle get page job async, so the thread free to do other job and after a specify time it check back to see is the get page job finished , if so then resume the script. the problem is the thread can t resume the job after async wait. here is my code I riped it from a class so a little messy sorry


////script:
 function Loginmegaupload_com(hp, user, pass, cookie)
    setURL(hp, "http://megaupload.com/?c=login")
    importPost(hp, "login=1&redir=1")
    addPost(hp, "username", user)
    addPost(hp, "password", pass)
    GetPage()
    if isHeaderContain(hp, "user=") ~= nil then
        SetFileLink(cookie, GetAllCookie(hp))
        return 1
    else
        return 0
    end
end
////c code
int FileSharingService::GetPage(lua_State *ls)
{
    return lua_yield(ls, 0);
}

void FileSharingService::AsyncWait(Http_RequestEx *Http, lua_State *LS, boost::asio::deadline_timer* Timer)
{
    if( (Http->status_code == Http_RequestEx::ERROR) || (Http->status_code == Http_RequestEx::FISNISHED))
    {
        if(Http->status_code == Http_RequestEx::FISNISHED)
        {
            int result = lua_resume(LS, 0); // here I got result  == 2 mean error ?
            if(result  == 0)//lua script exit normal, resume success
            {
                delete Http;
                delete Timer;
            }
        }
        else
            return;
    }
    else
    {
        Timer->expires_from_now(boost::posix_time::milliseconds(200));
        Timer->async_wait(boost::bind(&FileSharingService::AsyncWait, this, Http, LS, Timer));
    }
}



bool FileSharingService::Login(string URL, string User, string Pass, string &Cookie)
{
    Http_RequestEx *http = new Http_RequestEx;
    http->url = URL;
    LuaWarper* Lua = Lua_map[boost::this_thread::get_id()];  //one main luaState per ioservice thread
    lua_State *thread = lua_newthread(Lua->GetState());

    boost::asio::deadline_timer *timer = new boost::asio::deadline_timer(*HClient.ioservice);

    string functioname = "Login" + GetServicename(URL);
    if( Lua->isFunctionAvaliable(functioname.c_str()) == false )
    {
        throw(FileSharingService::SERVICE_NOT_AVALIABLE);
    }
    else
    {
        lua_getglobal(thread, functioname.c_str());
        lua_pushlightuserdata(thread, http);
        lua_pushstring(thread, User.c_str());
        lua_pushstring(thread, Pass.c_str());
        lua_pushlightuserdata(thread, &Cookie);
        int result = lua_resume(thread, 4);
        if(result == LUA_YIELD)
        {
            HClient.Do(*http, false);
            AsyncWait(http, thread, timer);
        }
        else if(result == 0)
        {
            //fisnished at first call
        }
        else
        {
            //yield error, will handle late
        }
    }
}
最佳回答

Sorry never mind this question, lua_resume return 2 mean error but script work just fine, asio get page work fine too, and I tracked down the line that respond for fail of lua_resume :

httpinfo.header.append(buffer, (HeaderEndIndex-buffer+2) );

if I comment that line lua_resume work as expected it return 0 mean script exit, this line don t do any thing that can affect the lua thread state it just a string assign, I checked there no overflow. so weird.

问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签