45 lines
837 B
Go
45 lines
837 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) error {
|
||
|
s.Connection.sendPacket(packets.DisconnectPacket{
|
||
|
ReasonCode: code,
|
||
|
})
|
||
|
|
||
|
err := s.Connection.close()
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
s.onDisconnect()
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
|
||
|
func (s *Session) getFreePacketId() uint16 {
|
||
|
s.freePacketID += 1
|
||
|
return s.freePacketID
|
||
|
}
|