mirror of
https://github.com/fhmq/hmq.git
synced 2026-04-24 10:38:34 +00:00
147 lines
2.9 KiB
Go
147 lines
2.9 KiB
Go
/* Copyright (c) 2018, joy.zhou <chowyu08@gmail.com>
|
|
*/
|
|
package broker
|
|
|
|
import (
|
|
"crypto/md5"
|
|
"crypto/rand"
|
|
"encoding/base64"
|
|
"encoding/hex"
|
|
"errors"
|
|
"io"
|
|
"reflect"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
// ACCEPT_MIN_SLEEP is the minimum acceptable sleep times on temporary errors.
|
|
ACCEPT_MIN_SLEEP = 100 * time.Millisecond
|
|
// ACCEPT_MAX_SLEEP is the maximum acceptable sleep times on temporary errors
|
|
ACCEPT_MAX_SLEEP = 10 * time.Second
|
|
// DEFAULT_ROUTE_CONNECT Route solicitation intervals.
|
|
DEFAULT_ROUTE_CONNECT = 5 * time.Second
|
|
// DEFAULT_TLS_TIMEOUT
|
|
DEFAULT_TLS_TIMEOUT = 5 * time.Second
|
|
)
|
|
|
|
const (
|
|
CONNECT = uint8(iota + 1)
|
|
CONNACK
|
|
PUBLISH
|
|
PUBACK
|
|
PUBREC
|
|
PUBREL
|
|
PUBCOMP
|
|
SUBSCRIBE
|
|
SUBACK
|
|
UNSUBSCRIBE
|
|
UNSUBACK
|
|
PINGREQ
|
|
PINGRESP
|
|
DISCONNECT
|
|
)
|
|
const (
|
|
QosAtMostOnce byte = iota
|
|
QosAtLeastOnce
|
|
QosExactlyOnce
|
|
QosFailure = 0x80
|
|
)
|
|
|
|
func SubscribeTopicCheckAndSpilt(topic string) ([]string, error) {
|
|
if strings.Index(topic, "#") != -1 && strings.Index(topic, "#") != len(topic)-1 {
|
|
return nil, errors.New("Topic format error with index of #")
|
|
}
|
|
re := strings.Split(topic, "/")
|
|
for i, v := range re {
|
|
if i != 0 && i != (len(re)-1) {
|
|
if v == "" {
|
|
return nil, errors.New("Topic format error with index of //")
|
|
}
|
|
if strings.Contains(v, "+") && v != "+" {
|
|
return nil, errors.New("Topic format error with index of +")
|
|
}
|
|
} else {
|
|
if v == "" {
|
|
re[i] = "/"
|
|
}
|
|
}
|
|
}
|
|
return re, nil
|
|
|
|
}
|
|
|
|
func PublishTopicCheckAndSpilt(topic string) ([]string, error) {
|
|
if strings.Index(topic, "#") != -1 || strings.Index(topic, "+") != -1 {
|
|
return nil, errors.New("Publish Topic format error with + and #")
|
|
}
|
|
re := strings.Split(topic, "/")
|
|
for i, v := range re {
|
|
if v == "" {
|
|
if i != 0 && i != (len(re)-1) {
|
|
return nil, errors.New("Topic format error with index of //")
|
|
} else {
|
|
re[i] = "/"
|
|
}
|
|
}
|
|
|
|
}
|
|
return re, nil
|
|
}
|
|
|
|
func equal(k1, k2 interface{}) bool {
|
|
if reflect.TypeOf(k1) != reflect.TypeOf(k2) {
|
|
return false
|
|
}
|
|
|
|
if reflect.ValueOf(k1).Kind() == reflect.Func {
|
|
return &k1 == &k2
|
|
}
|
|
|
|
if k1 == k2 {
|
|
return true
|
|
}
|
|
switch k1 := k1.(type) {
|
|
case string:
|
|
return k1 == k2.(string)
|
|
case int64:
|
|
return k1 == k2.(int64)
|
|
case int32:
|
|
return k1 == k2.(int32)
|
|
case int16:
|
|
return k1 == k2.(int16)
|
|
case int8:
|
|
return k1 == k2.(int8)
|
|
case int:
|
|
return k1 == k2.(int)
|
|
case float32:
|
|
return k1 == k2.(float32)
|
|
case float64:
|
|
return k1 == k2.(float64)
|
|
case uint:
|
|
return k1 == k2.(uint)
|
|
case uint8:
|
|
return k1 == k2.(uint8)
|
|
case uint16:
|
|
return k1 == k2.(uint16)
|
|
case uint32:
|
|
return k1 == k2.(uint32)
|
|
case uint64:
|
|
return k1 == k2.(uint64)
|
|
case uintptr:
|
|
return k1 == k2.(uintptr)
|
|
}
|
|
return false
|
|
}
|
|
|
|
func GenUniqueId() string {
|
|
b := make([]byte, 48)
|
|
if _, err := io.ReadFull(rand.Reader, b); err != nil {
|
|
return ""
|
|
}
|
|
h := md5.New()
|
|
h.Write([]byte(base64.URLEncoding.EncodeToString(b)))
|
|
return hex.EncodeToString(h.Sum(nil))
|
|
// return GetMd5String()
|
|
}
|