function - Golang packet strcuture returning buffer -


i have made packet package packet structure inside so:

//a packet buffer object package packet  import (     "bytes"     "encoding/binary" )  type packet struct {     buffer bytes.buffer }  func (p packet) getbytes() []byte {     return p.buffer.bytes() }  func (p packet) addstring(s string) {     p.buffer.write([]byte(s)) }  func (p packet) addint(i_ int) {     //convert int byte     b := make([]byte, 2)     binary.littleendian.putuint16(b, uint16(i_))     //push byte buffer     p.buffer.write([]byte(b)) }  func (p packet) addbyte(b []byte) {     p.buffer.write(b) } 

this session package uses packet structure form packets , send them client

package session  type maplesession struct {     connection net.conn     encryptiv, decryptiv []byte     isconnected bool }  func (session *maplesession) run(conn net.conn) {      //display new connection coming     session.connection = conn     fmt.println("client connected from:", session.connection.remoteaddr())      //set user connected variable on     session.isconnected = true      //send handshake     packet := maplepacket.createhandshake(&session.encryptiv, &session.decryptiv, 40, "", []byte("0x05"))     session.connection.write(packet) } 

this maplepacket package creates packets send client requested session package

package maplepacket  func createhandshake (eiv, div *[]byte, version int, location string, locale []byte) []byte{     packet := packet.packet{}      //create ivs     *eiv = (make([]byte, 4))     n1, _ := rand.read(*eiv)     *div = (make([]byte, 4))     n2, _ := rand.read(*div)      if (n1 + n2 < 8) {         fmt.println("error in iv generation")     }          //create packet     packet.addint(version)     packet.addstring(location)     packet.addbyte(*div)     packet.addbyte(*eiv)     packet.addbyte(locale)      fmt.println(packet.getbytes())      return packet.getbytes() } 

however when creating packet in example above , adding values, packet.getbytes() returns empty array. bytes.buffer correct way go is? or going wrong in how approaching this?

go passes arguments, including receivers, value.

try using pointer receivers: (p *packet). bytes.buffer contains state information being discarded.


package bytes

// simple byte buffer marshaling data. // buffer variable-sized buffer of bytes read , write methods. // 0 value buffer empty buffer ready use. type buffer struct {   buf       []byte            // contents bytes buf[off : len(buf)]   off       int               // read @ &buf[off], write @ &buf[len(buf)]   runebytes [utf8.utfmax]byte // avoid allocation of slice on each writebyte or rune   bootstrap [64]byte          // memory hold first slice; helps small buffers (printf) avoid allocation.   lastread  readop            // last read operation, unread* can work correctly. } 

the go programming language

effective go

methods

pointers vs. values

the rule pointers vs. values receivers value methods can invoked on pointers , values, pointer methods can invoked on pointers. because pointer methods can modify receiver; invoking them on copy of value cause modifications discarded.

your type package type equivalent following.

type packet struct {     buffer /* bytes.buffer */ struct {     buf       []byte            // contents bytes buf[off : len(buf)]     off       int               // read @ &buf[off], write @ &buf[len(buf)]     runebytes [utf8.utfmax]byte // avoid allocation of slice on each writebyte or rune     bootstrap [64]byte          // memory hold first slice; helps small buffers (printf) avoid allocation.     lastread  readop            // last read operation, unread* can work correctly. } 

you pass copy (by value) of package type variable methods. copy updated reflect new state and, upon return discarded.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -