|
|
|
@ -56,6 +56,7 @@ type Session struct { |
|
|
|
subscriptions map[string]*Session |
|
|
|
subscriptions map[string]*Session |
|
|
|
subscribers map[string]*Session |
|
|
|
subscribers map[string]*Session |
|
|
|
disconnected bool |
|
|
|
disconnected bool |
|
|
|
|
|
|
|
replaced bool |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func NewSession(manager SessionManager, unicaster Unicaster, broadcaster Broadcaster, rooms RoomStatusManager, buddyImages ImageCache, attestations *securecookie.SecureCookie, id, sid string) *Session { |
|
|
|
func NewSession(manager SessionManager, unicaster Unicaster, broadcaster Broadcaster, rooms RoomStatusManager, buddyImages ImageCache, attestations *securecookie.SecureCookie, id, sid string) *Session { |
|
|
|
@ -85,16 +86,13 @@ func (s *Session) authenticated() (authenticated bool) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (s *Session) Subscribe(session *Session) { |
|
|
|
func (s *Session) Subscribe(session *Session) { |
|
|
|
|
|
|
|
|
|
|
|
s.mutex.Lock() |
|
|
|
s.mutex.Lock() |
|
|
|
s.subscriptions[session.Id] = session |
|
|
|
s.subscriptions[session.Id] = session |
|
|
|
s.mutex.Unlock() |
|
|
|
s.mutex.Unlock() |
|
|
|
session.AddSubscriber(s) |
|
|
|
session.AddSubscriber(s) |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (s *Session) Unsubscribe(id string) { |
|
|
|
func (s *Session) Unsubscribe(id string) { |
|
|
|
|
|
|
|
|
|
|
|
s.mutex.Lock() |
|
|
|
s.mutex.Lock() |
|
|
|
if session, ok := s.subscriptions[id]; ok { |
|
|
|
if session, ok := s.subscriptions[id]; ok { |
|
|
|
delete(s.subscriptions, id) |
|
|
|
delete(s.subscriptions, id) |
|
|
|
@ -103,7 +101,6 @@ func (s *Session) Unsubscribe(id string) { |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
s.mutex.Unlock() |
|
|
|
s.mutex.Unlock() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (s *Session) AddSubscriber(session *Session) { |
|
|
|
func (s *Session) AddSubscriber(session *Session) { |
|
|
|
@ -209,6 +206,9 @@ func (s *Session) Close() { |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO(longsleep): Verify that it is ok to not do all this when replaced is true.
|
|
|
|
|
|
|
|
if !s.replaced { |
|
|
|
|
|
|
|
|
|
|
|
outgoing := &DataOutgoing{ |
|
|
|
outgoing := &DataOutgoing{ |
|
|
|
From: s.Id, |
|
|
|
From: s.Id, |
|
|
|
A: s.attestation.Token(), |
|
|
|
A: s.attestation.Token(), |
|
|
|
@ -232,11 +232,14 @@ func (s *Session) Close() { |
|
|
|
|
|
|
|
|
|
|
|
for _, session := range s.subscriptions { |
|
|
|
for _, session := range s.subscriptions { |
|
|
|
session.RemoveSubscriber(s.Id) |
|
|
|
session.RemoveSubscriber(s.Id) |
|
|
|
|
|
|
|
s.Unicaster.Unicast(session.Id, outgoing) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
s.SessionManager.DestroySession(s.Id, s.userid) |
|
|
|
s.SessionManager.DestroySession(s.Id, s.userid) |
|
|
|
s.buddyImages.Delete(s.Id) |
|
|
|
s.buddyImages.Delete(s.Id) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
s.subscriptions = make(map[string]*Session) |
|
|
|
s.subscriptions = make(map[string]*Session) |
|
|
|
s.subscribers = make(map[string]*Session) |
|
|
|
s.subscribers = make(map[string]*Session) |
|
|
|
s.disconnected = true |
|
|
|
s.disconnected = true |
|
|
|
@ -244,6 +247,27 @@ func (s *Session) Close() { |
|
|
|
s.mutex.Unlock() |
|
|
|
s.mutex.Unlock() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (s *Session) Replace(oldSession *Session) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
oldSession.mutex.Lock() |
|
|
|
|
|
|
|
if oldSession.disconnected { |
|
|
|
|
|
|
|
oldSession.mutex.Unlock() |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
s.mutex.Lock() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
s.subscriptions = oldSession.subscriptions |
|
|
|
|
|
|
|
s.subscribers = oldSession.subscribers |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
s.mutex.Unlock() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Mark old session as replaced.
|
|
|
|
|
|
|
|
oldSession.replaced = true |
|
|
|
|
|
|
|
oldSession.mutex.Unlock() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (s *Session) Update(update *SessionUpdate) uint64 { |
|
|
|
func (s *Session) Update(update *SessionUpdate) uint64 { |
|
|
|
s.mutex.Lock() |
|
|
|
s.mutex.Lock() |
|
|
|
defer s.mutex.Unlock() |
|
|
|
defer s.mutex.Unlock() |
|
|
|
|