using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Net; using System.Net.Sockets; using System.Text.RegularExpressions; using System.Threading; namespace AstWrapper { public class Manager { //Events public event EventMaker.HangupEvent OnHangup; public event EventMaker.NewStateEvent NewStateEvent; Thread manthread = new Thread(new ThreadStart(getdata)); static Queue<string> res_q = new Queue<string>(); public const string LINE_SEPARATOR = "\r\n"; private static Socket ManSock = null; public string Ast_Server { get; set; } public int Ast_Port { get; set; } public string Ast_Username { get; set; } public string Ast_Password { get; set; } public Manager() { } public Manager(string server, int port, string username, string password) { Ast_Server = server; Ast_Port = port; Ast_Username = username; Ast_Password = password; } public bool connect() { try { ManSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse(Ast_Server), Ast_Port); ManSock.Connect(serverEndPoint); if (ManSock.Connected) return true; else return false; } catch { return false; } } private static void getdata() { do { int bytesRead = 0; byte[] buffer = new byte[1024]; bytesRead = ManSock.Receive(buffer); string response = Encoding.ASCII.GetString(buffer, 0, bytesRead); if (response != "") { string[] pp = response.Split(new string[] { "\r\n" }, StringSplitOptions.None); foreach (string s in pp) { if (s.Trim().StartsWith("Event:")) //Here I wanna call the event } } Thread.Sleep(1000); } while (ManSock.Connected); } public void Login() { ManSock.Send(Encoding.ASCII.GetBytes("Action: Login" + LINE_SEPARATOR + "Username: " + Ast_Username + LINE_SEPARATOR + "Secret: " + Ast_Password + LINE_SEPARATOR + LINE_SEPARATOR)); //Console.WriteLine(bytesRead + " bytes from asterisk server."); manthread.Start(); } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)