C# Middle Man

Hi I recently began a new job and my first task was to develop a server to replace the TrixBox HUD Server. It is used as a middle man to relay messages to client software to notify our call center agents of incoming calls(screen pops), etc.

Apparently, our Asterisk box doesn’t like too many connections, so the HUD server is used for the one connection and then relays info to the clients.

But the HUD server has had intermittent losses of connections to Asterisk, etc.

I found an example C# manager piece that logs into the Asterisk server and listens for events. It catches and prints a large number of events and then just stops working. But it doesn’t say it’s been disconnected. A co-worker said he heard that Asterisk may drop the connection without notification. Is this true?

I’ll post the code below:

class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            Console.WriteLine("Quick and Dirty Asterisk Manager Daemon Test:\n");

            // Connect to the asterisk server.
            Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("192.168.15.13"), 5038);
            clientSocket.Connect(serverEndPoint);

            // Login to the server; manager.conf needs to be setup with matching credentials.
            clientSocket.Send(Encoding.ASCII.GetBytes("Action: Login\r\nUsername: remote_mgr\r\nSecret: 0chanc3yo\r\nActionID: 1\r\n\r\n"));

            int bytesRead = 0;


            do
            {
                byte[] buffer = new byte[1024];
                bytesRead = clientSocket.Receive(buffer);

                //Console.WriteLine(bytesRead + " bytes from asterisk server.");

                string response = Encoding.ASCII.GetString(buffer, 0, bytesRead);

                if (response != "")
                {
                    Console.WriteLine(response);
                }

                if (Regex.Match(response, "Message: Authentication accepted", RegexOptions.IgnoreCase).Success)
                {
                    // Send a ping request the asterisk server will send back a pong response.
                    clientSocket.Send(Encoding.ASCII.GetBytes("Action: Ping\r\nActionID: 2\r\n\r\n"));
                }
            } while (clientSocket.Connected == true) ; //while (bytesRead != 0);

            Console.WriteLine("Connection to server lost.");
            Console.ReadLine();
        } 
    }

Can anyone help? I’m a little out of my element having never worked with Asterisk. Thanks in advance for any help.

Eric Windham