starainrt/net.go

143 lines
2.9 KiB
Go
Raw Permalink Normal View History

2019-09-04 16:00:23 +08:00
package starainrt
import (
"bytes"
2019-09-26 10:51:13 +08:00
"errors"
2019-09-04 16:00:23 +08:00
)
2019-11-14 10:11:27 +08:00
/*
SecretKey 通信加密Key不应当被修改
*/
2019-09-26 10:51:13 +08:00
const SecretKey string = "1996victorique1127B612BTXL"
2019-09-04 16:00:23 +08:00
2019-09-26 10:51:13 +08:00
var header []byte = []byte{11, 27, 19, 96}
var Crypto *StarCrypto
2019-09-04 16:00:23 +08:00
2019-09-26 10:51:13 +08:00
func init() {
Crypto = new(StarCrypto)
2019-09-04 16:00:23 +08:00
}
2019-09-26 10:51:13 +08:00
type StarQueue struct {
Encode bool
Msgid uint16
MsgPool []MsgUsed
UnFinMsg []byte
LastID int //= -1
2019-09-04 16:00:23 +08:00
}
2019-09-26 10:51:13 +08:00
func NewQueue() *StarQueue {
que := new(StarQueue)
que.LastID = -1
que.Encode = true
return que
2019-09-04 16:00:23 +08:00
}
2019-09-26 10:51:13 +08:00
func (this *StarQueue) BuildMessage(str string) []byte {
var msg []byte
var buffer bytes.Buffer
if this.Encode {
msg = Crypto.VicqueEncodeV1([]byte(str), SecretKey)
2019-09-04 16:00:23 +08:00
} else {
2019-09-26 10:51:13 +08:00
msg = []byte(str)
}
buffer.Write([]byte(Crypto.CRC32(msg)))
buffer.Write([]byte{byte(this.Msgid >> 8), byte(this.Msgid)})
buffer.Write(msg)
lens := len(buffer.Bytes())
if lens > 65535 {
return nil
}
msg = make([]byte, lens)
copy(msg, buffer.Bytes())
buffer.Reset()
ulens := uint16(lens)
buffer.Write(header)
buffer.Write([]byte{byte(ulens >> 8), byte(ulens)})
buffer.Write(msg)
this.Msgid++
return buffer.Bytes()
2019-09-04 16:00:23 +08:00
}
2019-09-26 10:51:13 +08:00
type MsgUsed struct {
ID uint16
Msg string
Crc32 string
2019-11-14 10:11:27 +08:00
Conn interface{}
2019-09-04 16:00:23 +08:00
}
2019-11-14 10:11:27 +08:00
func (this *StarQueue) ParseMessage(msg []byte, conn interface{}) int {
2019-09-26 10:51:13 +08:00
var buffer bytes.Buffer
buffer.Write(this.UnFinMsg)
buffer.Write(msg)
msg = buffer.Bytes()
if len(msg) <= 6 {
this.UnFinMsg = msg
return -2
}
if msg[0] != byte(11) {
this.UnFinMsg = []byte{}
//resend last
return this.LastID + 1
}
if msg[1] != byte(27) || msg[2] != byte(19) || msg[3] != byte(96) {
//resend last
this.UnFinMsg = []byte{}
return this.LastID + 1
}
length := uint16(uint(msg[4])<<uint(8) + uint(msg[5]))
if 6+length > uint16(len(msg)) {
this.UnFinMsg = msg
return -2
2019-09-04 16:00:23 +08:00
} else {
2019-09-26 10:51:13 +08:00
this.UnFinMsg = []byte{}
strmsg := msg[6 : length+6]
crc := strmsg[0:8]
id := strmsg[8:10]
strmsg = strmsg[10:]
if Crypto.CRC32([]byte(strmsg)) != string(crc) {
//resend last
this.UnFinMsg = []byte{}
return this.LastID + 1
} else {
if this.Encode {
strmsg = Crypto.VicqueDecodeV1(strmsg, SecretKey)
2019-09-04 16:00:23 +08:00
}
2019-11-14 10:11:27 +08:00
msgs := MsgUsed{uint16(uint(id[0])<<8 + uint(id[1])), string(strmsg), string(crc), conn}
2019-09-26 10:51:13 +08:00
this.LastID = int(msgs.ID)
this.MsgPool = append(this.MsgPool, msgs)
}
if 6+length == uint16(len(msg)) {
return -2
2019-09-04 16:00:23 +08:00
}
2019-09-26 10:51:13 +08:00
msg = msg[length+6:]
2019-11-14 10:11:27 +08:00
return this.ParseMessage(msg, conn)
2019-09-04 16:00:23 +08:00
}
2019-09-26 10:51:13 +08:00
return -2
2019-09-04 16:00:23 +08:00
}
2019-09-26 10:51:13 +08:00
func (this *StarQueue) Restore(n int) ([]MsgUsed, error) {
if n > len(this.MsgPool) {
return nil, errors.New("N is Too Large")
2019-09-04 16:00:23 +08:00
}
2019-09-26 10:51:13 +08:00
tmp := this.MsgPool[0:n]
if n != len(this.MsgPool) {
this.MsgPool = this.MsgPool[n:]
2019-09-04 16:00:23 +08:00
} else {
2019-09-26 10:51:13 +08:00
this.MsgPool = []MsgUsed{}
2019-09-04 16:00:23 +08:00
}
2019-09-26 10:51:13 +08:00
return tmp, nil
2019-09-04 16:00:23 +08:00
}
2019-09-26 10:51:13 +08:00
func (this *StarQueue) RestoreOne() (MsgUsed, error) {
if len(this.MsgPool) == 0 {
return MsgUsed{}, errors.New("N is Too Large")
2019-09-04 16:00:23 +08:00
}
2019-09-26 10:51:13 +08:00
tmp := this.MsgPool[0]
if 1 != len(this.MsgPool) {
this.MsgPool = this.MsgPool[1:]
2019-09-04 16:00:23 +08:00
} else {
2019-09-26 10:51:13 +08:00
this.MsgPool = []MsgUsed{}
2019-09-04 16:00:23 +08:00
}
2019-09-26 10:51:13 +08:00
return tmp, nil
2019-09-04 16:00:23 +08:00
}