69 lines
1.6 KiB
Go
69 lines
1.6 KiB
Go
package grpcserver
|
|
|
|
import (
|
|
"context"
|
|
"git.hlsq.asia/alex/service-common/log"
|
|
"git.hlsq.asia/alex/service-common/proto/rs/rspb"
|
|
"git.hlsq.asia/alex/service-common/proto/ss/sspb"
|
|
"git.hlsq.asia/alex/service-scene/internal/instance"
|
|
"google.golang.org/protobuf/proto"
|
|
"sync"
|
|
)
|
|
|
|
func (s *Server) Enter(ctx context.Context, req *rspb.EnterReq) (*rspb.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(&sspb.S2C_EnterInstance{
|
|
Info: &sspb.PositionInfo{
|
|
USN: req.USN,
|
|
X: 1,
|
|
Y: 1,
|
|
},
|
|
})
|
|
return &rspb.EnterResp{
|
|
SceneSID: s.SID,
|
|
UniqueNo: ins.UniqueNo,
|
|
MessageID: int32(sspb.MessageID_MESSAGE_ID_ENTER_INSTANCE),
|
|
Payload: payload,
|
|
}, nil
|
|
}
|
|
|
|
func (s *Server) Leave(ctx context.Context, req *rspb.LeaveReq) (*rspb.LeaveResp, error) {
|
|
if i := instance.Mgr.GetByUniqueNo(req.UniqueNo); i != nil {
|
|
i.EventIn <- req
|
|
}
|
|
return &rspb.LeaveResp{}, nil
|
|
}
|
|
|
|
func (s *Server) Action(server rspb.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(&rspb.ActionResp{})
|
|
}
|