INotificationService.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using Ryujinx.Common;
  2. using Ryujinx.HLE.HOS.Ipc;
  3. using Ryujinx.HLE.HOS.Kernel.Common;
  4. using Ryujinx.HLE.HOS.Kernel.Threading;
  5. using Ryujinx.HLE.Utilities;
  6. using System;
  7. using System.Collections.Generic;
  8. using static Ryujinx.HLE.HOS.ErrorCode;
  9. namespace Ryujinx.HLE.HOS.Services.Friend
  10. {
  11. class INotificationService : IpcService, IDisposable
  12. {
  13. private readonly UInt128 _userId;
  14. private readonly FriendServicePermissionLevel _permissionLevel;
  15. private readonly object _lock = new object();
  16. private KEvent _notificationEvent;
  17. private int _notificationEventHandle = 0;
  18. private LinkedList<NotificationInfo> _notifications;
  19. private bool _hasNewFriendRequest;
  20. private bool _hasFriendListUpdate;
  21. private Dictionary<int, ServiceProcessRequest> _commands;
  22. public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => _commands;
  23. public INotificationService(ServiceCtx context, UInt128 userId, FriendServicePermissionLevel permissionLevel)
  24. {
  25. _commands = new Dictionary<int, ServiceProcessRequest>
  26. {
  27. { 0, GetEvent }, // 2.0.0+
  28. { 1, Clear }, // 2.0.0+
  29. { 2, Pop }, // 2.0.0+
  30. };
  31. _userId = userId;
  32. _permissionLevel = permissionLevel;
  33. _notifications = new LinkedList<NotificationInfo>();
  34. _notificationEvent = new KEvent(context.Device.System);
  35. _hasNewFriendRequest = false;
  36. _hasFriendListUpdate = false;
  37. NotificationEventHandler.Instance.RegisterNotificationService(this);
  38. }
  39. // nn::friends::detail::ipc::INotificationService::GetEvent() -> handle<copy>
  40. public long GetEvent(ServiceCtx context)
  41. {
  42. if (_notificationEventHandle == 0)
  43. {
  44. if (context.Process.HandleTable.GenerateHandle(_notificationEvent.ReadableEvent, out _notificationEventHandle) != KernelResult.Success)
  45. {
  46. throw new InvalidOperationException("Out of handles!");
  47. }
  48. }
  49. context.Response.HandleDesc = IpcHandleDesc.MakeCopy(_notificationEventHandle);
  50. return 0;
  51. }
  52. // nn::friends::detail::ipc::INotificationService::Clear()
  53. public long Clear(ServiceCtx context)
  54. {
  55. lock (_lock)
  56. {
  57. _hasNewFriendRequest = false;
  58. _hasFriendListUpdate = false;
  59. _notifications.Clear();
  60. }
  61. return 0;
  62. }
  63. // nn::friends::detail::ipc::INotificationService::Pop() -> nn::friends::detail::ipc::SizedNotificationInfo
  64. public long Pop(ServiceCtx context)
  65. {
  66. lock (_lock)
  67. {
  68. if (_notifications.Count >= 1)
  69. {
  70. NotificationInfo notificationInfo = _notifications.First.Value;
  71. _notifications.RemoveFirst();
  72. if (notificationInfo.Type == NotificationEventType.FriendListUpdate)
  73. {
  74. _hasFriendListUpdate = false;
  75. }
  76. else if (notificationInfo.Type == NotificationEventType.NewFriendRequest)
  77. {
  78. _hasNewFriendRequest = false;
  79. }
  80. context.ResponseData.WriteStruct(notificationInfo);
  81. return 0;
  82. }
  83. }
  84. return MakeError(ErrorModule.Friends, FriendError.NotificationQueueEmpty);
  85. }
  86. public void SignalFriendListUpdate(UInt128 targetId)
  87. {
  88. lock (_lock)
  89. {
  90. if (_userId == targetId)
  91. {
  92. if (!_hasFriendListUpdate)
  93. {
  94. NotificationInfo friendListNotification = new NotificationInfo();
  95. if (_notifications.Count != 0)
  96. {
  97. friendListNotification = _notifications.First.Value;
  98. _notifications.RemoveFirst();
  99. }
  100. friendListNotification.Type = NotificationEventType.FriendListUpdate;
  101. _hasFriendListUpdate = true;
  102. if (_hasNewFriendRequest)
  103. {
  104. NotificationInfo newFriendRequestNotification = new NotificationInfo();
  105. if (_notifications.Count != 0)
  106. {
  107. newFriendRequestNotification = _notifications.First.Value;
  108. _notifications.RemoveFirst();
  109. }
  110. newFriendRequestNotification.Type = NotificationEventType.NewFriendRequest;
  111. _notifications.AddFirst(newFriendRequestNotification);
  112. }
  113. // We defer this to make sure we are on top of the queue.
  114. _notifications.AddFirst(friendListNotification);
  115. }
  116. _notificationEvent.ReadableEvent.Signal();
  117. }
  118. }
  119. }
  120. public void SignalNewFriendRequest(UInt128 targetId)
  121. {
  122. lock (_lock)
  123. {
  124. if ((_permissionLevel & FriendServicePermissionLevel.OverlayMask) != 0 && _userId == targetId)
  125. {
  126. if (!_hasNewFriendRequest)
  127. {
  128. if (_notifications.Count == 100)
  129. {
  130. SignalFriendListUpdate(targetId);
  131. }
  132. NotificationInfo newFriendRequestNotification = new NotificationInfo
  133. {
  134. Type = NotificationEventType.NewFriendRequest
  135. };
  136. _notifications.AddLast(newFriendRequestNotification);
  137. _hasNewFriendRequest = true;
  138. }
  139. _notificationEvent.ReadableEvent.Signal();
  140. }
  141. }
  142. }
  143. public void Dispose()
  144. {
  145. NotificationEventHandler.Instance.UnregisterNotificationService(this);
  146. }
  147. }
  148. }