I have made a packet package with a packet structure inside like 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 to byte
b := make([]byte, 2)
binary.LittleEndian.PutUint16(b, uint16(i_))
//Push byte to buffer
p.buffer.Write([]byte(b))
}
func (p Packet) AddByte(b []byte) {
p.buffer.Write(b)
}
This is the session package that uses the packet structure to form packets and send them to the client
package Session
type MapleSession struct {
connection net.Conn
EncryptIV, DecryptIV []byte
isConnected bool
}
func (session *MapleSession) Run(conn net.Conn) {
//Display where the new connection is coming from
session.connection = conn
fmt.Println("Client connected from:", session.connection.RemoteAddr())
//Set the user connected variable on
session.isConnected = true
//Send Handshake
packet := MaplePacket.CreateHandShake(&session.EncryptIV, &session.DecryptIV, 40, "", []byte("0x05"))
session.connection.Write(packet)
}
This is the MaplePacket package that creates the packets to send to the client that are requested from the 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 the 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 a packet like in the example above and adding values, the Packet.GetBytes() returns an empty array. Is bytes.Buffer the correct way to go about this is? Or am I going completely wrong in how I am approaching this?