fix: handle cmpp2 deliver receipts
This commit is contained in:
@@ -605,25 +605,64 @@ func (c *connection) readLoop() {
|
||||
if ch != nil {
|
||||
ch <- submitPartResponse{seqID: p.SeqId, msgID: p.MsgId, result: p.Result}
|
||||
}
|
||||
case *cmpp.Cmpp2DeliverReqPkt:
|
||||
_ = c.client.SendRspPkt(&cmpp.Cmpp2DeliverRspPkt{MsgId: p.MsgId, Result: 0}, p.SeqId)
|
||||
c.handleDeliver(deliverPacketFromCMPP2(p))
|
||||
case *cmpp.Cmpp3DeliverReqPkt:
|
||||
c.handleDeliver(p)
|
||||
_ = c.client.SendRspPkt(&cmpp.Cmpp3DeliverRspPkt{MsgId: p.MsgId, Result: 0}, p.SeqId)
|
||||
c.handleDeliver(deliverPacketFromCMPP3(p))
|
||||
case *cmpp.CmppActiveTestReqPkt:
|
||||
_ = c.client.SendRspPkt(&cmpp.CmppActiveTestRspPkt{}, p.SeqId)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *connection) handleDeliver(pkt *cmpp.Cmpp3DeliverReqPkt) {
|
||||
_ = c.client.SendRspPkt(&cmpp.Cmpp3DeliverRspPkt{MsgId: pkt.MsgId, Result: 0}, pkt.SeqId)
|
||||
type deliverPacket struct {
|
||||
seqID uint32
|
||||
msgID uint64
|
||||
destID string
|
||||
tpUdhi uint8
|
||||
msgFmt uint8
|
||||
srcTerminalID string
|
||||
registerDelivery uint8
|
||||
msgContent string
|
||||
}
|
||||
|
||||
if pkt.RegisterDelivery == 1 {
|
||||
func deliverPacketFromCMPP2(pkt *cmpp.Cmpp2DeliverReqPkt) deliverPacket {
|
||||
return deliverPacket{
|
||||
seqID: pkt.SeqId,
|
||||
msgID: pkt.MsgId,
|
||||
destID: pkt.DestId,
|
||||
tpUdhi: pkt.TpUdhi,
|
||||
msgFmt: pkt.MsgFmt,
|
||||
srcTerminalID: pkt.SrcTerminalId,
|
||||
registerDelivery: pkt.RegisterDelivery,
|
||||
msgContent: pkt.MsgContent,
|
||||
}
|
||||
}
|
||||
|
||||
func deliverPacketFromCMPP3(pkt *cmpp.Cmpp3DeliverReqPkt) deliverPacket {
|
||||
return deliverPacket{
|
||||
seqID: pkt.SeqId,
|
||||
msgID: pkt.MsgId,
|
||||
destID: pkt.DestId,
|
||||
tpUdhi: pkt.TpUdhi,
|
||||
msgFmt: pkt.MsgFmt,
|
||||
srcTerminalID: pkt.SrcTerminalId,
|
||||
registerDelivery: pkt.RegisterDelivery,
|
||||
msgContent: pkt.MsgContent,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *connection) handleDeliver(pkt deliverPacket) {
|
||||
if pkt.registerDelivery == 1 {
|
||||
var receipt cmpp.CmppReceiptPkt
|
||||
if err := receipt.Unpack([]byte(pkt.MsgContent)); err != nil {
|
||||
if err := receipt.Unpack([]byte(pkt.msgContent)); err != nil {
|
||||
return
|
||||
}
|
||||
cmd, ok := c.commandFor(receipt.MsgId)
|
||||
if !ok {
|
||||
cmd, ok = c.commandFor(pkt.MsgId)
|
||||
cmd, ok = c.commandFor(pkt.msgID)
|
||||
}
|
||||
traceID := fmt.Sprintf("receipt-%d", receipt.MsgId)
|
||||
messageID := fmt.Sprintf("receipt-%d", receipt.MsgId)
|
||||
@@ -642,7 +681,7 @@ func (c *connection) handleDeliver(pkt *cmpp.Cmpp3DeliverReqPkt) {
|
||||
ChannelID: channelID,
|
||||
CreatedAt: time.Now().UTC(),
|
||||
},
|
||||
SequenceID: pkt.SeqId,
|
||||
SequenceID: pkt.seqID,
|
||||
GatewayMessageID: fmt.Sprint(receipt.MsgId),
|
||||
PhoneNumber: strings.TrimSpace(receipt.DestTerminalId),
|
||||
ReceiptStatus: receiptStatus(receipt.Stat),
|
||||
@@ -660,7 +699,7 @@ func (c *connection) handleDeliver(pkt *cmpp.Cmpp3DeliverReqPkt) {
|
||||
if !complete {
|
||||
return
|
||||
}
|
||||
cmd, _ := c.commandFor(pkt.MsgId)
|
||||
cmd, _ := c.commandFor(pkt.msgID)
|
||||
event := queue.UplinkEvent{
|
||||
Envelope: queue.Envelope{
|
||||
SchemaVersion: queue.SchemaVersion,
|
||||
@@ -670,32 +709,32 @@ func (c *connection) handleDeliver(pkt *cmpp.Cmpp3DeliverReqPkt) {
|
||||
ChannelID: c.channelID,
|
||||
CreatedAt: time.Now().UTC(),
|
||||
},
|
||||
SequenceID: pkt.SeqId,
|
||||
PhoneNumber: strings.TrimSpace(pkt.SrcTerminalId),
|
||||
DestID: strings.TrimSpace(pkt.DestId),
|
||||
SequenceID: pkt.seqID,
|
||||
PhoneNumber: strings.TrimSpace(pkt.srcTerminalID),
|
||||
DestID: strings.TrimSpace(pkt.destID),
|
||||
Content: content,
|
||||
ReceivedAt: time.Now().UTC(),
|
||||
}
|
||||
_ = postJSON(context.Background(), c.httpClient, c.apiBaseURL, "/gateway/events/uplink", event)
|
||||
}
|
||||
|
||||
func (c *connection) decodeUplinkContent(pkt *cmpp.Cmpp3DeliverReqPkt) (string, bool, error) {
|
||||
if pkt.TpUdhi != 1 {
|
||||
content, err := decodeContent(pkt.MsgFmt, pkt.MsgContent)
|
||||
func (c *connection) decodeUplinkContent(pkt deliverPacket) (string, bool, error) {
|
||||
if pkt.tpUdhi != 1 {
|
||||
content, err := decodeContent(pkt.msgFmt, pkt.msgContent)
|
||||
return content, true, err
|
||||
}
|
||||
ref, total, number, payload, ok := parseConcatSegment(pkt.MsgContent)
|
||||
ref, total, number, payload, ok := parseConcatSegment(pkt.msgContent)
|
||||
if !ok {
|
||||
content, err := decodeContent(pkt.MsgFmt, pkt.MsgContent)
|
||||
content, err := decodeContent(pkt.msgFmt, pkt.msgContent)
|
||||
return content, true, err
|
||||
}
|
||||
key := fmt.Sprintf("%s:%s:%s:%d:%d", c.channelID, strings.TrimSpace(pkt.SrcTerminalId), strings.TrimSpace(pkt.DestId), ref, total)
|
||||
key := fmt.Sprintf("%s:%s:%s:%d:%d", c.channelID, strings.TrimSpace(pkt.srcTerminalID), strings.TrimSpace(pkt.destID), ref, total)
|
||||
c.mu.Lock()
|
||||
if c.longUplink == nil {
|
||||
c.longUplink = make(map[string]*longUplinkAssembly)
|
||||
}
|
||||
pruneLongUplinkAssemblies(c.longUplink, time.Now(), 10*time.Minute)
|
||||
content, complete, err := assembleLongUplink(c.longUplink, key, pkt.MsgFmt, total, number, payload)
|
||||
content, complete, err := assembleLongUplink(c.longUplink, key, pkt.msgFmt, total, number, payload)
|
||||
c.mu.Unlock()
|
||||
return content, complete, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user