提交 9bc2dccc authored 作者: Michael Giagnocavo's avatar Michael Giagnocavo

Move input and hangup delegates to reside on MonoSession instead of bound to app.


git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@8791 d0543943-73ff-0310-b7d9-9358b9ac24b2
上级 ce07c5b2
......@@ -24139,7 +24139,7 @@ SWIGEXPORT int SWIGSTDCALL CSharp_CoreSession_Originate(void * jarg1, void * jar
}
SWIGEXPORT int SWIGSTDCALL CSharp_CoreSession_speak(void * jarg1, char * jarg2) {
SWIGEXPORT int SWIGSTDCALL CSharp_CoreSession_Speak(void * jarg1, char * jarg2) {
int jresult ;
CoreSession *arg1 = (CoreSession *) 0 ;
char *arg2 = (char *) 0 ;
......@@ -24153,7 +24153,7 @@ SWIGEXPORT int SWIGSTDCALL CSharp_CoreSession_speak(void * jarg1, char * jarg2)
}
SWIGEXPORT void SWIGSTDCALL CSharp_CoreSession_set_tts_parms(void * jarg1, char * jarg2, char * jarg3) {
SWIGEXPORT void SWIGSTDCALL CSharp_CoreSession_SetTtsParameters(void * jarg1, char * jarg2, char * jarg3) {
CoreSession *arg1 = (CoreSession *) 0 ;
char *arg2 = (char *) 0 ;
char *arg3 = (char *) 0 ;
......@@ -24305,7 +24305,7 @@ SWIGEXPORT int SWIGSTDCALL CSharp_CoreSession_flushDigits(void * jarg1) {
}
SWIGEXPORT int SWIGSTDCALL CSharp_CoreSession_setAutoHangup(void * jarg1, unsigned int jarg2) {
SWIGEXPORT int SWIGSTDCALL CSharp_CoreSession_SetAutoHangup(void * jarg1, unsigned int jarg2) {
int jresult ;
CoreSession *arg1 = (CoreSession *) 0 ;
bool arg2 ;
......
......@@ -43,60 +43,6 @@ namespace FreeSWITCH
protected static void Unload() { }
void hangupCallback()
{
Log.WriteLine(LogLevel.Debug, "AppFunction is in hangupCallback.");
try {
abortRun();
var f = HangupFunction;
if (f != null) f();
}
catch (Exception ex) {
Log.WriteLine(LogLevel.Warning, "Exception in hangupCallback: {0}", ex.ToString());
throw;
}
}
protected Action HangupFunction { get; set; }
protected Func<Char, TimeSpan, string> DtmfReceivedFunction { get; set; }
protected Func<Native.Event, string> EventReceivedFunction { get; set; }
string inputCallback(IntPtr input, Native.switch_input_type_t inputType)
{
switch (inputType) {
case FreeSWITCH.Native.switch_input_type_t.SWITCH_INPUT_TYPE_DTMF:
using (var dtmf = new Native.switch_dtmf_t(input, false)) {
return dtmfCallback(dtmf);
}
case FreeSWITCH.Native.switch_input_type_t.SWITCH_INPUT_TYPE_EVENT:
using (var swevt = new Native.switch_event(input, false)) {
return eventCallback(swevt);
}
default:
return "";
}
}
string dtmfCallback(Native.switch_dtmf_t dtmf)
{
var f = DtmfReceivedFunction;
return f == null ?
"-ERR No DtmfReceivedFunction set." :
f(((char)(byte)dtmf.digit), TimeSpan.FromMilliseconds(dtmf.duration));
}
string eventCallback(Native.switch_event swevt)
{
using (var evt = new FreeSWITCH.Native.Event(swevt, 0)) {
var f = EventReceivedFunction;
return f == null ?
"-ERR No EventReceivedFunction set." :
f(evt);
}
}
protected Native.MonoSession Session { get; private set; }
protected string Arguments { get; private set; }
......@@ -115,7 +61,7 @@ namespace FreeSWITCH
bool abortable = false;
readonly object abortLock = new object();
Thread runThread;
void abortRun()
internal void AbortRun()
{
if (!AbortOnHangup) return;
if (runThread == Thread.CurrentThread) {
......@@ -136,7 +82,7 @@ namespace FreeSWITCH
{
this.Session = session;
this.Arguments = args;
Session.SetDelegates(this.inputCallback, this.hangupCallback);
Session.AppToAbort = this;
try { this.Uuid = new Guid(Session.GetUuid()); }
catch { }
try {
......
......@@ -53,13 +53,12 @@ namespace FreeSWITCH.Demo
protected override void Run()
{
HangupFunction = hangupHook;
Session.Answer();
this.DtmfReceivedFunction = (d, t) => {
Session.DtmfReceivedFunction = (d, t) => {
Log.WriteLine(LogLevel.Info, "Received {0} for {1}.", d, t);
return "";
};
Log.WriteLine(LogLevel.Info, "Inside AppDemo.Run (args '{0}'); HookState is {1}.", Arguments, Session.HookState);
Log.WriteLine(LogLevel.Info, "Inside AppDemo.Run (args '{0}'); HookState is {1}. Now will collect digits.", Arguments, Session.HookState);
Session.CollectDigits(5000); // Hanging up here will cause an abort and the next line won't be written
Log.WriteLine(LogLevel.Info, "AppDemo is finishing its run and will now hang up.");
Session.Hangup("USER_BUSY");
......
......@@ -227,6 +227,7 @@ namespace FreeSWITCH
if (fType == null) return false;
using (var session = new Native.MonoSession(new Native.SWIGTYPE_p_switch_core_session(sessionHandle, false))) {
session.Initialize();
session.SetAutoHangup(false);
try {
var f = (AppFunction)Activator.CreateInstance(fType);
......
......@@ -35,7 +35,7 @@ using System.Text;
namespace FreeSWITCH.Native
{
//switch_status_t MonoSession::run_dtmf_callback(void *input, switch_input_type_t itype)
// switch_status_t MonoSession::run_dtmf_callback(void *input, switch_input_type_t itype)
// But, process_callback_result is used to turn a string into a switch_status_t
using DtmfCallback = Func<IntPtr, Native.switch_input_type_t, string>;
public partial class MonoSession
......@@ -44,9 +44,68 @@ namespace FreeSWITCH.Native
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.InternalCall)]
static extern void InitMonoSession(IntPtr sessionPtr, DtmfCallback dtmfDelegate, Action hangupDelegate);
internal void SetDelegates(DtmfCallback dtmfCallback, Action hangupHook)
/// <summary>Initializes the native MonoSession. Must be called after Originate.</summary>
public void Initialize()
{
InitMonoSession(MonoSession.getCPtr(this).Handle, dtmfCallback, hangupHook);
InitMonoSession(MonoSession.getCPtr(this).Handle, inputCallback, hangupCallback);
}
/// <summary>Function to execute when this session hangs up.</summary>
public Action HangupFunction { get; set; }
/// <summary>Sets the application that should have it's run thread aborted (if enabled) when this session is hungup.</summary>
internal AppFunction AppToAbort { get; set; }
void hangupCallback()
{
Log.WriteLine(LogLevel.Debug, "AppFunction is in hangupCallback.");
try {
if (AppToAbort != null) AppToAbort.AbortRun();
var f = HangupFunction;
if (f != null) f();
}
catch (Exception ex) {
Log.WriteLine(LogLevel.Warning, "Exception in hangupCallback: {0}", ex.ToString());
throw;
}
}
public Func<Char, TimeSpan, string> DtmfReceivedFunction { get; set; }
public Func<Native.Event, string> EventReceivedFunction { get; set; }
string inputCallback(IntPtr input, Native.switch_input_type_t inputType)
{
switch (inputType) {
case FreeSWITCH.Native.switch_input_type_t.SWITCH_INPUT_TYPE_DTMF:
using (var dtmf = new Native.switch_dtmf_t(input, false)) {
return dtmfCallback(dtmf);
}
case FreeSWITCH.Native.switch_input_type_t.SWITCH_INPUT_TYPE_EVENT:
using (var swevt = new Native.switch_event(input, false)) {
return eventCallback(swevt);
}
default:
return "";
}
}
string dtmfCallback(Native.switch_dtmf_t dtmf)
{
var f = DtmfReceivedFunction;
return f == null ?
"-ERR No DtmfReceivedFunction set." :
f(((char)(byte)dtmf.digit), TimeSpan.FromMilliseconds(dtmf.duration));
}
string eventCallback(Native.switch_event swevt)
{
using (var evt = new FreeSWITCH.Native.Event(swevt, 0)) {
var f = EventReceivedFunction;
return f == null ?
"-ERR No EventReceivedFunction set." :
f(evt);
}
}
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论