Skip to content

Commit

Permalink
Fix race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
brutella committed Oct 22, 2020
1 parent 1cbe4c2 commit b18775b
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions hap/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/brutella/hc/characteristic"
"github.com/brutella/hc/crypto"
"net"
"sync"
)

// Session contains objects (encrypter, decrypter, pairing handler,...) used to handle the data communication.
Expand Down Expand Up @@ -42,6 +43,7 @@ type session struct {
pairStartHandler ContainerHandler
pairVerifyHandler PairVerifyHandler
connection net.Conn
mu *sync.Mutex
subs map[*characteristic.Characteristic]bool

// Temporary variable to reference next cryptographer
Expand All @@ -52,6 +54,7 @@ type session struct {
func NewSession(connection net.Conn) Session {
s := session{
connection: connection,
mu: &sync.Mutex{},
subs: map[*characteristic.Characteristic]bool{},
}

Expand Down Expand Up @@ -100,13 +103,19 @@ func (s *session) SetPairVerifyHandler(c PairVerifyHandler) {
}

func (s *session) IsSubscribedTo(ch *characteristic.Characteristic) bool {
s.mu.Lock()
defer s.mu.Unlock()
return s.subs[ch] == true
}

func (s *session) Subscribe(ch *characteristic.Characteristic) {
s.mu.Lock()
s.subs[ch] = true
s.mu.Unlock()
}

func (s *session) Unsubscribe(ch *characteristic.Characteristic) {
s.mu.Lock()
s.subs[ch] = false
s.mu.Unlock()
}

0 comments on commit b18775b

Please sign in to comment.