Exemplo n.º 1
0
        /// <inheritdoc/>
        public bool Release(string name)
        {
            bool IsChannelStateOkForImmediateRelease(RealtimeChannel realtimeChannel)
            {
                var state = realtimeChannel.State;

                return(state == ChannelState.Initialized || state == ChannelState.Detached ||
                       state == ChannelState.Failed);
            }

            bool RemoveChannel()
            {
                if (Channels.TryRemove(name, out RealtimeChannel removedChannel))
                {
                    removedChannel.RemoveAllListeners();
                    _orderedChannels.Remove(removedChannel);
                    return(true);
                }

                return(false);
            }

            if (Logger.IsDebug)
            {
                Logger.Debug($"Releasing channel #{name}");
            }

            if (!Channels.TryGetValue(name, out RealtimeChannel channel))
            {
                return(false);
            }

            void DetachedCallback(bool detached, ErrorInfo error)
            {
                if (Logger.IsDebug)
                {
                    Logger.Debug(
                        error is null
                            ? $"Channel #{name} was removed from Channel list. Detached successfully: {detached}."
                            : $"Failed to cleanly detach channel #{name} before removing it from Channel list. Detach error: {error}.");
                }

                RemoveChannel();
            }

            if (IsChannelStateOkForImmediateRelease(channel))
            {
                return(RemoveChannel());
            }

            channel.Detach(DetachedCallback);

            return(true);
        }
Exemplo n.º 2
0
        private void Calculate()
        {
            //will prevent spam of location packets
            if (ExtDatetime.Utc - _lastCalcMs < RecalculateMinimalDelay)
            {
                return;
            }

            var allCreatures = Owner.Area.GetObjectsInRange(Owner.Position, MaximalVisibleRange);

            for (int i = 0; i < allCreatures.Count; i++)
            {
                var aVisibleObject = allCreatures[i];
                if (CanSeeObject(aVisibleObject))
                {
                    if (_objectsThatISee.AddIfNotPresent(aVisibleObject))
                    {
                        aVisibleObject.VisibleAi._objectThatSeeMe.AddIfNotPresent(Owner);
                    }
                }
                else if (_objectsThatISee.Remove(aVisibleObject))
                {
                    aVisibleObject.VisibleAi._objectThatSeeMe.Remove(Owner);
                }

                if (aVisibleObject.VisibleAi.CanSeeObject(Owner))
                {
                    if (aVisibleObject.VisibleAi._objectsThatISee.AddIfNotPresent(Owner))
                    {
                        _objectThatSeeMe.AddIfNotPresent(aVisibleObject);
                    }
                }
                else
                {
                    aVisibleObject.VisibleAi._objectsThatISee.Remove(Owner);
                    _objectThatSeeMe.Remove(aVisibleObject);
                }
            }

            _objectsThatISee.ParallelAction(element =>
            {
                if (!CanSeeObject(element))
                {
                    _objectsThatISee.Remove(element);
                    element.VisibleAi._objectThatSeeMe.Remove(element);
                }
            });

            _lastCalcMs = ExtDatetime.Utc;
        }
Exemplo n.º 3
0
            private void WatchThread()
            {
                for (; ;)
                {
                    long lockSize = 0;

                    foreach (var hash in new HashSet <Hash>(_infos.ToArray().Select(n => n.Hashes).Extract()))
                    {
                        lockSize += _cacheManager.GetLength(hash);
                    }

                    if (_cacheManager.Size * ((double)this.ProtectedPercentage / 100) > lockSize)
                    {
                        break;
                    }

                    var sortedList = _infos.ToArray().ToList();
                    sortedList.Sort((x, y) => x.CreationTime.CompareTo(y.CreationTime));

                    var info = sortedList.FirstOrDefault();
                    if (info == null)
                    {
                        break;
                    }

                    foreach (var hash in info.Hashes)
                    {
                        _cacheManager.Unlock(hash);
                    }

                    _infos.Remove(info);
                }
            }
Exemplo n.º 4
0
        /// <inheritdoc/>
        public bool Release(string name)
        {
            var result = _channels.TryRemove(name, out var channel);

            _orderedChannels.Remove(channel);
            return(result);
        }
        public void RemoveUser(PokerUser user)
        {
            var foundUser = users.FirstOrDefault(u => u.ChatId == user.ChatId);

            if (foundUser != null)
            {
                users.Remove((int)foundUser.ChatId);
            }
        }
Exemplo n.º 6
0
            public byte[] Create_1(byte[] value, int limit, TimeSpan computationTime)
            {
                if (value == null)
                {
                    throw new ArgumentNullException("value");
                }
                if (value.Length != 32)
                {
                    throw new ArgumentOutOfRangeException("value");
                }

                var info = new ProcessStartInfo(_path);

                info.CreateNoWindow         = true;
                info.UseShellExecute        = false;
                info.RedirectStandardOutput = true;

                {
                    if (limit < 0)
                    {
                        limit = -1;
                    }

                    int timeout;

                    if (computationTime < TimeSpan.Zero)
                    {
                        timeout = -1;
                    }
                    else
                    {
                        timeout = (int)computationTime.TotalSeconds;
                    }

                    info.Arguments = string.Format(
                        "hashcash1 create {0} {1} {2}",
                        NetworkConverter.ToHexString(value),
                        limit,
                        timeout);
                }

                using (var process = Process.Start(info))
                {
                    _processes.Add(process);

                    try
                    {
                        process.PriorityClass = ProcessPriorityClass.Idle;

                        try
                        {
                            var result = process.StandardOutput.ReadLine();

                            process.WaitForExit();
                            if (process.ExitCode != 0)
                            {
                                throw new MinerException();
                            }

                            return(NetworkConverter.FromHexString(result));
                        }
                        catch (MinerException)
                        {
                            throw;
                        }
                        catch (Exception e)
                        {
                            throw new MinerException(e.Message, e);
                        }
                    }
                    finally
                    {
                        _processes.Remove(process);
                    }
                }
            }
Exemplo n.º 7
0
 public bool RemoveFromArea(ABdoObject obj)
 {
     return(_objects.Remove(obj));
 }