| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 | 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;using MECF.Framework.Common.Event;namespace Aitex.Core.WCF{    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]    public class EventService : IEventService    {        ConcurrentDictionary<Guid, IEventServiceCallback> _callbackClientList = new ConcurrentDictionary<Guid, IEventServiceCallback>();        ConcurrentDictionary<Guid, bool> _isLockState = new ConcurrentDictionary<Guid, bool>();        public event Action<EventItem> OnEvent;        public event Action<bool> OnLockAndUnlockEvent;        private int _counter;        public EventService()        {        }        public void FireEvent(EventItem obj)        {            //DeviceTimer _timer = new DeviceTimer();            //_timer.Start(0);            //double t1 = 0;            //double t2 = 0;            //double t3 = 0;            //t1 = _timer.GetElapseTime();            Guid[] ids = _callbackClientList.Keys.ToArray();            for (int i = 0; i < ids.Length; i++)            {                try                {                    //t2 = _timer.GetElapseTime() - t1;                    _callbackClientList[ids[i]].SendEvent(obj);                    //t3 = _timer.GetElapseTime() - t2;                }                catch (Exception ex)                {                    LOG.Error(string.Format("给客户端{0}发送事件失败,客户端被删除.", ids[i]), ex);                    IEventServiceCallback service;                    _callbackClientList.TryRemove(ids[i], out service);                    bool lockState = false;                    _isLockState.TryRemove(ids[i], out lockState);                }            }            if (OnEvent != null)            {                OnEvent(obj);            }            //System.Diagnostics.Trace.WriteLine(string.Format("[{0}]  -----   [{1}]     --------- [{2}]",  t1, t2, t3));        }        public void SetLockAndUnLock(string cmd, Guid guid)        {            Guid[] ids = _callbackClientList.Keys.ToArray();            if (cmd == "Unlock")            {                try                {                    _callbackClientList.Where(x => x.Key == guid).FirstOrDefault().Value.SendLockEvent(false);                    _isLockState[guid] = false;                    if (OnLockAndUnlockEvent != null)                    {                        OnLockAndUnlockEvent(false);                    }                }                catch (Exception ex)                {                    LOG.Error(string.Format("给客户端{0}发送事件失败,客户端被删除.", guid), ex);                    IEventServiceCallback service;                    _callbackClientList.TryRemove(guid, out service);                    bool lockState = false;                    _isLockState.TryRemove(guid, out lockState);                }            }            if (cmd == "Lock")            {                for (int i = 0; i < ids.Length; i++)                {                    try                    {                        if (_isLockState[ids[i]] == true)                        {                            _callbackClientList[ids[i]].SendLockEvent(_isLockState[ids[i]]);                        }                    }                    catch (Exception ex)                    {                        LOG.Error(string.Format("给客户端{0}发送事件失败,客户端被删除.", ids[i]), ex);                        IEventServiceCallback service;                        _callbackClientList.TryRemove(ids[i], out service);                        bool lockState = false;                        _isLockState.TryRemove(guid, out lockState);                    }                }                if (_isLockState.Where(x => x.Value == true).Count() == 0)                {                    try                    {                        _callbackClientList[guid].SendLockEvent(true);                        _isLockState[guid] = true;                        if (OnLockAndUnlockEvent != null)                        {                            OnLockAndUnlockEvent(true);                        }                    }                    catch (Exception)                    {                    }                                }                            }        }        public bool Register(Guid id)        {            try            {                if (!_callbackClientList.ContainsKey(id))                {                    LOG.Info(string.Format("客户端{0}已连接", id.ToString()), true);                }                _callbackClientList[id] = OperationContext.Current.GetCallbackChannel<IEventServiceCallback>();                               _isLockState[id] = false;            }            catch (Exception ex)            {                LOG.Error("监听事件发生错误", ex);            }            return true;        }        public void UnRegister(Guid id)        {            if (!_callbackClientList.ContainsKey(id))            {                LOG.Info(string.Format("客户端{0}断开连接", id.ToString()), true);            }            IEventServiceCallback service;            _callbackClientList.TryRemove(id, out service);            bool lockState = false;            _isLockState.TryRemove(id,out lockState);        }        public int Heartbeat()        {            return _counter++ % 100000;        }    }}
 |