Files
go-sms-V2/internal/protocol/sgip/sgip.go
T

159 lines
4.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Package sgip SGIP 1.2(联通)协议基础(Day 33):20 字节消息头、
// Bind 登录、Submit 提交、Report 状态报告。
//
// 与 CMPP 的差异:头部长 20B(MessageLength+CommandID+SequenceNumber 3×4B:
// 源节点/时间/序号);无 ActiveTest 心跳(以 Bind 重连与读超时判活)。
package sgip
import (
"encoding/binary"
"errors"
"fmt"
"io"
)
// CommandID SGIP 命令字。
type CommandID uint32
// SGIP 1.2 命令常量。
const (
CmdBind CommandID = 0x00000001
CmdBindResp CommandID = 0x80000001
CmdSubmit CommandID = 0x00000003
CmdSubmitResp CommandID = 0x80000003
CmdDeliver CommandID = 0x00000004
CmdDeliverResp CommandID = 0x80000004
CmdReport CommandID = 0x00000005
CmdReportResp CommandID = 0x80000005
CmdUnbind CommandID = 0x00000006
CmdUnbindResp CommandID = 0x80000006
)
// HeaderLen SGIP 头固定 20 字节。
const HeaderLen = 20
// MaxPacketLength 单包上限。
const MaxPacketLength = 64 * 1024
// ErrBadHeader 非法消息头。
var ErrBadHeader = errors.New("sgip: 非法消息头")
// Sequence SGIP 三段流水号(源节点/时间 MMDDHHMMSS/序号)。
type Sequence struct {
NodeID uint32
DateTime uint32
Seq uint32
}
// Header SGIP 消息头。
type Header struct {
MessageLength uint32
CommandID CommandID
Sequence Sequence
}
// Marshal 编码 20B 头。
func (h *Header) Marshal() []byte {
b := make([]byte, HeaderLen)
binary.BigEndian.PutUint32(b[0:4], h.MessageLength)
binary.BigEndian.PutUint32(b[4:8], uint32(h.CommandID))
binary.BigEndian.PutUint32(b[8:12], h.Sequence.NodeID)
binary.BigEndian.PutUint32(b[12:16], h.Sequence.DateTime)
binary.BigEndian.PutUint32(b[16:20], h.Sequence.Seq)
return b
}
// ParseHeader 解析 20B 头。
func ParseHeader(b []byte) (*Header, error) {
if len(b) < HeaderLen {
return nil, ErrBadHeader
}
h := &Header{
MessageLength: binary.BigEndian.Uint32(b[0:4]),
CommandID: CommandID(binary.BigEndian.Uint32(b[4:8])),
Sequence: Sequence{
NodeID: binary.BigEndian.Uint32(b[8:12]),
DateTime: binary.BigEndian.Uint32(b[12:16]),
Seq: binary.BigEndian.Uint32(b[16:20]),
},
}
if h.MessageLength < HeaderLen || h.MessageLength > MaxPacketLength {
return nil, fmt.Errorf("%w: length=%d", ErrBadHeader, h.MessageLength)
}
return h, nil
}
// ReadPacket 读完整包(头+体)。
func ReadPacket(r io.Reader) (*Header, []byte, error) {
head := make([]byte, HeaderLen)
if _, err := io.ReadFull(r, head); err != nil {
return nil, nil, err
}
h, err := ParseHeader(head)
if err != nil {
return nil, nil, err
}
body := make([]byte, h.MessageLength-HeaderLen)
if _, err := io.ReadFull(r, body); err != nil {
return nil, nil, err
}
return h, body, nil
}
// WritePacket 写完整包(自动补 MessageLength)。
func WritePacket(w io.Writer, h *Header, body []byte) error {
h.MessageLength = uint32(HeaderLen + len(body))
if _, err := w.Write(append(h.Marshal(), body...)); err != nil {
return err
}
return nil
}
// Bind 登录包体:LoginName 16B + Password 16B + Reserve 8B。
type Bind struct {
Name string
Password string
}
// BindBodyLen Bind 包体长度。
const BindBodyLen = 16 + 16 + 8
// Marshal 编码 Bind 包体。
func (b *Bind) Marshal() []byte {
body := make([]byte, BindBodyLen)
copy(body[0:16], b.Name)
copy(body[16:32], b.Password)
return body
}
// ParseBind 解析 Bind 包体。
func ParseBind(body []byte) (name, password string, err error) {
if len(body) < BindBodyLen {
return "", "", fmt.Errorf("sgip: bind 包体 %d 字节不足 %d", len(body), BindBodyLen)
}
return trimZero(body[0:16]), trimZero(body[16:32]), nil
}
// trimZero 去右补零。
func trimZero(b []byte) string {
for i := len(b) - 1; i >= 0; i-- {
if b[i] != 0 {
return string(b[:i+1])
}
}
return ""
}
// MakeResultBody 构造 Result 应答体(9B)。
func MakeResultBody(result uint8) []byte {
return []byte{result, 0, 0, 0, 0, 0, 0, 0, 0}
}
// ParseResult 解析 Result 应答体。
func ParseResult(body []byte) (uint8, error) {
if len(body) < 1 {
return 0, errors.New("sgip: result 包体为空")
}
return body[0], nil
}