Skip to content

Commit

Permalink
Add SessionPresent() to ConnectToken
Browse files Browse the repository at this point in the history
resovles #240
  • Loading branch information
alsm committed Oct 25, 2018
1 parent c237818 commit 1f58d8a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
14 changes: 7 additions & 7 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ func (c *client) Connect() Token {
}
cm.Write(c.conn)

rc = c.connect()
rc, t.sessionPresent = c.connect()
if rc != packets.Accepted {
if c.conn != nil {
c.conn.Close()
Expand Down Expand Up @@ -371,7 +371,7 @@ func (c *client) reconnect() {
}
cm.Write(c.conn)

rc = c.connect()
rc, _ = c.connect()
if rc != packets.Accepted {
c.conn.Close()
c.conn = nil
Expand Down Expand Up @@ -435,27 +435,27 @@ func (c *client) reconnect() {
// when the connection is first started.
// This prevents receiving incoming data while resume
// is in progress if clean session is false.
func (c *client) connect() byte {
func (c *client) connect() (byte, bool) {
DEBUG.Println(NET, "connect started")

ca, err := packets.ReadPacket(c.conn)
if err != nil {
ERROR.Println(NET, "connect got error", err)
return packets.ErrNetworkError
return packets.ErrNetworkError, false
}
if ca == nil {
ERROR.Println(NET, "received nil packet")
return packets.ErrNetworkError
return packets.ErrNetworkError, false
}

msg, ok := ca.(*packets.ConnackPacket)
if !ok {
ERROR.Println(NET, "received msg that was not CONNACK")
return packets.ErrNetworkError
return packets.ErrNetworkError, false
}

DEBUG.Println(NET, "received connack")
return msg.ReturnCode
return msg.ReturnCode, msg.SessionPresent
}

// Disconnect will end the connection with the server, but not before waiting
Expand Down
11 changes: 10 additions & 1 deletion token.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ func newToken(tType byte) tokenCompletor {
//required to provide information about calls to Connect()
type ConnectToken struct {
baseToken
returnCode byte
returnCode byte
sessionPresent bool
}

//ReturnCode returns the acknowlegement code in the connack sent
Expand All @@ -132,6 +133,14 @@ func (c *ConnectToken) ReturnCode() byte {
return c.returnCode
}

//SessionPresent returns a bool representing the value of the
//session present field in the connack sent in response to a Connect()
func (c *ConnectToken) SessionPresent() bool {
c.m.RLock()
defer c.m.RUnlock()
return c.sessionPresent
}

//PublishToken is an extension of Token containing the extra fields
//required to provide information about calls to Publish()
type PublishToken struct {
Expand Down

0 comments on commit 1f58d8a

Please sign in to comment.