private async Task <AnswerResult> InternalAnswerAsync() { await API.API.SetupAsync(); AnswerResult resultAnswer = new AnswerResult(); TaskCompletionSource <bool> tcsCompletion = new TaskCompletionSource <bool>(); // Hook callbacks temporarily to catch required events AnsweredCallback answeredCallback = (a, c, e, p) => { resultAnswer.Event = new Event(e.EventType, JObject.FromObject(p)); tcsCompletion.SetResult(true); }; EndedCallback endedCallback = (a, c, e, p) => { resultAnswer.Event = new Event(e.EventType, JObject.FromObject(p)); tcsCompletion.SetResult(false); }; OnAnswered += answeredCallback; OnEnded += endedCallback; try { Task <LL_AnswerResult> taskLLAnswer = mAPI.LL_AnswerAsync(new LL_AnswerParams() { NodeID = mNodeID, CallID = mID, }); // The use of await rethrows exceptions from the task LL_AnswerResult resultLLAnswer = await taskLLAnswer; if (resultLLAnswer.Code == "200") { mLogger.LogDebug("Answer for call {0} waiting for completion events", ID); resultAnswer.Successful = await tcsCompletion.Task; mLogger.LogDebug("Answer for call {0} {1}", ID, resultAnswer.Successful ? "successful" : "unsuccessful"); } } catch (Exception exc) { mLogger.LogError(exc, "Answer for call {0} exception", ID); } // Unhook temporary callbacks OnAnswered -= answeredCallback; OnEnded -= endedCallback; return(resultAnswer); }
private async Task <HangupResult> InternalHangupAsync(DisconnectReason reason = DisconnectReason.hangup) { await API.API.SetupAsync(); HangupResult resultHangup = new HangupResult(); TaskCompletionSource <bool> tcsCompletion = new TaskCompletionSource <bool>(); // Hook callbacks temporarily to catch required events EndedCallback endedCallback = (a, c, e, p) => { resultHangup.Event = new Event(e.EventType, JObject.FromObject(p)); resultHangup.Reason = p.EndReason.GetValueOrDefault(); tcsCompletion.SetResult(true); }; OnEnded += endedCallback; try { Task <LL_EndResult> taskLLEnd = mAPI.LL_EndAsync(new LL_EndParams() { NodeID = mNodeID, CallID = mID, Reason = reason, }); // The use of await rethrows exceptions from the task LL_EndResult resultLLEnd = await taskLLEnd; if (resultLLEnd.Code == "200") { mLogger.LogDebug("Hangup for call {0} waiting for completion events", ID); resultHangup.Successful = await tcsCompletion.Task; mLogger.LogDebug("Hangup for call {0} {1}", ID, resultHangup.Successful ? "successful" : "unsuccessful"); } } catch (Exception exc) { mLogger.LogError(exc, "Hangup for call {0} exception", ID); } // Unhook temporary callbacks OnEnded -= endedCallback; return(resultHangup); }
protected override async Task <DialResult> InternalDialAsync() { await API.API.SetupAsync(); DialResult resultDial = new DialResult(); TaskCompletionSource <bool> tcsCompletion = new TaskCompletionSource <bool>(); // Hook callbacks temporarily to catch required events AnsweredCallback answeredCallback = (a, c, e, p) => { resultDial.Event = new Event(e.EventType, JObject.FromObject(p)); resultDial.Call = c; tcsCompletion.SetResult(true); }; EndedCallback endedCallback = (a, c, e, p) => { resultDial.Event = new Event(e.EventType, JObject.FromObject(p)); tcsCompletion.SetResult(false); }; OnAnswered += answeredCallback; OnEnded += endedCallback; try { Task <LL_BeginResult> taskLLBegin = mAPI.LL_BeginAsync(new LL_BeginParams() { Device = new CallDevice() { Type = CallDevice.DeviceType.phone, Parameters = new CallDevice.PhoneParams() { ToNumber = To, FromNumber = From, Timeout = Timeout, }, }, TemporaryCallID = mTemporaryID, }); // The use of await rethrows exceptions from the task LL_BeginResult resultLLBegin = await taskLLBegin; if (resultLLBegin.Code == "200") { mLogger.LogDebug("Dial for call {0} waiting for completion events", ID); resultDial.Successful = await tcsCompletion.Task; mLogger.LogDebug("Dial for call {0} {1}", ID, resultDial.Successful ? "successful" : "unsuccessful"); } } catch (Exception exc) { mLogger.LogError(exc, "Dial for call {0} exception", ID); } // Unhook temporary callbacks OnAnswered -= answeredCallback; OnEnded -= endedCallback; return(resultDial); }