From dc543d81b09ef24a40f7d37d7362053ae40a16e9 Mon Sep 17 00:00:00 2001 From: Andrew Schneider Date: Mon, 24 Aug 2020 21:07:58 -0700 Subject: [PATCH] Make connect flags more explicit 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. --- connect.go | 4 ++++ packets/connect.go | 42 +++++++++++++++++++++++++++++++++++------- subscribe.go | 3 --- 3 files changed, 39 insertions(+), 10 deletions(-) diff --git a/connect.go b/connect.go index b4712f2..8fc4b80 100644 --- a/connect.go +++ b/connect.go @@ -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 } diff --git a/packets/connect.go b/packets/connect.go index 281d780..3bb783a 100644 --- a/packets/connect.go +++ b/packets/connect.go @@ -4,9 +4,6 @@ import ( "bytes" "fmt" "io" - "os" - "strconv" - "time" ) type ConnectPacket struct { @@ -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) } diff --git a/subscribe.go b/subscribe.go index 05a143a..0f26039 100644 --- a/subscribe.go +++ b/subscribe.go @@ -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 }