From 735bdc5b3639cddd15f3c7b387b580723bc03296 Mon Sep 17 00:00:00 2001 From: CPunch Date: Sat, 18 Mar 2023 16:40:20 -0500 Subject: [PATCH] protocol: added CNPeer - moved Peer from the server package to the protocol package, it was also renamed to CNPeer as most fusionfall specific constants in the client use the 'CN' prefix. --- server/peer.go => protocol/cnpeer.go | 36 ++++++++++++++-------------- server/join.go | 6 +++-- server/login.go | 18 +++++++------- server/loginServer.go | 12 +++++----- server/shardServer.go | 20 +++++++++------- server/shared.go | 4 ++-- 6 files changed, 50 insertions(+), 46 deletions(-) rename server/peer.go => protocol/cnpeer.go (77%) diff --git a/server/peer.go b/protocol/cnpeer.go similarity index 77% rename from server/peer.go rename to protocol/cnpeer.go index acd0cdb..de965e5 100644 --- a/server/peer.go +++ b/protocol/cnpeer.go @@ -1,4 +1,4 @@ -package server +package protocol import ( "encoding/binary" @@ -7,7 +7,6 @@ import ( "log" "net" - "github.com/CPunch/gopenfusion/protocol" "github.com/CPunch/gopenfusion/protocol/pool" ) @@ -17,12 +16,12 @@ const ( ) type PeerHandler interface { - HandlePacket(client *Peer, typeID uint32, pkt protocol.Packet) error - Connect(client *Peer) - Disconnect(client *Peer) + HandlePacket(client *CNPeer, typeID uint32, pkt Packet) error + Disconnect(client *CNPeer) } -type Peer struct { +// CNPeer is a simple wrapper for net.Conn connections to send/recv packets over the Fusionfall packet protocol. +type CNPeer struct { conn net.Conn handler PeerHandler SzID string @@ -33,12 +32,12 @@ type Peer struct { alive bool } -func NewPeer(handler PeerHandler, conn net.Conn) *Peer { - return &Peer{ +func NewCNPeer(handler PeerHandler, conn net.Conn) *CNPeer { + return &CNPeer{ conn: conn, handler: handler, SzID: "", - E_key: []byte(protocol.DEFAULT_KEY), + E_key: []byte(DEFAULT_KEY), FE_key: nil, AccountID: -1, whichKey: USE_E, @@ -46,13 +45,13 @@ func NewPeer(handler PeerHandler, conn net.Conn) *Peer { } } -func (peer *Peer) Send(typeID uint32, data ...interface{}) error { +func (peer *CNPeer) Send(typeID uint32, data ...interface{}) error { // grab buffer from pool buf := pool.Get() defer pool.Put(buf) // body start - pkt := protocol.NewPacket(buf) + pkt := NewPacket(buf) // encode type id if err := pkt.Encode(typeID); err != nil { @@ -69,9 +68,9 @@ func (peer *Peer) Send(typeID uint32, data ...interface{}) error { // encrypt body switch peer.whichKey { case USE_E: - protocol.EncryptData(buf.Bytes(), peer.E_key) + EncryptData(buf.Bytes(), peer.E_key) case USE_FE: - protocol.EncryptData(buf.Bytes(), peer.FE_key) + EncryptData(buf.Bytes(), peer.FE_key) } // write packet size @@ -88,7 +87,7 @@ func (peer *Peer) Send(typeID uint32, data ...interface{}) error { return nil } -func (peer *Peer) Kill() { +func (peer *CNPeer) Kill() { if !peer.alive { return } @@ -98,7 +97,8 @@ func (peer *Peer) Kill() { peer.handler.Disconnect(peer) } -func (peer *Peer) Handler() { +// meant to be invoked as a goroutine +func (peer *CNPeer) Handler() { defer peer.Kill() for { @@ -110,7 +110,7 @@ func (peer *Peer) Handler() { } // client should never send a packet size outside of this range - if sz > protocol.CN_PACKET_BUFFER_SIZE || sz < 4 { + if sz > CN_PACKET_BUFFER_SIZE || sz < 4 { log.Printf("[FATAL] malicious packet size received! %d", sz) return } @@ -124,8 +124,8 @@ func (peer *Peer) Handler() { } // decrypt - protocol.DecryptData(buf.Bytes(), peer.E_key) - pkt := protocol.NewPacket(buf) + DecryptData(buf.Bytes(), peer.E_key) + pkt := NewPacket(buf) // create packet && read typeID var typeID uint32 diff --git a/server/join.go b/server/join.go index 9220cd2..78de08c 100644 --- a/server/join.go +++ b/server/join.go @@ -2,7 +2,9 @@ package server import "github.com/CPunch/gopenfusion/protocol" -func (server *ShardServer) RequestEnter(peer *Peer, pkt protocol.Packet) error { - +func (server *ShardServer) RequestEnter(peer *protocol.CNPeer, pkt protocol.Packet) error { + var enter protocol.SP_CL2FE_REQ_PC_ENTER + pkt.Decode(&enter) + return nil } diff --git a/server/login.go b/server/login.go index 2726dcb..a5f07bf 100644 --- a/server/login.go +++ b/server/login.go @@ -23,7 +23,7 @@ const ( LOGIN_UPDATED_EUALA_REQUIRED = 9 ) -func (server *LoginServer) AcceptLogin(peer *Peer, SzID string, IClientVerC int32, ISlotNum int8, data []protocol.SP_LS2CL_REP_CHAR_INFO) error { +func (server *LoginServer) AcceptLogin(peer *protocol.CNPeer, SzID string, IClientVerC int32, ISlotNum int8, data []protocol.SP_LS2CL_REP_CHAR_INFO) error { peer.SzID = SzID resp := protocol.SP_LS2CL_REP_LOGIN_SUCC{ @@ -61,7 +61,7 @@ func (server *LoginServer) AcceptLogin(peer *Peer, SzID string, IClientVerC int3 return nil } -func (server *LoginServer) Login(peer *Peer, pkt protocol.Packet) error { +func (server *LoginServer) Login(peer *protocol.CNPeer, pkt protocol.Packet) error { var loginPkt protocol.SP_CL2LS_REQ_LOGIN pkt.Decode(&loginPkt) @@ -137,7 +137,7 @@ func (server *LoginServer) Login(peer *Peer, pkt protocol.Packet) error { return server.AcceptLogin(peer, loginPkt.SzID, loginPkt.IClientVerC, 1, charInfo[:len(plrs)]) } -func (server *LoginServer) CheckCharacterName(peer *Peer, pkt protocol.Packet) error { +func (server *LoginServer) CheckCharacterName(peer *protocol.CNPeer, pkt protocol.Packet) error { var charPkt protocol.SP_CL2LS_REQ_CHECK_CHAR_NAME pkt.Decode(&charPkt) @@ -148,7 +148,7 @@ func (server *LoginServer) CheckCharacterName(peer *Peer, pkt protocol.Packet) e }) } -func (server *LoginServer) SaveCharacterName(peer *Peer, pkt protocol.Packet) error { +func (server *LoginServer) SaveCharacterName(peer *protocol.CNPeer, pkt protocol.Packet) error { var charPkt protocol.SP_CL2LS_REQ_SAVE_CHAR_NAME pkt.Decode(&charPkt) @@ -200,7 +200,7 @@ func validateCharacterCreation(character *protocol.SP_CL2LS_REQ_CHAR_CREATE) boo return true } -func SendFail(peer *Peer) error { +func SendFail(peer *protocol.CNPeer) error { if err := peer.Send(protocol.P_LS2CL_REP_SHARD_SELECT_FAIL, protocol.SP_LS2CL_REP_SHARD_SELECT_FAIL{ IErrorCode: 2, }); err != nil { @@ -210,7 +210,7 @@ func SendFail(peer *Peer) error { return nil } -func (server *LoginServer) CharacterCreate(peer *Peer, pkt protocol.Packet) error { +func (server *LoginServer) CharacterCreate(peer *protocol.CNPeer, pkt protocol.Packet) error { var charPkt protocol.SP_CL2LS_REQ_CHAR_CREATE pkt.Decode(&charPkt) @@ -236,7 +236,7 @@ func (server *LoginServer) CharacterCreate(peer *Peer, pkt protocol.Packet) erro }) } -func (server *LoginServer) CharacterDelete(peer *Peer, pkt protocol.Packet) error { +func (server *LoginServer) CharacterDelete(peer *protocol.CNPeer, pkt protocol.Packet) error { var charPkt protocol.SP_CL2LS_REQ_CHAR_DELETE pkt.Decode(&charPkt) @@ -250,7 +250,7 @@ func (server *LoginServer) CharacterDelete(peer *Peer, pkt protocol.Packet) erro }) } -func (server *LoginServer) ShardSelect(peer *Peer, pkt protocol.Packet) error { +func (server *LoginServer) ShardSelect(peer *protocol.CNPeer, pkt protocol.Packet) error { var selection protocol.SP_CL2LS_REQ_CHAR_SELECT pkt.Decode(&selection) @@ -282,7 +282,7 @@ func (server *LoginServer) ShardSelect(peer *Peer, pkt protocol.Packet) error { return peer.Send(protocol.P_LS2CL_REP_SHARD_SELECT_SUCC, resp) } -func (server *LoginServer) FinishTutorial(peer *Peer, pkt protocol.Packet) error { +func (server *LoginServer) FinishTutorial(peer *protocol.CNPeer, pkt protocol.Packet) error { var charPkt protocol.SP_CL2LS_REQ_SAVE_CHAR_TUTOR pkt.Decode(&charPkt) diff --git a/server/loginServer.go b/server/loginServer.go index d648c20..b2ba991 100644 --- a/server/loginServer.go +++ b/server/loginServer.go @@ -15,7 +15,7 @@ type LoginServer struct { port int dbHndlr *db.DBHandler packetHandlers map[uint32]PacketHandler - peers map[*Peer]bool + peers map[*protocol.CNPeer]bool peersLock sync.Mutex shard *ShardServer } @@ -30,7 +30,7 @@ func NewLoginServer(dbHndlr *db.DBHandler, port int) (*LoginServer, error) { listener: listener, port: port, dbHndlr: dbHndlr, - peers: make(map[*Peer]bool), + peers: make(map[*protocol.CNPeer]bool), } server.packetHandlers = map[uint32]PacketHandler{ @@ -63,13 +63,13 @@ func (server *LoginServer) Start() { return } - client := NewPeer(server, conn) + client := protocol.NewCNPeer(server, conn) server.Connect(client) go client.Handler() } } -func (server *LoginServer) HandlePacket(peer *Peer, typeID uint32, pkt protocol.Packet) error { +func (server *LoginServer) HandlePacket(peer *protocol.CNPeer, typeID uint32, pkt protocol.Packet) error { if hndlr, ok := server.packetHandlers[typeID]; ok { if err := hndlr(peer, pkt); err != nil { return err @@ -81,14 +81,14 @@ func (server *LoginServer) HandlePacket(peer *Peer, typeID uint32, pkt protocol. return nil } -func (server *LoginServer) Disconnect(peer *Peer) { +func (server *LoginServer) Disconnect(peer *protocol.CNPeer) { server.peersLock.Lock() log.Printf("Peer %p disconnected from LOGIN\n", peer) delete(server.peers, peer) server.peersLock.Unlock() } -func (server *LoginServer) Connect(peer *Peer) { +func (server *LoginServer) Connect(peer *protocol.CNPeer) { server.peersLock.Lock() log.Printf("New peer %p connected to LOGIN\n", peer) server.peers[peer] = true diff --git a/server/shardServer.go b/server/shardServer.go index b8aafb0..5c78716 100644 --- a/server/shardServer.go +++ b/server/shardServer.go @@ -22,7 +22,7 @@ type ShardServer struct { port int dbHndlr *db.DBHandler packetHandlers map[uint32]PacketHandler - peers sync.Map // [*Peer]*db.Player + peers sync.Map // [*protocol.CNPeer]*db.Player loginMetadataQueue sync.Map // [int64]*LoginMetadata w/ int64 = serialKey } @@ -39,7 +39,9 @@ func NewShardServer(dbHndlr *db.DBHandler, port int) (*ShardServer, error) { packetHandlers: make(map[uint32]PacketHandler), } - server.packetHandlers = map[uint32]PacketHandler{} + server.packetHandlers = map[uint32]PacketHandler{ + protocol.P_CL2FE_REQ_PC_ENTER: server.RequestEnter, + } return server, nil } @@ -58,13 +60,13 @@ func (server *ShardServer) Start() { return } - client := NewPeer(server, conn) + client := protocol.NewCNPeer(server, conn) server.Connect(client) go client.Handler() } } -func (server *ShardServer) HandlePacket(peer *Peer, typeID uint32, pkt protocol.Packet) error { +func (server *ShardServer) HandlePacket(peer *protocol.CNPeer, typeID uint32, pkt protocol.Packet) error { if hndlr, ok := server.packetHandlers[typeID]; ok { if err := hndlr(peer, pkt); err != nil { return err @@ -76,24 +78,24 @@ func (server *ShardServer) HandlePacket(peer *Peer, typeID uint32, pkt protocol. return nil } -func (server *ShardServer) Disconnect(peer *Peer) { +func (server *ShardServer) Disconnect(peer *protocol.CNPeer) { log.Printf("Peer %p disconnected from SHARD\n", peer) server.peers.Delete(peer) } -func (server *ShardServer) Connect(peer *Peer) { +func (server *ShardServer) Connect(peer *protocol.CNPeer) { log.Printf("New peer %p connected to SHARD\n", peer) server.peers.Store(peer, nil) } -func (server *ShardServer) JoinPlayer(peer *Peer, player *db.Player) { +func (server *ShardServer) JoinPlayer(peer *protocol.CNPeer, player *db.Player) { server.peers.Store(peer, player) } // Simple wrapper for server.peers.Range, if f returns false the iteration is stopped. -func (server *ShardServer) RangePeers(f func(peer *Peer, player *db.Player) bool) { +func (server *ShardServer) RangePeers(f func(peer *protocol.CNPeer, player *db.Player) bool) { server.peers.Range(func(key, value any) bool { // simple wrapper to cast the datatypes - peer, ok := key.(*Peer) + peer, ok := key.(*protocol.CNPeer) if !ok { // this should never happen panic(fmt.Errorf("ShardServer.peers has an invalid key: peers[%#v] = %#v", key, value)) } diff --git a/server/shared.go b/server/shared.go index 181f0f1..24a19ee 100644 --- a/server/shared.go +++ b/server/shared.go @@ -4,6 +4,6 @@ import ( "github.com/CPunch/gopenfusion/protocol" ) -type PacketHandler func(peer *Peer, pkt protocol.Packet) error +type PacketHandler func(peer *protocol.CNPeer, pkt protocol.Packet) error -func stubbedPacket(_ *Peer, _ protocol.Packet) error { /* stubbed */ return nil } +func stubbedPacket(_ *protocol.CNPeer, _ protocol.Packet) error { /* stubbed */ return nil }