コード例 #1
0
        /// <summary>
        /// Attaches an adapter to this switch. Starts the adapter if the switch is started.
        /// </summary>
        /// <param name="adapter">Adapter to be attached.</param>
        public void AddAdapter(IRemoteHubAdapter<byte[]> adapter)
        {
            var idList = new ConcurrentDictionary<Guid, DateTime>();
            if (adapters.TryAdd(adapter, idList))
            {
                adapter.AddOnMessageReceivedCallback(OnPrivateMessageReceivedCallback);

                adapter.RemoteClientUpdated += OnAdapterRemoteClientUpdated;
                adapter.RemoteClientRemoved += OnAdapterRemoteClientRemoved;

                foreach (var remoteClientId in adapter.GetAllRemoteClients())
                {
                    adapter.TryGetVirtualHosts(remoteClientId, out var settings);
                    AddAdapterToAdapterOfClients(remoteClientId, adapter, settings);
                    idList[remoteClientId] = DateTime.Now;
                }
            }
            else
            {
                return;
            }

            if (ConnectionErrorOccurredInternal != null)
            {
                adapter.ConnectionErrorOccurred += OnAdapterConnectionErrorOccurred;
            }
            if (AdapterStartedInternal != null)
            {
                adapter.AdapterStarted += OnAdapterStarted;
            }
            if (AdapterStoppedInternal != null)
            {
                adapter.AdapterStopped += OnAdapterStopped;
            }

            //get clients id
            KeyValuePair<Guid, IRemoteHubAdapter<byte[]>>[] allClients;
            lock (adapterOfClients)
            {
                allClients = adapterOfClients.Where(i => i.Value != adapter).ToArray();
            }
            foreach (var client in allClients)
            {
                if (client.Value.TryGetVirtualHosts(client.Key, out var settings))
                {
                    adapter.ApplyVirtualHosts(client.Key, settings); //will add client if not existed.
                }
            }

            adapter.Start();

            AdapterAdded?.Invoke(this, new AdapterEventArgs(adapter));
        }