merged internal/service -> cnet/service

This commit is contained in:
CPunch 2023-12-01 19:22:49 -06:00
parent af867ccff2
commit b07e9ddbcb
4 changed files with 37 additions and 40 deletions

View File

@ -1,4 +1,4 @@
package service package cnet
import ( import (
"context" "context"
@ -10,14 +10,13 @@ import (
"strconv" "strconv"
"sync" "sync"
"github.com/CPunch/gopenfusion/cnet"
"github.com/CPunch/gopenfusion/config" "github.com/CPunch/gopenfusion/config"
"github.com/CPunch/gopenfusion/internal/protocol" "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 return nil
} }
@ -29,18 +28,18 @@ type Service struct {
started chan struct{} started chan struct{}
stopped chan struct{} stopped chan struct{}
packetHandlers map[uint32]PacketHandler packetHandlers map[uint32]PacketHandler
peers map[chan *cnet.PacketEvent]*cnet.Peer peers map[chan *PacketEvent]*Peer
stateLock sync.Mutex stateLock sync.Mutex
// OnDisconnect is called when a peer disconnects from the service. // OnDisconnect is called when a peer disconnects from the service.
// uData is the stored value of the key/value pair in the peer map. // 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) // 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. // OnConnect is called when a peer connects to the service.
// return value is used as the value in the peer map. // 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) // 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) { 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) { func (srvc *Service) Reset(ctx context.Context) {
srvc.ctx = ctx srvc.ctx = ctx
srvc.packetHandlers = make(map[uint32]PacketHandler) 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.started = make(chan struct{})
srvc.stopped = make(chan struct{}) srvc.stopped = make(chan struct{})
} }
@ -81,8 +80,8 @@ func (srvc *Service) AddPacketHandler(pktID uint32, handler PacketHandler) {
} }
type newPeerConnection struct { type newPeerConnection struct {
peer *cnet.Peer peer *Peer
channel chan *cnet.PacketEvent channel chan *PacketEvent
} }
func (srvc *Service) Start() error { 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 // create a new peer and pass it to the event loop
peer := cnet.NewPeer(srvc.ctx, conn) peer := NewPeer(srvc.ctx, conn)
eRecv := make(chan *cnet.PacketEvent) eRecv := make(chan *PacketEvent)
peerConnections <- newPeerConnection{channel: eRecv, peer: peer} peerConnections <- newPeerConnection{channel: eRecv, peer: peer}
go peer.Handler(eRecv) 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] 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 srvc.peers[channel] = peer
} }
func (srvc *Service) removePeer(channel chan *cnet.PacketEvent) { func (srvc *Service) removePeer(channel chan *PacketEvent) {
delete(srvc.peers, channel) delete(srvc.peers, channel)
} }
@ -148,7 +147,7 @@ func (srvc *Service) Stopped() <-chan struct{} {
// if f returns false, the iteration is stopped. // if f returns false, the iteration is stopped.
// NOTE: the peer map is not locked while iterating, if you're calling this // 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. // 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 { for _, peer := range srvc.peers {
if !f(peer) { if !f(peer) {
break break
@ -168,7 +167,7 @@ func (srvc *Service) Unlock() {
func (srvc *Service) stop() { func (srvc *Service) stop() {
// OnDisconnect handler might need to do something important // OnDisconnect handler might need to do something important
srvc.RangePeers(func(peer *cnet.Peer) bool { srvc.RangePeers(func(peer *Peer) bool {
peer.Kill() peer.Kill()
if srvc.OnDisconnect != nil { if srvc.OnDisconnect != nil {
srvc.OnDisconnect(peer) srvc.OnDisconnect(peer)
@ -197,7 +196,7 @@ func (srvc *Service) handleEvents(peerPipe <-chan newPeerConnection) {
Chan: reflect.ValueOf(peerPipe), Chan: reflect.ValueOf(peerPipe),
}) })
addPoll := func(channel chan *cnet.PacketEvent) { addPoll := func(channel chan *PacketEvent) {
poll = append(poll, reflect.SelectCase{ poll = append(poll, reflect.SelectCase{
Dir: reflect.SelectRecv, Dir: reflect.SelectRecv,
Chan: reflect.ValueOf(channel), Chan: reflect.ValueOf(channel),
@ -222,7 +221,7 @@ func (srvc *Service) handleEvents(peerPipe <-chan newPeerConnection) {
addPoll(evnt.channel) addPoll(evnt.channel)
srvc.connect(evnt.channel, evnt.peer) srvc.connect(evnt.channel, evnt.peer)
default: // peer event default: // peer event
channel := poll[chosen].Chan.Interface().(chan *cnet.PacketEvent) channel := poll[chosen].Chan.Interface().(chan *PacketEvent)
peer := srvc.getPeer(channel) peer := srvc.getPeer(channel)
if peer == nil { if peer == nil {
log.Printf("Unknown peer event: %v", value) log.Printf("Unknown peer event: %v", value)
@ -230,7 +229,7 @@ func (srvc *Service) handleEvents(peerPipe <-chan newPeerConnection) {
continue continue
} }
evnt, ok := value.Interface().(*cnet.PacketEvent) evnt, ok := value.Interface().(*PacketEvent)
if !recvOK || !ok || evnt == nil { if !recvOK || !ok || evnt == nil {
// peer disconnected, remove it from our poll queue // peer disconnected, remove it from our poll queue
removePoll(chosen) 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 { if hndlr, ok := srvc.packetHandlers[typeID]; ok {
// fmt.Printf("Handling packet %x\n", typeID) // fmt.Printf("Handling packet %x\n", typeID)
if err := hndlr(peer, pkt); err != nil { 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 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) log.Printf("Peer %p disconnected from %s\n", peer, srvc.Name)
if srvc.OnDisconnect != nil { if srvc.OnDisconnect != nil {
srvc.OnDisconnect(peer) srvc.OnDisconnect(peer)
@ -273,7 +272,7 @@ func (srvc *Service) disconnect(channel chan *cnet.PacketEvent, peer *cnet.Peer)
srvc.removePeer(channel) 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) log.Printf("New peer %p connected to %s\n", peer, srvc.Name)
if srvc.OnConnect != nil { if srvc.OnConnect != nil {
srvc.OnConnect(peer) srvc.OnConnect(peer)

View File

@ -1,4 +1,4 @@
package service_test package cnet_test
import ( import (
"context" "context"
@ -12,7 +12,6 @@ import (
"github.com/CPunch/gopenfusion/cnet" "github.com/CPunch/gopenfusion/cnet"
"github.com/CPunch/gopenfusion/internal/protocol" "github.com/CPunch/gopenfusion/internal/protocol"
"github.com/CPunch/gopenfusion/internal/service"
"github.com/matryer/is" "github.com/matryer/is"
) )
@ -46,7 +45,7 @@ func waitWithTimeout(wg *sync.WaitGroup, seconds int) bool {
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
var err error var err error
srvcPort, err = service.RandomPort() srvcPort, err = cnet.RandomPort()
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -57,7 +56,7 @@ func TestMain(m *testing.M) {
func TestService(t *testing.T) { func TestService(t *testing.T) {
is := is.New(t) is := is.New(t)
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
srvc := service.NewService(ctx, "TEST", srvcPort) srvc := cnet.NewService(ctx, "TEST", srvcPort)
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
// shutdown service when test is done // shutdown service when test is done

View File

@ -3,20 +3,20 @@ package login
import ( import (
"context" "context"
"github.com/CPunch/gopenfusion/cnet"
"github.com/CPunch/gopenfusion/internal/db" "github.com/CPunch/gopenfusion/internal/db"
"github.com/CPunch/gopenfusion/internal/protocol" "github.com/CPunch/gopenfusion/internal/protocol"
"github.com/CPunch/gopenfusion/internal/redis" "github.com/CPunch/gopenfusion/internal/redis"
"github.com/CPunch/gopenfusion/internal/service"
) )
type LoginServer struct { type LoginServer struct {
service *service.Service service *cnet.Service
dbHndlr *db.DBHandler dbHndlr *db.DBHandler
redisHndlr *redis.RedisHandler redisHndlr *redis.RedisHandler
} }
func NewLoginServer(ctx context.Context, dbHndlr *db.DBHandler, redisHndlr *redis.RedisHandler, port int) (*LoginServer, error) { 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{ server := &LoginServer{
service: srvc, 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_CREATE, server.CharacterCreate)
srvc.AddPacketHandler(protocol.P_CL2LS_REQ_CHAR_SELECT, server.ShardSelect) 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_CHAR_DELETE, server.CharacterDelete)
srvc.AddPacketHandler(protocol.P_CL2LS_REQ_SHARD_SELECT, service.StubbedPacket) srvc.AddPacketHandler(protocol.P_CL2LS_REQ_SHARD_SELECT, cnet.StubbedPacket)
srvc.AddPacketHandler(protocol.P_CL2LS_REQ_SHARD_LIST_INFO, service.StubbedPacket) srvc.AddPacketHandler(protocol.P_CL2LS_REQ_SHARD_LIST_INFO, cnet.StubbedPacket)
srvc.AddPacketHandler(protocol.P_CL2LS_CHECK_NAME_LIST, service.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_SAVE_CHAR_TUTOR, server.FinishTutorial)
srvc.AddPacketHandler(protocol.P_CL2LS_REQ_PC_EXIT_DUPLICATE, service.StubbedPacket) srvc.AddPacketHandler(protocol.P_CL2LS_REQ_PC_EXIT_DUPLICATE, cnet.StubbedPacket)
srvc.AddPacketHandler(protocol.P_CL2LS_REP_LIVE_CHECK, service.StubbedPacket) srvc.AddPacketHandler(protocol.P_CL2LS_REP_LIVE_CHECK, cnet.StubbedPacket)
srvc.AddPacketHandler(protocol.P_CL2LS_REQ_CHANGE_CHAR_NAME, service.StubbedPacket) srvc.AddPacketHandler(protocol.P_CL2LS_REQ_CHANGE_CHAR_NAME, cnet.StubbedPacket)
srvc.AddPacketHandler(protocol.P_CL2LS_REQ_SERVER_SELECT, service.StubbedPacket) srvc.AddPacketHandler(protocol.P_CL2LS_REQ_SERVER_SELECT, cnet.StubbedPacket)
return server, nil return server, nil
} }

View File

@ -8,21 +8,20 @@ import (
"github.com/CPunch/gopenfusion/internal/db" "github.com/CPunch/gopenfusion/internal/db"
"github.com/CPunch/gopenfusion/internal/protocol" "github.com/CPunch/gopenfusion/internal/protocol"
"github.com/CPunch/gopenfusion/internal/redis" "github.com/CPunch/gopenfusion/internal/redis"
"github.com/CPunch/gopenfusion/internal/service"
"github.com/CPunch/gopenfusion/shard/entity" "github.com/CPunch/gopenfusion/shard/entity"
) )
type PacketHandler func(peer *cnet.Peer, pkt protocol.Packet) error type PacketHandler func(peer *cnet.Peer, pkt protocol.Packet) error
type ShardServer struct { type ShardServer struct {
service *service.Service service *cnet.Service
dbHndlr *db.DBHandler dbHndlr *db.DBHandler
redisHndlr *redis.RedisHandler redisHndlr *redis.RedisHandler
chunks map[entity.ChunkPosition]*entity.Chunk chunks map[entity.ChunkPosition]*entity.Chunk
} }
func NewShardServer(ctx context.Context, dbHndlr *db.DBHandler, redisHndlr *redis.RedisHandler, port int) (*ShardServer, error) { 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{ server := &ShardServer{
service: srvc, service: srvc,