コード例 #1
0
        public void HandleRequestLease(Frame rx)
        {
            Guid  key   = new Guid(rx.Data);
            Lease lease = null;

            lock (leases)
            {
                if (leases.TryGetValue(key, out lease))
                {
                    //Extend lease
                    lease.Expire = DateTime.Now.AddSeconds(Settings.Items.LeaseTime);
                }
                else
                {
                    lease        = new Lease();
                    lease.Expire = DateTime.Now.AddSeconds(Settings.Items.LeaseTime);
                    lease.Key    = key;

                    //TODO: Reuse expired leases.
                    //TODO: Optimize this!
                    UInt16 id = Settings.Items.LeaseStartID;
                    while (leases.Any(a => a.Value.ID == id))
                    {
                        id++;
                    }
                    lease.ID = id;

                    leases[lease.Key] = lease;
                }
            }
            ReplyLease(lease);
            Logger.LOGI($"New lease accepted {lease.ToString()}");
        }