mirror of
https://github.com/fhmq/hmq.git
synced 2026-04-26 19:48:34 +00:00
modify
This commit is contained in:
130
broker/comm.go
Normal file
130
broker/comm.go
Normal file
@@ -0,0 +1,130 @@
|
||||
package broker
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"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
|
||||
)
|
||||
|
||||
func SubscribeTopicCheckAndSpilt(subject []byte) ([]string, error) {
|
||||
|
||||
topic := string(subject)
|
||||
|
||||
if bytes.IndexByte(subject, '#') != -1 {
|
||||
if bytes.IndexByte(subject, '#') != len(subject)-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(subject []byte) ([]string, error) {
|
||||
if bytes.IndexByte(subject, '#') != -1 || bytes.IndexByte(subject, '+') != -1 {
|
||||
return nil, errors.New("Publish Topic format error with + and #")
|
||||
}
|
||||
topic := string(subject)
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user