Nhu Phuong
Nhu Phuong

Reputation: 427

Can't lua_resume after async_wait?

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
        }
    }
}

Upvotes: 0

Views: 643

Answers (1)

Nhu Phuong
Nhu Phuong

Reputation: 427

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.

Upvotes: 1

Related Questions