Packets in LUBE

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
Przemator
Party member
Posts: 107
Joined: Fri Sep 28, 2012 6:59 pm

Re: Packets in LUBE

Post by Przemator »

Sekaru wrote:Assuming we have 3 players online, let's assume they all send these things:
P1 Sends: Moving, Combat, Skills
P2 Sends: Combat, Skills, Moving
P3 Sends: Skills, Moving, Combat

As you can see they all send data in a different order. How does the server know to execute the function that handles Moving if that is sent first and so on for the rest of them?
I think we misunderstood each other by different perception of what "order" is. What I mean by order refers to all packets sent by a client, no matter what is their content.

So, if I understand, you want to know how to detect the type of a message?

Well, you can do sth like this:

Code: Select all

-------CLIENT--------
message = "M" .. tostring(move)
socket:sent(message) --or whatever is the name of the function

-----------SERVER--------
message = socket:receive() --or whatever is the name of the function
if message:sub(1,1) == "M" then
  --handle movement
elseif message:sub(1,1) == "C"
  --handle combat
elseif message:sub(1,1) == "S"
  --handle skills
end
User avatar
Sekaru
Prole
Posts: 17
Joined: Sun Oct 07, 2012 5:09 pm

Re: Packets in LUBE

Post by Sekaru »

Przemator wrote:
Sekaru wrote:Assuming we have 3 players online, let's assume they all send these things:
P1 Sends: Moving, Combat, Skills
P2 Sends: Combat, Skills, Moving
P3 Sends: Skills, Moving, Combat

As you can see they all send data in a different order. How does the server know to execute the function that handles Moving if that is sent first and so on for the rest of them?
I think we misunderstood each other by different perception of what "order" is. What I mean by order refers to all packets sent by a client, no matter what is their content.

So, if I understand, you want to know how to detect the type of a message?

Well, you can do sth like this:

Code: Select all

-------CLIENT--------
message = "M" .. tostring(move)
socket:sent(message) --or whatever is the name of the function

-----------SERVER--------
message = socket:receive() --or whatever is the name of the function
if message:sub(1,1) == "M" then
  --handle movement
elseif message:sub(1,1) == "C"
  --handle combat
elseif message:sub(1,1) == "S"
  --handle skills
end
Oh! That clears it up quite a lot!

Could you explain "message = "M" .. tostring(move)" this line though, is that what gives (1,1)? (not really sure here).
User avatar
Przemator
Party member
Posts: 107
Joined: Fri Sep 28, 2012 6:59 pm

Re: Packets in LUBE

Post by Przemator »

Sekaru wrote:Oh! That clears it up quite a lot!

Could you explain "message = "M" .. tostring(move)" this line though, is that what gives (1,1)? (not really sure here).
Well, since you can only send strings via LUBE, and you want to put more than one variable in a packet, you have to concatenate these variables.

Let's say you want to inform the server, that client has moved by 100 units. From message = "M" .. tostring(move) you will get sth like "M100". Then when you receive it, you just need to check the first character of the string to see what type of message it is.

you can even put whole table into strings, it's called serlialization. The only good pure Lua serialize library that I've found is Serpent:

https://github.com/pkulchenko/serpent

So what you do is define a table, put data in it, serialize it and then deserialize it on the server:

Code: Select all

------------CLIENT
message = {}
message.type = "movement"
message.value = 153
message.text = "something, something"
text = serialize(message)
socket:send(text)

-----------SERVER
text = socket:receive()
message = deserialize(text)

if message.type == "movement" then
  --you know what to do
end
But be careful, serializing array may generate long strings, which may be unsuitable for network transport. You just need to do the math. If the length of your serialized string is, lets say 1000 (bytes), and you want to send it 60 times per second, then you will get about 60 KBps of transfer, which is huge. But usually the string length should not exceed 100 bytes, or it is not send in each frame.
User avatar
Sekaru
Prole
Posts: 17
Joined: Sun Oct 07, 2012 5:09 pm

Re: Packets in LUBE

Post by Sekaru »

Thanks a lot! I've got everything working (I think). I've downloaded Serpent and figured out how to serialize (serpent.line(message)), but I can't figure out how to deserialize, do you happen to know the name of the deserialization function?
User avatar
Przemator
Party member
Posts: 107
Joined: Fri Sep 28, 2012 6:59 pm

Re: Packets in LUBE

Post by Przemator »

Sekaru wrote:Thanks a lot! I've got everything working (I think). I've downloaded Serpent and figured out how to serialize (serpent.line(message)), but I can't figure out how to deserialize, do you happen to know the name of the deserialization function?
It's there in the link I gave you ;).

Code: Select all

local serpent = require("serpent")
local a = {type = "move", x = 153, y = 356}

local x1 = serpent.dump(a)
local x2 = serpent.line(a)
local x3 = serpent.block(a)

local fun, err = loadstring(x1)
if err then error(err) end
local copy = fun()

function love.draw()
	love.graphics.print(string.format("%s\n\n%s\n\n%s\n\n%s", x1, x2, x3, copy.type), 0, 0)
end
You have to use serpent.dump, not serpent.line.
User avatar
Sekaru
Prole
Posts: 17
Joined: Sun Oct 07, 2012 5:09 pm

Re: Packets in LUBE

Post by Sekaru »

Oh okay so you use all three.

And do you always have to do the "string.format("%s\n\n%s\n\n%s\n\n%s")" bit for everything you send over or is that specific to that example?
User avatar
Przemator
Party member
Posts: 107
Joined: Fri Sep 28, 2012 6:59 pm

Re: Packets in LUBE

Post by Przemator »

Sekaru wrote:Oh okay so you use all three.

And do you always have to do the "string.format("%s\n\n%s\n\n%s\n\n%s")" bit for everything you send over or is that specific to that example?
I have a feeling like you didn't analyse the code thoroughly ;). You don't use all three functions, you only use serpent.dump. I think the example would become more clear if you pasted it into main.lua and saw what it does.

The function that converts the string into data is the built-in loadstring function.

The string.format function is a convenient function that dates back to C printf function. First you specify a string, and then you fill it with variables, according to the format. I only used it to present the differences between each of the serpent functions. I must say this is some basic programming stuff, I'm no pro myself and I think string formatting is a life saver.
User avatar
Sekaru
Prole
Posts: 17
Joined: Sun Oct 07, 2012 5:09 pm

Re: Packets in LUBE

Post by Sekaru »

Well thanks a lot man. I've finally got it all working and I pretty much understand it (hadn't slept for around 30 hours when I first read your post - I'll blame that :P).
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 4 guests