|
|
|
@ -56,6 +56,7 @@ type Session struct {
@@ -56,6 +56,7 @@ type Session struct {
|
|
|
|
|
subscriptions map[string]*Session |
|
|
|
|
subscribers map[string]*Session |
|
|
|
|
disconnected bool |
|
|
|
|
replaced bool |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
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) {
@@ -85,16 +86,13 @@ func (s *Session) authenticated() (authenticated bool) {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (s *Session) Subscribe(session *Session) { |
|
|
|
|
|
|
|
|
|
s.mutex.Lock() |
|
|
|
|
s.subscriptions[session.Id] = session |
|
|
|
|
s.mutex.Unlock() |
|
|
|
|
session.AddSubscriber(s) |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (s *Session) Unsubscribe(id string) { |
|
|
|
|
|
|
|
|
|
s.mutex.Lock() |
|
|
|
|
if session, ok := s.subscriptions[id]; ok { |
|
|
|
|
delete(s.subscriptions, id) |
|
|
|
@ -103,7 +101,6 @@ func (s *Session) Unsubscribe(id string) {
@@ -103,7 +101,6 @@ func (s *Session) Unsubscribe(id string) {
|
|
|
|
|
} else { |
|
|
|
|
s.mutex.Unlock() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (s *Session) AddSubscriber(session *Session) { |
|
|
|
@ -209,33 +206,39 @@ func (s *Session) Close() {
@@ -209,33 +206,39 @@ func (s *Session) Close() {
|
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
outgoing := &DataOutgoing{ |
|
|
|
|
From: s.Id, |
|
|
|
|
A: s.attestation.Token(), |
|
|
|
|
Data: &DataSession{ |
|
|
|
|
Type: "Left", |
|
|
|
|
Id: s.Id, |
|
|
|
|
Status: "hard", |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
// TODO(longsleep): Verify that it is ok to not do all this when replaced is true.
|
|
|
|
|
if !s.replaced { |
|
|
|
|
|
|
|
|
|
if s.Hello { |
|
|
|
|
// NOTE(lcooper): If we don't check for Hello here, we could deadlock
|
|
|
|
|
// when implicitly creating a room while a user is reconnecting.
|
|
|
|
|
s.Broadcaster.Broadcast(s.Id, s.Roomid, outgoing) |
|
|
|
|
s.RoomStatusManager.LeaveRoom(s.Roomid, s.Id) |
|
|
|
|
} |
|
|
|
|
outgoing := &DataOutgoing{ |
|
|
|
|
From: s.Id, |
|
|
|
|
A: s.attestation.Token(), |
|
|
|
|
Data: &DataSession{ |
|
|
|
|
Type: "Left", |
|
|
|
|
Id: s.Id, |
|
|
|
|
Status: "hard", |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for _, session := range s.subscribers { |
|
|
|
|
s.Unicaster.Unicast(session.Id, outgoing) |
|
|
|
|
} |
|
|
|
|
if s.Hello { |
|
|
|
|
// NOTE(lcooper): If we don't check for Hello here, we could deadlock
|
|
|
|
|
// when implicitly creating a room while a user is reconnecting.
|
|
|
|
|
s.Broadcaster.Broadcast(s.Id, s.Roomid, outgoing) |
|
|
|
|
s.RoomStatusManager.LeaveRoom(s.Roomid, s.Id) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for _, session := range s.subscriptions { |
|
|
|
|
session.RemoveSubscriber(s.Id) |
|
|
|
|
} |
|
|
|
|
for _, session := range s.subscribers { |
|
|
|
|
s.Unicaster.Unicast(session.Id, outgoing) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for _, session := range s.subscriptions { |
|
|
|
|
session.RemoveSubscriber(s.Id) |
|
|
|
|
s.Unicaster.Unicast(session.Id, outgoing) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
s.SessionManager.DestroySession(s.Id, s.userid) |
|
|
|
|
s.buddyImages.Delete(s.Id) |
|
|
|
|
s.SessionManager.DestroySession(s.Id, s.userid) |
|
|
|
|
s.buddyImages.Delete(s.Id) |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
s.subscriptions = make(map[string]*Session) |
|
|
|
|
s.subscribers = make(map[string]*Session) |
|
|
|
@ -244,6 +247,27 @@ func (s *Session) Close() {
@@ -244,6 +247,27 @@ func (s *Session) Close() {
|
|
|
|
|
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 { |
|
|
|
|
s.mutex.Lock() |
|
|
|
|
defer s.mutex.Unlock() |
|
|
|
|