diff --git a/cnet/cnpeer.go b/cnet/peer.go similarity index 85% rename from cnet/cnpeer.go rename to cnet/peer.go index b094f72..968c865 100644 --- a/cnet/cnpeer.go +++ b/cnet/peer.go @@ -23,8 +23,8 @@ type PacketEvent struct { PktID uint32 } -// CNPeer is a simple wrapper for net.Conn connections to send/recv packets over the Fusionfall packet protocol. -type CNPeer struct { +// Peer is a simple wrapper for net.Conn connections to send/recv packets over the Fusionfall packet protocol. +type Peer struct { uData interface{} conn net.Conn ctx context.Context @@ -38,8 +38,8 @@ type CNPeer struct { FE_key []byte } -func NewCNPeer(ctx context.Context, conn net.Conn) *CNPeer { - p := &CNPeer{ +func NewPeer(ctx context.Context, conn net.Conn) *Peer { + p := &Peer{ conn: conn, ctx: ctx, whichKey: USE_E, @@ -52,15 +52,15 @@ func NewCNPeer(ctx context.Context, conn net.Conn) *CNPeer { return p } -func (peer *CNPeer) SetUserData(uData interface{}) { +func (peer *Peer) SetUserData(uData interface{}) { peer.uData = uData } -func (peer *CNPeer) UserData() interface{} { +func (peer *Peer) UserData() interface{} { return peer.uData } -func (peer *CNPeer) Send(typeID uint32, data ...interface{}) error { +func (peer *Peer) Send(typeID uint32, data ...interface{}) error { // grab buffer from pool buf := protocol.GetBuffer() defer protocol.PutBuffer(buf) @@ -104,11 +104,11 @@ func (peer *CNPeer) Send(typeID uint32, data ...interface{}) error { return nil } -func (peer *CNPeer) SetActiveKey(whichKey int) { +func (peer *Peer) SetActiveKey(whichKey int) { peer.whichKey = whichKey } -func (peer *CNPeer) Kill() { +func (peer *Peer) Kill() { // de-bounce: only kill if alive if !peer.alive.CompareAndSwap(true, false) { return @@ -118,7 +118,7 @@ func (peer *CNPeer) Kill() { } // meant to be invoked as a goroutine -func (peer *CNPeer) Handler(eRecv chan<- *PacketEvent) error { +func (peer *Peer) Handler(eRecv chan<- *PacketEvent) error { defer func() { close(eRecv) peer.Kill() diff --git a/internal/service/service.go b/internal/service/service.go index 7aa0087..225b331 100644 --- a/internal/service/service.go +++ b/internal/service/service.go @@ -15,9 +15,9 @@ import ( "github.com/CPunch/gopenfusion/internal/protocol" ) -type PacketHandler func(peer *cnet.CNPeer, pkt protocol.Packet) error +type PacketHandler func(peer *cnet.Peer, pkt protocol.Packet) error -func StubbedPacket(_ *cnet.CNPeer, _ protocol.Packet) error { +func StubbedPacket(_ *cnet.Peer, _ protocol.Packet) error { return nil } @@ -29,18 +29,18 @@ type Service struct { started chan struct{} stopped chan struct{} packetHandlers map[uint32]PacketHandler - peers map[chan *cnet.PacketEvent]*cnet.CNPeer + peers map[chan *cnet.PacketEvent]*cnet.Peer stateLock sync.Mutex // OnDisconnect is called when a peer disconnects from the service. // uData is the stored value of the key/value pair in the peer map. // It may not be set while the service is running. (eg. srvc.Start() has been called) - OnDisconnect func(peer *cnet.CNPeer) + OnDisconnect func(peer *cnet.Peer) // OnConnect is called when a peer connects to the service. // return value is used as the value in the peer map. // It may not be set while the service is running. (eg. srvc.Start() has been called) - OnConnect func(peer *cnet.CNPeer) + OnConnect func(peer *cnet.Peer) } func RandomPort() (int, error) { @@ -70,7 +70,7 @@ func NewService(ctx context.Context, name string, port int) *Service { func (srvc *Service) Reset(ctx context.Context) { srvc.ctx = ctx srvc.packetHandlers = make(map[uint32]PacketHandler) - srvc.peers = make(map[chan *cnet.PacketEvent]*cnet.CNPeer) + srvc.peers = make(map[chan *cnet.PacketEvent]*cnet.Peer) srvc.started = make(chan struct{}) srvc.stopped = make(chan struct{}) } @@ -81,7 +81,7 @@ func (srvc *Service) AddPacketHandler(pktID uint32, handler PacketHandler) { } type newPeerConnection struct { - peer *cnet.CNPeer + peer *cnet.Peer channel chan *cnet.PacketEvent } @@ -113,18 +113,18 @@ func (srvc *Service) Start() error { } // create a new peer and pass it to the event loop - peer := cnet.NewCNPeer(srvc.ctx, conn) + peer := cnet.NewPeer(srvc.ctx, conn) eRecv := make(chan *cnet.PacketEvent) peerConnections <- newPeerConnection{channel: eRecv, peer: peer} go peer.Handler(eRecv) } } -func (srvc *Service) getPeer(channel chan *cnet.PacketEvent) *cnet.CNPeer { +func (srvc *Service) getPeer(channel chan *cnet.PacketEvent) *cnet.Peer { return srvc.peers[channel] } -func (srvc *Service) setPeer(channel chan *cnet.PacketEvent, peer *cnet.CNPeer) { +func (srvc *Service) setPeer(channel chan *cnet.PacketEvent, peer *cnet.Peer) { srvc.peers[channel] = peer } @@ -148,7 +148,7 @@ func (srvc *Service) Stopped() <-chan struct{} { // if f returns false, the iteration is stopped. // NOTE: the peer map is not locked while iterating, if you're calling this // outside of the service's event loop, you'll need to lock the peer map yourself. -func (srvc *Service) RangePeers(f func(peer *cnet.CNPeer) bool) { +func (srvc *Service) RangePeers(f func(peer *cnet.Peer) bool) { for _, peer := range srvc.peers { if !f(peer) { break @@ -168,7 +168,7 @@ func (srvc *Service) Unlock() { func (srvc *Service) stop() { // OnDisconnect handler might need to do something important - srvc.RangePeers(func(peer *cnet.CNPeer) bool { + srvc.RangePeers(func(peer *cnet.Peer) bool { peer.Kill() if srvc.OnDisconnect != nil { srvc.OnDisconnect(peer) @@ -251,7 +251,7 @@ func (srvc *Service) handleEvents(peerPipe <-chan newPeerConnection) { } } -func (srvc *Service) handlePacket(peer *cnet.CNPeer, typeID uint32, pkt protocol.Packet) error { +func (srvc *Service) handlePacket(peer *cnet.Peer, typeID uint32, pkt protocol.Packet) error { if hndlr, ok := srvc.packetHandlers[typeID]; ok { // fmt.Printf("Handling packet %x\n", typeID) if err := hndlr(peer, pkt); err != nil { @@ -264,7 +264,7 @@ func (srvc *Service) handlePacket(peer *cnet.CNPeer, typeID uint32, pkt protocol return nil } -func (srvc *Service) disconnect(channel chan *cnet.PacketEvent, peer *cnet.CNPeer) { +func (srvc *Service) disconnect(channel chan *cnet.PacketEvent, peer *cnet.Peer) { log.Printf("Peer %p disconnected from %s\n", peer, srvc.Name) if srvc.OnDisconnect != nil { srvc.OnDisconnect(peer) @@ -273,7 +273,7 @@ func (srvc *Service) disconnect(channel chan *cnet.PacketEvent, peer *cnet.CNPee srvc.removePeer(channel) } -func (srvc *Service) connect(channel chan *cnet.PacketEvent, peer *cnet.CNPeer) { +func (srvc *Service) connect(channel chan *cnet.PacketEvent, peer *cnet.Peer) { log.Printf("New peer %p connected to %s\n", peer, srvc.Name) if srvc.OnConnect != nil { srvc.OnConnect(peer) diff --git a/internal/service/service_test.go b/internal/service/service_test.go index faba080..7844653 100644 --- a/internal/service/service_test.go +++ b/internal/service/service_test.go @@ -68,7 +68,7 @@ func TestService(t *testing.T) { // our dummy packet handler wg.Add(maxDummyPeers) - srvc.AddPacketHandler(0x1234, func(peer *cnet.CNPeer, pkt protocol.Packet) error { + srvc.AddPacketHandler(0x1234, func(peer *cnet.Peer, pkt protocol.Packet) error { log.Printf("Received packet %#v", pkt) wg.Done() return nil @@ -76,12 +76,12 @@ func TestService(t *testing.T) { // wait for all dummy peers to connect and disconnect wg.Add(maxDummyPeers) - srvc.OnConnect = func(peer *cnet.CNPeer) { + srvc.OnConnect = func(peer *cnet.Peer) { wg.Done() } wg.Add(maxDummyPeers) - srvc.OnDisconnect = func(peer *cnet.CNPeer) { + srvc.OnDisconnect = func(peer *cnet.Peer) { wg.Done() } @@ -96,7 +96,7 @@ func TestService(t *testing.T) { conn, err := net.Dial("tcp", fmt.Sprintf("127.0.0.1:%d", srvcPort)) is.NoErr(err) // net.Dial error - peer := cnet.NewCNPeer(ctx, conn) + peer := cnet.NewPeer(ctx, conn) go func() { defer peer.Kill() diff --git a/login/login.go b/login/login.go index f35a5a2..84ea1a3 100644 --- a/login/login.go +++ b/login/login.go @@ -27,7 +27,7 @@ const ( LOGIN_UPDATED_EUALA_REQUIRED = 9 ) -func (server *LoginServer) AcceptLogin(peer *cnet.CNPeer, SzID string, IClientVerC int32, ISlotNum int8, data []protocol.SP_LS2CL_REP_CHAR_INFO) error { +func (server *LoginServer) AcceptLogin(peer *cnet.Peer, SzID string, IClientVerC int32, ISlotNum int8, data []protocol.SP_LS2CL_REP_CHAR_INFO) error { resp := protocol.SP_LS2CL_REP_LOGIN_SUCC{ SzID: SzID, ICharCount: int8(len(data)), @@ -63,7 +63,7 @@ func (server *LoginServer) AcceptLogin(peer *cnet.CNPeer, SzID string, IClientVe return nil } -func (server *LoginServer) Login(peer *cnet.CNPeer, pkt protocol.Packet) error { +func (server *LoginServer) Login(peer *cnet.Peer, pkt protocol.Packet) error { var loginPkt protocol.SP_CL2LS_REQ_LOGIN pkt.Decode(&loginPkt) @@ -139,7 +139,7 @@ func (server *LoginServer) Login(peer *cnet.CNPeer, pkt protocol.Packet) error { return server.AcceptLogin(peer, loginPkt.SzID, loginPkt.IClientVerC, 1, charInfo[:len(plrs)]) } -func (server *LoginServer) CheckCharacterName(peer *cnet.CNPeer, pkt protocol.Packet) error { +func (server *LoginServer) CheckCharacterName(peer *cnet.Peer, pkt protocol.Packet) error { var charPkt protocol.SP_CL2LS_REQ_CHECK_CHAR_NAME pkt.Decode(&charPkt) @@ -150,7 +150,7 @@ func (server *LoginServer) CheckCharacterName(peer *cnet.CNPeer, pkt protocol.Pa }) } -func (server *LoginServer) SaveCharacterName(peer *cnet.CNPeer, pkt protocol.Packet) error { +func (server *LoginServer) SaveCharacterName(peer *cnet.Peer, pkt protocol.Packet) error { var charPkt protocol.SP_CL2LS_REQ_SAVE_CHAR_NAME pkt.Decode(&charPkt) @@ -203,7 +203,7 @@ func validateCharacterCreation(character *protocol.SP_CL2LS_REQ_CHAR_CREATE) boo return true } -func SendFail(peer *cnet.CNPeer) error { +func SendFail(peer *cnet.Peer) error { if err := peer.Send(protocol.P_LS2CL_REP_SHARD_SELECT_FAIL, protocol.SP_LS2CL_REP_SHARD_SELECT_FAIL{ IErrorCode: 2, }); err != nil { @@ -213,7 +213,7 @@ func SendFail(peer *cnet.CNPeer) error { return nil } -func (server *LoginServer) CharacterCreate(peer *cnet.CNPeer, pkt protocol.Packet) error { +func (server *LoginServer) CharacterCreate(peer *cnet.Peer, pkt protocol.Packet) error { var charPkt protocol.SP_CL2LS_REQ_CHAR_CREATE pkt.Decode(&charPkt) @@ -246,7 +246,7 @@ func (server *LoginServer) CharacterCreate(peer *cnet.CNPeer, pkt protocol.Packe }) } -func (server *LoginServer) CharacterDelete(peer *cnet.CNPeer, pkt protocol.Packet) error { +func (server *LoginServer) CharacterDelete(peer *cnet.Peer, pkt protocol.Packet) error { var charPkt protocol.SP_CL2LS_REQ_CHAR_DELETE pkt.Decode(&charPkt) @@ -265,7 +265,7 @@ func (server *LoginServer) CharacterDelete(peer *cnet.CNPeer, pkt protocol.Packe }) } -func (server *LoginServer) ShardSelect(peer *cnet.CNPeer, pkt protocol.Packet) error { +func (server *LoginServer) ShardSelect(peer *cnet.Peer, pkt protocol.Packet) error { var selection protocol.SP_CL2LS_REQ_CHAR_SELECT pkt.Decode(&selection) @@ -321,7 +321,7 @@ func (server *LoginServer) ShardSelect(peer *cnet.CNPeer, pkt protocol.Packet) e return peer.Send(protocol.P_LS2CL_REP_SHARD_SELECT_SUCC, resp) } -func (server *LoginServer) FinishTutorial(peer *cnet.CNPeer, pkt protocol.Packet) error { +func (server *LoginServer) FinishTutorial(peer *cnet.Peer, pkt protocol.Packet) error { var charPkt protocol.SP_CL2LS_REQ_SAVE_CHAR_TUTOR pkt.Decode(&charPkt) diff --git a/shard/chat.go b/shard/chat.go index 0a98d9a..c10eccd 100644 --- a/shard/chat.go +++ b/shard/chat.go @@ -8,7 +8,7 @@ import ( "github.com/CPunch/gopenfusion/shard/entity" ) -func (server *ShardServer) freeChat(peer *cnet.CNPeer, pkt protocol.Packet) error { +func (server *ShardServer) freeChat(peer *cnet.Peer, pkt protocol.Packet) error { var chat protocol.SP_CL2FE_REQ_SEND_FREECHAT_MESSAGE pkt.Decode(&chat) @@ -25,7 +25,7 @@ func (server *ShardServer) freeChat(peer *cnet.CNPeer, pkt protocol.Packet) erro }) } -func (server *ShardServer) menuChat(peer *cnet.CNPeer, pkt protocol.Packet) error { +func (server *ShardServer) menuChat(peer *cnet.Peer, pkt protocol.Packet) error { var chat protocol.SP_CL2FE_REQ_SEND_MENUCHAT_MESSAGE pkt.Decode(&chat) @@ -42,7 +42,7 @@ func (server *ShardServer) menuChat(peer *cnet.CNPeer, pkt protocol.Packet) erro }) } -func (server *ShardServer) emoteChat(peer *cnet.CNPeer, pkt protocol.Packet) error { +func (server *ShardServer) emoteChat(peer *cnet.Peer, pkt protocol.Packet) error { var chat protocol.SP_CL2FE_REQ_PC_AVATAR_EMOTES_CHAT pkt.Decode(&chat) diff --git a/shard/entity/entity.go b/shard/entity/entity.go index cc84d38..232b3a4 100644 --- a/shard/entity/entity.go +++ b/shard/entity/entity.go @@ -20,6 +20,6 @@ type Entity interface { SetPosition(x, y, z int) SetAngle(angle int) - DisappearFromViewOf(peer *cnet.CNPeer) - EnterIntoViewOf(peer *cnet.CNPeer) + DisappearFromViewOf(peer *cnet.Peer) + EnterIntoViewOf(peer *cnet.Peer) } diff --git a/shard/entity/npc.go b/shard/entity/npc.go index 4793f8d..e61326f 100644 --- a/shard/entity/npc.go +++ b/shard/entity/npc.go @@ -63,13 +63,13 @@ func (npc *NPC) SetAngle(angle int) { npc.Angle = angle } -func (npc *NPC) DisappearFromViewOf(peer *cnet.CNPeer) { +func (npc *NPC) DisappearFromViewOf(peer *cnet.Peer) { peer.Send(protocol.P_FE2CL_NPC_EXIT, protocol.SP_FE2CL_NPC_EXIT{ INPC_ID: int32(npc.ID), }) } -func (npc *NPC) EnterIntoViewOf(peer *cnet.CNPeer) { +func (npc *NPC) EnterIntoViewOf(peer *cnet.Peer) { peer.Send(protocol.P_FE2CL_NPC_NEW, protocol.SP_FE2CL_NPC_NEW{ NPCAppearanceData: npc.GetAppearanceData(), }) diff --git a/shard/entity/player.go b/shard/entity/player.go index 8fd2178..0d10927 100644 --- a/shard/entity/player.go +++ b/shard/entity/player.go @@ -8,11 +8,11 @@ import ( type Player struct { db.Player - Peer *cnet.CNPeer + Peer *cnet.Peer Chunk ChunkPosition } -func NewPlayer(peer *cnet.CNPeer, player *db.Player) *Player { +func NewPlayer(peer *cnet.Peer, player *db.Player) *Player { return &Player{ Player: *player, Peer: peer, @@ -52,13 +52,13 @@ func (plr *Player) SetAngle(angle int) { plr.Angle = angle } -func (plr *Player) DisappearFromViewOf(peer *cnet.CNPeer) { +func (plr *Player) DisappearFromViewOf(peer *cnet.Peer) { peer.Send(protocol.P_FE2CL_PC_EXIT, protocol.SP_FE2CL_PC_EXIT{ IID: int32(plr.PlayerID), }) } -func (plr *Player) EnterIntoViewOf(peer *cnet.CNPeer) { +func (plr *Player) EnterIntoViewOf(peer *cnet.Peer) { peer.Send(protocol.P_FE2CL_PC_NEW, protocol.SP_FE2CL_PC_NEW{ PCAppearanceData: plr.GetAppearanceData(), }) diff --git a/shard/join.go b/shard/join.go index 49d1f39..c0aff6d 100644 --- a/shard/join.go +++ b/shard/join.go @@ -11,7 +11,7 @@ import ( "github.com/CPunch/gopenfusion/util" ) -func (server *ShardServer) attachPlayer(peer *cnet.CNPeer, meta redis.LoginMetadata) (*entity.Player, error) { +func (server *ShardServer) attachPlayer(peer *cnet.Peer, meta redis.LoginMetadata) (*entity.Player, error) { dbPlr, err := server.dbHndlr.GetPlayer(int(meta.PlayerID)) if err != nil { return nil, err @@ -26,7 +26,7 @@ func (server *ShardServer) attachPlayer(peer *cnet.CNPeer, meta redis.LoginMetad return plr, nil } -func (server *ShardServer) RequestEnter(peer *cnet.CNPeer, pkt protocol.Packet) error { +func (server *ShardServer) RequestEnter(peer *cnet.Peer, pkt protocol.Packet) error { var enter protocol.SP_CL2FE_REQ_PC_ENTER pkt.Decode(&enter) @@ -67,7 +67,7 @@ func (server *ShardServer) RequestEnter(peer *cnet.CNPeer, pkt protocol.Packet) return nil } -func (server *ShardServer) LoadingComplete(peer *cnet.CNPeer, pkt protocol.Packet) error { +func (server *ShardServer) LoadingComplete(peer *cnet.Peer, pkt protocol.Packet) error { var loadComplete protocol.SP_CL2FE_REQ_PC_LOADING_COMPLETE pkt.Decode(&loadComplete) diff --git a/shard/movement.go b/shard/movement.go index 2dce0ec..a8317c1 100644 --- a/shard/movement.go +++ b/shard/movement.go @@ -17,7 +17,7 @@ func (server *ShardServer) updatePlayerPosition(plr *entity.Player, X, Y, Z, Ang server.updateEntityChunk(plr, plr.GetChunkPos(), entity.MakeChunkPosition(X, Y)) } -func (server *ShardServer) playerMove(peer *cnet.CNPeer, pkt protocol.Packet) error { +func (server *ShardServer) playerMove(peer *cnet.Peer, pkt protocol.Packet) error { var move protocol.SP_CL2FE_REQ_PC_MOVE pkt.Decode(&move) @@ -45,7 +45,7 @@ func (server *ShardServer) playerMove(peer *cnet.CNPeer, pkt protocol.Packet) er }) } -func (server *ShardServer) playerStop(peer *cnet.CNPeer, pkt protocol.Packet) error { +func (server *ShardServer) playerStop(peer *cnet.Peer, pkt protocol.Packet) error { var stop protocol.SP_CL2FE_REQ_PC_STOP pkt.Decode(&stop) @@ -67,7 +67,7 @@ func (server *ShardServer) playerStop(peer *cnet.CNPeer, pkt protocol.Packet) er }) } -func (server *ShardServer) playerJump(peer *cnet.CNPeer, pkt protocol.Packet) error { +func (server *ShardServer) playerJump(peer *cnet.Peer, pkt protocol.Packet) error { var jump protocol.SP_CL2FE_REQ_PC_JUMP pkt.Decode(&jump) diff --git a/shard/shardserver.go b/shard/shardserver.go index 847d6c9..a9a123d 100644 --- a/shard/shardserver.go +++ b/shard/shardserver.go @@ -12,7 +12,7 @@ import ( "github.com/CPunch/gopenfusion/shard/entity" ) -type PacketHandler func(peer *cnet.CNPeer, pkt protocol.Packet) error +type PacketHandler func(peer *cnet.Peer, pkt protocol.Packet) error type ShardServer struct { service *service.Service @@ -56,7 +56,7 @@ func (server *ShardServer) Start() { server.service.Start() } -func (server *ShardServer) onDisconnect(peer *cnet.CNPeer) { +func (server *ShardServer) onDisconnect(peer *cnet.Peer) { // remove from chunks plr, ok := peer.UserData().(*entity.Player) if ok && plr != nil { @@ -64,6 +64,6 @@ func (server *ShardServer) onDisconnect(peer *cnet.CNPeer) { } } -func (server *ShardServer) onConnect(peer *cnet.CNPeer) { +func (server *ShardServer) onConnect(peer *cnet.Peer) { }