EventService.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.ServiceModel;
  6. using Aitex.Core.RT.Log;
  7. using Aitex.Core.RT.Event;
  8. using Aitex.Core.Util;
  9. using Aitex.Core.WCF.Interface;
  10. using System.Collections.Concurrent;
  11. using MECF.Framework.Common.Event;
  12. namespace Aitex.Core.WCF
  13. {
  14. [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
  15. public class EventService : IEventService
  16. {
  17. ConcurrentDictionary<Guid, IEventServiceCallback> _callbackClientList = new ConcurrentDictionary<Guid, IEventServiceCallback>();
  18. ConcurrentDictionary<Guid, bool> _isLockState = new ConcurrentDictionary<Guid, bool>();
  19. public event Action<EventItem> OnEvent;
  20. public event Action<bool> OnLockAndUnlockEvent;
  21. private int _counter;
  22. public EventService()
  23. {
  24. }
  25. public void FireEvent(EventItem obj)
  26. {
  27. //DeviceTimer _timer = new DeviceTimer();
  28. //_timer.Start(0);
  29. //double t1 = 0;
  30. //double t2 = 0;
  31. //double t3 = 0;
  32. //t1 = _timer.GetElapseTime();
  33. Guid[] ids = _callbackClientList.Keys.ToArray();
  34. for (int i = 0; i < ids.Length; i++)
  35. {
  36. try
  37. {
  38. //t2 = _timer.GetElapseTime() - t1;
  39. _callbackClientList[ids[i]].SendEvent(obj);
  40. //t3 = _timer.GetElapseTime() - t2;
  41. }
  42. catch (Exception ex)
  43. {
  44. LOG.Error(string.Format("给客户端{0}发送事件失败,客户端被删除.", ids[i]), ex);
  45. IEventServiceCallback service;
  46. _callbackClientList.TryRemove(ids[i], out service);
  47. bool lockState = false;
  48. _isLockState.TryRemove(ids[i], out lockState);
  49. }
  50. }
  51. if (OnEvent != null)
  52. {
  53. OnEvent(obj);
  54. }
  55. //System.Diagnostics.Trace.WriteLine(string.Format("[{0}] ----- [{1}] --------- [{2}]", t1, t2, t3));
  56. }
  57. public void SetLockAndUnLock(string cmd, Guid guid)
  58. {
  59. Guid[] ids = _callbackClientList.Keys.ToArray();
  60. if (cmd == "Unlock")
  61. {
  62. try
  63. {
  64. _callbackClientList.Where(x => x.Key == guid).FirstOrDefault().Value.SendLockEvent(false);
  65. _isLockState[guid] = false;
  66. if (OnLockAndUnlockEvent != null)
  67. {
  68. OnLockAndUnlockEvent(false);
  69. }
  70. }
  71. catch (Exception ex)
  72. {
  73. LOG.Error(string.Format("给客户端{0}发送事件失败,客户端被删除.", guid), ex);
  74. IEventServiceCallback service;
  75. _callbackClientList.TryRemove(guid, out service);
  76. bool lockState = false;
  77. _isLockState.TryRemove(guid, out lockState);
  78. }
  79. }
  80. if (cmd == "Lock")
  81. {
  82. for (int i = 0; i < ids.Length; i++)
  83. {
  84. try
  85. {
  86. if (_isLockState[ids[i]] == true)
  87. {
  88. _callbackClientList[ids[i]].SendLockEvent(_isLockState[ids[i]]);
  89. }
  90. }
  91. catch (Exception ex)
  92. {
  93. LOG.Error(string.Format("给客户端{0}发送事件失败,客户端被删除.", ids[i]), ex);
  94. IEventServiceCallback service;
  95. _callbackClientList.TryRemove(ids[i], out service);
  96. bool lockState = false;
  97. _isLockState.TryRemove(guid, out lockState);
  98. }
  99. }
  100. if (_isLockState.Where(x => x.Value == true).Count() == 0)
  101. {
  102. try
  103. {
  104. _callbackClientList[guid].SendLockEvent(true);
  105. _isLockState[guid] = true;
  106. if (OnLockAndUnlockEvent != null)
  107. {
  108. OnLockAndUnlockEvent(true);
  109. }
  110. }
  111. catch (Exception)
  112. {
  113. }
  114. }
  115. }
  116. }
  117. public bool Register(Guid id)
  118. {
  119. try
  120. {
  121. if (!_callbackClientList.ContainsKey(id))
  122. {
  123. LOG.Info(string.Format("客户端{0}已连接", id.ToString()), true);
  124. }
  125. _callbackClientList[id] = OperationContext.Current.GetCallbackChannel<IEventServiceCallback>();
  126. _isLockState[id] = false;
  127. }
  128. catch (Exception ex)
  129. {
  130. LOG.Error("监听事件发生错误", ex);
  131. }
  132. return true;
  133. }
  134. public void UnRegister(Guid id)
  135. {
  136. if (!_callbackClientList.ContainsKey(id))
  137. {
  138. LOG.Info(string.Format("客户端{0}断开连接", id.ToString()), true);
  139. }
  140. IEventServiceCallback service;
  141. _callbackClientList.TryRemove(id, out service);
  142. bool lockState = false;
  143. _isLockState.TryRemove(id,out lockState);
  144. }
  145. public int Heartbeat()
  146. {
  147. return _counter++ % 100000;
  148. }
  149. }
  150. }