-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSessionUsersTracker.cs
169 lines (143 loc) · 5.5 KB
/
SessionUsersTracker.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using System;
using System.Collections.Generic;
using HoloToolkit.Unity;
namespace HoloToolkit.Sharing
{
/// <summary>
/// Keeps track of the users in the current session.
/// Instance is created by Sharing Stage when a connection is found.
/// </summary>
public class SessionUsersTracker : IDisposable
{
/// <summary>
/// UserJoined event notifies when a user joins the current session.
/// </summary>
public event Action<User> UserJoined;
/// <summary>
/// UserLeft event notifies when a user leaves the current session.
/// </summary>
public event Action<User> UserLeft;
/// <summary>
/// Local cached pointer to the sessions tracker..
/// </summary>
private readonly ServerSessionsTracker serverSessionsTracker;
/// <summary>
/// List of users that are in the current session.
/// </summary>
public List<User> CurrentUsers { get; private set; }
public SessionUsersTracker(ServerSessionsTracker sessionsTracker)
{
CurrentUsers = new List<User>();
serverSessionsTracker = sessionsTracker;
serverSessionsTracker.CurrentUserJoined += OnCurrentUserJoinedSession;
serverSessionsTracker.CurrentUserLeft += OnCurrentUserLeftSession;
serverSessionsTracker.UserJoined += OnUserJoinedSession;
serverSessionsTracker.UserLeft += OnUserLeftSession;
}
/// <summary>
/// Finds and returns an object representing a user who has the supplied id number. Returns null if the user is not found.
/// </summary>
/// <param name="userId">The numerical id of the session User to find</param>
/// <returns>The User with the specified id or null (if not found)</returns>
public User GetUserById(int userId)
{
for (int u = 0; u < CurrentUsers.Count; u++)
{
User user = CurrentUsers[u];
if (user.GetID() == userId)
{
return user;
}
}
return null;
}
#region IDisposable
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
serverSessionsTracker.CurrentUserJoined -= OnCurrentUserJoinedSession;
serverSessionsTracker.CurrentUserLeft -= OnCurrentUserLeftSession;
serverSessionsTracker.UserJoined -= OnUserJoinedSession;
serverSessionsTracker.UserLeft -= OnUserLeftSession;
}
}
#endregion
private void OnCurrentUserJoinedSession(Session joinedSession)
{
//Debug.LogFormat("Joining session {0}.", joinedSession.GetName());
// If joining a new session, any user in the previous session (if any) have left
ClearCurrentSession();
// Send a join event for every user currently in the session we joined
for (int i = 0; i < joinedSession.GetUserCount(); i++)
{
User user = joinedSession.GetUser(i);
CurrentUsers.Add(user);
UserJoined.RaiseEvent(user);
}
}
private void OnCurrentUserLeftSession(Session leftSession)
{
//Debug.Log("Left current session.");
// If we leave a session, notify that every user has left the current session of this app
ClearCurrentSession();
}
private void OnUserJoinedSession(Session session, User user)
{
if (!session.IsJoined())
{
return;
}
if (!CurrentUsers.Contains(user))
{
// Remove any old users with the same ID
for (int i = CurrentUsers.Count - 1; i >= 0; i--)
{
if (CurrentUsers[i].GetID() == user.GetID())
{
CurrentUsers.RemoveAt(i);
}
}
CurrentUsers.Add(user);
UserJoined.RaiseEvent(user);
// Debug.LogFormat("User {0} joined current session.", user.GetName());
}
}
private void OnUserLeftSession(Session session, User user)
{
if (!session.IsJoined())
{
return;
}
for (int i = CurrentUsers.Count - 1; i >= 0; i--)
{
if (CurrentUsers[i].GetID() == user.GetID())
{
CurrentUsers.RemoveAt(i);
UserLeft.RaiseEvent(user);
// Debug.LogFormat("User {0} left current session.", user.GetName());
}
}
}
/// <summary>
/// Clears the current session, removing any users being tracked.
/// This should be called whenever the current session changes, to reset this class
/// and handle a new curren session.
/// </summary>
private void ClearCurrentSession()
{
for (int i = 0; i < CurrentUsers.Count; i++)
{
UserLeft.RaiseEvent(CurrentUsers[i]);
}
CurrentUsers.Clear();
}
}
}