diff --git a/internal/service/service.go b/cnet/service.go similarity index 83% rename from internal/service/service.go rename to cnet/service.go index 225b331..fad569c 100644 --- a/internal/service/service.go +++ b/cnet/service.go @@ -1,4 +1,4 @@ -package service +package cnet import ( "context" @@ -10,14 +10,13 @@ import ( "strconv" "sync" - "github.com/CPunch/gopenfusion/cnet" "github.com/CPunch/gopenfusion/config" "github.com/CPunch/gopenfusion/internal/protocol" ) -type PacketHandler func(peer *cnet.Peer, pkt protocol.Packet) error +type PacketHandler func(peer *Peer, pkt protocol.Packet) error -func StubbedPacket(_ *cnet.Peer, _ protocol.Packet) error { +func StubbedPacket(_ *Peer, _ protocol.Packet) error { return nil } @@ -29,18 +28,18 @@ type Service struct { started chan struct{} stopped chan struct{} packetHandlers map[uint32]PacketHandler - peers map[chan *cnet.PacketEvent]*cnet.Peer + peers map[chan *PacketEvent]*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.Peer) + OnDisconnect func(peer *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.Peer) + OnConnect func(peer *Peer) } func RandomPort() (int, error) { @@ -70,7 +69,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.Peer) + srvc.peers = make(map[chan *PacketEvent]*Peer) srvc.started = make(chan struct{}) srvc.stopped = make(chan struct{}) } @@ -81,8 +80,8 @@ func (srvc *Service) AddPacketHandler(pktID uint32, handler PacketHandler) { } type newPeerConnection struct { - peer *cnet.Peer - channel chan *cnet.PacketEvent + peer *Peer + channel chan *PacketEvent } func (srvc *Service) Start() error { @@ -113,22 +112,22 @@ func (srvc *Service) Start() error { } // create a new peer and pass it to the event loop - peer := cnet.NewPeer(srvc.ctx, conn) - eRecv := make(chan *cnet.PacketEvent) + peer := NewPeer(srvc.ctx, conn) + eRecv := make(chan *PacketEvent) peerConnections <- newPeerConnection{channel: eRecv, peer: peer} go peer.Handler(eRecv) } } -func (srvc *Service) getPeer(channel chan *cnet.PacketEvent) *cnet.Peer { +func (srvc *Service) getPeer(channel chan *PacketEvent) *Peer { return srvc.peers[channel] } -func (srvc *Service) setPeer(channel chan *cnet.PacketEvent, peer *cnet.Peer) { +func (srvc *Service) setPeer(channel chan *PacketEvent, peer *Peer) { srvc.peers[channel] = peer } -func (srvc *Service) removePeer(channel chan *cnet.PacketEvent) { +func (srvc *Service) removePeer(channel chan *PacketEvent) { delete(srvc.peers, channel) } @@ -148,7 +147,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.Peer) bool) { +func (srvc *Service) RangePeers(f func(peer *Peer) bool) { for _, peer := range srvc.peers { if !f(peer) { break @@ -168,7 +167,7 @@ func (srvc *Service) Unlock() { func (srvc *Service) stop() { // OnDisconnect handler might need to do something important - srvc.RangePeers(func(peer *cnet.Peer) bool { + srvc.RangePeers(func(peer *Peer) bool { peer.Kill() if srvc.OnDisconnect != nil { srvc.OnDisconnect(peer) @@ -197,7 +196,7 @@ func (srvc *Service) handleEvents(peerPipe <-chan newPeerConnection) { Chan: reflect.ValueOf(peerPipe), }) - addPoll := func(channel chan *cnet.PacketEvent) { + addPoll := func(channel chan *PacketEvent) { poll = append(poll, reflect.SelectCase{ Dir: reflect.SelectRecv, Chan: reflect.ValueOf(channel), @@ -222,7 +221,7 @@ func (srvc *Service) handleEvents(peerPipe <-chan newPeerConnection) { addPoll(evnt.channel) srvc.connect(evnt.channel, evnt.peer) default: // peer event - channel := poll[chosen].Chan.Interface().(chan *cnet.PacketEvent) + channel := poll[chosen].Chan.Interface().(chan *PacketEvent) peer := srvc.getPeer(channel) if peer == nil { log.Printf("Unknown peer event: %v", value) @@ -230,7 +229,7 @@ func (srvc *Service) handleEvents(peerPipe <-chan newPeerConnection) { continue } - evnt, ok := value.Interface().(*cnet.PacketEvent) + evnt, ok := value.Interface().(*PacketEvent) if !recvOK || !ok || evnt == nil { // peer disconnected, remove it from our poll queue removePoll(chosen) @@ -251,7 +250,7 @@ func (srvc *Service) handleEvents(peerPipe <-chan newPeerConnection) { } } -func (srvc *Service) handlePacket(peer *cnet.Peer, typeID uint32, pkt protocol.Packet) error { +func (srvc *Service) handlePacket(peer *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 +263,7 @@ func (srvc *Service) handlePacket(peer *cnet.Peer, typeID uint32, pkt protocol.P return nil } -func (srvc *Service) disconnect(channel chan *cnet.PacketEvent, peer *cnet.Peer) { +func (srvc *Service) disconnect(channel chan *PacketEvent, peer *Peer) { log.Printf("Peer %p disconnected from %s\n", peer, srvc.Name) if srvc.OnDisconnect != nil { srvc.OnDisconnect(peer) @@ -273,7 +272,7 @@ func (srvc *Service) disconnect(channel chan *cnet.PacketEvent, peer *cnet.Peer) srvc.removePeer(channel) } -func (srvc *Service) connect(channel chan *cnet.PacketEvent, peer *cnet.Peer) { +func (srvc *Service) connect(channel chan *PacketEvent, peer *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/cnet/service_test.go similarity index 93% rename from internal/service/service_test.go rename to cnet/service_test.go index 7844653..896a307 100644 --- a/internal/service/service_test.go +++ b/cnet/service_test.go @@ -1,4 +1,4 @@ -package service_test +package cnet_test import ( "context" @@ -12,7 +12,6 @@ import ( "github.com/CPunch/gopenfusion/cnet" "github.com/CPunch/gopenfusion/internal/protocol" - "github.com/CPunch/gopenfusion/internal/service" "github.com/matryer/is" ) @@ -46,7 +45,7 @@ func waitWithTimeout(wg *sync.WaitGroup, seconds int) bool { func TestMain(m *testing.M) { var err error - srvcPort, err = service.RandomPort() + srvcPort, err = cnet.RandomPort() if err != nil { panic(err) } @@ -57,7 +56,7 @@ func TestMain(m *testing.M) { func TestService(t *testing.T) { is := is.New(t) ctx, cancel := context.WithCancel(context.Background()) - srvc := service.NewService(ctx, "TEST", srvcPort) + srvc := cnet.NewService(ctx, "TEST", srvcPort) wg := sync.WaitGroup{} // shutdown service when test is done diff --git a/login/loginserver.go b/login/loginserver.go index 77a59e8..aae6304 100644 --- a/login/loginserver.go +++ b/login/loginserver.go @@ -3,20 +3,20 @@ package login import ( "context" + "github.com/CPunch/gopenfusion/cnet" "github.com/CPunch/gopenfusion/internal/db" "github.com/CPunch/gopenfusion/internal/protocol" "github.com/CPunch/gopenfusion/internal/redis" - "github.com/CPunch/gopenfusion/internal/service" ) type LoginServer struct { - service *service.Service + service *cnet.Service dbHndlr *db.DBHandler redisHndlr *redis.RedisHandler } func NewLoginServer(ctx context.Context, dbHndlr *db.DBHandler, redisHndlr *redis.RedisHandler, port int) (*LoginServer, error) { - srvc := service.NewService(ctx, "LOGIN", port) + srvc := cnet.NewService(ctx, "LOGIN", port) server := &LoginServer{ service: srvc, @@ -30,14 +30,14 @@ func NewLoginServer(ctx context.Context, dbHndlr *db.DBHandler, redisHndlr *redi srvc.AddPacketHandler(protocol.P_CL2LS_REQ_CHAR_CREATE, server.CharacterCreate) srvc.AddPacketHandler(protocol.P_CL2LS_REQ_CHAR_SELECT, server.ShardSelect) srvc.AddPacketHandler(protocol.P_CL2LS_REQ_CHAR_DELETE, server.CharacterDelete) - srvc.AddPacketHandler(protocol.P_CL2LS_REQ_SHARD_SELECT, service.StubbedPacket) - srvc.AddPacketHandler(protocol.P_CL2LS_REQ_SHARD_LIST_INFO, service.StubbedPacket) - srvc.AddPacketHandler(protocol.P_CL2LS_CHECK_NAME_LIST, service.StubbedPacket) + srvc.AddPacketHandler(protocol.P_CL2LS_REQ_SHARD_SELECT, cnet.StubbedPacket) + srvc.AddPacketHandler(protocol.P_CL2LS_REQ_SHARD_LIST_INFO, cnet.StubbedPacket) + srvc.AddPacketHandler(protocol.P_CL2LS_CHECK_NAME_LIST, cnet.StubbedPacket) srvc.AddPacketHandler(protocol.P_CL2LS_REQ_SAVE_CHAR_TUTOR, server.FinishTutorial) - srvc.AddPacketHandler(protocol.P_CL2LS_REQ_PC_EXIT_DUPLICATE, service.StubbedPacket) - srvc.AddPacketHandler(protocol.P_CL2LS_REP_LIVE_CHECK, service.StubbedPacket) - srvc.AddPacketHandler(protocol.P_CL2LS_REQ_CHANGE_CHAR_NAME, service.StubbedPacket) - srvc.AddPacketHandler(protocol.P_CL2LS_REQ_SERVER_SELECT, service.StubbedPacket) + srvc.AddPacketHandler(protocol.P_CL2LS_REQ_PC_EXIT_DUPLICATE, cnet.StubbedPacket) + srvc.AddPacketHandler(protocol.P_CL2LS_REP_LIVE_CHECK, cnet.StubbedPacket) + srvc.AddPacketHandler(protocol.P_CL2LS_REQ_CHANGE_CHAR_NAME, cnet.StubbedPacket) + srvc.AddPacketHandler(protocol.P_CL2LS_REQ_SERVER_SELECT, cnet.StubbedPacket) return server, nil } diff --git a/shard/shardserver.go b/shard/shardserver.go index a9a123d..aa71b59 100644 --- a/shard/shardserver.go +++ b/shard/shardserver.go @@ -8,21 +8,20 @@ import ( "github.com/CPunch/gopenfusion/internal/db" "github.com/CPunch/gopenfusion/internal/protocol" "github.com/CPunch/gopenfusion/internal/redis" - "github.com/CPunch/gopenfusion/internal/service" "github.com/CPunch/gopenfusion/shard/entity" ) type PacketHandler func(peer *cnet.Peer, pkt protocol.Packet) error type ShardServer struct { - service *service.Service + service *cnet.Service dbHndlr *db.DBHandler redisHndlr *redis.RedisHandler chunks map[entity.ChunkPosition]*entity.Chunk } func NewShardServer(ctx context.Context, dbHndlr *db.DBHandler, redisHndlr *redis.RedisHandler, port int) (*ShardServer, error) { - srvc := service.NewService(ctx, "SHARD", port) + srvc := cnet.NewService(ctx, "SHARD", port) server := &ShardServer{ service: srvc,