69 lines
1.7 KiB
Go
69 lines
1.7 KiB
Go
package grpc_server
|
|
|
|
import (
|
|
"context"
|
|
"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"
|
|
"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) {
|
|
ins, loaded := instance.Mgr.LoadOrStoreByInstanceID(req.InstanceID, instance.NewScene(s.SID, req.InstanceID))
|
|
if !loaded {
|
|
ins.Start(s.Cfg.TTL)
|
|
}
|
|
ins.EventIn <- req
|
|
|
|
payload, _ := proto.Marshal(&ss_pb.S2C_EnterInstance{
|
|
Info: &ss_pb.PositionInfo{
|
|
USN: req.USN,
|
|
X: 1,
|
|
Y: 1,
|
|
},
|
|
})
|
|
return &grpc_pb.EnterResp{
|
|
SceneSID: s.SID,
|
|
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 := instance.Mgr.GetByUniqueNo(req.UniqueNo); i != nil {
|
|
i.EventIn <- req
|
|
}
|
|
return &grpc_pb.LeaveResp{}, nil
|
|
}
|
|
|
|
func (s *Server) Action(server grpc_pb.Scene_ActionServer) error {
|
|
wg := &sync.WaitGroup{}
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
defer func() {
|
|
if err := recover(); err != nil {
|
|
log.Errorf("Action panic: %v", err)
|
|
}
|
|
}()
|
|
for {
|
|
if args, err := server.Recv(); err != nil {
|
|
return
|
|
} else {
|
|
if ins := instance.Mgr.GetByUniqueNo(args.UniqueNo); ins != nil {
|
|
select {
|
|
case ins.EventIn <- args:
|
|
default:
|
|
log.Warnf("instance event in full: %v, %v", ins.InstanceID, ins.UniqueNo)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}()
|
|
wg.Wait()
|
|
return server.SendAndClose(&grpc_pb.ActionResp{})
|
|
}
|