IPCNotifier.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using InABox.API;
  2. using InABox.Core;
  3. namespace InABox.IPC
  4. {
  5. public class IPCPusher : IPusher
  6. {
  7. IPCPushState PushState { get; set; }
  8. public IPCPusher(IPCPushState pushState)
  9. {
  10. PushState = pushState;
  11. PushState.OnPoll += PushState_OnPoll;
  12. }
  13. private void PushState_OnPoll(IPCPushState.Session session)
  14. {
  15. PushManager.Poll(session.SessionID);
  16. }
  17. public IEnumerable<Guid> GetSessions(Platform platform)
  18. {
  19. return PushState.SessionMap.Where(x => x.Value.Platform == platform).Select(x => x.Key);
  20. }
  21. public IEnumerable<Guid> GetUserSessions(Guid userID)
  22. {
  23. return CredentialsCache.GetUserSessions(userID);
  24. }
  25. public void PushToAll<TPush>(TPush push) where TPush : BaseObject
  26. {
  27. foreach(var session in PushState.SessionMap.Values)
  28. {
  29. session.Connection.WriteAsync(IPCMessage.Push(push)).ContinueWith(task =>
  30. {
  31. if(task.Exception != null)
  32. {
  33. Logger.Send(LogType.Error, "", $"Error in push: {CoreUtils.FormatException(task.Exception)}");
  34. }
  35. });
  36. }
  37. }
  38. public void PushToSession<TPush>(Guid sessionID, TPush push) where TPush : BaseObject
  39. {
  40. if(PushState.SessionMap.TryGetValue(sessionID, out var session))
  41. {
  42. session.Connection.WriteAsync(IPCMessage.Push(push)).ContinueWith(task =>
  43. {
  44. if(task.Exception != null)
  45. {
  46. Logger.Send(LogType.Error, "", $"Error in push: {CoreUtils.FormatException(task.Exception)}");
  47. }
  48. });
  49. }
  50. }
  51. public void PushToSession(Guid sessionID, Type TPush, BaseObject push)
  52. {
  53. if(PushState.SessionMap.TryGetValue(sessionID, out var session))
  54. {
  55. session.Connection.WriteAsync(IPCMessage.Push(TPush, push)).ContinueWith(task =>
  56. {
  57. if(task.Exception != null)
  58. {
  59. Logger.Send(LogType.Error, "", $"Error in push: {CoreUtils.FormatException(task.Exception)}");
  60. }
  61. });
  62. }
  63. }
  64. }
  65. }