protocol/packet: switch away from allocation

- Packet struct is small enough that allocation for it is really unnecessary, and can be passed around by value fairly easily
This commit is contained in:
CPunch 2023-03-10 20:00:06 -06:00
parent 985890d351
commit f1552830a0
4 changed files with 14 additions and 17 deletions

View File

@ -19,14 +19,13 @@ type Packet struct {
readWriter io.ReadWriter readWriter io.ReadWriter
} }
func NewPacket(readWriter io.ReadWriter) *Packet { func NewPacket(readWriter io.ReadWriter) Packet {
pkt := &Packet{ return Packet{
readWriter: readWriter, readWriter: readWriter,
} }
return pkt
} }
func (pkt *Packet) encodeStructField(field reflect.StructField, value reflect.Value) { func (pkt Packet) encodeStructField(field reflect.StructField, value reflect.Value) {
log.Printf("Encoding '%s'", field.Name) log.Printf("Encoding '%s'", field.Name)
switch field.Type.Kind() { switch field.Type.Kind() {
@ -65,7 +64,7 @@ func (pkt *Packet) encodeStructField(field reflect.StructField, value reflect.Va
} }
} }
func (pkt *Packet) Encode(data interface{}) { func (pkt Packet) Encode(data interface{}) {
rv := reflect.Indirect(reflect.ValueOf(data)) rv := reflect.Indirect(reflect.ValueOf(data))
switch rv.Kind() { switch rv.Kind() {
@ -81,7 +80,7 @@ func (pkt *Packet) Encode(data interface{}) {
} }
} }
func (pkt *Packet) decodeStructField(field reflect.StructField, value reflect.Value) { func (pkt Packet) decodeStructField(field reflect.StructField, value reflect.Value) {
log.Printf("Decoding '%s'", field.Name) log.Printf("Decoding '%s'", field.Name)
switch field.Type.Kind() { switch field.Type.Kind() {
@ -116,7 +115,7 @@ func (pkt *Packet) decodeStructField(field reflect.StructField, value reflect.Va
} }
} }
func (pkt *Packet) Decode(data interface{}) { func (pkt Packet) Decode(data interface{}) {
rv := reflect.Indirect(reflect.ValueOf(data)) rv := reflect.Indirect(reflect.ValueOf(data))
switch rv.Kind() { switch rv.Kind() {

View File

@ -54,7 +54,7 @@ func (server *LoginServer) AcceptLogin(peer *Peer, SzID string, IClientVerC int3
} }
} }
func (server *LoginServer) Login(peer *Peer, pkt *protocol.Packet) { func (server *LoginServer) Login(peer *Peer, pkt protocol.Packet) {
var loginPkt protocol.SP_CL2LS_REQ_LOGIN var loginPkt protocol.SP_CL2LS_REQ_LOGIN
pkt.Decode(&loginPkt) pkt.Decode(&loginPkt)
@ -125,7 +125,7 @@ func (server *LoginServer) Login(peer *Peer, pkt *protocol.Packet) {
server.AcceptLogin(peer, loginPkt.SzID, loginPkt.IClientVerC, 1, charInfo) server.AcceptLogin(peer, loginPkt.SzID, loginPkt.IClientVerC, 1, charInfo)
} }
func (server *LoginServer) CheckCharacterName(peer *Peer, pkt *protocol.Packet) { func (server *LoginServer) CheckCharacterName(peer *Peer, pkt protocol.Packet) {
var charPkt protocol.SP_CL2LS_REQ_CHECK_CHAR_NAME var charPkt protocol.SP_CL2LS_REQ_CHECK_CHAR_NAME
pkt.Decode(&charPkt) pkt.Decode(&charPkt)
@ -136,7 +136,7 @@ func (server *LoginServer) CheckCharacterName(peer *Peer, pkt *protocol.Packet)
}, protocol.P_LS2CL_REP_CHECK_CHAR_NAME_SUCC) }, protocol.P_LS2CL_REP_CHECK_CHAR_NAME_SUCC)
} }
func (server *LoginServer) SaveCharacterName(peer *Peer, pkt *protocol.Packet) { func (server *LoginServer) SaveCharacterName(peer *Peer, pkt protocol.Packet) {
var charPkt protocol.SP_CL2LS_REQ_SAVE_CHAR_NAME var charPkt protocol.SP_CL2LS_REQ_SAVE_CHAR_NAME
pkt.Decode(&charPkt) pkt.Decode(&charPkt)
@ -187,7 +187,7 @@ func validateCharacterCreation(character *protocol.SP_CL2LS_REQ_CHAR_CREATE) boo
return true return true
} }
func (server *LoginServer) CharacterCreate(peer *Peer, pkt *protocol.Packet) { func (server *LoginServer) CharacterCreate(peer *Peer, pkt protocol.Packet) {
var charPkt protocol.SP_CL2LS_REQ_CHAR_CREATE var charPkt protocol.SP_CL2LS_REQ_CHAR_CREATE
pkt.Decode(&charPkt) pkt.Decode(&charPkt)
@ -216,7 +216,7 @@ func (server *LoginServer) CharacterCreate(peer *Peer, pkt *protocol.Packet) {
}, protocol.P_LS2CL_REP_CHAR_CREATE_SUCC) }, protocol.P_LS2CL_REP_CHAR_CREATE_SUCC)
} }
func (server *LoginServer) CharacterDelete(peer *Peer, pkt *protocol.Packet) { func (server *LoginServer) CharacterDelete(peer *Peer, pkt protocol.Packet) {
var charPkt protocol.SP_CL2LS_REQ_CHAR_DELETE var charPkt protocol.SP_CL2LS_REQ_CHAR_DELETE
pkt.Decode(&charPkt) pkt.Decode(&charPkt)
@ -231,7 +231,7 @@ func (server *LoginServer) CharacterDelete(peer *Peer, pkt *protocol.Packet) {
}, protocol.P_LS2CL_REP_CHAR_DELETE_SUCC) }, protocol.P_LS2CL_REP_CHAR_DELETE_SUCC)
} }
func (server *LoginServer) FinishTutorial(peer *Peer, pkt *protocol.Packet) { func (server *LoginServer) FinishTutorial(peer *Peer, pkt protocol.Packet) {
var charPkt protocol.SP_CL2LS_REQ_SAVE_CHAR_TUTOR var charPkt protocol.SP_CL2LS_REQ_SAVE_CHAR_TUTOR
pkt.Decode(&charPkt) pkt.Decode(&charPkt)

View File

@ -42,7 +42,7 @@ func (server *LoginServer) Start() {
} }
} }
func (server *LoginServer) HandlePacket(peer *Peer, typeID uint32, pkt *protocol.Packet) { func (server *LoginServer) HandlePacket(peer *Peer, typeID uint32, pkt protocol.Packet) {
switch typeID { switch typeID {
case protocol.P_CL2LS_REQ_LOGIN: case protocol.P_CL2LS_REQ_LOGIN:
server.Login(peer, pkt) server.Login(peer, pkt)

View File

@ -18,7 +18,7 @@ const (
) )
type PeerHandler interface { type PeerHandler interface {
HandlePacket(client *Peer, typeID uint32, pkt *protocol.Packet) HandlePacket(client *Peer, typeID uint32, pkt protocol.Packet)
Connect(client *Peer) Connect(client *Peer)
Disconnect(client *Peer) Disconnect(client *Peer)
} }
@ -116,8 +116,6 @@ func (client *Peer) ClientHandler() {
panic(fmt.Errorf("[FATAL] failed to read packet body! %v", err)) panic(fmt.Errorf("[FATAL] failed to read packet body! %v", err))
} }
fmt.Printf("%#v", buf)
// decrypt // decrypt
protocol.DecryptData(buf.Bytes(), client.E_key) protocol.DecryptData(buf.Bytes(), client.E_key)