Page 1 of 2
(SOLVED) I can't close program if...
Posted: Wed Oct 08, 2014 10:05 am
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.
Re: I can't close program if...
Posted: Wed Oct 08, 2014 2:24 pm
by bartbes
I assume this is because of lzmq, probably trying desperately to get its messages out, so look there for answers?
Re: I can't close program if...
Posted: Wed Oct 08, 2014 5:59 pm
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
Re: I can't close program if...
Posted: Wed Oct 08, 2014 6:15 pm
by DaedalusYoung
Don't call love.graphics.print outside of love.draw though.
Re: I can't close program if...
Posted: Wed Oct 08, 2014 6:24 pm
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?
Re: I can't close program if...
Posted: Wed Oct 08, 2014 9:23 pm
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.
Re: I can't close program if...
Posted: Thu Oct 09, 2014 3:00 am
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
Re: I can't close program if...
Posted: Thu Oct 09, 2014 3:23 am
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?
Re: I can't close program if...
Posted: Thu Oct 09, 2014 3:38 am
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
Re: (SOLVED) I can't close program if...
Posted: Thu Oct 09, 2014 8:25 am
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.)