コード例 #1
0
        /// <inheritdoc/>
        public void ApplyVirtualHosts(Guid clientId, params KeyValuePair <Guid, VirtualHostSetting>[] settings)
        {
            var existed = clients.TryGetValue(clientId, out var currentSettingId);

            if (settings == null || settings.Length == 0)
            {
                if (!existed || currentSettingId != Guid.Empty)
                {
                    clients.AddOrUpdate(clientId, Guid.Empty, (i, j) => Guid.Empty);
                    if (IsStarted)
                    {
                        SendingRefreshClient(clientId, null);
                    }

                    if (existed)
                    {
                        clientTable.ClearVirtualHosts(clientId);
                    }
                }
            }
            else
            {
                int length = settings.Length;
                if (length > 65536)
                {
                    throw new InvalidDataException("Too many (>64k) settings applied.");
                }

                var newSettingId = Guid.NewGuid();

                clients.AddOrUpdate(clientId, newSettingId, (i, j) => newSettingId);

                if (IsStarted)
                {
                    byte[] newSetting = BuildSettingData(newSettingId, settings);

                    SendingRefreshClient(clientId, newSetting);
                }

                clientTable.AddOrUpdate(clientId, newSettingId, settings);
            }
        }
コード例 #2
0
        /// <inheritdoc/>
        void IRemoteHubAdapter.ApplyVirtualHosts(Guid clientId, params KeyValuePair <Guid, VirtualHostSetting>[] settings)
        {
            var currentSetting = clientTable.Get(clientId);

            var existed = currentSetting != null;

            if (existed && (settings == null || settings.Length == 0))
            {
                if (currentSetting.IsVirtualHostsDisabled)
                {
                    return;
                }
                else
                {
                    clientTable.ClearVirtualHosts(clientId);
                    if (isStarted && RemoteClientUpdated != null)
                    {
                        RemoteClientUpdated(this, new ClientWithVirtualHostSettingEventArgs(clientId, Guid.Empty, null));
                    }
                }
            }
            else if (settings == null || settings.Length == 0)
            {
                clientTable.AddOrUpdate(clientId);
                if (isStarted && RemoteClientUpdated != null)
                {
                    RemoteClientUpdated(this, new ClientWithVirtualHostSettingEventArgs(clientId, Guid.Empty, null));
                }
            }
            else
            {
                var newSettingId = Guid.NewGuid();
                clientTable.AddOrUpdate(clientId, newSettingId, settings);
                if (isStarted && RemoteClientUpdated != null)
                {
                    RemoteClientUpdated(this, new ClientWithVirtualHostSettingEventArgs(clientId, newSettingId, settings));
                }
            }
        }
コード例 #3
0
        void OnMainChannelReceived(RedisChannel channel, RedisValue value)
        {
            var texts = ((string)value).Split(':');

            if (texts[0] == "v1")
            {
                if (texts[1] == "Refresh")
                {
                    var clientId = Guid.Parse(texts[2]);
                    var seconds  = int.Parse(texts[3]);
                    clientTable.AddOrRefresh(clientId, seconds, out var currentVirtualHostSettingId, out bool isNewCreated);
                    if (texts[4] != "")
                    {
                        Guid virtualHostId = Guid.Parse(texts[4]);
                        if (currentVirtualHostSettingId != virtualHostId)
                        {
                            //new created with having-virtual-host
                            MainChannelPublishing("v1:NeedRefreshFull:" + texts[2]);
                            //delay sending RemoteClientUpdated until RefreshFull received.
                        }
                    }
                    else
                    {
                        if (currentVirtualHostSettingId != Guid.Empty)
                        {
                            //From having-virtual-host state to no-virtual-host state
                            clientTable.ClearVirtualHosts(clientId);
                            if (RemoteClientUpdated != null && !IsSelf(clientId))
                            {
                                RemoteClientUpdated(this, new ClientWithVirtualHostSettingEventArgs(clientId, Guid.Empty, null));
                            }
                        }
                        else if (isNewCreated)
                        {
                            //new created with no-virtual-host
                            if (RemoteClientUpdated != null && !IsSelf(clientId))
                            {
                                RemoteClientUpdated(this, new ClientWithVirtualHostSettingEventArgs(clientId, Guid.Empty, null));
                            }
                        }
                    }
                }
                else if (texts[1] == "RefreshFull")
                {
                    var clientId = Guid.Parse(texts[2]);
                    var seconds  = int.Parse(texts[3]);
                    clientTable.AddOrRefresh(clientId, seconds, out var currentVirtualHostSettingId, out bool isNewCreated);
                    if (texts[4] != "")
                    {
                        Guid virtualHostId = Guid.Parse(texts[4]);
                        if (currentVirtualHostSettingId != virtualHostId)
                        {
                            //From no-virtual-host state to having-virtual-host state, or change virtual host setting
                            var setting = clientTable.ApplyVirtualHosts(clientId, virtualHostId, texts[5]);
                            if (RemoteClientUpdated != null && !IsSelf(clientId))
                            {
                                RemoteClientUpdated(this, new ClientWithVirtualHostSettingEventArgs(clientId, virtualHostId, setting.ToArray()));
                            }
                        }
                    }
                    else
                    {
                        if (currentVirtualHostSettingId != Guid.Empty)
                        {
                            clientTable.ClearVirtualHosts(clientId);
                            //From having-virtual-host state to no-virtual-host state
                            if (RemoteClientUpdated != null && !IsSelf(clientId))
                            {
                                RemoteClientUpdated(this, new ClientWithVirtualHostSettingEventArgs(clientId, Guid.Empty, null));
                            }
                        }
                        else if (isNewCreated)
                        {
                            //new created with no-virtual-host
                            if (RemoteClientUpdated != null && !IsSelf(clientId))
                            {
                                RemoteClientUpdated(this, new ClientWithVirtualHostSettingEventArgs(clientId, Guid.Empty, null));
                            }
                        }
                    }
                }
                else if (texts[1] == "Hello")
                {
                    needRefreshFull = true;
                    var old = updatingRedisWaitingCancellation;
                    updatingRedisWaitingCancellation = new CancellationTokenSource();
                    old.Cancel();
                }
                else if (texts[1] == "NeedRefreshFull")
                {
                    var    clientId = Guid.Parse(texts[2]);
                    string refreshText;
                    if (clients.TryGetValue(clientId, out var client))
                    {
                        refreshText = client.CommandTextRefreshFull;
                    }
                    else
                    {
                        refreshText = null;
                    }
                    if (refreshText != null)
                    {
                        MainChannelPublishing(refreshText);
                    }
                }
                else if (texts[1] == "Shutdown")
                {
                    var clientId = Guid.Parse(texts[2]);
                    if (!IsSelf(clientId) && clientTable.Remove(clientId) && RemoteClientRemoved != null)
                    {
                        RemoteClientRemoved(this, new ClientIdEventArgs(clientId));
                    }
                }
            }
        }