maeqtt/session/utils.go
2021-10-17 21:01:13 +02:00

41 lines
860 B
Go

package session
import (
"encoding/base64"
"fmt"
"math/rand"
"time"
"badat.dev/maeqtt/v2/mqtt/packets"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
func genClientID() *string {
buf := make([]byte, 32)
_, err := rand.Read(buf)
if err != nil {
// I don't think this can actually happen but just in case panic
panic(fmt.Errorf("Failed to generate a client id, %e", err))
}
id := "Client_rand_" + base64.URLEncoding.WithPadding(base64.NoPadding).EncodeToString(buf)
return &id
}
func (s *Session) Disconnect(code packets.DisconnectReasonCode) {
// If disconnetion fails that means we are already disconnected, great!
_ = s.Connection.sendPacket(packets.DisconnectPacket{
ReasonCode: code,
})
_ = s.Connection.close()
s.onDisconnect()
}
func (s *Session) getFreePacketId() uint16 {
s.freePacketID += 1
return s.freePacketID
}