1 Star 0 Fork 38

Archosaur/go808

forked from coco/go808 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
session_handler.go 2.91 KB
一键复制 编辑 原始数据 按行查看 历史
anchor 提交于 2021-05-28 17:18 . 更新包名
package go808
import (
"fmt"
"gitee.com/coco/go808/protocol"
"github.com/funny/link"
log "github.com/sirupsen/logrus"
"io/ioutil"
"reflect"
"strconv"
)
// Session处理
type sessionHandler struct {
server *Server
autoMergePacket bool
}
func (handler sessionHandler) HandleSession(sess *link.Session) {
log.WithFields(log.Fields{
"id": sess.ID(),
}).Debug("[JT/T 808] new session created")
// 创建Session
session := newSession(handler.server, sess)
handler.server.mutex.Lock()
handler.server.sessions[sess.ID()] = session
handler.server.mutex.Unlock()
handler.server.timer.Update(strconv.FormatUint(session.ID(), 10))
sess.AddCloseCallback(nil, nil, func() {
handler.server.handleClose(session)
})
for {
// 接收消息
msg, err := sess.Receive()
if err != nil {
sess.Close()
break
}
// 分发消息
message := msg.(protocol.Message)
if message.Body == nil || reflect.ValueOf(message.Body).IsNil() {
session.Reply(&message, protocol.T808_0x8001ResultUnsupported)
continue
}
if !handler.autoMergePacket || !message.Header.Property.IsEnablePacket() {
session.message(&message)
handler.server.dispatchMessage(session, &message)
continue
}
// 处理分包消息
entityPacket, ok := interface{}(message.Body).(protocol.EntityPacket)
if !ok {
session.message(&message)
handler.server.dispatchMessage(session, &message)
continue
}
multipartFile := MultipartFile{
IccID: message.Header.IccID,
MsgID: message.Header.MsgID,
Tag: entityPacket.GetTag(),
Sum: message.Header.Packet.Sum,
}
buf, err := ioutil.ReadAll(entityPacket.GetReader())
if err != nil {
log.WithFields(log.Fields{
"iccid": message.Header.IccID,
"msgid": fmt.Sprintf("0x%x", message.Header.MsgID),
"seq": message.Header.Packet.Seq,
"reason": err,
}).Warn("[JT/T 808] failed to read packet data")
session.Reply(&message, protocol.T808_0x8001ResultFail)
continue
}
err = multipartFile.Write(message.Header.Packet.Seq, buf)
if err != nil {
log.WithFields(log.Fields{
"iccid": message.Header.IccID,
"msgid": fmt.Sprintf("0x%x", message.Header.MsgID),
"seq": message.Header.Packet.Seq,
"reason": err,
}).Warn("[JT/T 808] failed to write packet data to file")
session.Reply(&message, protocol.T808_0x8001ResultFail)
continue
}
session.Reply(&message, protocol.T808_0x8001ResultSuccess)
if message.Header.Packet.Seq != message.Header.Packet.Sum || !multipartFile.IsFull() {
continue
}
reader, err := multipartFile.Merge()
if err != nil {
log.WithFields(log.Fields{
"iccid": message.Header.IccID,
"msgid": fmt.Sprintf("0x%x", message.Header.MsgID),
"reason": err,
}).Warn("[JT/T 808] failed to merge packet file parts")
continue
}
// 分发分包消息
entityPacket.SetReader(reader)
session.message(&message)
handler.server.dispatchMessage(session, &message)
reader.Close()
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/oviyang/go808.git
git@gitee.com:oviyang/go808.git
oviyang
go808
go808
master

搜索帮助