Page 3 of 4

Re: Getting a server for a love2d game

Posted: Wed Jan 06, 2021 4:08 am
by Xii
Gunroar:Cannon() wrote: Tue Jan 05, 2021 7:30 pm How do I do that( use UDP instead of TCP and the quick connect(broadcast I think?) thing
https://love2d.org/wiki/Tutorial:Networking_with_UDP
https://love2d.org/wiki/socket
http://w3.impa.br/~diego/software/luasocket/udp.html

Re: Getting a server for a love2d game

Posted: Wed Jan 06, 2021 10:40 pm
by Gunroar:Cannon()
Thnx, you've been really helpful, but how do I broadcast (with sock.lua, which uses enet).

Edited: Sorry, I was getting lazy. I found it here: https://love2d.org/wiki/enet.host:broadcast. Though now the question is do I even need to broadcast or just quickly connect and disconnect?

Re: Getting a server for a love2d game

Posted: Thu Jan 07, 2021 6:05 am
by Xii
You don't need broadcast. There is no "connect/disconnect" with UDP. Think of it like sending and receiving mail. You can send mail to any IP address, and you can wait to receive mail from any IP addresses.

To get the match info, send a packet to each IP with a query code. Then, when a packet arrives with a response code + the data, update the table you store the available matches in to reflect that data.

Re: Getting a server for a love2d game

Posted: Thu Jan 07, 2021 8:42 am
by Gunroar:Cannon()
pls give a simple example.

Re: Getting a server for a love2d game

Posted: Thu Jan 07, 2021 2:33 pm
by Xii

Re: Getting a server for a love2d game

Posted: Fri Jan 15, 2021 1:05 pm
by Gunroar:Cannon()
I just got the chance to look at that tutorial in depth. So could i do something like:

Code: Select all

    --client side
    local IPs = get_matches()
    local port = 27344--random port
    upd = socket.udp()
    udp:settimeout(0)
    
    for x, i in ipairs(IPs) do
        udp:setpeername(i, port)
        udp:send("get data")
    end
    
    --somewhere in update
    local data = udp:recieve()
    if data then
        --sort data
    end
    ...

Code: Select all

    --server side
    offer_match()
    udp = socket.udp()
    udp:setsockname("*",27344)
    udp:settimeout(0)
    
    --somewhere in update
    local data = udp:recievefrom()
    if data == "get data" then
        udp:sendTo(...)--send data stuff
    end
Then I destroy it when a game starts, or is this wrong because it seems it can be done better. You said query code but I see no way to put it. Am I missing some info.

Re: Getting a server for a love2d game

Posted: Fri Jan 15, 2021 2:55 pm
by Xii
On the server side, you need to store the received query's IP and port in order to send the reply back to the correct address. Something like:

Code: Select all

--server load
match_name = "casual free-for-all, no cheaters plz"

--server update
local data, ip, port = udp:receivefrom()
if data == "get data" then
    udp:sendTo("match named "..match_name, ip, port)
end
On the client, you want to maintain a list of confirmed matches:

Code: Select all

--client load
confirmed_matches = {}
local IPs = get_matches()
...

--client update
local data, ip = udp:recievefrom()
if strsub(data, 1, 12) == "match named " then
    local match_name = strsub(data, 13)
    table.insert(confirmed_matches, {ip=ip, name=match_name})
end
Now confirmed_matches will fill up with responses from the servers as they arrive, including the match names. In this example, the data is only the reply "match named " and then the rest of the data is the name. Obviously you'll need to figure out your wire format by your actual data requirements.

Re: Getting a server for a love2d game

Posted: Fri Jan 15, 2021 4:54 pm
by Gunroar:Cannon()
Thnx for the details. So question is that everything is okay with the for loop and setpeername?(Plus do I need to send the IP since it's already known to the client through get_matches()?)

Re: Getting a server for a love2d game

Posted: Sat Jan 16, 2021 9:44 am
by Xii
Gunroar:Cannon() wrote: Fri Jan 15, 2021 4:54 pm everything is okay with the for loop and setpeername?
Yes.
Gunroar:Cannon() wrote: Fri Jan 15, 2021 4:54 pm do I need to send the IP since it's already known to the client through get_matches()?
When receiving data from a socket, the sender's IP address is included automatically in receivefrom()

Re: Getting a server for a love2d game

Posted: Wed Apr 21, 2021 9:21 am
by Gunroar:Cannon()
I checked it now and it said udp has no index reciecefrom() and recieve, does this happen?