sock.lua - A simple networking library for LÖVE

Showcase your libraries, tools and other projects that help your fellow love users.
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: sock.lua - A simple networking library for LÖVE

Post by Nixola »

A thread is "killed" when it returns; that should be enough.
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
Ikroth
Citizen
Posts: 79
Joined: Thu Jul 18, 2013 4:44 am

Re: sock.lua - A simple networking library for LÖVE

Post by Ikroth »

4aiman wrote: Tue Jul 11, 2017 10:17 pm
Sir_Silver wrote: Tue Jul 11, 2017 8:01 pm I believe that, once you've yielded the coroutine...
I'm using love.thread rather than coroutines.
Actually, there's no way to kill a server too - no server:stop() or any other equivalent.
Not sure nilling will do the job and underlying eNet will acknowledge the fact that the nilled server is no more.
So, now there're 2 things I need to be able to kill in reasonable time (of several minutes perhaps?)
You can destroy the server using this function: https://camchenry.com/sock.lua/#Server:destroy
User avatar
4aiman
Party member
Posts: 262
Joined: Sat Jan 16, 2016 10:30 am

Re: sock.lua - A simple networking library for LÖVE

Post by 4aiman »

Nixola wrote: Tue Jul 11, 2017 10:36 pm A thread is "killed" when it returns; that should be enough.
Except when an error occurs in a thread. Then it (a thread) seems to not exit at all. I can do thread:getError() as many times as I want till the rest of the lifespan of the main app that has created that thread in the first place.

Ikroth wrote: Tue Jul 11, 2017 10:40 pm You can destroy the server using this function: https://camchenry.com/sock.lua/#Server:destroy
Wow! How could I possibly overlook that? Thanks a lot! At least now I can make thread stop via sock's server message. :awesome:


It feels wrong to continue to discuss my threads-related problems here. I've made a different thread here. Please, visit it if you want to help ^_^
User avatar
4aiman
Party member
Posts: 262
Joined: Sat Jan 16, 2016 10:30 am

Re: sock.lua - A simple networking library for LÖVE

Post by 4aiman »

Is there a way to know the progress of data transmitting?
Say, I'm sending a file of 10Mb.
I need to give some feedback to a client.
Currently it's just a flashing message "Getting updates", but how about a progress bar?

I'm sending data like this:

Code: Select all

client:send("here's your data",binary_data)
I'm fine with sending messages from server like

Code: Select all

client:send("progress",percentage)
, but how is it possible to know the percentage?

Do I have to manually split that binary_data into pieces?


P.S. Sorry for double post, but this post has a different question :neko:
User avatar
Nuthen224
Citizen
Posts: 50
Joined: Sun Jul 28, 2013 9:40 pm

Re: sock.lua - A simple networking library for LÖVE

Post by Nuthen224 »

I have some suggestions. Maybe a simpler way would be to just make a guess of how long it will take. You could perhaps calculate a guess based on ping and file size.

If you are sending percentage updates from the server, ensure that is sent over a different channel than the file data or the progress updates wouldn't get sent until after the file is done sending.

Perhaps you could even have the server store the average download time for that file and use that.
Or instead of sending that from the server, instead tell the client the file size in advance and it can calculate the guess.

I don't think there's any need for an exact measurement. Even if you split up the file into pieces and calculated the percent that way, it would fill up the progress bar inconsistently, since the internet is inconsistent. With the way I mentioned, it would increase the percent linearly at least, but it might have finished the file download before it is at 100% if it was an overestimate. Or it would hang at 100% momentarily if it was an underestimate.
User avatar
4aiman
Party member
Posts: 262
Joined: Sat Jan 16, 2016 10:30 am

Re: sock.lua - A simple networking library for LÖVE

Post by 4aiman »

Uhmm...
I've studied undelying enet lib's articles on the wiki.
It seems threre's no way to send percentage updates, since I can't control how enet splits a huge 20Mb file into tiny packets. Sock counts packets, true, but it calls any message a packet - be it a number or a serialized bunary data of 20+Mb.
On the other hand, sending every file separately increases bandwith (needs measurment) and complexity of a client-server communication. I don't want the latter :)


But I like the idea of estimating.
There's setBandwidth method for a server, I think I can use that to calculate sending time server-side and give that to a client.

Is it worth determining max bandwidth of a hardware connection (presumably using some online service upon server startup) to make my estimates closer to real times, or I can get away with you-run-a-server-so-those-are-your-problems thinking?

Now's the only problem (since threads' behaviour got explained by a number of community members) is the time that server needs to launch yet another thread. Could use some profiling lib, probably.

Thanks, Nuthen224!
User avatar
Sir_Silver
Party member
Posts: 286
Joined: Mon Aug 22, 2016 2:25 pm
Contact:

Re: sock.lua - A simple networking library for LÖVE

Post by Sir_Silver »

I'm trying to create a multiplayer game where a person can enter an ip and port to try to connect to a server, but if the ip is something that sock, or rather enet, doesn't like, for instance "192.", then it will crash with an error like "Unable to parse host name", or something like that. Is there a way to make it so that sock.lua won't throw this kind of an error if a bad ip is passed, or is the only solution here to try to make it so only viable ip can be passed?
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: sock.lua - A simple networking library for LÖVE

Post by bartbes »

Can't you just use pcall?
User avatar
Sir_Silver
Party member
Posts: 286
Joined: Mon Aug 22, 2016 2:25 pm
Contact:

Re: sock.lua - A simple networking library for LÖVE

Post by Sir_Silver »

Yes I can. Thanks for the reminder that that is a thing! :D
Shadowblitz16
Citizen
Posts: 73
Joined: Wed Oct 28, 2015 11:18 pm

Re: sock.lua - A simple networking library for LÖVE

Post by Shadowblitz16 »

what am i doing wrong? I keep getting error during service and can;'t serialize message: serialize was not sent

Code: Select all


net = require "lib.sock"

local id       = "" 
local joined   = false;
local server   = net.newServer("localhost", 50301)
local client   = net.newClient("localhost", 50302)
local messages = {}
local message  = ""

function clientConnect(id)
    clientSendMessage(id, "has connected")
    joined = true
end

function clientSendMessage(id, message)
    client:send("message", {id, message}) 
end

function serverGetMessage(id, message)
    server:sendToAll("message", {id, message})
end

function clientGetMessage(id, message)
    message = ""
    table.insert(messages, message)
end

function love.load(dt)
    server:on("message", serverGetMessage(id, message))
    client:on("message", clientGetMessage(id, message))
    client:connect()
end


function love.keypressed(key, scancode, isrepeat)

    
    if joined == false then
        if     key == "return" then clientConnect(id)
        elseif key == "space"  then id = id .. " "
        else                        id = id .. key
        end
    else
        if     key == "return" then clientSendMessage(id, message)
        elseif key == "space"  then message = message .. " "
        else                        message = message .. key      
        end
    end
    
end

function love.update()
    server:update()
    client:update()
end

function love.draw()

    love.graphics.clear(0,0,0,1)

    love.graphics.setColor(0.1,0.1,0.1,1)
    love.graphics.rectangle("fill",0,480-16,640,480)

    love.graphics.setColor(1,1,1,1)
    if     joined == false then love.graphics.print(id,     16,480-16,0,1,1,0,0)
    elseif joined == true  then love.graphics.print(message,16,480-16,0,1,1,0,0) end

    for i,v in ipairs(messages) do love.graphics.print(v,   16,480-(i*16),0,1,1,0,0)
    end

end
simplified it and it still doesn't work..

Code: Select all


net = require "lib.sock"

local str      = ""
local server   = net.newServer("localhost", 50301)
local client   = net.newClient("localhost", 50302)


server:on("connect", function(message) server:sendToAll("connect", message) end)
client:on("connect", function(message) str = message end)
client:connect()
client:send("connect", "hello")

function love.update()
    client:update()
    server:update()
end

function love.draw()
    love.graphics.print(str,320,240,0,1,1,0,0)
end
Post Reply

Who is online

Users browsing this forum: No registered users and 53 guests