feat 包命名规范
This commit is contained in:
68
internal/grpcserver/server.go
Normal file
68
internal/grpcserver/server.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package grpcserver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.hlsq.asia/mmorpg/service-common/log"
|
||||
"git.hlsq.asia/mmorpg/service-common/proto/rs/rspb"
|
||||
"git.hlsq.asia/mmorpg/service-common/proto/ss/sspb"
|
||||
"git.hlsq.asia/mmorpg/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{})
|
||||
}
|
||||
39
internal/grpcserver/server_init.go
Normal file
39
internal/grpcserver/server_init.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package grpcserver
|
||||
|
||||
import (
|
||||
"git.hlsq.asia/mmorpg/service-common/config"
|
||||
"git.hlsq.asia/mmorpg/service-common/discover/common"
|
||||
"git.hlsq.asia/mmorpg/service-common/net/grpc/service"
|
||||
"git.hlsq.asia/mmorpg/service-common/proto/rs/rspb"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
rspb.UnimplementedSceneServer
|
||||
service.Base
|
||||
}
|
||||
|
||||
func NewServer(cfg *config.GrpcConfig) *Server {
|
||||
s := &Server{
|
||||
Base: service.Base{
|
||||
Target: common.KeyDiscoverScene,
|
||||
ServiceName: common.KeyDiscoverServiceNameScene,
|
||||
Cfg: cfg,
|
||||
},
|
||||
}
|
||||
s.Base.OnCustomGrpcServerOption = s.OnCustomGrpcServerOption
|
||||
s.Base.OnInit = s.OnInit
|
||||
s.Base.OnClose = s.OnClose
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Server) OnCustomGrpcServerOption() []grpc.ServerOption {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) OnInit(serve *grpc.Server) {
|
||||
rspb.RegisterSceneServer(serve, s)
|
||||
}
|
||||
|
||||
func (s *Server) OnClose() {
|
||||
}
|
||||
Reference in New Issue
Block a user