pkg

package
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 20, 2021 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SmgpActiveTestReqPktLen  uint32 = 12 //12d, 0xc
	SmgpActiveTestRespPktLen uint32 = 12 //12d, 0xc
)
View Source
const (
	SmgpExitReqPktLen  uint32 = 12 //12d, 0xc
	SmgpExitRespPktLen uint32 = 12 //12d, 0xc
)
View Source
const (
	SmgpLoginReqPktLen  = HeaderPktLen + 8 + 16 + 1 + 4 + 1 //42d, 0x2a
	SmgpLoginRespPktLen = HeaderPktLen + 4 + 16 + 1         //33d, 0x21
)
View Source
const (
	SMGP_HEADER_LEN uint32 = 12
	SMGP_PACKET_MAX uint32 = 2477
	SMGP_PACKET_MIN uint32 = 12
)
View Source
const (
	SmgpQueryReqPktLen  = HeaderPktLen + 8 + 1 + 10           //42d, 0x2a
	SmgpQueryRespPktLen = HeaderPktLen + 8 + 1 + 10 + 4*8 + 8 //33d, 0x21
)
View Source
const (
	SEND_MODE = iota
	RECEIVE_MODE
	TRANSMIT_MODE
)
View Source
const (
	MO = 0 // MO消息(终端发给SP)
	MT = 6 // MT消息(SP发给终端,包括WEB上发送的点对点短消息)
)

MsgType

View Source
const (
	ASCII   = 0  // ASCII编码
	BINARY  = 4  // 二进制短消息
	UCS2    = 8  // UCS2编码
	GB18030 = 15 // GB18030编码
)

MsgFormat 短消息内容体的编码格式 对于文字短消息,要求MsgFormat=15, 对于回执消息,要求MsgFormat=0

View Source
const (
	NOT_REPORT = 0 // 不是状态报告
	IS_REPORT  = 1 // 是状态报告
)
View Source
const (
	NO_NEED_REPORT = 0
	NEED_REPORT    = 1
)

是否要求返回状态报告

View Source
const (
	LOW_PRIORITY = iota
	NORMAL_PRIORITY
	HIGHER_PRIORITY
	HIGHEST_PRIORITY
)

短消息发送优先级

View Source
const HeaderPktLen uint32 = 4 + 4 + 4
View Source
const (
	SmgpDeliverRespPktLen = HeaderPktLen + 10 + 4 //26d, 0x1a
)
View Source
const (
	SmgpSubmitRespPktLen = HeaderPktLen + 10 + 4 //26d, 0x1a
)
View Source
const (
	VERSION uint8 = 0x30
)

Variables

View Source
var (
	// Common errors.
	ErrMethodParamsInvalid = errors.New("params passed to method is invalid")

	// Protocol errors.
	ErrTotalLengthInvalid    = errors.New("PacketLength in Packet data is invalid")
	ErrRequestIDInvalid      = errors.New("RequestID in Packet data is invalid")
	ErrRequestIDNotSupported = errors.New("RequestID in Packet data is not supported")

	// Connection errors.
	ErrConnIsClosed       = errors.New("connection is closed")
	ErrReadHeaderTimeout  = errors.New("read header timeout")
	ErrReadPktBodyTimeout = errors.New("read packet body timeout")
)
View Source
var ErrLength = errors.New("Options: error length")
View Source
var TagName = map[Tag]string{
	TAG_TP_pid:           "TAG_TP_pid",
	TAG_TP_udhi:          "TAG_TP_udhi",
	TAG_LinkID:           "TAG_LinkID",
	TAG_ChargeUserType:   "TAG_ChargeUserType",
	TAG_ChargeTermType:   "TAG_ChargeTermType",
	TAG_ChargeTermPseudo: "TAG_ChargeTermPseudo",
	TAG_DestTermType:     "TAG_DestTermType",
	TAG_PkTotal:          "TAG_PkTotal",
	TAG_PkNumber:         "TAG_PkNumber",
	TAG_SubmitMsgType:    "TAG_SubmitMsgType",
	TAG_SPDealResult:     "TAG_SPDealResult",
	TAG_SrcTermType:      "TAG_SrcTermType",
	TAG_SrcTermPseudo:    "TAG_SrcTermPseudo",
	TAG_NodesCount:       "TAG_NodesCount",
	TAG_MsgSrc:           "TAG_MsgSrc",
	TAG_SrcType:          "TAG_SrcType",
	TAG_MServiceID:       "TAG_MServiceID",
}
View Source
var TpUdhiSeq byte = 0x00

Functions

func GB18030ToUtf8

func GB18030ToUtf8(in string) (string, error)

func GenAuthenticatorClient

func GenAuthenticatorClient(clientId, secret string, timestamp uint32) ([]byte, error)

生成客户端认证码 其值通过单向MD5 hash计算得出,表示如下: AuthenticatorClient =MD5(ClientID+7 字节的二进制0(0x00) + Shared secret+Timestamp) Shared secret 由服务器端与客户端事先商定,最长15字节。 此处Timestamp格式为:MMDDHHMMSS(月日时分秒),经TimeStamp字段值转换成字符串,转换后右对齐,左补0x30得到。 例如3月1日0时0分0秒,TimeStamp字段值为0x11F0E540,此处为0301000000。

func GenAuthenticatorServer added in v0.0.1

func GenAuthenticatorServer(status Status, secret, AuthenticatorClient string) ([]byte, error)

生成服务端认证码 Login服务器端返回给客户端的认证码,当客户端认证出错时,此项为空。 其值通过单向MD5 hash计算得出,表示如下: AuthenticatorServer =MD5(Status+AuthenticatorClient + Shared secret) Shared secret 由服务器端与客户端事先商定,最长15字节AuthenticatorClient为客户端发送给服务器端的Login中的值。参见6.2.2节。

func GenMsgID

func GenMsgID(spId string, sequenceNum uint32) (string, error)

MsgId字段包含以下三部分内容: SMGW代码:3字节(BCD码)

编码规则如下:
3位区号(不足前添0)+2位设备类别+1位序号
区号:所在省长途区号
设备类别:SMGW取06
序号:所在省的设备编码,例如第一个网关编号为1

时间:4字节(BCD码),格式为MMDDHHMM(月日时分) 序列号:3字节(BCD码),取值范围为000000~999999,从0开始,顺序累加,步长为1,循环使用。 例如某SMGW的代码为010061,在2003年1月16日下午5时0分收到一条短消息,这条短消息的MsgID为:0x01006101161700012345,其中010061表示SMGW代码,01161700表示接收时间,012345表示消息序列号。 MsgID 无法用数字的形式存储,16进制字符串,总长度 10*2

func GenNowTimeYYStr

func GenNowTimeYYStr() string

func GenNowTimeYYYYStr

func GenNowTimeYYYYStr() string

func GenTimestamp

func GenTimestamp() uint32

func SplitLongSms

func SplitLongSms(content string) [][]byte

func Ucs2ToUtf8

func Ucs2ToUtf8(in string) (string, error)

func UnpackMsgId

func UnpackMsgId(msgId string) string

func Utf8ToGB18030

func Utf8ToGB18030(in string) (string, error)

func Utf8ToUcs2

func Utf8ToUcs2(in string) (string, error)

Types

type Conn

type Conn struct {
	net.Conn
	State   State
	Version uint8

	// for SequenceID generator goroutine
	SequenceID <-chan uint32

	// for SequenceNum generator goroutine
	SequenceNum <-chan uint32
	// contains filtered or unexported fields
}

func NewConnection

func NewConnection(conn net.Conn, v uint8) *Conn

func (*Conn) Close

func (c *Conn) Close()

func (*Conn) RecvAndUnpackPkt

func (c *Conn) RecvAndUnpackPkt(timeout time.Duration) (Packer, error)

func (*Conn) SendPkt

func (c *Conn) SendPkt(packet Packer, seqId uint32) error

func (*Conn) SetState

func (c *Conn) SetState(state State)
type Header struct {
	PacketLength uint32 // 数据包长度
	RequestID    uint32 // 请求标识
	SequenceID   uint32 // 消息流水号
}

消息头(所有消息公共包头)

func (*Header) Pack

func (p *Header) Pack(w *pkgWriter, pktLen, requestId, seqId uint32) *pkgWriter

func (*Header) String

func (p *Header) String() string

func (*Header) Unpack

func (p *Header) Unpack(r *pkgReader) *Header

type OctetString

type OctetString string

不强制以0x00结尾的定长字符串。 当位数不足时,在不明确注明的 情况下, 应左对齐,右补0x00。 在明确注明的情况下,以该字段的明确注明为准。

func NewOctetString

func NewOctetString(o string) OctetString

func (OctetString) Byte

func (o OctetString) Byte(fixedLength int) []byte

按需补零

func (OctetString) FixedString

func (o OctetString) FixedString(fixedLength int) string

func (OctetString) String

func (o OctetString) String(fixedLength int) string

去除补零,转为字符串

type OpError

type OpError struct {
	// contains filtered or unexported fields
}

func NewOpError

func NewOpError(e error, op string) *OpError

func (*OpError) Cause

func (e *OpError) Cause() error

func (*OpError) Error

func (e *OpError) Error() string

func (*OpError) Op

func (e *OpError) Op() string

type Options

type Options map[Tag]*TLV

可选参数 map

func ParseOptions

func ParseOptions(rawData []byte) (Options, error)

func (Options) Len

func (o Options) Len() int

返回可选字段部分的长度

func (Options) String

func (o Options) String() string

type Packer

type Packer interface {
	Pack(seqId uint32) ([]byte, error)
	Unpack(data []byte) error
	String() string
}

type RequestID

type RequestID uint32
const (
	SMGP_REQUEST_MIN, SMGP_RESPONSE_MIN RequestID = iota, 0x80000000 + iota
	SMGP_LOGIN, SMGP_LOGIN_RESP
	SMGP_SUBMIT, SMGP_SUBMIT_RESP
	SMGP_DELIVER, SMGP_DELIVER_RESP
	SMGP_ACTIVE_TEST, SMGP_ACTIVE_TEST_RESP
	SMGP_FORWARD, SMGP_FORWARD_RESP
	SMGP_EXIT, SMGP_EXIT_RESP
	SMGP_QUERY, SMGP_QUERY_RESP

	SMGP_QUERY_TE_ROUTE, SMGP_QUERY_TE_ROUTE_RESP
	SMGP_QUERY_SP_ROUTE, SMGP_QUERY_SP_ROUTE_RESP
	SMGP_PAYMENT_REQUEST, SMGP_PAYMENT_REQUEST_RESP
	SMGP_PAYMENT_AFFIRM, SMGP_PAYMENT_AFFIRM_RESP
	SMGP_QUERY_USERSTATE, SMGP_QUERY_USERSTATE_RESP
	SMGP_GET_ALL_TE_ROUTE, SMGP_GET_ALL_TE_ROUTE_RESP
	SMGP_GET_ALL_SP_ROUTE, SMGP_GET_ALL_SP_ROUTE_RESP
	SMGP_UPDATE_TE_ROUTE, SMGP_UPDATE_TE_ROUTE_RESP
	SMGP_UPDATE_SP_ROUTE, SMGP_UPDATE_SP_ROUTE_RESP
	SMGP_PUSH_UPDATE_TE_ROUTE, SMGP_PUSH_UPDATE_TE_ROUTE_RESP
	SMGP_PUSH_UPDATE_SP_ROUTE, SMGP_PUSH_UPDATE_SP_ROUTE_RESP

	SMGP_REQUEST_MAX, SMGP_RESPONSE_MAX
)

func (RequestID) String

func (id RequestID) String() string

type SmgpActiveTestReqPkt

type SmgpActiveTestReqPkt struct {
	// used in session
	SequenceID uint32
}

func (*SmgpActiveTestReqPkt) Pack

func (p *SmgpActiveTestReqPkt) Pack(seqId uint32) ([]byte, error)

func (*SmgpActiveTestReqPkt) String

func (p *SmgpActiveTestReqPkt) String() string

func (*SmgpActiveTestReqPkt) Unpack

func (p *SmgpActiveTestReqPkt) Unpack(data []byte) error

type SmgpActiveTestRespPkt

type SmgpActiveTestRespPkt struct {
	// used in session
	SequenceID uint32
}

func (*SmgpActiveTestRespPkt) Pack

func (p *SmgpActiveTestRespPkt) Pack(seqId uint32) ([]byte, error)

func (*SmgpActiveTestRespPkt) String

func (p *SmgpActiveTestRespPkt) String() string

func (*SmgpActiveTestRespPkt) Unpack

func (p *SmgpActiveTestRespPkt) Unpack(data []byte) error

type SmgpDeliverMsgContent

type SmgpDeliverMsgContent struct {
	SubmitMsgID string // submit resp 的 MsgID
	Sub         string
	Dlvrd       string
	SubmitDate  string
	DoneDate    string
	Stat        string
	Err         string
	Txt         string
}

func DecodeDeliverMsgContent

func DecodeDeliverMsgContent(data []byte) *SmgpDeliverMsgContent

func (*SmgpDeliverMsgContent) Encode

func (p *SmgpDeliverMsgContent) Encode() string

func (*SmgpDeliverMsgContent) String

func (p *SmgpDeliverMsgContent) String() string

type SmgpDeliverReqPkt

type SmgpDeliverReqPkt struct {
	MsgID      string // 短消息流水号
	IsReport   uint8  // 是否为状态报告
	MsgFormat  uint8  // 短消息格式
	RecvTime   string // 短消息接收时间
	SrcTermID  string // 短消息发送号码
	DestTermID string // 短消息接收号码
	MsgLength  uint8  // 短消息长度
	MsgContent []byte // 短消息内容
	Reserve    string // 保留

	// 可选字段
	Options Options

	// used in session
	SequenceID     uint32
	MsgStatContent *SmgpDeliverMsgContent
}

func (*SmgpDeliverReqPkt) Pack

func (p *SmgpDeliverReqPkt) Pack(seqId uint32) ([]byte, error)

func (*SmgpDeliverReqPkt) String

func (p *SmgpDeliverReqPkt) String() string

func (*SmgpDeliverReqPkt) Unpack

func (p *SmgpDeliverReqPkt) Unpack(data []byte) error

type SmgpDeliverRespPkt

type SmgpDeliverRespPkt struct {
	MsgID  string
	Status Status

	// used in session
	SequenceID uint32
}

func (*SmgpDeliverRespPkt) Pack

func (p *SmgpDeliverRespPkt) Pack(seqId uint32) ([]byte, error)

func (*SmgpDeliverRespPkt) String

func (p *SmgpDeliverRespPkt) String() string

func (*SmgpDeliverRespPkt) Unpack

func (p *SmgpDeliverRespPkt) Unpack(data []byte) error

type SmgpExitReqPkt

type SmgpExitReqPkt struct {
	// used in session
	SequenceID uint32
}

func (*SmgpExitReqPkt) Pack

func (p *SmgpExitReqPkt) Pack(seqId uint32) ([]byte, error)

func (*SmgpExitReqPkt) String

func (p *SmgpExitReqPkt) String() string

func (*SmgpExitReqPkt) Unpack

func (p *SmgpExitReqPkt) Unpack(data []byte) error

type SmgpExitRespPkt

type SmgpExitRespPkt struct {
	// used in session
	SequenceID uint32
}

func (*SmgpExitRespPkt) Pack

func (p *SmgpExitRespPkt) Pack(seqId uint32) ([]byte, error)

func (*SmgpExitRespPkt) String

func (p *SmgpExitRespPkt) String() string

func (*SmgpExitRespPkt) Unpack

func (p *SmgpExitRespPkt) Unpack(data []byte) error

type SmgpLoginReqPkt

type SmgpLoginReqPkt struct {
	ClientID            string
	AuthenticatorClient string
	LoginMode           uint8
	TimeStamp           uint32
	ClientVersion       uint8

	// used in session
	SequenceID uint32
	Secret     string
}

func (*SmgpLoginReqPkt) Pack

func (p *SmgpLoginReqPkt) Pack(seqId uint32) ([]byte, error)

func (*SmgpLoginReqPkt) String

func (p *SmgpLoginReqPkt) String() string

func (*SmgpLoginReqPkt) Unpack

func (p *SmgpLoginReqPkt) Unpack(data []byte) error

type SmgpLoginRespPkt

type SmgpLoginRespPkt struct {
	Status              Status // 请求返回结果
	AuthenticatorServer string // 服务器端返回给客户端的认证码
	ServerVersion       uint8  // 服务器端支持的最高版本号

	// auth
	Secret              string
	AuthenticatorClient string
	// used in session
	SequenceID uint32
}

func (*SmgpLoginRespPkt) Pack

func (p *SmgpLoginRespPkt) Pack(seqId uint32) ([]byte, error)

func (*SmgpLoginRespPkt) String

func (p *SmgpLoginRespPkt) String() string

func (*SmgpLoginRespPkt) Unpack

func (p *SmgpLoginRespPkt) Unpack(data []byte) error

type SmgpQueryReqPkt added in v0.0.3

type SmgpQueryReqPkt struct {
	QueryTime string
	QueryType uint8
	QueryCode string

	// used in session
	SequenceID uint32
}

func (*SmgpQueryReqPkt) Pack added in v0.0.3

func (p *SmgpQueryReqPkt) Pack(seqId uint32) ([]byte, error)

func (*SmgpQueryReqPkt) String added in v0.0.3

func (p *SmgpQueryReqPkt) String() string

func (*SmgpQueryReqPkt) Unpack added in v0.0.3

func (p *SmgpQueryReqPkt) Unpack(data []byte) error

type SmgpQueryRespPkt added in v0.0.3

type SmgpQueryRespPkt struct {
	QueryTime string
	QueryType uint8
	QueryCode string
	MT_TLMsg  uint32
	MT_Tlusr  uint32
	MT_Scs    uint32
	MT_WT     uint32
	MT_FL     uint32
	MO_Scs    uint32
	MO_WT     uint32
	MO_FL     uint32
	Reserve   string

	// used in session
	SequenceID uint32
}

func (*SmgpQueryRespPkt) Pack added in v0.0.3

func (p *SmgpQueryRespPkt) Pack(seqId uint32) ([]byte, error)

func (*SmgpQueryRespPkt) String added in v0.0.3

func (p *SmgpQueryRespPkt) String() string

func (*SmgpQueryRespPkt) Unpack added in v0.0.3

func (p *SmgpQueryRespPkt) Unpack(data []byte) error

type SmgpSubmitReqPkt

type SmgpSubmitReqPkt struct {
	MsgType         uint8    // 短消息类型
	NeedReport      uint8    // SP是否要求返回状态报告
	Priority        uint8    // 短消息发送优先级
	ServiceID       string   // 业务代码
	FeeType         string   // 收费类型
	FeeCode         string   // 资费代码
	FixedFee        string   // 包月费/封顶费
	MsgFormat       uint8    // 短消息格式
	ValidTime       string   // 短消息有效时间
	AtTime          string   // 短消息定时发送时间
	SrcTermID       string   // 短信息发送方号码
	ChargeTermID    string   // 计费用户号码
	DestTermIDCount uint8    // 短消息接收号码总数,最多 100
	DestTermID      []string // 短消息接收号码
	MsgLength       uint8    // 短消息长度
	MsgContent      string   // 短消息内容
	Reserve         string   // 保留

	// 可选参数
	Options Options

	// used in session
	SequenceID uint32
}

func GetLongMsgPkgs

func GetLongMsgPkgs(pkg *SmgpSubmitReqPkt) ([]*SmgpSubmitReqPkt, error)

func (*SmgpSubmitReqPkt) Pack

func (p *SmgpSubmitReqPkt) Pack(seqId uint32) ([]byte, error)

func (*SmgpSubmitReqPkt) String

func (p *SmgpSubmitReqPkt) String() string

func (*SmgpSubmitReqPkt) Unpack

func (p *SmgpSubmitReqPkt) Unpack(data []byte) error

type SmgpSubmitRespPkt

type SmgpSubmitRespPkt struct {
	MsgID  string
	Status Status

	// used in session
	SequenceID uint32
}

func (*SmgpSubmitRespPkt) Pack

func (p *SmgpSubmitRespPkt) Pack(seqId uint32) ([]byte, error)

func (*SmgpSubmitRespPkt) String

func (p *SmgpSubmitRespPkt) String() string

func (*SmgpSubmitRespPkt) Unpack

func (p *SmgpSubmitRespPkt) Unpack(data []byte) error

type State

type State uint8
const (
	CONNECTION_CLOSED State = iota
	CONNECTION_CONNECTED
	CONNECTION_AUTHOK
)

type Status

type Status uint32
const (
	STAT_OK Status = iota
)

func (*Status) Data

func (s *Status) Data() uint32

func (*Status) Error

func (s *Status) Error() error

func (Status) String

func (s Status) String() string

type TLV

type TLV struct {
	// 字段的标签,用于唯一标识可选参数
	Tag Tag
	// 字段的长度
	Length uint16
	// 可变类型 字段内容
	Value []byte
}

可选参数采用TLV(Tag、Length、Value)形式定义, 每个可选参数的Tag、Length、Value的定义见6.3节。

func NewTLV

func NewTLV(tag Tag, value []byte) *TLV

func (*TLV) Byte

func (t *TLV) Byte() ([]byte, error)

序列化为字节流

func (*TLV) String

func (t *TLV) String() string

type Tag

type Tag uint16
const (
	TAG_TP_pid Tag = 0x0001 + iota
	TAG_TP_udhi
	TAG_LinkID
	TAG_ChargeUserType
	TAG_ChargeTermType
	TAG_ChargeTermPseudo
	TAG_DestTermType
	TAG_DestTermPseudo
	TAG_PkTotal
	TAG_PkNumber
	TAG_SubmitMsgType
	TAG_SPDealResult
	TAG_SrcTermType
	TAG_SrcTermPseudo
	TAG_NodesCount
	TAG_MsgSrc
	TAG_SrcType
	TAG_MServiceID
)

可选参数标签定义 Option Tag

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL