feat 错误包装

This commit is contained in:
2026-01-16 22:13:21 +08:00
parent 82d863d4a5
commit 3ee6527eba
3 changed files with 23 additions and 16 deletions

View File

@@ -5,23 +5,17 @@ import (
"git.hlsq.asia/mmorpg/service-common/log" "git.hlsq.asia/mmorpg/service-common/log"
"git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb" "git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb"
"git.hlsq.asia/mmorpg/service-common/proto/ss/ss_pb" "git.hlsq.asia/mmorpg/service-common/proto/ss/ss_pb"
instance2 "git.hlsq.asia/mmorpg/service-scene/internal/instance" "git.hlsq.asia/mmorpg/service-scene/internal/instance"
"google.golang.org/protobuf/proto" "google.golang.org/protobuf/proto"
"sync" "sync"
) )
func (s *Server) Enter(ctx context.Context, req *grpc_pb.EnterReq) (*grpc_pb.EnterResp, error) { func (s *Server) Enter(ctx context.Context, req *grpc_pb.EnterReq) (*grpc_pb.EnterResp, error) {
var i *instance2.Instance ins, loaded := instance.Mgr.LoadOrStoreByInstanceID(req.InstanceID, instance.NewScene(s.SID, req.InstanceID))
if len(instance2.Mgr.GetAll()) == 0 { if !loaded {
i = instance2.NewScene(s.SID, req.InstanceID) ins.Start(s.EtcdTTL)
i.Start(s.EtcdTTL)
} else {
for _, v := range instance2.Mgr.GetAll() {
i = v
break
} }
} ins.EventIn <- req
i.EventIn <- req
payload, _ := proto.Marshal(&ss_pb.S2C_EnterInstance{ payload, _ := proto.Marshal(&ss_pb.S2C_EnterInstance{
Info: &ss_pb.PositionInfo{ Info: &ss_pb.PositionInfo{
@@ -32,14 +26,14 @@ func (s *Server) Enter(ctx context.Context, req *grpc_pb.EnterReq) (*grpc_pb.Ent
}) })
return &grpc_pb.EnterResp{ return &grpc_pb.EnterResp{
SceneSID: s.SID, SceneSID: s.SID,
UniqueNo: i.UniqueNo, UniqueNo: ins.UniqueNo,
MessageID: int32(ss_pb.MessageID_MESSAGE_ID_ENTER_INSTANCE), MessageID: int32(ss_pb.MessageID_MESSAGE_ID_ENTER_INSTANCE),
Payload: payload, Payload: payload,
}, nil }, nil
} }
func (s *Server) Leave(ctx context.Context, req *grpc_pb.LeaveReq) (*grpc_pb.LeaveResp, error) { func (s *Server) Leave(ctx context.Context, req *grpc_pb.LeaveReq) (*grpc_pb.LeaveResp, error) {
if i := instance2.Mgr.GetByUniqueNo(req.UniqueNo); i != nil { if i := instance.Mgr.GetByUniqueNo(req.UniqueNo); i != nil {
i.EventIn <- req i.EventIn <- req
} }
return &grpc_pb.LeaveResp{}, nil return &grpc_pb.LeaveResp{}, nil
@@ -59,7 +53,7 @@ func (s *Server) Action(server grpc_pb.Scene_ActionServer) error {
if args, err := server.Recv(); err != nil { if args, err := server.Recv(); err != nil {
return return
} else { } else {
if ins := instance2.Mgr.GetByUniqueNo(args.UniqueNo); ins != nil { if ins := instance.Mgr.GetByUniqueNo(args.UniqueNo); ins != nil {
select { select {
case ins.EventIn <- args: case ins.EventIn <- args:
default: default:

View File

@@ -16,6 +16,7 @@ func NewServer(ttl int64) *Server {
s := &Server{ s := &Server{
Base: service.Base{ Base: service.Base{
Target: common.KeyDiscoverScene, Target: common.KeyDiscoverScene,
ServiceName: common.KeyDiscoverServiceNameScene,
EtcdTTL: ttl, EtcdTTL: ttl,
}, },
} }

View File

@@ -40,3 +40,15 @@ func (m *insManager) GetByUniqueNo(uniqueNo string) *Instance {
defer m.RUnlock() defer m.RUnlock()
return m.insMap[uniqueNo] return m.insMap[uniqueNo]
} }
func (m *insManager) LoadOrStoreByInstanceID(instanceID int32, ins *Instance) (*Instance, bool) {
m.Lock()
defer m.Unlock()
for _, instance := range m.insMap {
if instance.InstanceID == instanceID {
return instance, true
}
}
m.insMap[ins.UniqueNo] = ins
return ins, false
}