我正在尝试使用FreeSwitch的事件套接字库(wiki.freeswitch.org/wiki/Event_Socket_Library),但我想这个问题更多的是关于技术和“如何”的问题.
下面的代码是我的问题,可以找到here.
static void OutboundModeSync(Object stateInfo)
{
//...
while (true)
{
Socket sckClient = tcpListener.AcceptSocket();
//Initializes a new instance of ESLconnection, and connects to the host $host on the port $port, and supplies $password to freeswitch
ESLconnection eslConnection = new ESLconnection(sckClient.Handle.ToInt32());
Console.WriteLine("Execute(\"answer\")");
eslConnection.Execute("answer", String.Empty, String.Empty);
Console.WriteLine("Execute(\"playback\")");
eslConnection.Execute("playback", "music/8000/suite-espanola-op-47-leyenda.wav", String.Empty);
Console.WriteLine("Execute(\"hangup\")");
eslConnection.Execute("hangup", String.Empty, String.Empty);
}
//...
}
static void OutboundModeAsync(Object stateInfo)
{
//...
while (true)
{
tcpListener.BeginAcceptSocket((asyncCallback) =>
{
TcpListener tcpListened = (TcpListener)asyncCallback.AsyncState;
Socket sckClient = tcpListened.EndAcceptSocket(asyncCallback);
//Initializes a new instance of ESLconnection, and connects to the host $host on the port $port, and supplies $password to freeswitch
ESLconnection eslConnection = new ESLconnection(sckClient.Handle.ToInt32());
ESLevent eslEvent = eslConnection.GetInfo();
//...
while (eslConnection.Connected() == ESL_SUCCESS)
{
eslEvent = eslConnection.RecvEvent();
Console.WriteLine(eslEvent.Serialize(String.Empty));
}
sckClient.Close();
}, tcpListener);
Thread.Sleep(50);
}
}
(我在出站模式下使用FreeSwitch).据我了解,“同步”和“ ASync”方法都处于无限循环中,等待(阻塞)事件.每当从FreeSwitch接收到某些事件时做出反应.
但是,我想创建一个Windows服务来跟踪FreeSwitch对我的所有调用.我希望另一个应用程序调用我的服务,并告诉它将“ Foo”发送到连接“ XYZ”.因此,我要跟踪一个连接字典,其中的关键是连接-(GU)ID.但是,如果该连接正在等待(阻止)某个事件,那么无论如何从FreeSwitch接收到一个事件以“释放”阻止,我都看不到如何使用它来“对接”并发送消息线.
顺便说一句:“插入”是指,如果出于某种原因,例如,我决定要挂断连接(例如触发了某个计时器),则我希望能够发送挂断,无论是否连接正在等待从FreeSwitch发送事件(如果什么也没发生,则可能要花很长时间).
我可以使用recvEventTimed创建一个“轮询机制”,但是这种方法存在三个问题:1)轮询感觉很脏,2)每当我想“对接”时,我都希望尽快发送消息,而不是50ms(或1ms)以后发送, 3)recvEventTimed似乎忽略了〜500ms以下的任何事件;它会疯狂地轮询. 500ms对我来说是太多的延迟.除此之外(我可以提交错误报告),轮询感觉不正确.
我想“接受连接”,告诉ESL等待(阻止)一个事件,然后引发一个事件,以便可以使用基于事件的方法.我希望处理大量连接,因此我希望延迟时间短,没有(或很少)轮询并且采用干净的方法来处理所有这些连接,但它们会持续存在,从而为我提供免费的“双向,未轮询,基于事件”的通信.
我考虑过RX(反应性扩展),并使用.Net异步套接字(与FreeSwitch提供的二进制文件相对)实现了自己的“ ESL”和其他方法.但是我似乎找不到一种没有很多样板或/和令人讨厌的线程之类的东西来整齐地实现我的愿望/要求的方法.我很确定我应该能够使用异步/等待,但会迷路就是这样.任何帮助将不胜感激.
简单描述:
* Connection comes in
* Accept connection + get GUID from connection
* Put connection in some collection for later reference
* Bind delegate to connection (`EventReceived` event)
* Use collection to get connection and send a message (preferrably async ("fire and forget"), but blocking COULD be acceptable as long as it only blocks that specific connection) to "butt-in" whenever I want to
该集合将托管在Windows服务中,我可以通过该服务与我的FreeSwitch连接进行I / O /列出可用的连接等.
因此,我想像“伪代码”中的某些内容:
MyService {
stopped = false;
void OnStart {
while (!stopped) {
var someconn = esl.waitforconnection();
var id = someconn.getCallId();
someconn.EventReceived += MyDelegate(...);
mydict.add(id, someconn);
}
}
void OnStop {
stopped = true;
}
void ButtIn(string callid, string msg) {
mydict[callid].send(msg);
}
void MyDelegate(EventData evtdata) {
//Do something with evtdata if desired
}
}
鉴于FreeSwitch提供的ESL并非基于事件,并且只有阻塞的“等待事件”或轮询“等待事件的X ms”,我将如何实现这一目标?
tl; dr:不想使用无限循环/轮询,而是更多的基于事件的方法,最好使用async / await,在将“底层“ ESL连接””包装/抽象后使用干净的简单代码来使用自己的班级.需要最佳方法的帮助/建议.没有线程/套接字大师.
解决方法:
NEventSocket和ReactiveExtensions可能会帮助您.
private static async Task CallTracking()
{
var client = await InboundSocket.Connect("10.10.10.36", 8021, "ClueCon");
string uuid = null;
client.Events.Where(x => x.EventName == EventName.ChannelAnswer)
.Subscribe(x =>
{
uuid = x.UUID;
Console.WriteLine("Channel Answer Event {0}", x.UUID);
});
client.Events.Where(x => x.EventName == EventName.ChannelHangup)
.Subscribe(x =>
{
uuid = null;
Console.WriteLine("Channel Hangup Event {0}", x.UUID);
});
Console.WriteLine("Press enter to hang up the current call");
Console.ReadLine();
if (uuid != null)
{
Console.WriteLine("Hanging up {0}", uuid);
await client.Play(uuid, "ivr/8000/ivr-call_rejected.wav");
await client.Hangup(uuid, HangupCause.CallRejected);
}
client.Exit();
}