This commit is contained in:
2025-07-04 23:41:19 +08:00
parent 7c2c32a31a
commit f0fd00d706
27 changed files with 1206 additions and 163 deletions

View File

@@ -1,19 +1,30 @@
package grpc_server
package server
import (
"common/log"
"common/proto/gen/common"
"common/proto/gen/grpc_pb"
"context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"scene/instance"
"sync"
)
func (s *Server) Enter(ctx context.Context, req *grpc_pb.EnterReq) (*grpc_pb.EnterResp, error) {
log.Infof("enter 触发 %v", req.SID)
return nil, status.Errorf(codes.Unimplemented, "")
var i *instance.Instance
if len(instance.Mgr.GetAll()) == 0 {
i = instance.NewScene(s.SID, int(req.InstanceID))
i.Start(s.EtcdTTL)
} else {
for _, v := range instance.Mgr.GetAll() {
i = v
break
}
}
i.EventIn <- req
return &grpc_pb.EnterResp{
SceneSID: s.SID,
UniqueNo: i.UniqueNo,
}, nil
}
func (s *Server) Action(server grpc_pb.Scene_ActionServer) error {

View File

@@ -1,4 +1,4 @@
package grpc_server
package server
import (
"common/discover/common"

View File

@@ -0,0 +1,65 @@
package stream_client
import (
"common/log"
"common/net/grpc/service"
"context"
"google.golang.org/grpc"
"google.golang.org/protobuf/proto"
)
var gatewayServerM map[int64]map[GatewayFun]grpc.ClientStream // map[sid]map[方法名]流连接
type GatewayFun int
const (
FunToClient GatewayFun = iota
)
func init() {
gatewayServerM = make(map[int64]map[GatewayFun]grpc.ClientStream)
}
func FindGatewayBySID(sid int64, fun GatewayFun) (grpc.ClientStream, error) {
g := gatewayServerM[sid]
if g == nil {
g = make(map[GatewayFun]grpc.ClientStream)
gatewayServerM[sid] = g
}
gatewayLink := g[fun]
if gatewayLink == nil {
gatewayClient, err := service.GatewayNewClient(sid)
if err != nil {
log.Errorf("cannot find gatewayClient: %v", err)
return nil, err
}
var link grpc.ClientStream
switch fun {
case FunToClient:
link, err = gatewayClient.ToClient(context.Background())
}
if err != nil {
log.Errorf("FindGatewayBySID %v err: %v, sid: %v", fun, err, sid)
return nil, err
}
g[fun] = link
gatewayLink = link
}
return gatewayLink, nil
}
func SendMessageToGateway(sid int64, fun GatewayFun, msg proto.Message, re ...bool) error {
stream, err := FindGatewayBySID(sid, fun)
if err != nil {
return err
}
if err = stream.SendMsg(msg); err != nil {
if re == nil || !re[0] {
_ = stream.CloseSend()
delete(gatewayServerM[sid], fun)
return SendMessageToGateway(sid, fun, msg, true)
}
return err
}
return nil
}