Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.
 Entire forum ➜ MUSHclient ➜ Lua ➜ socket.http freezing mushclient

socket.http freezing mushclient

Posting of new messages is disabled at present.

Refresh page


Posted by Victorious   (89 posts)  Bio
Date Sat 21 Dec 2013 03:45 AM (UTC)
Message
I just started experimenting with socket.http for my plugin and started noticing that while the code was running (e.g retrieving version information from a website), you can't do anything in mushclient before the code finishes. Is it possible to have the code not cause the freezing?
Top

Posted by Nick Gammon   Australia  (23,122 posts)  Bio   Forum Administrator
Date Reply #1 on Sat 21 Dec 2013 11:42 AM (UTC)
Message
I think you can do non-blocking calls, but I can't remember how. Browse the documentation for the Lua socket library, it may have examples.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Victorious   (89 posts)  Bio
Date Reply #2 on Sat 21 Dec 2013 11:02 PM (UTC)

Amended on Sun 22 Dec 2013 12:38 PM (UTC) by Nick Gammon

Message
I've been looking into using threads to try solving this problem and based on section 9.4 of the programming in lua manual, I've came up with the following code. I'm still trying to grasp the concepts but so far, this increases download speeds but mushclient is still unresponsive during this process because the dispatcher() has control.


    threads = {}    -- list of all live threads
    function get (host, file, output)
      -- create coroutine
      local co = coroutine.create(function ()
        download(host, file, output)
      end)
      -- insert it in the list
      table.insert(threads, co)
    end

    function dispatcher ()
      while true do
        local n = table.getn(threads)
        if n == 0 then break end   -- no more threads to run
        local connections = {}
        for i=1,n do
          local status, res = coroutine.resume(threads[i])
          if res == nil then -- thread finished its task
            table.remove(threads, i)
            break
          else    -- timeout
            table.insert(connections, res)
          end
        end
        if table.getn(connections) == n then
          socket.select(connections)
        end
      end
    end

    function receive (connection)
connection:settimeout(0.051)   -- do not block
      local s, status = connection:receive(2^10)
      if status == "timeout" then
        coroutine.yield(connection)
      end
      return s, status
    end

    function download (host, file, output)
f = io.output(output)
      local c = assert(socket.connect(host, 80))
      local count = 0    -- counts number of bytes read
      c:send("GET " .. file .. " HTTP/1.0\r\n\r\n")
      while true do
s, status = receive(c)

if s ~= nil then
        count = count + string.len(s)
--assembles the actual file
if actualfile == nil then
actualfile = s
else
actualfile = actualfile..s
end
end

        if status == "closed" then break end
      end
      c:close()
      print(file, count) --filename and length
f:write(actualfile)
f:flush()
f:close()
    end


[EDIT] Code tags added.
Top

Posted by Nick Gammon   Australia  (23,122 posts)  Bio   Forum Administrator
Date Reply #3 on Sun 22 Dec 2013 12:40 PM (UTC)
Message
I think the problem with trying to make something non-blocking is that you would then have to periodically check if the I/O was completed. I'm not sure how you would do that, maybe with a timer.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Victorious   (89 posts)  Bio
Date Reply #4 on Wed 01 Jan 2014 08:14 AM (UTC)
Message
Hmmm, maybe something like lua lanes might work but was having trouble compiling the dll and getting it integrated with mushclient.
Top

Posted by Nick Gammon   Australia  (23,122 posts)  Bio   Forum Administrator
Date Reply #5 on Thu 02 Jan 2014 08:46 PM (UTC)
Message
I think you will find there are problems with this sort of thing because MUSHclient has a "main loop" and non-blocking code tends to want to have its own "main loop".

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


17,201 views.

Posting of new messages is disabled at present.

Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.