Skip to content

Commit

Permalink
Make connect flags more explicit
Browse files Browse the repository at this point in the history
Instead of just having a "2" for the connect flags I made it defined as a function. This should make it more clear what is being used now, and also make i easier to add more options later.
  • Loading branch information
andschneider committed Aug 25, 2020
1 parent 14977b2 commit dc543d8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
4 changes: 4 additions & 0 deletions connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ func (c *Client) Connect() error {
typeErrorResponseLogger(cp.Name(), r.Name(), r)
return fmt.Errorf("did not receive a CONNACK packet, got %s instead", r.Name())
}

// start a keepAlive process which will send Ping packets to prevent a disconnect
// TODO I don't think this should be called in here - should be a background thing for a Client
go c.keepAlivePing()
return nil
}

Expand Down
42 changes: 35 additions & 7 deletions packets/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import (
"bytes"
"fmt"
"io"
"os"
"strconv"
"time"
)

type ConnectPacket struct {
Expand All @@ -29,23 +26,54 @@ func (c *ConnectPacket) Name() string {
return c.name
}

func (c *ConnectPacket) CreateConnectPacket(keepAlive int8, clientId string) {

}

// CreatePacket creates a new packet with the appropriate FixedHeader.
// It sets default values where needed as well.
func (c *ConnectPacket) CreatePacket() {
c.FixedHeader = FixedHeader{PacketType: connectType}
c.ProtocolName = "MQTT"
c.ProtocolVersion = MQTT3
c.ConnectFlags = 2
c.ConnectFlags = connectFlags()
c.KeepAlive = defaultKeepAlive
c.ClientIdentifier = "goqtt"
// TODO hostName() is making testing hard as it changes each time CreatePacket is called
}

func hostName() string {
hostname, _ := os.Hostname()
return hostname + strconv.Itoa(time.Now().Second())
// connectFlags sets the Connect Flag byte in the ConnectPacket.
// It is normally used for specifying the desired behavior of the
// MQTT connection. However, currently goqtt limits these options
// to only the clean session flag (everything else is off). The
// clean session, as it's set to 1, tells the broker to disregard
// any previous Session information. The new Session will last as
// long as the network connection.
//
// For more information, please see section 3.1.2.3 of the MQTT
// 3.1.1 specification.
func connectFlags() byte {
// these are the optional flags. reserved should never be
// called, but the others can be combined.
const (
reserved = 1 << iota
cleanSession
willFlag
willQoS_1
willQoS_2
willRetain
password
username
)
//example := cleanSession | willFlag
return cleanSession
}

//func hostName() string {
// hostname, _ := os.Hostname()
// return hostname + strconv.Itoa(time.Now().Second())
//}

func (c *ConnectPacket) String() string {
return fmt.Sprintf("%v protocolname: %v protocolversion: %v connectflags: %08b clientid: %s", c.FixedHeader, c.ProtocolName, c.ProtocolVersion, c.ConnectFlags, c.ClientIdentifier)
}
Expand Down
3 changes: 0 additions & 3 deletions subscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ func (c *Client) Subscribe() error {
return fmt.Errorf("did not receive a SUBACK packet, got %s instead", r.Name())
}

// start a keepAlive process which will send Ping packets to prevent a disconnect
// TODO I don't think this should be called in here - should be a background thing for a Client
go c.keepAlivePing()
return nil
}

Expand Down

0 comments on commit dc543d8

Please sign in to comment.