Coder Social home page Coder Social logo

utils's Introduction

GM-Com @gd-com/utils

Binary serialization helper godot and nodejs !

Written with this api

Requirements

  • Godot 3.X
  • NodeJS 10.14.0 LTS or greater

How to install

npm install --save @gd-com/utils

For an example take a look @gd-com/examples !
const gdCom = require('@gd-com/utils') // var { GdBuffer } = require('@gd-com/utils')

const test1 = gdCom.putVar(8)

const lengthBuffer = Buffer.alloc(4)
lengthBuffer.writeUInt32LE(test1.length, 0)

const finalBuffer = Buffer.concat([lengthBuffer, test1])

console.log(finalBuffer)

Available from gdCom

Encode and Decode

- getX

Method Return
getVar(buffer, offset = 0) Object { value, length }
get8(buffer, offset = 0) Object { value, length }
get16(buffer, offset = 0) Object { value, length }
get32(buffer, offset = 0) Object { value, length }
get64(buffer, offset = 0) Object { value, length }
getU8(buffer, offset = 0) Object { value, length }
getU16(buffer, offset = 0) Object { value, length }
getU32(buffer, offset = 0) Object { value, length }
getU64(buffer, offset = 0) Object { value, length }
getFloat(buffer, offset = 0) Object { value, length }
getDouble(buffer, offset = 0) Object { value, length }
getString(buffer, offset = 0) Object { value, length }

- putX

Method Return
putVar(value, type) Buffer
put8(value) Buffer
put16(value) Buffer
put32(value) Buffer
put64(value) Buffer
putU8(value) Buffer
putU16(value) Buffer
putU32(value) Buffer
putU64(value) Buffer
putFloat(value) Buffer
putDouble(value) Buffer
putString(value) Buffer

TYPE

Name Value
NULL 0
BOOL 1
INTEGER 2
FLOAT 3
STRING 4
VECTOR2 5
RECT2 6
VECTOR3 7
TRANSFORM2D 8
PLANE 9
QUATERNION 10
AABB 11
BASIS 12
TRANSFORM 13
COLOR 14
NODE_PATH 15
RID // unsupported 16
OBJECT // unsupported 17
DICTIONARY 18
ARRAY 19
POOL_BYTE_ARRAY 20
POOL_INT_ARRAY 21
POOL_REAL_ARRAY 22
POOL_STRING_ARRAY 23
POOL_VECTOR2_ARRAY 24
POOL_VECTOR3_ARRAY 25
POOL_COLOR_ARRAY 26
MAX 27

StreamTcp Splitter

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(4, 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()
  }
}

module.exports = StreamTcp
const net = require('net')
const { putVar, getVar } = require('@gd-com/utils')

const tcpSplit = new StreamTcp()

let server = net.createServer((socket) => {
  socket.pipe(tcpSplit).on('data', (data) => {
    const packet = new Buffer.from(data)

    const decoded = getVar(packet)
    console.log('receive :', decoded.value)

    const packetToSend = putVar(Math.random())

    // we need to put the packet length on top cause it's tcp
    const lengthBuffer = Buffer.alloc(4)
    lengthBuffer.writeUInt32LE(packetToSend.length, 0)
    const finalBuffer = Buffer.concat([lengthBuffer, packetToSend])

    console.log('send :', finalBuffer)
    socket.write(finalBuffer)
  })

  socket.on('error', () => console.log('Bye :('))
})

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

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

Test

git clone [email protected]:gd-com/utils.git gd-com-utils
cd gd-com-utils
npm install or yarn install
npm run lint && npm run test

Contributing

Please read CONTRIBUTING for details on our code of conduct, and the process for submitting pull requests to us.

TODO & CHANGELOG

CHANGELOG

TODO

License

This project is licensed under the MIT License - see the LICENSE file for details

Thanks

  • Godot
  • Godot France
  • GDQuest
  • IG-Dev
  • Salsa2k for the initial work

Buy me a coffe

BTC : 3DHU92kNHRbe2374cE2DrxDfE6i6Mu7ZSv

ETH : 0x316dcd4F84606B7165f6ae2928594c3ef0FF3828

utils's People

Contributors

phated avatar timoschwarzer avatar dependabot[bot] avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.