From f1552830a0f7219e9acfa01fe4045a854a8ef1d6 Mon Sep 17 00:00:00 2001 From: CPunch Date: Fri, 10 Mar 2023 20:00:06 -0600 Subject: [PATCH] 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 --- protocol/packet.go | 13 ++++++------- server/login.go | 12 ++++++------ server/loginserver.go | 2 +- server/peer.go | 4 +--- 4 files changed, 14 insertions(+), 17 deletions(-) diff --git a/protocol/packet.go b/protocol/packet.go index d2b0829..ad3d74d 100644 --- a/protocol/packet.go +++ b/protocol/packet.go @@ -19,14 +19,13 @@ type Packet struct { readWriter io.ReadWriter } -func NewPacket(readWriter io.ReadWriter) *Packet { - pkt := &Packet{ +func NewPacket(readWriter io.ReadWriter) Packet { + return Packet{ 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) 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)) 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) 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)) switch rv.Kind() { diff --git a/server/login.go b/server/login.go index 8a5bdd0..16d44a3 100644 --- a/server/login.go +++ b/server/login.go @@ -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 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) } -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 pkt.Decode(&charPkt) @@ -136,7 +136,7 @@ func (server *LoginServer) CheckCharacterName(peer *Peer, pkt *protocol.Packet) }, 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 pkt.Decode(&charPkt) @@ -187,7 +187,7 @@ func validateCharacterCreation(character *protocol.SP_CL2LS_REQ_CHAR_CREATE) boo 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 pkt.Decode(&charPkt) @@ -216,7 +216,7 @@ func (server *LoginServer) CharacterCreate(peer *Peer, pkt *protocol.Packet) { }, 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 pkt.Decode(&charPkt) @@ -231,7 +231,7 @@ func (server *LoginServer) CharacterDelete(peer *Peer, pkt *protocol.Packet) { }, 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 pkt.Decode(&charPkt) diff --git a/server/loginserver.go b/server/loginserver.go index b575d23..9ff41c7 100644 --- a/server/loginserver.go +++ b/server/loginserver.go @@ -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 { case protocol.P_CL2LS_REQ_LOGIN: server.Login(peer, pkt) diff --git a/server/peer.go b/server/peer.go index 0862269..b122257 100644 --- a/server/peer.go +++ b/server/peer.go @@ -18,7 +18,7 @@ const ( ) type PeerHandler interface { - HandlePacket(client *Peer, typeID uint32, pkt *protocol.Packet) + HandlePacket(client *Peer, typeID uint32, pkt protocol.Packet) Connect(client *Peer) Disconnect(client *Peer) } @@ -116,8 +116,6 @@ func (client *Peer) ClientHandler() { panic(fmt.Errorf("[FATAL] failed to read packet body! %v", err)) } - fmt.Printf("%#v", buf) - // decrypt protocol.DecryptData(buf.Bytes(), client.E_key)