gopenfusion/shard/shardserver.go
CPunch c0ba365cf5 CNPeer/Service refactor
- each CNPeer is given a unique chan *protocol.Event to pass events to
the service.handleEvents() loop. this is now passed to CNPeer.Handler()
as opposed to NewCNPeer().
- service has basically been rewritten. handleEvents() main loop uses
reflect.SelectCase() now to handle all of the eRecv channels for each
peer
- new protocol Event type: EVENT_CLIENT_CONNECT
- Added service_test.go; blackbox-styled testing like the others.
TestService() starts a service and spins up  a bunch of dummy peers
and verifies that each packet sent causes the corresponding packet
handler to be called.
2023-11-29 19:57:45 -06:00

66 lines
2.0 KiB
Go

package shard
import (
"github.com/CPunch/gopenfusion/config"
"github.com/CPunch/gopenfusion/internal/db"
"github.com/CPunch/gopenfusion/internal/entity"
"github.com/CPunch/gopenfusion/internal/protocol"
"github.com/CPunch/gopenfusion/internal/redis"
"github.com/CPunch/gopenfusion/internal/service"
)
type PacketHandler func(peer *protocol.CNPeer, pkt protocol.Packet) error
type ShardServer struct {
service *service.Service
dbHndlr *db.DBHandler
redisHndlr *redis.RedisHandler
chunks map[entity.ChunkPosition]*entity.Chunk
}
func NewShardServer(dbHndlr *db.DBHandler, redisHndlr *redis.RedisHandler, port int) (*ShardServer, error) {
srvc := service.NewService("SHARD", port)
server := &ShardServer{
service: srvc,
dbHndlr: dbHndlr,
redisHndlr: redisHndlr,
chunks: make(map[entity.ChunkPosition]*entity.Chunk),
}
srvc.AddPacketHandler(protocol.P_CL2FE_REQ_PC_ENTER, server.RequestEnter)
srvc.AddPacketHandler(protocol.P_CL2FE_REQ_PC_LOADING_COMPLETE, server.LoadingComplete)
srvc.AddPacketHandler(protocol.P_CL2FE_REQ_PC_MOVE, server.playerMove)
srvc.AddPacketHandler(protocol.P_CL2FE_REQ_PC_STOP, server.playerStop)
srvc.AddPacketHandler(protocol.P_CL2FE_REQ_PC_JUMP, server.playerJump)
srvc.AddPacketHandler(protocol.P_CL2FE_REQ_SEND_FREECHAT_MESSAGE, server.freeChat)
srvc.AddPacketHandler(protocol.P_CL2FE_REQ_SEND_MENUCHAT_MESSAGE, server.menuChat)
srvc.AddPacketHandler(protocol.P_CL2FE_REQ_PC_AVATAR_EMOTES_CHAT, server.emoteChat)
srvc.OnConnect = server.onConnect
srvc.OnDisconnect = server.onDisconnect
redisHndlr.RegisterShard(redis.ShardMetadata{
IP: config.GetAnnounceIP(),
Port: port,
})
return server, nil
}
func (server *ShardServer) Start() {
server.LoadNPCs()
server.service.Start()
}
func (server *ShardServer) onDisconnect(peer *protocol.CNPeer, _plr interface{}) {
// remove from chunks
if _plr != nil {
server.removeEntity(_plr.(*entity.Player))
}
}
func (server *ShardServer) onConnect(peer *protocol.CNPeer) interface{} {
return nil
}