public async Task <string> SetupAsync() { if (SetupCompleted) { return(mProtocol); } return(await Task.Run(async() => { if (mClient.Session.State != UpstreamSession.SessionState.Running) { string message = "Setup failed because the session is not running"; Log(LogLevel.Error, message); throw new InvalidOperationException(message); } if (!await CheckProtocolAvailableAsync("signalwire", TimeSpan.FromSeconds(5))) { string message = "Setup failed due to timeout waiting for protocol 'signalwire'"; Log(LogLevel.Error, message); throw new TimeoutException(message); } SetupParams setupParams = new SetupParams(); if (!string.IsNullOrWhiteSpace(mProtocol)) { setupParams.Protocol = mProtocol; } Task <ResponseTaskResult <ExecuteResult> > setupTask = mClient.Session.ExecuteAsync("signalwire", "setup", setupParams); ResponseTaskResult <ExecuteResult> setupTaskResult = await setupTask; if (setupTask.IsFaulted) { Log(LogLevel.Error, string.Format("Setup Faulted\n{0}", setupTask.Exception)); throw setupTask.Exception; } SetupResult setupResult = setupTaskResult.Result.ResultAs <SetupResult>(); if (!await CheckProtocolAvailableAsync(setupResult.Protocol, TimeSpan.FromSeconds(5))) { string message = string.Format("Setup failed due to timeout waiting for protocol '{0}'", setupResult.Protocol); Log(LogLevel.Error, message); throw new TimeoutException(message); } mClient.Session.RegisterSubscriptionHandler(setupResult.Protocol, "notifications", (s, r, p) => OnNotification?.Invoke(mClient, p)); Task <ResponseTaskResult <SubscriptionResult> > subscriptionTask = mClient.Session.SubscriptionAddAsync(setupResult.Protocol, "notifications"); ResponseTaskResult <SubscriptionResult> subscriptionTaskResult = await subscriptionTask; if (subscriptionTask.IsFaulted) { Log(LogLevel.Error, string.Format("Setup subscription faulted\n{0}", subscriptionTask.Exception)); throw subscriptionTask.Exception; } // @todo check subscribe_channels mProtocol = setupResult.Protocol; mSetupCompleted = true; return mProtocol; })); }
protected async Task <string> LL_SetupAsync() { return(await Task.Run(async() => { if (mClient.Session.State != Blade.UpstreamSession.SessionState.Running) { string message = string.Format("Setup failed for '{0}' because the session is not running", mService); mLogger.LogError(message); throw new InvalidOperationException(message); } if (!await CheckProtocolAvailableAsync("signalwire", TimeSpan.FromSeconds(5))) { string message = string.Format("Setup failed for '{0}' due to timeout waiting for protocol 'signalwire'", mService); mLogger.LogError(message); throw new TimeoutException(message); } SetupParams setupParams = new SetupParams() { Service = mService }; if (!string.IsNullOrWhiteSpace(mProtocol)) { setupParams.Protocol = mProtocol; } Task <ResponseTaskResult <ExecuteResult> > setupTask = mClient.Session.ExecuteAsync("signalwire", "setup", setupParams); ResponseTaskResult <ExecuteResult> setupTaskResult = await setupTask; if (setupTask.IsFaulted) { mLogger.LogError("Setup fault for '{0}', {1}", mService, setupTask.Exception); throw setupTask.Exception; } SetupResult setupResult = setupTaskResult.Result.ResultAs <SetupResult>(); if (!await CheckProtocolAvailableAsync(setupResult.Protocol, TimeSpan.FromSeconds(5))) { string message = string.Format("Setup failed for '{0}' due to timeout waiting for protocol '{1}'", mService, setupResult.Protocol); mLogger.LogError(message); throw new TimeoutException(message); } mClient.Session.RegisterSubscriptionHandler(setupResult.Protocol, "notifications", (s, r, p) => OnEvent(mClient, p)); Task <ResponseTaskResult <SubscriptionResult> > subscriptionTask = mClient.Session.SubscriptionAddAsync(setupResult.Protocol, "notifications"); ResponseTaskResult <SubscriptionResult> subscriptionTaskResult = await subscriptionTask; if (subscriptionTask.IsFaulted) { mLogger.LogError("Setup subscription fault for '{0}', {1}", mService, subscriptionTask.Exception); throw subscriptionTask.Exception; } // @todo check subscribe_channels mProtocol = setupResult.Protocol; return mProtocol; })); }