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); }
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); }