public ModuleClient(ISdkModuleClient inner, TimeSpan idleTimeout, bool closeOnIdleTimeout, UpstreamProtocol protocol) { this.inner = Preconditions.CheckNotNull(inner, nameof(inner)); this.UpstreamProtocol = protocol; this.inactivityTimer = new ResettableTimer(this.CloseOnInactivity, idleTimeout, Events.Log, closeOnIdleTimeout); this.inactivityTimer.Start(); }
public async Task <IModuleClient> Create(ConnectionStatusChangesHandler statusChangedHandler) { ISdkModuleClient sdkModuleClient = await this.CreateSdkModuleClientWithRetry(statusChangedHandler); IModuleClient moduleClient = new ModuleClient(sdkModuleClient, this.idleTimeout, this.closeOnIdleTimeout); return(moduleClient); }
async Task <ISdkModuleClient> CreateSdkModuleClientWithRetry(ConnectionStatusChangesHandler statusChangedHandler) { try { ISdkModuleClient moduleClient = await ExecuteWithRetry( () => this.CreateSdkModuleClient(statusChangedHandler), Events.RetryingDeviceClientConnection); Events.DeviceClientCreated(); return(moduleClient); } catch (Exception e) { Events.DeviceClientSetupFailed(e); Environment.Exit(1); return(null); } }
async Task <ISdkModuleClient> CreateAndOpenSdkModuleClient(UpstreamProtocol upstreamProtocol, ConnectionStatusChangesHandler statusChangedHandler) { ITransportSettings settings = GetTransportSettings(upstreamProtocol, this.proxy); Events.AttemptingConnectionWithTransport(settings.GetTransportType()); ISdkModuleClient moduleClient = await this.connectionString .Map(cs => Task.FromResult(this.sdkModuleClientProvider.GetSdkModuleClient(cs, settings))) .GetOrElse(this.sdkModuleClientProvider.GetSdkModuleClient(settings)); moduleClient.SetProductInfo(this.productInfo); // note: it's important to set the status-changed handler and // timeout value *before* we open a connection to the hub moduleClient.SetOperationTimeoutInMilliseconds(ModuleClientTimeoutMilliseconds); moduleClient.SetConnectionStatusChangesHandler(statusChangedHandler); await moduleClient.OpenAsync(); Events.ConnectedWithTransport(settings.GetTransportType()); return(moduleClient); }