2013-01-18 15:55:50 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Net.Sockets;
|
2015-01-03 15:37:29 +00:00
|
|
|
using System.Threading;
|
2014-01-08 14:16:39 +00:00
|
|
|
using AsterNET.IO;
|
2015-01-03 15:37:29 +00:00
|
|
|
using AsterNET.Manager.Action;
|
2014-01-08 14:16:39 +00:00
|
|
|
using AsterNET.Manager.Event;
|
|
|
|
using AsterNET.Manager.Response;
|
2013-01-18 15:55:50 +00:00
|
|
|
|
2014-01-08 14:16:39 +00:00
|
|
|
namespace AsterNET.Manager
|
2013-01-18 15:55:50 +00:00
|
|
|
{
|
|
|
|
/// <summary>
|
2015-01-03 15:37:29 +00:00
|
|
|
/// Default implementation of the ManagerReader interface.
|
2013-01-18 15:55:50 +00:00
|
|
|
/// </summary>
|
|
|
|
public class ManagerReader
|
|
|
|
{
|
|
|
|
#if LOGGER
|
2015-01-03 15:37:29 +00:00
|
|
|
private readonly Logger logger = Logger.Instance();
|
2013-01-18 15:55:50 +00:00
|
|
|
#endif
|
|
|
|
|
2015-01-03 15:37:29 +00:00
|
|
|
private readonly ManagerConnection mrConnector;
|
2013-01-18 15:55:50 +00:00
|
|
|
private SocketConnection mrSocket;
|
|
|
|
|
2015-01-03 15:37:29 +00:00
|
|
|
private bool die;
|
|
|
|
private bool is_logoff;
|
|
|
|
private bool disconnect;
|
2013-01-18 15:55:50 +00:00
|
|
|
private byte[] lineBytes;
|
|
|
|
private string lineBuffer;
|
2015-01-03 15:37:29 +00:00
|
|
|
private readonly Queue<string> lineQueue;
|
2013-01-18 15:55:50 +00:00
|
|
|
private ResponseHandler pingHandler;
|
|
|
|
private bool processingCommandResult;
|
|
|
|
private bool wait4identiier;
|
|
|
|
private DateTime lastPacketTime;
|
2015-01-03 15:37:29 +00:00
|
|
|
private readonly Dictionary<string, string> packet;
|
|
|
|
private readonly List<string> commandList;
|
2013-01-18 15:55:50 +00:00
|
|
|
|
|
|
|
#region ManagerReader(dispatcher, asteriskServer)
|
2015-01-03 15:37:29 +00:00
|
|
|
|
2013-01-18 15:55:50 +00:00
|
|
|
/// <summary>
|
2015-01-03 15:37:29 +00:00
|
|
|
/// Creates a new ManagerReader.
|
2013-01-18 15:55:50 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="dispatcher">the dispatcher to use for dispatching events and responses.</param>
|
|
|
|
public ManagerReader(ManagerConnection connection)
|
|
|
|
{
|
2015-01-03 15:37:29 +00:00
|
|
|
mrConnector = connection;
|
|
|
|
die = false;
|
2013-01-18 15:55:50 +00:00
|
|
|
lineQueue = new Queue<string>();
|
|
|
|
packet = new Dictionary<string, string>();
|
|
|
|
commandList = new List<string>();
|
|
|
|
}
|
2015-01-03 15:37:29 +00:00
|
|
|
|
2013-01-18 15:55:50 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Socket
|
2015-01-03 15:37:29 +00:00
|
|
|
|
2013-01-18 15:55:50 +00:00
|
|
|
/// <summary>
|
2015-01-03 15:37:29 +00:00
|
|
|
/// Sets the socket to use for reading from the asterisk server.
|
2013-01-18 15:55:50 +00:00
|
|
|
/// </summary>
|
2015-01-03 15:37:29 +00:00
|
|
|
internal SocketConnection Socket
|
2013-01-18 15:55:50 +00:00
|
|
|
{
|
2015-01-03 15:37:29 +00:00
|
|
|
set { mrSocket = value; }
|
2013-01-18 15:55:50 +00:00
|
|
|
}
|
2015-01-03 15:37:29 +00:00
|
|
|
|
2013-01-18 15:55:50 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Die
|
2015-01-03 15:37:29 +00:00
|
|
|
|
2013-01-18 15:55:50 +00:00
|
|
|
internal bool Die
|
|
|
|
{
|
|
|
|
get { return die; }
|
|
|
|
set
|
|
|
|
{
|
|
|
|
die = value;
|
|
|
|
if (die)
|
2015-01-03 15:37:29 +00:00
|
|
|
mrSocket = null;
|
2013-01-18 15:55:50 +00:00
|
|
|
}
|
|
|
|
}
|
2015-01-03 15:37:29 +00:00
|
|
|
|
2013-01-18 15:55:50 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region IsLogoff
|
2015-01-03 15:37:29 +00:00
|
|
|
|
2013-01-18 15:55:50 +00:00
|
|
|
internal bool IsLogoff
|
|
|
|
{
|
|
|
|
set { is_logoff = value; }
|
|
|
|
}
|
2015-01-03 15:37:29 +00:00
|
|
|
|
2013-01-18 15:55:50 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region mrReaderCallbback(IAsyncResult ar)
|
2015-01-03 15:37:29 +00:00
|
|
|
|
2016-10-19 07:20:23 +00:00
|
|
|
/// <summary>
|
2013-01-18 15:55:50 +00:00
|
|
|
/// Async Read callback
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="ar">IAsyncResult</param>
|
|
|
|
private void mrReaderCallbback(IAsyncResult ar)
|
|
|
|
{
|
|
|
|
// mreader = Mr.Reader
|
2015-01-03 15:37:29 +00:00
|
|
|
var mrReader = (ManagerReader) ar.AsyncState;
|
2013-01-18 15:55:50 +00:00
|
|
|
if (mrReader.die)
|
|
|
|
return;
|
|
|
|
|
|
|
|
SocketConnection mrSocket = mrReader.mrSocket;
|
|
|
|
if (mrSocket == null || mrSocket.TcpClient == null)
|
|
|
|
{
|
|
|
|
// No socket - it's DISCONNECT !!!
|
|
|
|
disconnect = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NetworkStream nstream = mrSocket.NetworkStream;
|
|
|
|
if (nstream == null)
|
|
|
|
{
|
|
|
|
// No network stream - it's DISCONNECT !!!
|
|
|
|
disconnect = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
int count = nstream.EndRead(ar);
|
|
|
|
if (count == 0)
|
|
|
|
{
|
|
|
|
// No received data - it's may be DISCONNECT !!!
|
2015-01-03 15:37:29 +00:00
|
|
|
if (!is_logoff)
|
2013-01-18 15:55:50 +00:00
|
|
|
disconnect = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
string line = mrSocket.Encoding.GetString(mrReader.lineBytes, 0, count);
|
|
|
|
mrReader.lineBuffer += line;
|
|
|
|
int idx;
|
|
|
|
// \n - because not all dev in Digium use \r\n
|
|
|
|
// .Trim() kill \r
|
2015-01-03 15:37:29 +00:00
|
|
|
lock (((ICollection) lineQueue).SyncRoot)
|
2018-03-26 23:10:29 +00:00
|
|
|
while (!string.IsNullOrEmpty(mrReader.lineBuffer) && (idx = mrReader.lineBuffer.IndexOf('\n')) >= 0)
|
2013-01-18 15:55:50 +00:00
|
|
|
{
|
|
|
|
line = idx > 0 ? mrReader.lineBuffer.Substring(0, idx).Trim() : string.Empty;
|
2015-01-03 15:37:29 +00:00
|
|
|
mrReader.lineBuffer = (idx + 1 < mrReader.lineBuffer.Length
|
|
|
|
? mrReader.lineBuffer.Substring(idx + 1)
|
|
|
|
: string.Empty);
|
2013-01-18 15:55:50 +00:00
|
|
|
lineQueue.Enqueue(line);
|
|
|
|
}
|
|
|
|
// Give a next portion !!!
|
|
|
|
nstream.BeginRead(mrReader.lineBytes, 0, mrReader.lineBytes.Length, mrReaderCallbback, mrReader);
|
|
|
|
}
|
|
|
|
#if LOGGER
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
mrReader.logger.Error("Read data error", ex.Message);
|
|
|
|
#else
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
#endif
|
|
|
|
// Any catch - disconncatch !
|
|
|
|
disconnect = true;
|
|
|
|
if (mrReader.mrSocket != null)
|
|
|
|
mrReader.mrSocket.Close();
|
|
|
|
mrReader.mrSocket = null;
|
|
|
|
}
|
|
|
|
}
|
2015-01-03 15:37:29 +00:00
|
|
|
|
2013-01-18 15:55:50 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Reinitialize
|
2015-01-03 15:37:29 +00:00
|
|
|
|
2013-01-18 15:55:50 +00:00
|
|
|
internal void Reinitialize()
|
|
|
|
{
|
|
|
|
mrSocket.Initial = false;
|
|
|
|
disconnect = false;
|
|
|
|
lineQueue.Clear();
|
|
|
|
packet.Clear();
|
|
|
|
commandList.Clear();
|
|
|
|
lineBuffer = string.Empty;
|
|
|
|
lineBytes = new byte[mrSocket.TcpClient.ReceiveBufferSize];
|
|
|
|
lastPacketTime = DateTime.Now;
|
|
|
|
wait4identiier = true;
|
|
|
|
processingCommandResult = false;
|
|
|
|
mrSocket.NetworkStream.BeginRead(lineBytes, 0, lineBytes.Length, mrReaderCallbback, this);
|
|
|
|
lastPacketTime = DateTime.Now;
|
|
|
|
}
|
2015-01-03 15:37:29 +00:00
|
|
|
|
2013-01-18 15:55:50 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Run()
|
2015-01-03 15:37:29 +00:00
|
|
|
|
2016-10-19 07:20:23 +00:00
|
|
|
/// <summary>
|
2013-01-18 15:55:50 +00:00
|
|
|
/// Reads line by line from the asterisk server, sets the protocol identifier as soon as it is
|
|
|
|
/// received and dispatches the received events and responses via the associated dispatcher.
|
|
|
|
/// </summary>
|
|
|
|
/// <seealso cref="ManagerConnection.DispatchEvent(ManagerEvent)" />
|
|
|
|
/// <seealso cref="ManagerConnection.DispatchResponse(Response.ManagerResponse)" />
|
|
|
|
/// <seealso cref="ManagerConnection.setProtocolIdentifier(String)" />
|
2015-01-03 15:37:29 +00:00
|
|
|
internal void Run()
|
2013-01-18 15:55:50 +00:00
|
|
|
{
|
|
|
|
if (mrSocket == null)
|
|
|
|
throw new SystemException("Unable to run: socket is null.");
|
|
|
|
|
|
|
|
string line;
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
while (!die)
|
|
|
|
{
|
|
|
|
#region check line from *
|
2015-01-03 15:37:29 +00:00
|
|
|
|
2013-01-18 15:55:50 +00:00
|
|
|
if (!is_logoff)
|
|
|
|
{
|
|
|
|
if (mrSocket != null && mrSocket.Initial)
|
|
|
|
{
|
|
|
|
Reinitialize();
|
|
|
|
}
|
|
|
|
else if (disconnect)
|
|
|
|
{
|
|
|
|
disconnect = false;
|
|
|
|
mrConnector.DispatchEvent(new DisconnectEvent(mrConnector));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (lineQueue.Count == 0)
|
|
|
|
{
|
2015-01-03 15:37:29 +00:00
|
|
|
if (lastPacketTime.AddMilliseconds(mrConnector.PingInterval) < DateTime.Now
|
|
|
|
&& mrConnector.PingInterval > 0
|
|
|
|
&& mrSocket != null
|
|
|
|
&& !wait4identiier
|
|
|
|
&& !is_logoff
|
|
|
|
)
|
|
|
|
{
|
|
|
|
if (pingHandler != null)
|
|
|
|
{
|
|
|
|
if (pingHandler.Response == null)
|
|
|
|
{
|
|
|
|
// If one PingInterval from Ping without Pong then send Disconnect event
|
|
|
|
mrConnector.RemoveResponseHandler(pingHandler);
|
|
|
|
mrConnector.DispatchEvent(new DisconnectEvent(mrConnector));
|
|
|
|
}
|
|
|
|
pingHandler.Free();
|
|
|
|
pingHandler = null;
|
2013-01-18 15:55:50 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Send PING to *
|
|
|
|
try
|
|
|
|
{
|
2015-01-03 15:37:29 +00:00
|
|
|
pingHandler = new ResponseHandler(new PingAction(), null);
|
2013-01-18 15:55:50 +00:00
|
|
|
mrConnector.SendAction(pingHandler.Action, pingHandler);
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
disconnect = true;
|
|
|
|
mrSocket = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lastPacketTime = DateTime.Now;
|
|
|
|
}
|
|
|
|
Thread.Sleep(50);
|
2014-06-12 08:08:52 +00:00
|
|
|
|
2013-01-18 15:55:50 +00:00
|
|
|
continue;
|
|
|
|
}
|
2015-01-03 15:37:29 +00:00
|
|
|
|
2013-01-18 15:55:50 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
lastPacketTime = DateTime.Now;
|
2015-01-03 15:37:29 +00:00
|
|
|
lock (((ICollection) lineQueue).SyncRoot)
|
2013-01-18 15:55:50 +00:00
|
|
|
line = lineQueue.Dequeue().Trim();
|
|
|
|
#if LOGGER
|
|
|
|
logger.Debug(line);
|
|
|
|
#endif
|
2015-01-03 15:37:29 +00:00
|
|
|
|
2013-01-18 15:55:50 +00:00
|
|
|
#region processing Response: Follows
|
2015-01-03 15:37:29 +00:00
|
|
|
|
2013-01-18 15:55:50 +00:00
|
|
|
if (processingCommandResult)
|
|
|
|
{
|
2016-12-29 14:04:17 +00:00
|
|
|
string lineLower = line.ToLower(Helper.CultureInfo);
|
|
|
|
if (lineLower == "--end command--")
|
2013-01-18 15:55:50 +00:00
|
|
|
{
|
2015-01-03 15:37:29 +00:00
|
|
|
var commandResponse = new CommandResponse();
|
2013-01-18 15:55:50 +00:00
|
|
|
Helper.SetAttributes(commandResponse, packet);
|
2016-12-29 14:04:17 +00:00
|
|
|
commandList.Add(line);
|
2013-01-18 15:55:50 +00:00
|
|
|
commandResponse.Result = commandList;
|
|
|
|
processingCommandResult = false;
|
|
|
|
packet.Clear();
|
|
|
|
mrConnector.DispatchResponse(commandResponse);
|
|
|
|
}
|
2016-12-29 14:04:17 +00:00
|
|
|
else if (lineLower.StartsWith("privilege: ")
|
2015-01-03 15:37:29 +00:00
|
|
|
|| lineLower.StartsWith("actionid: ")
|
|
|
|
|| lineLower.StartsWith("timestamp: ")
|
|
|
|
|| lineLower.StartsWith("server: ")
|
|
|
|
)
|
|
|
|
Helper.AddKeyValue(packet, line);
|
2013-01-18 15:55:50 +00:00
|
|
|
else
|
2015-01-03 15:37:29 +00:00
|
|
|
commandList.Add(line);
|
2013-01-18 15:55:50 +00:00
|
|
|
continue;
|
|
|
|
}
|
2015-01-03 15:37:29 +00:00
|
|
|
|
2013-01-18 15:55:50 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region collect key: value and ProtocolIdentifier
|
2015-01-03 15:37:29 +00:00
|
|
|
|
2013-01-18 15:55:50 +00:00
|
|
|
if (!string.IsNullOrEmpty(line))
|
|
|
|
{
|
|
|
|
if (wait4identiier && line.StartsWith("Asterisk Call Manager"))
|
|
|
|
{
|
|
|
|
wait4identiier = false;
|
2015-01-03 15:37:29 +00:00
|
|
|
var connectEvent = new ConnectEvent(mrConnector);
|
2013-01-18 15:55:50 +00:00
|
|
|
connectEvent.ProtocolIdentifier = line;
|
|
|
|
mrConnector.DispatchEvent(connectEvent);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (line.Trim().ToLower(Helper.CultureInfo) == "response: follows")
|
|
|
|
{
|
|
|
|
// Switch to wait "--END COMMAND--" mode
|
|
|
|
processingCommandResult = true;
|
|
|
|
packet.Clear();
|
|
|
|
commandList.Clear();
|
|
|
|
Helper.AddKeyValue(packet, line);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Helper.AddKeyValue(packet, line);
|
|
|
|
continue;
|
|
|
|
}
|
2015-01-03 15:37:29 +00:00
|
|
|
|
2013-01-18 15:55:50 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region process events and responses
|
|
|
|
|
|
|
|
if (packet.ContainsKey("event"))
|
|
|
|
mrConnector.DispatchEvent(packet);
|
|
|
|
|
|
|
|
else if (packet.ContainsKey("response"))
|
|
|
|
mrConnector.DispatchResponse(packet);
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
packet.Clear();
|
|
|
|
}
|
2015-01-03 15:37:29 +00:00
|
|
|
if (mrSocket != null)
|
2013-01-18 15:55:50 +00:00
|
|
|
mrSocket.Close();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#if LOGGER
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
logger.Info("Exception : {0}", ex.Message);
|
|
|
|
#else
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
if (die)
|
|
|
|
break;
|
|
|
|
|
|
|
|
#if LOGGER
|
|
|
|
logger.Info("No die, any error - send disconnect.");
|
|
|
|
#endif
|
|
|
|
mrConnector.DispatchEvent(new DisconnectEvent(mrConnector));
|
|
|
|
}
|
|
|
|
}
|
2015-01-03 15:37:29 +00:00
|
|
|
|
2013-01-18 15:55:50 +00:00
|
|
|
#endregion
|
|
|
|
}
|
2016-12-29 14:04:17 +00:00
|
|
|
}
|