Пример #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));
        }
Пример #2
0
        /// <summary>Refreshes the device list and their state, and raises events.</summary>
        public static void Update()
        {
            configuration.Refresh();

            var adaptersToRefresh     = new List <DisplayDevice>(adaptersByDeviceName.Count);
            var removedAdapters       = new List <string>(adaptersByDeviceName.Keys);
            var newAdapters           = new List <DisplayAdapter>();
            var primaryAdapterChanged = false;

            DisplayAdapter adapter;
            var            displayDevices = DisplayAdapter.EnumDisplayDevices(null, false);
            var            aMax           = displayDevices.Count;

            for (var a = 0; a < aMax; ++a)
            {
                var displayDevice = displayDevices[a];

                if (adaptersByDeviceName.TryGetValue(displayDevice.DeviceName, out adapter))
                {
                    adaptersToRefresh.Add(displayDevice);
                    removedAdapters.Remove(adapter.DeviceName);
                }
                else
                {
                    adapter = new DisplayAdapter(displayDevice);
                    GetDisplayConfigInfo(adapter);
                    adaptersByDeviceName.Add(adapter.DeviceName, adapter);
                    newAdapters.Add(adapter);
                }

                if (adapter.State.HasFlag(DisplayAdapterStateIndicators.PrimaryDevice) && (primaryAdapterDeviceName != adapter.DeviceName))
                {
                    primaryAdapterChanged    = (primaryAdapterDeviceName != null);
                    primaryAdapterDeviceName = displayDevice.DeviceName;
                }
            }

            while (removedAdapters.Count > 0)
            {
                var s = removedAdapters[0];
                removedAdapters.RemoveAt(0);

                adapter = adaptersByDeviceName[s];
                adaptersByDeviceName.Remove(s);

                adapter.OnRemoved();
                AdapterRemoved?.Invoke(null, new DisplayDeviceManagerEventArgs(adapter));
            }

            while (newAdapters.Count > 0)
            {
                AdapterAdded?.Invoke(null, new DisplayDeviceManagerEventArgs(newAdapters[0]));
                newAdapters.RemoveAt(0);
            }

            while (adaptersToRefresh.Count > 0)
            {
                var device = adaptersToRefresh[0];
                adaptersToRefresh.RemoveAt(0);
                if (adaptersByDeviceName.TryGetValue(device.DeviceName, out adapter))
                {
                    adapter.Refresh(ref device);
                    GetDisplayConfigInfo(adapter);
                }
            }


            if (primaryAdapterChanged)
            {
                PrimaryAdapterChanged?.Invoke(null, EventArgs.Empty);
            }

            isInitialized = true;
        }