|
|
|
@ -5,14 +5,11 @@ import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/binary"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
"sync"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var ErrDeadlineExceeded error = errors.New("deadline exceeded")
|
|
|
|
|
|
|
|
|
|
// 识别头
|
|
|
|
|
var header = []byte{11, 27, 19, 96, 12, 25, 02, 20}
|
|
|
|
|
|
|
|
|
@ -25,13 +22,13 @@ type MsgQueue struct {
|
|
|
|
|
|
|
|
|
|
// StarQueue 为流数据中的消息队列分发
|
|
|
|
|
type StarQueue struct {
|
|
|
|
|
maxLength uint32
|
|
|
|
|
count int64
|
|
|
|
|
Encode bool
|
|
|
|
|
msgID uint16
|
|
|
|
|
msgPool chan MsgQueue
|
|
|
|
|
unFinMsg sync.Map
|
|
|
|
|
lastID int //= -1
|
|
|
|
|
Reserve uint16
|
|
|
|
|
Msgid uint16
|
|
|
|
|
MsgPool chan MsgQueue
|
|
|
|
|
UnFinMsg sync.Map
|
|
|
|
|
LastID int //= -1
|
|
|
|
|
ctx context.Context
|
|
|
|
|
cancel context.CancelFunc
|
|
|
|
|
duration time.Duration
|
|
|
|
@ -40,22 +37,21 @@ type StarQueue struct {
|
|
|
|
|
//restoreMu sync.Mutex
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewQueueCtx(ctx context.Context, count int64, maxMsgLength uint32) *StarQueue {
|
|
|
|
|
var q StarQueue
|
|
|
|
|
q.Encode = false
|
|
|
|
|
q.count = count
|
|
|
|
|
q.maxLength = maxMsgLength
|
|
|
|
|
q.msgPool = make(chan MsgQueue, count)
|
|
|
|
|
func NewQueueCtx(ctx context.Context, count int64) *StarQueue {
|
|
|
|
|
var que StarQueue
|
|
|
|
|
que.Encode = false
|
|
|
|
|
que.count = count
|
|
|
|
|
que.MsgPool = make(chan MsgQueue, count)
|
|
|
|
|
if ctx == nil {
|
|
|
|
|
q.ctx, q.cancel = context.WithCancel(context.Background())
|
|
|
|
|
que.ctx, que.cancel = context.WithCancel(context.Background())
|
|
|
|
|
} else {
|
|
|
|
|
q.ctx, q.cancel = context.WithCancel(ctx)
|
|
|
|
|
que.ctx, que.cancel = context.WithCancel(ctx)
|
|
|
|
|
}
|
|
|
|
|
q.duration = 0
|
|
|
|
|
return &q
|
|
|
|
|
que.duration = 0
|
|
|
|
|
return &que
|
|
|
|
|
}
|
|
|
|
|
func NewQueueWithCount(count int64) *StarQueue {
|
|
|
|
|
return NewQueueCtx(nil, count, 0)
|
|
|
|
|
return NewQueueCtx(nil, count)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewQueue 建立一个新消息队列
|
|
|
|
@ -98,27 +94,27 @@ func ByteToUint16(src []byte) uint16 {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// BuildMessage 生成编码后的信息用于发送
|
|
|
|
|
func (q *StarQueue) BuildMessage(src []byte) []byte {
|
|
|
|
|
func (que *StarQueue) BuildMessage(src []byte) []byte {
|
|
|
|
|
var buff bytes.Buffer
|
|
|
|
|
q.msgID++
|
|
|
|
|
if q.Encode {
|
|
|
|
|
src = q.EncodeFunc(src)
|
|
|
|
|
que.Msgid++
|
|
|
|
|
if que.Encode {
|
|
|
|
|
src = que.EncodeFunc(src)
|
|
|
|
|
}
|
|
|
|
|
length := uint32(len(src))
|
|
|
|
|
buff.Write(header)
|
|
|
|
|
buff.Write(Uint32ToByte(length))
|
|
|
|
|
buff.Write(Uint16ToByte(q.msgID))
|
|
|
|
|
buff.Write(Uint16ToByte(que.Msgid))
|
|
|
|
|
buff.Write(src)
|
|
|
|
|
return buff.Bytes()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// BuildHeader 生成编码后的Header用于发送
|
|
|
|
|
func (q *StarQueue) BuildHeader(length uint32) []byte {
|
|
|
|
|
func (que *StarQueue) BuildHeader(length uint32) []byte {
|
|
|
|
|
var buff bytes.Buffer
|
|
|
|
|
q.msgID++
|
|
|
|
|
que.Msgid++
|
|
|
|
|
buff.Write(header)
|
|
|
|
|
buff.Write(Uint32ToByte(length))
|
|
|
|
|
buff.Write(Uint16ToByte(q.msgID))
|
|
|
|
|
buff.Write(Uint16ToByte(que.Msgid))
|
|
|
|
|
return buff.Bytes()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -130,18 +126,18 @@ type unFinMsg struct {
|
|
|
|
|
RecvMsg []byte
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (q *StarQueue) push2list(msg MsgQueue) {
|
|
|
|
|
q.msgPool <- msg
|
|
|
|
|
func (que *StarQueue) push2list(msg MsgQueue) {
|
|
|
|
|
que.MsgPool <- msg
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ParseMessage 用于解析收到的msg信息
|
|
|
|
|
func (q *StarQueue) ParseMessage(msg []byte, conn interface{}) error {
|
|
|
|
|
return q.parseMessage(msg, conn)
|
|
|
|
|
func (que *StarQueue) ParseMessage(msg []byte, conn interface{}) error {
|
|
|
|
|
return que.parseMessage(msg, conn)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// parseMessage 用于解析收到的msg信息
|
|
|
|
|
func (q *StarQueue) parseMessage(msg []byte, conn interface{}) error {
|
|
|
|
|
tmp, ok := q.unFinMsg.Load(conn)
|
|
|
|
|
func (que *StarQueue) parseMessage(msg []byte, conn interface{}) error {
|
|
|
|
|
tmp, ok := que.UnFinMsg.Load(conn)
|
|
|
|
|
if ok { //存在未完成的信息
|
|
|
|
|
lastMsg := tmp.(*unFinMsg)
|
|
|
|
|
headerLen := len(lastMsg.HeaderMsg)
|
|
|
|
@ -150,7 +146,7 @@ func (q *StarQueue) parseMessage(msg []byte, conn interface{}) error {
|
|
|
|
|
if len(msg) < 14-headerLen {
|
|
|
|
|
//加入header头并退出
|
|
|
|
|
lastMsg.HeaderMsg = bytesMerge(lastMsg.HeaderMsg, msg)
|
|
|
|
|
q.unFinMsg.Store(conn, lastMsg)
|
|
|
|
|
que.UnFinMsg.Store(conn, lastMsg)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
//获取14字节完整的header
|
|
|
|
@ -159,32 +155,28 @@ func (q *StarQueue) parseMessage(msg []byte, conn interface{}) error {
|
|
|
|
|
//检查收到的header是否为认证header
|
|
|
|
|
//若不是,丢弃并重新来过
|
|
|
|
|
if !checkHeader(lastMsg.HeaderMsg[0:8]) {
|
|
|
|
|
q.unFinMsg.Delete(conn)
|
|
|
|
|
que.UnFinMsg.Delete(conn)
|
|
|
|
|
if len(msg) == 0 {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return q.parseMessage(msg, conn)
|
|
|
|
|
return que.parseMessage(msg, conn)
|
|
|
|
|
}
|
|
|
|
|
//获得本数据包长度
|
|
|
|
|
lastMsg.LengthRecv = ByteToUint32(lastMsg.HeaderMsg[8:12])
|
|
|
|
|
if q.maxLength != 0 && lastMsg.LengthRecv > q.maxLength {
|
|
|
|
|
q.unFinMsg.Delete(conn)
|
|
|
|
|
return fmt.Errorf("msg length is %d ,too large than %d", lastMsg.LengthRecv, q.maxLength)
|
|
|
|
|
}
|
|
|
|
|
//获得本数据包ID
|
|
|
|
|
lastMsg.ID = ByteToUint16(lastMsg.HeaderMsg[12:14])
|
|
|
|
|
//存入列表
|
|
|
|
|
q.unFinMsg.Store(conn, lastMsg)
|
|
|
|
|
que.UnFinMsg.Store(conn, lastMsg)
|
|
|
|
|
msg = msg[14-headerLen:]
|
|
|
|
|
if uint32(len(msg)) < lastMsg.LengthRecv {
|
|
|
|
|
lastMsg.RecvMsg = msg
|
|
|
|
|
q.unFinMsg.Store(conn, lastMsg)
|
|
|
|
|
que.UnFinMsg.Store(conn, lastMsg)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
if uint32(len(msg)) >= lastMsg.LengthRecv {
|
|
|
|
|
lastMsg.RecvMsg = msg[0:lastMsg.LengthRecv]
|
|
|
|
|
if q.Encode {
|
|
|
|
|
lastMsg.RecvMsg = q.DecodeFunc(lastMsg.RecvMsg)
|
|
|
|
|
if que.Encode {
|
|
|
|
|
lastMsg.RecvMsg = que.DecodeFunc(lastMsg.RecvMsg)
|
|
|
|
|
}
|
|
|
|
|
msg = msg[lastMsg.LengthRecv:]
|
|
|
|
|
storeMsg := MsgQueue{
|
|
|
|
@ -192,38 +184,38 @@ func (q *StarQueue) parseMessage(msg []byte, conn interface{}) error {
|
|
|
|
|
Msg: lastMsg.RecvMsg,
|
|
|
|
|
Conn: conn,
|
|
|
|
|
}
|
|
|
|
|
//q.restoreMu.Lock()
|
|
|
|
|
q.push2list(storeMsg)
|
|
|
|
|
//q.restoreMu.Unlock()
|
|
|
|
|
q.unFinMsg.Delete(conn)
|
|
|
|
|
return q.parseMessage(msg, conn)
|
|
|
|
|
//que.restoreMu.Lock()
|
|
|
|
|
que.push2list(storeMsg)
|
|
|
|
|
//que.restoreMu.Unlock()
|
|
|
|
|
que.UnFinMsg.Delete(conn)
|
|
|
|
|
return que.parseMessage(msg, conn)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
lastID := int(lastMsg.LengthRecv) - len(lastMsg.RecvMsg)
|
|
|
|
|
if lastID < 0 {
|
|
|
|
|
q.unFinMsg.Delete(conn)
|
|
|
|
|
return q.parseMessage(msg, conn)
|
|
|
|
|
que.UnFinMsg.Delete(conn)
|
|
|
|
|
return que.parseMessage(msg, conn)
|
|
|
|
|
}
|
|
|
|
|
if len(msg) >= lastID {
|
|
|
|
|
lastMsg.RecvMsg = bytesMerge(lastMsg.RecvMsg, msg[0:lastID])
|
|
|
|
|
if q.Encode {
|
|
|
|
|
lastMsg.RecvMsg = q.DecodeFunc(lastMsg.RecvMsg)
|
|
|
|
|
if que.Encode {
|
|
|
|
|
lastMsg.RecvMsg = que.DecodeFunc(lastMsg.RecvMsg)
|
|
|
|
|
}
|
|
|
|
|
storeMsg := MsgQueue{
|
|
|
|
|
ID: lastMsg.ID,
|
|
|
|
|
Msg: lastMsg.RecvMsg,
|
|
|
|
|
Conn: conn,
|
|
|
|
|
}
|
|
|
|
|
q.push2list(storeMsg)
|
|
|
|
|
q.unFinMsg.Delete(conn)
|
|
|
|
|
que.push2list(storeMsg)
|
|
|
|
|
que.UnFinMsg.Delete(conn)
|
|
|
|
|
if len(msg) == lastID {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
msg = msg[lastID:]
|
|
|
|
|
return q.parseMessage(msg, conn)
|
|
|
|
|
return que.parseMessage(msg, conn)
|
|
|
|
|
}
|
|
|
|
|
lastMsg.RecvMsg = bytesMerge(lastMsg.RecvMsg, msg)
|
|
|
|
|
q.unFinMsg.Store(conn, lastMsg)
|
|
|
|
|
que.UnFinMsg.Store(conn, lastMsg)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -236,8 +228,8 @@ func (q *StarQueue) parseMessage(msg []byte, conn interface{}) error {
|
|
|
|
|
}
|
|
|
|
|
msg = msg[start:]
|
|
|
|
|
lastMsg := unFinMsg{}
|
|
|
|
|
q.unFinMsg.Store(conn, &lastMsg)
|
|
|
|
|
return q.parseMessage(msg, conn)
|
|
|
|
|
que.UnFinMsg.Store(conn, &lastMsg)
|
|
|
|
|
return que.parseMessage(msg, conn)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func checkHeader(msg []byte) bool {
|
|
|
|
@ -283,19 +275,19 @@ func bytesMerge(src ...[]byte) []byte {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Restore 获取收到的信息
|
|
|
|
|
func (q *StarQueue) Restore() (MsgQueue, error) {
|
|
|
|
|
if q.duration.Seconds() == 0 {
|
|
|
|
|
q.duration = 86400 * time.Second
|
|
|
|
|
func (que *StarQueue) Restore() (MsgQueue, error) {
|
|
|
|
|
if que.duration.Seconds() == 0 {
|
|
|
|
|
que.duration = 86400 * time.Second
|
|
|
|
|
}
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case <-q.ctx.Done():
|
|
|
|
|
case <-que.ctx.Done():
|
|
|
|
|
return MsgQueue{}, errors.New("Stoped By External Function Call")
|
|
|
|
|
case <-time.After(q.duration):
|
|
|
|
|
if q.duration != 0 {
|
|
|
|
|
return MsgQueue{}, ErrDeadlineExceeded
|
|
|
|
|
case <-time.After(que.duration):
|
|
|
|
|
if que.duration != 0 {
|
|
|
|
|
return MsgQueue{}, os.ErrDeadlineExceeded
|
|
|
|
|
}
|
|
|
|
|
case data, ok := <-q.msgPool:
|
|
|
|
|
case data, ok := <-que.MsgPool:
|
|
|
|
|
if !ok {
|
|
|
|
|
return MsgQueue{}, os.ErrClosed
|
|
|
|
|
}
|
|
|
|
@ -305,21 +297,21 @@ func (q *StarQueue) Restore() (MsgQueue, error) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RestoreOne 获取收到的一个信息
|
|
|
|
|
// 兼容性修改
|
|
|
|
|
func (q *StarQueue) RestoreOne() (MsgQueue, error) {
|
|
|
|
|
return q.Restore()
|
|
|
|
|
//兼容性修改
|
|
|
|
|
func (que *StarQueue) RestoreOne() (MsgQueue, error) {
|
|
|
|
|
return que.Restore()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Stop 立即停止Restore
|
|
|
|
|
func (q *StarQueue) Stop() {
|
|
|
|
|
q.cancel()
|
|
|
|
|
func (que *StarQueue) Stop() {
|
|
|
|
|
que.cancel()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RestoreDuration Restore最大超时时间
|
|
|
|
|
func (q *StarQueue) RestoreDuration(tm time.Duration) {
|
|
|
|
|
q.duration = tm
|
|
|
|
|
func (que *StarQueue) RestoreDuration(tm time.Duration) {
|
|
|
|
|
que.duration = tm
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (q *StarQueue) RestoreChan() <-chan MsgQueue {
|
|
|
|
|
return q.msgPool
|
|
|
|
|
func (que *StarQueue) RestoreChan() <-chan MsgQueue {
|
|
|
|
|
return que.MsgPool
|
|
|
|
|
}
|
|
|
|
|