2024-07-23 10:23:43 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2024-07-31 16:49:14 +08:00
|
|
|
"gitea.bvbej.com/bvbej/base-golang/pkg/websocket/peer"
|
2024-07-23 10:23:43 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type callBackEntity struct{}
|
|
|
|
|
|
|
|
func GetSessionManager() *peer.SessionManager {
|
|
|
|
return peer.NewSessionMgr(&callBackEntity{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cb *callBackEntity) OnClosed(session *peer.Session) {
|
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
fmt.Println(fmt.Sprintf("OnClosed: session:%d err:%v", session.Conn.ID(), err))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
for _, v := range RegisteredServiceList {
|
|
|
|
if ok := v.OnSessionClose(session); ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cb *callBackEntity) OnReceive(session *peer.Session, msg []byte) error {
|
|
|
|
_, msgPack, err := RouterCodec.Unmarshal(msg)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("onreceive: %v", err)
|
|
|
|
}
|
|
|
|
router, ok := msgPack.Router.(string)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("onreceive: invalid router:%v", msgPack.Router)
|
|
|
|
}
|
|
|
|
routerArr := strings.Split(router, ".")
|
|
|
|
if len(routerArr) != 2 {
|
|
|
|
return fmt.Errorf("onreceive: invalid router:%s", msgPack.Router)
|
|
|
|
}
|
|
|
|
s, ok := RegisteredServiceList[routerArr[0]]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("onreceive: function not registed router:%s err:%v", msgPack.Router, err)
|
|
|
|
}
|
|
|
|
h, ok := s.Handlers[routerArr[1]]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("onreceive: function not registed router:%s err:%v", msgPack.Router, err)
|
|
|
|
}
|
|
|
|
t1 := time.Now()
|
|
|
|
|
|
|
|
var args = []reflect.Value{h.Receiver, reflect.ValueOf(session), reflect.ValueOf(msgPack.DataPtr)}
|
|
|
|
var res any
|
|
|
|
var rb []byte
|
|
|
|
res, err = CallHandlerFunc(h.Method, args)
|
|
|
|
if res != nil && !reflect.ValueOf(res).IsNil() {
|
|
|
|
rb, err = RouterCodec.Marshal(router, res, nil)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("service: %v", err)
|
|
|
|
}
|
|
|
|
err = session.Conn.Send(rb)
|
|
|
|
if err != nil {
|
|
|
|
serviceLogger.Sugar().Warnf("warn! service send msg failed router:%s err:%v", router, err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rb, err = RouterCodec.Marshal(router, nil, err)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("service: %v", err)
|
|
|
|
}
|
|
|
|
err = session.Conn.Send(rb)
|
|
|
|
if err != nil {
|
|
|
|
serviceLogger.Sugar().Warnf("warn! service send msg failed router:%s err:%v", router, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var errs string
|
|
|
|
if err != nil {
|
|
|
|
errs = err.Error()
|
|
|
|
}
|
|
|
|
dt := time.Since(t1)
|
|
|
|
go s.Component.OnRequestFinished(session, router, RouterCodec.ToString(msgPack.DataPtr), errs, dt)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func CallHandlerFunc(foo reflect.Method, args []reflect.Value) (retValue any, retErr error) {
|
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
fmt.Println(fmt.Sprintf("CallHandlerFunc: %v", err))
|
|
|
|
retValue = nil
|
|
|
|
retErr = fmt.Errorf("CallHandlerFunc: call method pkg:%s method:%s err:%v", foo.PkgPath, foo.Name, err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if ret := foo.Func.Call(args); len(ret) > 0 {
|
|
|
|
var err error = nil
|
|
|
|
if r1 := ret[1].Interface(); r1 != nil {
|
|
|
|
err = r1.(error)
|
|
|
|
}
|
|
|
|
return ret[0].Interface(), err
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("CallHandlerFunc: call method pkg:%s method:%s", foo.PkgPath, foo.Name)
|
|
|
|
}
|