How put_var works ?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Tilican

Hi,

For developing my networking library i want to know more about put_var.
I find something really weird, i’ll explain it :

TCP

Client :

extends Node

var connection = null
var peerstream = null
var test = null

func _ready():
    print("Start client TCP")
    # Connect
    connection = StreamPeerTCP.new()
    connection.connect_to_host("127.0.0.1", 9090)
    peerstream = PacketPeerStream.new()
    peerstream.set_stream_peer(connection)

func _process(delta):
    if connection.is_connected_to_host():
        peerstream.put_var("1")
        get_tree().quit()

Server :

( Transform is here to split stream cause sometime i get multiple “message” )

const net = require('net')
const Transform = require('stream').Transform

class StreamTcp extends Transform {
  _transform (chunk, enc, done) {
    let buffer = chunk
    while (buffer.length > 0) {
      const length = buffer.readUInt16LE(0)

      const bufferSplitted = buffer.slice(0, length + 4) // 4 cause the length bytes is in buffer
      buffer = buffer.slice(length + 4, buffer.length) // 4 cause the length bytes is in buffer

      this.push(bufferSplitted)
    }
    done()
  }
}

const server = net.createServer((socket) => {
  const tcpSplit = new StreamTcp()
  socket.pipe(tcpSplit).on('data', (data) => {
    console.log('Recieve :', Buffer.from(data))
  })
  socket.on('error', () => console.log('Bye :('))
})

server.on('error', (err) => {
  throw err
})

server.listen(9090, '127.0.0.1', () => {
  console.log(`Server launched TCP 127.0.0.1:${9090}`)
})

I recieve : <Buffer 0c 00 00 00 04 00 00 00 01 00 00 00 31 00 00 00>

For TCP it’s ok cause 0c 00 00 00 it’s the length of the message

Now i change client code

Client :

extends Node

var connection = null
var peerstream = null

func _ready():
    connection = StreamPeerTCP.new()
    connection.connect_to_host("127.0.0.1", 9090)
    peerstream = PacketPeerStream.new()
    peerstream.set_stream_peer(connection)

func _process(delta):
    if connection.is_connected_to_host():
        var buffer = StreamPeerBuffer.new()
        buffer.put_u8(1)
        buffer.put_var("1")
        peerstream.put_packet(buffer.get_data_array())
        get_tree().quit()

Result : <Buffer 11 00 00 00 01 0c 00 00 00 04 00 00 00 01 00 00 00 31 00 00 00>

  • 11 00 00 00 is for the total size here 17 (it’s hex)
  • 01 => uint8
  • 0c 00 00 00 the length of the variant
  • 04 00 00 00 01 00 00 00 31 00 00 00 the variant string itself

why not <Buffer 0d 00 00 00 01 04 00 00 00 01 00 00 00 31 00 00 00>

TCP add an header with the total size of the message

UDP

Client :

extends Node

var connection = PacketPeerUDP.new()
var test = null

func _ready():
	print("Start client UDP")
	# Connect
	connection.set_dest_address("127.0.0.1", 9091)
	connection.put_var(null)

func _process(delta):
	if connection.is_listening():
		connection.put_var("1")
		get_tree().quit()

Server :

const dgram = require('dgram')

const server = dgram.createSocket('udp4')

server.on('listening', () => {
  const address = server.address()
  console.log(`UDP Server listening on ${address.address}:${address.port}`)
})

server.on('message', (buf, remote) => {
  console.log('Recieve ', Buffer.from(buf))
})

server.bind(9091, '127.0.0.1')

I recieve <Buffer 04 00 00 00 01 00 00 00 31 00 00 00>

Now i change the client code

extends Node

var connection = PacketPeerUDP.new()
var test = null

func _ready():
	connection.set_dest_address("127.0.0.1", 9091)
	connection.put_var(null)

func _process(delta):
	if connection.is_listening():
		var buffer = StreamPeerBuffer.new()
		buffer.put_u8(1)
		buffer.put_var("1")
		connection.put_packet(buffer.get_data_array())
		get_tree().quit()

result : <Buffer 01 0c 00 00 00 04 00 00 00 01 00 00 00 31 00 00 00>

  • 01 => uint8
  • 0c 00 00 00 the length of the variant
  • 04 00 00 00 01 00 00 00 31 00 00 00 the variant string itself

Why i got this 0c 00 00 00 i’m on UDP i don’t need this !?

So why on UDP i don’t get <Buffer 01 04 00 00 00 01 00 00 00 31 00 00 00> ?

Sorry for this big message ^^

Thx,

:bust_in_silhouette: Reply From: Tilican

From @Faless :

put_var always put the variant size

put_packet and put_var in TCP always add the packet size

Thx