1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.ServiceModel;
- using Aitex.Core.RT.Log;
- using Aitex.Core.RT.Event;
- using Aitex.Core.Util;
- using Aitex.Core.WCF.Interface;
- using System.Collections.Concurrent;
- namespace Aitex.Core.WCF
- {
- [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
- public class EventService : IEventService
- {
- ConcurrentDictionary<Guid, IEventServiceCallback> _callbackClientList = new ConcurrentDictionary<Guid, IEventServiceCallback>();
- public event Action<EventItem> OnEvent;
- private int _counter;
- public EventService()
- {
- }
- public void FireEvent(EventItem obj)
- {
-
-
-
-
-
-
-
- Guid[] ids = _callbackClientList.Keys.ToArray();
- for (int i = 0; i < ids.Length; i++)
- {
- try
- {
-
- _callbackClientList[ids[i]].SendEvent(obj);
-
- }
- catch (Exception ex)
- {
- LOG.WriteLog(eEvent.WARN_WINRESOURCE, "system", $"给客户端{ids[i]}发送事件失败,客户端被删除。");
- IEventServiceCallback service;
- _callbackClientList.TryRemove(ids[i], out service);
- }
- }
- if (OnEvent != null)
- {
- OnEvent(obj);
- }
-
- }
- public bool Register(Guid id)
- {
- try
- {
- if (!_callbackClientList.ContainsKey(id))
- {
-
- }
- _callbackClientList[id] = OperationContext.Current.GetCallbackChannel<IEventServiceCallback>();
- }
- catch (Exception ex)
- {
- LOG.WriteExeption(ex);
- }
- return true;
- }
- public void UnRegister(Guid id)
- {
- if (!_callbackClientList.ContainsKey(id))
- {
-
- }
- IEventServiceCallback service;
- _callbackClientList.TryRemove(id, out service);
- }
- public int Heartbeat()
- {
- return _counter++ % 100000;
- }
- }
- }
|