Getting a server for a love2d game

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.
User avatar
Xii
Citizen
Posts: 67
Joined: Thu Aug 13, 2020 9:09 pm
Contact:

Re: Getting a server for a love2d game

Post by Xii » Wed Jan 06, 2021 4:08 am

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

User avatar
Gunroar:Cannon()
Party member
Posts: 142
Joined: Thu Dec 10, 2020 1:57 am

Re: Getting a server for a love2d game

Post by Gunroar:Cannon() » Wed Jan 06, 2021 10:40 pm

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?
me: I don't always code but when I do it's done flawlessly.
also me:

Code: Select all

 function Gunroar:Cannon()
    for x, enemy in ipairs(self.allEnemies) do
        self:Cannon(enemy)
    end
end

Code: Select all

Lua Error: [file Gunroar.lua]:18: C stack overflow

User avatar
Xii
Citizen
Posts: 67
Joined: Thu Aug 13, 2020 9:09 pm
Contact:

Re: Getting a server for a love2d game

Post by Xii » Thu Jan 07, 2021 6:05 am

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.

User avatar
Gunroar:Cannon()
Party member
Posts: 142
Joined: Thu Dec 10, 2020 1:57 am

Re: Getting a server for a love2d game

Post by Gunroar:Cannon() » Thu Jan 07, 2021 8:42 am

pls give a simple example.
me: I don't always code but when I do it's done flawlessly.
also me:

Code: Select all

 function Gunroar:Cannon()
    for x, enemy in ipairs(self.allEnemies) do
        self:Cannon(enemy)
    end
end

Code: Select all

Lua Error: [file Gunroar.lua]:18: C stack overflow

User avatar
Xii
Citizen
Posts: 67
Joined: Thu Aug 13, 2020 9:09 pm
Contact:

Re: Getting a server for a love2d game

Post by Xii » Thu Jan 07, 2021 2:33 pm


User avatar
Gunroar:Cannon()
Party member
Posts: 142
Joined: Thu Dec 10, 2020 1:57 am

Re: Getting a server for a love2d game

Post by Gunroar:Cannon() » Fri Jan 15, 2021 1:05 pm

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.
me: I don't always code but when I do it's done flawlessly.
also me:

Code: Select all

 function Gunroar:Cannon()
    for x, enemy in ipairs(self.allEnemies) do
        self:Cannon(enemy)
    end
end

Code: Select all

Lua Error: [file Gunroar.lua]:18: C stack overflow

User avatar
Xii
Citizen
Posts: 67
Joined: Thu Aug 13, 2020 9:09 pm
Contact:

Re: Getting a server for a love2d game

Post by Xii » Fri Jan 15, 2021 2:55 pm

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.

User avatar
Gunroar:Cannon()
Party member
Posts: 142
Joined: Thu Dec 10, 2020 1:57 am

Re: Getting a server for a love2d game

Post by Gunroar:Cannon() » Fri Jan 15, 2021 4:54 pm

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()?)
me: I don't always code but when I do it's done flawlessly.
also me:

Code: Select all

 function Gunroar:Cannon()
    for x, enemy in ipairs(self.allEnemies) do
        self:Cannon(enemy)
    end
end

Code: Select all

Lua Error: [file Gunroar.lua]:18: C stack overflow

User avatar
Xii
Citizen
Posts: 67
Joined: Thu Aug 13, 2020 9:09 pm
Contact:

Re: Getting a server for a love2d game

Post by Xii » Sat Jan 16, 2021 9:44 am

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()

Post Reply

Who is online

Users browsing this forum: nineret and 44 guests