feat 历史记录
This commit is contained in:
74
internal/grpc_server/server.go
Normal file
74
internal/grpc_server/server.go
Normal file
@@ -0,0 +1,74 @@
|
||||
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"
|
||||
instance2 "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
|
||||
}
|
||||
}
|
||||
i.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: i.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 {
|
||||
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 := instance2.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{})
|
||||
}
|
||||
Reference in New Issue
Block a user