mirror of
https://github.com/CPunch/gopenfusion.git
synced 2024-11-25 00:21:05 +00:00
packet: refactoring; use go's binary package for encoding *most* things
This commit is contained in:
parent
52f0c4b1c7
commit
5cc3f275c1
@ -3,10 +3,11 @@ package protocol
|
|||||||
import (
|
import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"unicode/utf16"
|
"unicode/utf16"
|
||||||
"unsafe"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -17,60 +18,61 @@ import (
|
|||||||
type Packet struct {
|
type Packet struct {
|
||||||
ByteOrder binary.ByteOrder
|
ByteOrder binary.ByteOrder
|
||||||
Buf []byte
|
Buf []byte
|
||||||
cursor int // to keep track of things like member alignment for easier debugging
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPacket(buf []byte) *Packet {
|
func NewPacket(buf []byte) *Packet {
|
||||||
pkt := &Packet{
|
pkt := &Packet{
|
||||||
ByteOrder: binary.LittleEndian,
|
ByteOrder: binary.LittleEndian,
|
||||||
Buf: buf,
|
Buf: buf,
|
||||||
cursor: 0,
|
|
||||||
}
|
}
|
||||||
return pkt
|
return pkt
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pkt *Packet) ResetCursor() {
|
|
||||||
pkt.cursor = 0
|
|
||||||
}
|
|
||||||
|
|
||||||
func (pkt *Packet) writeRaw(data []byte) {
|
func (pkt *Packet) writeRaw(data []byte) {
|
||||||
pkt.Buf = append(pkt.Buf, data...)
|
pkt.Buf = append(pkt.Buf, data...)
|
||||||
pkt.cursor += len(data)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pkt *Packet) Write(data []byte) {
|
func (pkt *Packet) Write(data []byte) (int, error) {
|
||||||
pkt.writeRaw(data)
|
pkt.writeRaw(data)
|
||||||
|
|
||||||
if len(pkt.Buf) > CN_PACKET_BUFFER_SIZE {
|
if len(pkt.Buf) > CN_PACKET_BUFFER_SIZE {
|
||||||
panic(fmt.Errorf("Failed to write to packet, invalid size!"))
|
return 0, fmt.Errorf("Failed to write to packet, invalid size!")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return len(data), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pkt *Packet) writeByte(data byte) {
|
func (pkt *Packet) writeByte(data byte) {
|
||||||
pkt.Write([]byte{data})
|
pkt.Write([]byte{data})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pkt *Packet) readRaw(sz int) []byte {
|
func (pkt *Packet) readRaw(data []byte) (int, error) {
|
||||||
data := pkt.Buf[:sz]
|
sz := copy(data, pkt.Buf)
|
||||||
pkt.Buf = pkt.Buf[sz:]
|
pkt.Buf = pkt.Buf[sz:]
|
||||||
pkt.cursor += sz
|
|
||||||
return data
|
|
||||||
}
|
|
||||||
|
|
||||||
func (pkt *Packet) Read(sz int) []byte {
|
if sz != len(data) {
|
||||||
if sz > len(pkt.Buf) {
|
return sz, io.EOF
|
||||||
panic(fmt.Errorf("Failed to read from packet, invalid size!"))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return pkt.readRaw(sz)
|
return sz, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pkt *Packet) Read(data []byte) (int, error) {
|
||||||
|
if len(data) > len(pkt.Buf) {
|
||||||
|
return 0, fmt.Errorf("Failed to read from packet, invalid size!")
|
||||||
|
}
|
||||||
|
|
||||||
|
return pkt.readRaw(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pkt *Packet) readByte() byte {
|
func (pkt *Packet) readByte() byte {
|
||||||
return pkt.Read(1)[0]
|
data := pkt.Buf[0]
|
||||||
|
pkt.Buf = pkt.Buf[1:]
|
||||||
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pkt *Packet) encodeStructField(field reflect.StructField, value reflect.Value) {
|
func (pkt *Packet) encodeStructField(field reflect.StructField, value reflect.Value) {
|
||||||
// log.Printf("Encoding '%s', current cursor: %d", field.Name, len(pkt.Buf))
|
log.Printf("Encoding '%s'", field.Name)
|
||||||
|
|
||||||
switch field.Type.Kind() {
|
switch field.Type.Kind() {
|
||||||
case reflect.String: // all strings in fusionfall packets are encoded as utf16, we'll need to encode it
|
case reflect.String: // all strings in fusionfall packets are encoded as utf16, we'll need to encode it
|
||||||
@ -79,22 +81,21 @@ func (pkt *Packet) encodeStructField(field reflect.StructField, value reflect.Va
|
|||||||
panic(fmt.Errorf("Failed to grab string 'size' tag!!"))
|
panic(fmt.Errorf("Failed to grab string 'size' tag!!"))
|
||||||
}
|
}
|
||||||
|
|
||||||
sz *= 2
|
|
||||||
buf16 := utf16.Encode([]rune(value.String()))
|
buf16 := utf16.Encode([]rune(value.String()))
|
||||||
buf := *(*[]byte)(unsafe.Pointer(&buf16))
|
|
||||||
|
|
||||||
// len(buf) needs to be the same size as sz
|
// len(buf16) needs to be the same size as sz
|
||||||
if len(buf) > sz {
|
if len(buf16) > sz {
|
||||||
// truncate
|
// truncate
|
||||||
buf = buf[:sz]
|
buf16 = buf16[:sz]
|
||||||
} else {
|
} else {
|
||||||
// grow
|
// grow
|
||||||
for len(buf) < sz {
|
for len(buf16) < sz {
|
||||||
buf = append(buf, 0)
|
buf16 = append(buf16, 0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pkt.Write(buf)
|
// write
|
||||||
|
binary.Write(pkt, pkt.ByteOrder, buf16)
|
||||||
default:
|
default:
|
||||||
pkt.Encode(value.Addr().Interface())
|
pkt.Encode(value.Addr().Interface())
|
||||||
}
|
}
|
||||||
@ -118,48 +119,14 @@ func (pkt *Packet) Encode(data interface{}) {
|
|||||||
for i := 0; i < sz; i++ {
|
for i := 0; i < sz; i++ {
|
||||||
pkt.encodeStructField(rv.Type().Field(i), rv.Field(i))
|
pkt.encodeStructField(rv.Type().Field(i), rv.Field(i))
|
||||||
}
|
}
|
||||||
case reflect.Array:
|
default:
|
||||||
sz := rv.Len()
|
// we pass everything else to go's binary package
|
||||||
|
binary.Write(pkt, pkt.ByteOrder, data)
|
||||||
// encode data
|
|
||||||
for i := 0; i < sz; i++ {
|
|
||||||
elem := rv.Index(i)
|
|
||||||
pkt.Encode(elem.Addr().Interface())
|
|
||||||
}
|
|
||||||
case reflect.Uint8:
|
|
||||||
pkt.writeByte(byte(rv.Uint()))
|
|
||||||
case reflect.Uint16:
|
|
||||||
tmp := make([]byte, 2)
|
|
||||||
pkt.ByteOrder.PutUint16(tmp, uint16(rv.Uint()))
|
|
||||||
pkt.Write(tmp)
|
|
||||||
case reflect.Uint32:
|
|
||||||
tmp := make([]byte, 4)
|
|
||||||
pkt.ByteOrder.PutUint32(tmp, uint32(rv.Uint()))
|
|
||||||
pkt.Write(tmp)
|
|
||||||
case reflect.Uint64:
|
|
||||||
tmp := make([]byte, 8)
|
|
||||||
pkt.ByteOrder.PutUint64(tmp, uint64(rv.Uint()))
|
|
||||||
pkt.Write(tmp)
|
|
||||||
// im using the PutUintX() api here because it's functionally the same and apparently PutIntX doesn't exist?
|
|
||||||
case reflect.Int8:
|
|
||||||
pkt.writeByte(byte(rv.Int()))
|
|
||||||
case reflect.Int16:
|
|
||||||
tmp := make([]byte, 2)
|
|
||||||
pkt.ByteOrder.PutUint16(tmp, uint16(rv.Int()))
|
|
||||||
pkt.Write(tmp)
|
|
||||||
case reflect.Int32:
|
|
||||||
tmp := make([]byte, 4)
|
|
||||||
pkt.ByteOrder.PutUint32(tmp, uint32(rv.Int()))
|
|
||||||
pkt.Write(tmp)
|
|
||||||
case reflect.Int64:
|
|
||||||
tmp := make([]byte, 8)
|
|
||||||
pkt.ByteOrder.PutUint64(tmp, uint64(rv.Int()))
|
|
||||||
pkt.Write(tmp)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pkt *Packet) decodeStructField(field reflect.StructField, value reflect.Value) {
|
func (pkt *Packet) decodeStructField(field reflect.StructField, value reflect.Value) {
|
||||||
// log.Printf("Decoding '%s', current cursor: %d", field.Name, pkt.cursor)
|
log.Printf("Decoding '%s'", field.Name)
|
||||||
|
|
||||||
switch field.Type.Kind() {
|
switch field.Type.Kind() {
|
||||||
case reflect.String: // all strings in fusionfall packets are encoded as utf16, we'll need to decode it
|
case reflect.String: // all strings in fusionfall packets are encoded as utf16, we'll need to decode it
|
||||||
@ -168,12 +135,8 @@ func (pkt *Packet) decodeStructField(field reflect.StructField, value reflect.Va
|
|||||||
panic(fmt.Errorf("Failed to grab string 'size' tag!!"))
|
panic(fmt.Errorf("Failed to grab string 'size' tag!!"))
|
||||||
}
|
}
|
||||||
|
|
||||||
// sz * sizeof(char16_t)
|
buf16 := make([]uint16, sz)
|
||||||
sz *= 2
|
binary.Read(pkt, pkt.ByteOrder, buf16)
|
||||||
buf := make([]byte, 0, sz)
|
|
||||||
buf = append(buf, pkt.Read(sz)...)
|
|
||||||
|
|
||||||
buf16 := *(*[]uint16)(unsafe.Pointer(&buf))
|
|
||||||
|
|
||||||
// find null terminator
|
// find null terminator
|
||||||
var realSize int
|
var realSize int
|
||||||
@ -183,8 +146,7 @@ func (pkt *Packet) decodeStructField(field reflect.StructField, value reflect.Va
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
str := string(utf16.Decode(buf16[:realSize]))
|
value.SetString(string(utf16.Decode(buf16[:realSize])))
|
||||||
value.SetString(str)
|
|
||||||
default:
|
default:
|
||||||
pkt.Decode(value.Addr().Interface())
|
pkt.Decode(value.Addr().Interface())
|
||||||
}
|
}
|
||||||
@ -208,29 +170,7 @@ func (pkt *Packet) Decode(data interface{}) {
|
|||||||
for i := 0; i < sz; i++ {
|
for i := 0; i < sz; i++ {
|
||||||
pkt.decodeStructField(rv.Type().Field(i), rv.Field(i))
|
pkt.decodeStructField(rv.Type().Field(i), rv.Field(i))
|
||||||
}
|
}
|
||||||
case reflect.Array:
|
default:
|
||||||
sz := rv.Len()
|
binary.Read(pkt, pkt.ByteOrder, data)
|
||||||
|
|
||||||
// decode data
|
|
||||||
for i := 0; i < sz; i++ {
|
|
||||||
elem := rv.Index(i)
|
|
||||||
pkt.Decode(elem.Addr().Interface())
|
|
||||||
}
|
|
||||||
case reflect.Uint8:
|
|
||||||
rv.SetUint(uint64(pkt.readByte()))
|
|
||||||
case reflect.Uint16:
|
|
||||||
rv.SetUint(uint64(pkt.ByteOrder.Uint16(pkt.Read(2))))
|
|
||||||
case reflect.Uint32:
|
|
||||||
rv.SetUint(uint64(pkt.ByteOrder.Uint32(pkt.Read(4))))
|
|
||||||
case reflect.Uint64:
|
|
||||||
rv.SetUint(pkt.ByteOrder.Uint64(pkt.Read(8)))
|
|
||||||
case reflect.Int8:
|
|
||||||
rv.SetInt(int64(pkt.readByte()))
|
|
||||||
case reflect.Int16:
|
|
||||||
rv.SetInt(int64(pkt.ByteOrder.Uint16(pkt.Read(2))))
|
|
||||||
case reflect.Int32:
|
|
||||||
rv.SetInt(int64(pkt.ByteOrder.Uint32(pkt.Read(4))))
|
|
||||||
case reflect.Int64:
|
|
||||||
rv.SetInt(int64(pkt.ByteOrder.Uint64(pkt.Read(8))))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,6 +35,7 @@ func (server *LoginServer) AcceptLogin(peer *Peer, SzID string, IClientVerC int3
|
|||||||
UiSvrTime: uint64(time.Now().Unix()),
|
UiSvrTime: uint64(time.Now().Unix()),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// swap keys
|
||||||
peer.Send(resp, protocol.P_LS2CL_REP_LOGIN_SUCC)
|
peer.Send(resp, protocol.P_LS2CL_REP_LOGIN_SUCC)
|
||||||
peer.E_key = protocol.CreateNewKey(
|
peer.E_key = protocol.CreateNewKey(
|
||||||
resp.UiSvrTime,
|
resp.UiSvrTime,
|
||||||
|
Loading…
Reference in New Issue
Block a user