Skip to content

Commit

Permalink
fix(gate): 修复session rpc结构注入问题
Browse files Browse the repository at this point in the history
  • Loading branch information
liangdas committed May 19, 2023
1 parent 985635b commit d5761a1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"encoding/json"
"flag"
"fmt"
basegate "github.com/liangdas/mqant/gate/base"
"os"
"os/exec"
"os/signal"
Expand Down Expand Up @@ -210,6 +211,9 @@ func (app *DefaultApp) Run(mods ...module.Module) error {
app.configurationLoaded(app)
}

app.AddRPCSerialize("gate", &basegate.SessionSerialize{
App: app,
})
// log.InitLog(app.opts.Debug, app.opts.ProcessID, app.opts.LogDir, cof.Log)
// log.InitBI(app.opts.Debug, app.opts.ProcessID, app.opts.BIDir, cof.BI)
log.Init(log.WithDebug(app.opts.Debug),
Expand Down
48 changes: 48 additions & 0 deletions gate/base/mqtt_gate.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,54 @@ func (gt *Gate) Deserialize(ptype string, b []byte) (param interface{}, err erro
func (gt *Gate) GetTypes() []string {
return []string{RPCParamSessionType}
}

type SessionSerialize struct {
App module.App
}

/**
自定义rpc参数序列化反序列化 Session
*/
func (gt *SessionSerialize) Serialize(param interface{}) (ptype string, p []byte, err error) {
rv := reflect.ValueOf(param)
if rv.Kind() != reflect.Ptr || rv.IsNil() {
//不是指针
return "", nil, fmt.Errorf("Serialize [%v ] or not pointer type", rv.Type())
}
switch v2 := param.(type) {
case gate.Session:
bytes, err := v2.Serializable()
if err != nil {
return RPCParamSessionType, nil, err
}
return RPCParamSessionType, bytes, nil
case module.ProtocolMarshal:
bytes := v2.GetData()
return RPCParamProtocolMarshalType, bytes, nil
default:
return "", nil, fmt.Errorf("args [%s] Types not allowed", reflect.TypeOf(param))
}
}

func (gt *SessionSerialize) Deserialize(ptype string, b []byte) (param interface{}, err error) {
switch ptype {
case RPCParamSessionType:
mps, errs := NewSession(gt.App, b)
if errs != nil {
return nil, errs
}
return mps.Clone(), nil
case RPCParamProtocolMarshalType:
return gt.App.NewProtocolMarshal(b), nil
default:
return nil, fmt.Errorf("args [%s] Types not allowed", ptype)
}
}

func (gt *SessionSerialize) GetTypes() []string {
return []string{RPCParamSessionType}
}

func (gt *Gate) OnAppConfigurationLoaded(app module.App) {
//添加Session结构体的序列化操作类
gt.BaseModule.OnAppConfigurationLoaded(app) //这是必须的
Expand Down

0 comments on commit d5761a1

Please sign in to comment.