コード例 #1
0
        /// <summary>
        /// Returns a DeviceConnection from the DeviceConnection pool. If a connection to the specified device does not exist in the pool one is created.
        /// </summary>
        /// <param name="deviceId">Name of the target device.</param>
        /// <param name="createdImplicitly">Indicates wether the connection was established explicitly by an application or implicitly through the CreateStream or CreateTunnel methods.</param>
        /// <returns>The DeviceConnection.</returns>
        DeviceConnection GetDeviceConnection(string deviceId, bool createdImplicitly)
        {
            DeviceConnection deviceConnection = null;

            WeakReference wr;

            if (deviceConnections.TryGetValue(deviceId, out wr))
            {
                deviceConnection = wr.Target as DeviceConnection;
            }

            if (deviceConnection == null)
            {
                deviceConnection = DeviceConnection.Create(this, deviceId, createdImplicitly);
            }
            else
            {
                if (deviceConnection.CreatedImplicitly != createdImplicitly)
                {
                    throw new NabtoClientException(NabtoStatus.Failed, "Can not connect to a device both explicitly and implicitly.");
                }
            }

            return(deviceConnection);
        }
コード例 #2
0
        internal void Unregister(DeviceConnection deviceConnection)
        {
            lock (deviceConnections)
            {
                Log.Write("Session.Unregister<DeviceConnection>()");

                deviceConnections.Remove(deviceConnection.DeviceId);
            }
        }
コード例 #3
0
        internal void Register(DeviceConnection deviceConnection)
        {
            lock (deviceConnections)
            {
                Log.Write("Session.Register<DeviceConnection>()");

                deviceConnections[deviceConnection.DeviceId] = new WeakReference(deviceConnection);
            }
        }
コード例 #4
0
        internal static DeviceConnection Create(Session owner, string deviceId, bool createdImplicitly)
        {
            if (deviceId == null)
            {
                throw new ArgumentNullException("deviceId");
            }

            var instance = new DeviceConnection(owner, deviceId, createdImplicitly);

            owner.Register(instance);

            return(instance);
        }