blob: 534bf63ed68eca029498bee3ad56e0b03038d433 (
plain) (
blame)
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
170
171
172
|
using Ryujinx.Horizon.Common;
using Ryujinx.Horizon.Sdk.Account;
using Ryujinx.Horizon.Sdk.OsTypes;
using Ryujinx.Horizon.Sdk.Sf;
using System;
using System.Collections.Generic;
namespace Ryujinx.Horizon.Sdk.Friends.Detail.Ipc
{
partial class NotificationService : INotificationService, IDisposable
{
private readonly NotificationEventHandler _notificationEventHandler;
private readonly Uid _userId;
private readonly FriendsServicePermissionLevel _permissionLevel;
private readonly object _lock = new();
private SystemEventType _notificationEvent;
private readonly LinkedList<SizedNotificationInfo> _notifications;
private bool _hasNewFriendRequest;
private bool _hasFriendListUpdate;
public NotificationService(NotificationEventHandler notificationEventHandler, Uid userId, FriendsServicePermissionLevel permissionLevel)
{
_notificationEventHandler = notificationEventHandler;
_userId = userId;
_permissionLevel = permissionLevel;
_notifications = new LinkedList<SizedNotificationInfo>();
Os.CreateSystemEvent(out _notificationEvent, EventClearMode.AutoClear, interProcess: true).AbortOnFailure();
_hasNewFriendRequest = false;
_hasFriendListUpdate = false;
notificationEventHandler.RegisterNotificationService(this);
}
[CmifCommand(0)]
public Result GetEvent([CopyHandle] out int eventHandle)
{
eventHandle = Os.GetReadableHandleOfSystemEvent(ref _notificationEvent);
return Result.Success;
}
[CmifCommand(1)]
public Result Clear()
{
lock (_lock)
{
_hasNewFriendRequest = false;
_hasFriendListUpdate = false;
_notifications.Clear();
}
return Result.Success;
}
[CmifCommand(2)]
public Result Pop(out SizedNotificationInfo sizedNotificationInfo)
{
lock (_lock)
{
if (_notifications.Count >= 1)
{
sizedNotificationInfo = _notifications.First.Value;
_notifications.RemoveFirst();
if (sizedNotificationInfo.Type == NotificationEventType.FriendListUpdate)
{
_hasFriendListUpdate = false;
}
else if (sizedNotificationInfo.Type == NotificationEventType.NewFriendRequest)
{
_hasNewFriendRequest = false;
}
return Result.Success;
}
}
sizedNotificationInfo = default;
return FriendResult.NotificationQueueEmpty;
}
public void SignalFriendListUpdate(Uid targetId)
{
lock (_lock)
{
if (_userId == targetId)
{
if (!_hasFriendListUpdate)
{
SizedNotificationInfo friendListNotification = new();
if (_notifications.Count != 0)
{
friendListNotification = _notifications.First.Value;
_notifications.RemoveFirst();
}
friendListNotification.Type = NotificationEventType.FriendListUpdate;
_hasFriendListUpdate = true;
if (_hasNewFriendRequest)
{
SizedNotificationInfo newFriendRequestNotification = new();
if (_notifications.Count != 0)
{
newFriendRequestNotification = _notifications.First.Value;
_notifications.RemoveFirst();
}
newFriendRequestNotification.Type = NotificationEventType.NewFriendRequest;
_notifications.AddFirst(newFriendRequestNotification);
}
// We defer this to make sure we are on top of the queue.
_notifications.AddFirst(friendListNotification);
}
Os.SignalSystemEvent(ref _notificationEvent);
}
}
}
public void SignalNewFriendRequest(Uid targetId)
{
lock (_lock)
{
if (_permissionLevel.HasFlag(FriendsServicePermissionLevel.ViewerMask) && _userId == targetId)
{
if (!_hasNewFriendRequest)
{
if (_notifications.Count == 100)
{
SignalFriendListUpdate(targetId);
}
SizedNotificationInfo newFriendRequestNotification = new()
{
Type = NotificationEventType.NewFriendRequest,
};
_notifications.AddLast(newFriendRequestNotification);
_hasNewFriendRequest = true;
}
Os.SignalSystemEvent(ref _notificationEvent);
}
}
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
_notificationEventHandler.UnregisterNotificationService(this);
}
}
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}
|