feat 错误包装
This commit is contained in:
@@ -5,23 +5,17 @@ import (
|
||||
"git.hlsq.asia/mmorpg/service-common/log"
|
||||
"git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_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"
|
||||
"sync"
|
||||
)
|
||||
|
||||
func (s *Server) Enter(ctx context.Context, req *grpc_pb.EnterReq) (*grpc_pb.EnterResp, error) {
|
||||
var i *instance2.Instance
|
||||
if len(instance2.Mgr.GetAll()) == 0 {
|
||||
i = instance2.NewScene(s.SID, req.InstanceID)
|
||||
i.Start(s.EtcdTTL)
|
||||
} else {
|
||||
for _, v := range instance2.Mgr.GetAll() {
|
||||
i = v
|
||||
break
|
||||
}
|
||||
ins, loaded := instance.Mgr.LoadOrStoreByInstanceID(req.InstanceID, instance.NewScene(s.SID, req.InstanceID))
|
||||
if !loaded {
|
||||
ins.Start(s.EtcdTTL)
|
||||
}
|
||||
i.EventIn <- req
|
||||
ins.EventIn <- req
|
||||
|
||||
payload, _ := proto.Marshal(&ss_pb.S2C_EnterInstance{
|
||||
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{
|
||||
SceneSID: s.SID,
|
||||
UniqueNo: i.UniqueNo,
|
||||
UniqueNo: ins.UniqueNo,
|
||||
MessageID: int32(ss_pb.MessageID_MESSAGE_ID_ENTER_INSTANCE),
|
||||
Payload: payload,
|
||||
}, nil
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
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 {
|
||||
return
|
||||
} else {
|
||||
if ins := instance2.Mgr.GetByUniqueNo(args.UniqueNo); ins != nil {
|
||||
if ins := instance.Mgr.GetByUniqueNo(args.UniqueNo); ins != nil {
|
||||
select {
|
||||
case ins.EventIn <- args:
|
||||
default:
|
||||
|
||||
@@ -15,8 +15,9 @@ type Server struct {
|
||||
func NewServer(ttl int64) *Server {
|
||||
s := &Server{
|
||||
Base: service.Base{
|
||||
Target: common.KeyDiscoverScene,
|
||||
EtcdTTL: ttl,
|
||||
Target: common.KeyDiscoverScene,
|
||||
ServiceName: common.KeyDiscoverServiceNameScene,
|
||||
EtcdTTL: ttl,
|
||||
},
|
||||
}
|
||||
s.Base.OnInit = s.OnInit
|
||||
|
||||
@@ -40,3 +40,15 @@ func (m *insManager) GetByUniqueNo(uniqueNo string) *Instance {
|
||||
defer m.RUnlock()
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user