/// <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); }
internal void Unregister(DeviceConnection deviceConnection) { lock (deviceConnections) { Log.Write("Session.Unregister<DeviceConnection>()"); deviceConnections.Remove(deviceConnection.DeviceId); } }
internal void Register(DeviceConnection deviceConnection) { lock (deviceConnections) { Log.Write("Session.Register<DeviceConnection>()"); deviceConnections[deviceConnection.DeviceId] = new WeakReference(deviceConnection); } }
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); }