(SOLVED) I can't close program if...

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
camryfu
Prole
Posts: 5
Joined: Wed Oct 08, 2014 9:34 am

(SOLVED) I can't close program if...

Post by camryfu »

my code:

Code: Select all

local zmq = require "lzmq"
local context = zmq.init(1)
local sender = context:socket(zmq.PUSH)
c=0

function love.load(arg)
    r = sender:connect("tcp://192.168.1.10:8830")
end

function love.quit()
    sender:close()
end

function love.draw()
    c = c + 1
    str = string.format("test : %i",c)
    love.graphics.print(str, 50, 50)
end

function love.update(dt)
     if love.mouse.isDown("l") then
        x, y = love.mouse.getPosition()
        sender:send(tostring(x)..","..tostring(y))
    end
end
I use love2d with lzmq.
when I click mouse left button, it push x,y to another zeromq pull server, if the server is up before i click, it works perfectly.
but....
if the server isn't up, and I clicked, love2d program keep updating the frame counter, and if I want the close love2d, if stopped updating.... and can't be closed until I start the server pull program.

I think we can not expect the pull server always be there, if not , how can I close the frozen love2d?

thanks in advanced.
Last edited by camryfu on Thu Oct 09, 2014 3:00 am, edited 1 time in total.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: I can't close program if...

Post by bartbes »

I assume this is because of lzmq, probably trying desperately to get its messages out, so look there for answers?
User avatar
artofwork
Citizen
Posts: 91
Joined: Mon Sep 15, 2014 1:17 am
Location: East Coast USA

Re: I can't close program if...

Post by artofwork »

Edited
camryfu wrote: if the server isn't up, and I clicked, love2d program keep updating the frame counter, and if I want the close love2d, if stopped updating.... and can't be closed until I start the server pull program.
The solution is in your question.. Everything in programming is a condition, if this do that elseif do something different else default to this

Never assume your always going to be connected, that is what this code states

Code: Select all

r = sender:connect("tcp://192.168.1.10:8830")

Using an if / else statement you can write it as

Code: Select all

function love.load()
    if sender:connect("tcp://192.168.1.10:8830") then
        r = sender:connect("tcp://192.168.1.10:8830")
    else
        print("You are not connected", 0, 0)
    end
end

function love.update(dt)
     if love.mouse.isDown("l") then
        x, y = love.mouse.getPosition()
        if sender:connect("tcp://192.168.1.10:8830") then
                sender:send(tostring(x)..","..tostring(y))
        else
        -- do or print something else
        end
    end
end
Or write a separate generic function

Code: Select all

function isConnected(address, send)
    if sender:connect(address) then
            sender:send(send)
    else
            print("You are not connected", 0, 0) -- or whatever you want to put here
    end
end
Then you can just place it inside of update and not bother with love.load

Code: Select all

function love.update(dt)
     if love.mouse.isDown("l") then
        x, y = love.mouse.getPosition()
        isConnected("tcp://192.168.1.10:8830",  tostring(x)..","..tostring(y)  )
    end
end
Last edited by artofwork on Wed Oct 08, 2014 6:20 pm, edited 1 time in total.
User avatar
DaedalusYoung
Party member
Posts: 413
Joined: Sun Jul 14, 2013 8:04 pm

Re: I can't close program if...

Post by DaedalusYoung »

Don't call love.graphics.print outside of love.draw though.
camryfu
Prole
Posts: 5
Joined: Wed Oct 08, 2014 9:34 am

Re: I can't close program if...

Post by camryfu »

Thanks for your replies.

I know I must make sure that server is up by checking result on sender:connect, but unfortunately it always return "True".
That's why there is one variable "r" in my code, I have tried it.

The zeromq lua binding I installed is from http://zeromq.org/bindings:lua ($ sudo luarocks install lzmq)
Maybe it's one bug in lzmq or something else I have no idea.

So, how can I fix it? Is there any method or function I can call to force terminate program?
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: I can't close program if...

Post by bartbes »

As this page tells you, I was right, it is the close call that is waiting until all messages are sent. This can be disabled by turning off the 'linger' option (as the page mentions), and I've even done your homework for you, the 'linger' option defines the amount of time waited until it gives up (infinite, by default), 0 meaning instant shutdown, and the method on the socket object to call is 'set_linger', from what I can tell.
camryfu
Prole
Posts: 5
Joined: Wed Oct 08, 2014 9:34 am

Re: I can't close program if...

Post by camryfu »

bartbes wrote:As this page tells you, I was right, it is the close call that is waiting until all messages are sent. This can be disabled by turning off the 'linger' option (as the page mentions), and I've even done your homework for you, the 'linger' option defines the amount of time waited until it gives up (infinite, by default), 0 meaning instant shutdown, and the method on the socket object to call is 'set_linger', from what I can tell.
Just as you said, you are right, I add one line before sender:close(), and it solved, I appreciate for your help, thanks.

Code: Select all

function love.quit()
    sender:set_linger(0)
    sender:close()
end
User avatar
artofwork
Citizen
Posts: 91
Joined: Mon Sep 15, 2014 1:17 am
Location: East Coast USA

Re: I can't close program if...

Post by artofwork »

camryfu wrote: I know I must make sure that server is up by checking result on sender:connect, but unfortunately it always return "True".
That's why there is one variable "r" in my code, I have tried it.
I am happy you resolved the issue, just a quick question...

As you stated "but unfortunately it always return "True"", a function will always return true when used in a conditional statement unless the value is either nil or false and sometimes 0 or null ( language dependent ).

So what is the actual value that sender:connect returns? is it a string, number, table etc?
camryfu
Prole
Posts: 5
Joined: Wed Oct 08, 2014 9:34 am

Re: I can't close program if...

Post by camryfu »

artofwork wrote:
camryfu wrote: I know I must make sure that server is up by checking result on sender:connect, but unfortunately it always return "True".
That's why there is one variable "r" in my code, I have tried it.
I am happy you resolved the issue, just a quick question...

As you stated "but unfortunately it always return "True"", a function will always return true when used in a conditional statement unless the value is either nil or false and sometimes 0 or null ( language dependent ).

So what is the actual value that sender:connect returns? is it a string, number, table etc?
sender:connect returns a boolean, and value always be "True", at least on those two conditions: server up and down.

For more additional solution on my last reply, it can also be solved by simply add an "0" in sender:close(0) as parameter Linger

Code: Select all

function love.quit()
    sender:close(0)
end
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: (SOLVED) I can't close program if...

Post by bartbes »

According to the documentation it actually returns 0 on success, but I'm not sure not being able to connect is a failure condition for it. (Because otherwise, why would it queue? It's probably trying to reconnect behind the scenes.)
Post Reply

Who is online

Users browsing this forum: Amazon [Bot], Bing [Bot] and 0 guests