0 votes

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,

in Engine by (32 points)
edited by

1 Answer

0 votes
Best answer

From @Faless :

put_var always put the variant size

putpacket and putvar in TCP always add the packet size

Thx

by (32 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.