示例#1
0
            public Client(
                long id,
                ILiveUpdateReceiver liveUpdateReceiver)
            {
                _id              = id;
                _responseEvent   = new ManualResetEventSlim(false);
                _maximumWaitTime = TimeSpan.FromSeconds(60);
                _expiryTime      = TimeSpan.FromMinutes(30);
                _lock            = new object();

                _subscription = liveUpdateReceiver.Subscribe(OnMessageReceived);
                SetExpiry();
            }
示例#2
0
        public LiveUpdateService(ILiveUpdateReceiver liveUpdateReceiver)
        {
            _liveUpdateReceiver = liveUpdateReceiver;
            _connectedClients   = new Dictionary <long, Client>();

            // Randomize nextid so that if the server is restarted previously
            // connected clients will not likely kill sessions from newly
            // connected clients
            _nextId = new Random().Next();

            _expiryThread = new Thread(() =>
            {
                var count = 60;
                while (true)
                {
                    try
                    {
                        Thread.Sleep(1000);
                        if (--count == 0)
                        {
                            count = 60;
                            lock (_connectedClients)
                            {
                                var ids = _connectedClients.Keys.ToList();
                                foreach (var id in ids)
                                {
                                    var client = _connectedClients[id];
                                    if (client.Expired)
                                    {
#if DEBUG
                                        Trace.WriteLine("Removing expired LiveUpdate session #" + id);
#endif
                                        _connectedClients.Remove(id);
                                        client.Dispose();
                                    }
                                }
                            }
                        }
                    }
                    catch
                    { }
                }
            })
            {
                IsBackground = true,
                Priority     = ThreadPriority.BelowNormal,
                Name         = "LiveUpdate expiry"
            };

            _expiryThread.Start();
        }