2025-01-24 18:26:20 +08:00
|
|
|
|
package message
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"git.skcks.cn/Shikong/go-gb28181/pkg/config"
|
|
|
|
|
"git.skcks.cn/Shikong/go-gb28181/pkg/log"
|
|
|
|
|
"git.skcks.cn/Shikong/go-gb28181/pkg/manscdp"
|
|
|
|
|
"git.skcks.cn/Shikong/go-gb28181/pkg/utils"
|
|
|
|
|
"github.com/emiago/sipgo"
|
|
|
|
|
"github.com/emiago/sipgo/sip"
|
|
|
|
|
)
|
|
|
|
|
|
2025-01-25 15:53:25 +08:00
|
|
|
|
// messageHandler 是一个处理SIP消息的函数类型
|
2025-01-24 18:26:20 +08:00
|
|
|
|
type messageHandler = func(client *sipgo.Client, clientConfig *config.ClientConfig, req *sip.Request, tx sip.ServerTransaction)
|
|
|
|
|
|
2025-01-25 15:53:25 +08:00
|
|
|
|
// handlers 是一个存储消息处理器的映射,键为命令类型,值为消息处理器函数
|
2025-01-24 18:26:20 +08:00
|
|
|
|
var handlers = make(map[string]messageHandler)
|
|
|
|
|
|
2025-01-25 15:53:25 +08:00
|
|
|
|
// SetupMessageHandler 设置SIP服务器的消息处理器,根据消息的命令类型调用相应的处理器
|
2025-01-24 18:26:20 +08:00
|
|
|
|
func SetupMessageHandler(srv *sipgo.Server, client *sipgo.Client, clientConfig *config.ClientConfig) {
|
|
|
|
|
srv.OnMessage(func(req *sip.Request, tx sip.ServerTransaction) {
|
|
|
|
|
body := req.Body()
|
2025-01-25 15:53:25 +08:00
|
|
|
|
if len(body) == 0 {
|
|
|
|
|
log.Log().Warn().Msgf("消息体为空")
|
|
|
|
|
_ = tx.Respond(sip.NewResponseFromRequest(req, sip.StatusOK, "OK", nil))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-24 18:26:20 +08:00
|
|
|
|
message := new(manscdp.MessageReq)
|
|
|
|
|
err := utils.XMLUnmarshal(body, message)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Log().Error().Msgf("XML Unmarshal error: %v", err)
|
2025-01-25 15:53:25 +08:00
|
|
|
|
_ = tx.Respond(sip.NewResponseFromRequest(req, sip.StatusBadRequest, "Bad Request", nil))
|
|
|
|
|
return
|
2025-01-24 18:26:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handlers, ok := handlers[message.CmdType]
|
|
|
|
|
if ok {
|
|
|
|
|
handlers(client, clientConfig, req, tx)
|
|
|
|
|
} else {
|
|
|
|
|
log.Log().Warn().Msgf("未找到可用的消息处理器: %s", message.CmdType)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_ = tx.Respond(sip.NewResponseFromRequest(req, sip.StatusOK, "OK", nil))
|
|
|
|
|
})
|
|
|
|
|
}
|