// This is the FreeSWITCH APP interface callback handler which is bound to "coreclr" APP commands private static void NativeAPPHandler(IntPtr sessionptr, string data) { using ManagedSession session = new ManagedSession(new SWIGTYPE_p_switch_core_session_t(sessionptr, false)); string args = data; if (!ParseArgument(ref args, out string command, ' ')) { Log.WriteLine(LogLevel.Error, "Missing Managed APP"); return; } Log.WriteLine(LogLevel.Info, "Managed APP: {0} {1}", command, args); if (!sAPPRegistry.TryGetValue(command.ToLower(), out APPCallback callback)) { Log.WriteLine(LogLevel.Error, "Managed APP does not exist"); return; } try { callback(args, session); } catch (Exception) { // TODO: Log more of the exception data out Log.WriteLine(LogLevel.Error, "Managed APP exception"); } }
/// <summary> /// Constructor. /// </summary> /// <param name="session">The low-level FreeSWITCH session.</param> internal CallSession(ManagedSession session) { this.session = session; this.collectedDigits = string.Empty; this.AutoHangup = true; // Setup the low-level event handlers. session.DtmfReceivedFunction = (digit, duration) => { if (DtmfReceived == null) { return(string.Empty); } var args = new DtmfInputEventArgs(digit, duration); DtmfReceived(this, args); if (args.Break) { return("break"); } else { return(string.Empty); } }; session.EventReceivedFunction = (evt) => { if (EventReceived != null) { EventReceived(this, new SwitchEventArgs(new SwitchEvent(evt.InternalEvent))); } return(string.Empty); }; session.HangupFunction = () => { if (HangupEvent != null) { HangupEvent(this, new HangupEventArgs(this.HangupReason, this.CallDetails)); } }; }
// Managed API for loading user assemblies and reflecting on attributes to register callbacks private static string LoadAPI(string args, ManagedSession session) { string path = Path.GetFullPath(args); if (!Path.HasExtension(path)) { path = Path.ChangeExtension(path, ".dll"); } if (!File.Exists(path)) { Log.WriteLine(LogLevel.Error, "File not found: {0}", path); return("-ERROR File not found"); } // TODO: Load the assembly, kick off reflection scan for relevant attributes, add API's to sAPIRegistry return("+OK " + path); }
// This is the FreeSWITCH API interface callback handler which is bound to "coreclr" API commands private static int NativeAPIHandler(string command, IntPtr sessionptr, IntPtr streamptr) { using ManagedSession session = new ManagedSession(new SWIGTYPE_p_switch_core_session_t(sessionptr, false)); using Stream stream = new Stream(new SWIGTYPE_p_switch_stream_handle_t(streamptr, false)); string args = command; if (!ParseArgument(ref args, out command, ' ')) { Log.WriteLine(LogLevel.Error, "Missing Managed API"); stream.Write("-ERROR Missing Managed API"); return(0); } Log.WriteLine(LogLevel.Info, "Managed API: {0} {1}", command, args); if (!sAPIRegistry.TryGetValue(command.ToLower(), out APICallback callback)) { Log.WriteLine(LogLevel.Error, "Managed API does not exist"); stream.Write("-ERROR Managed API does not exist"); return(0); } string result = null; try { result = callback(args, session); } catch (Exception) { // TODO: Log more of the exception data out Log.WriteLine(LogLevel.Error, "Managed API exception"); result = "-ERROR Managed API exception"; } if (result != null) { stream.Write(result); } return(0); }