Example #1
0
        private AppTimeMetric ProcessJob(object job)
        {
            Socket fastAGISocket = null;

            try
            {
                fastAGISocket = (Socket)job;
                fastAGISocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, true);

                logger.Debug("fastagi connection from " + IPSocket.GetSocketString((IPEndPoint)fastAGISocket.RemoteEndPoint) + "(" + Thread.CurrentThread.Name + " " + DateTime.Now.ToString("dd MMM yyyy HH:mm:ss") + ")");

                byte[] buffer    = new byte[2048];
                int    bytesRead = 1;

                // Caution - it could take the Asterisk server more than one socket send to get the all the request parameters sent.
                StringBuilder request = new StringBuilder();
                while (bytesRead > 0)
                {
                    bytesRead = fastAGISocket.Receive(buffer, 0, 2048, SocketFlags.None);
                    request.Append(Encoding.ASCII.GetString(buffer, 0, bytesRead));
                    logger.Debug(Encoding.ASCII.GetString(buffer, 0, bytesRead));

                    if (request.ToString() != null && (Regex.Match(request.ToString(), @"\n\n", RegexOptions.Singleline).Success || Regex.Match(request.ToString(), @"\r\n\r\n", RegexOptions.Singleline).Success))
                    {
                        break;
                    }
                }

                FastAGIRequest fastAGIRequest = new FastAGIRequest();
                return(fastAGIRequest.Run(fastAGISocket, request.ToString()));
            }
            catch (Exception excp)
            {
                logger.Error("Exception FastAGIWorker ProcessJobEvent. " + excp.Message);
                ExceptionAlert(excp.Message);
                return(new AppTimeMetric());
            }
            finally
            {
                if (fastAGISocket != null)
                {
                    try
                    {
                        logger.Debug("connection closed.");
                        fastAGISocket.Close();
                    }
                    catch (Exception sockCkoseExcp)
                    {
                        logger.Error("Exception FastAGIQueueDaemon ProceesJob (closing AGI socket). " + sockCkoseExcp);
                    }
                }
            }
        }
        private AppTimeMetric ProcessJob(object job)
        {
            Socket fastAGISocket = null;

            try
            {
                fastAGISocket = (Socket)job;
                fastAGISocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, true);

                logger.Debug("fastagi connection from " + IPSocket.GetSocketString((IPEndPoint)fastAGISocket.RemoteEndPoint) + "(" + Thread.CurrentThread.Name + " " + DateTime.Now.ToString("dd MMM yyyy HH:mm:ss") + ")");

                byte[] buffer = new byte[2048];
                int bytesRead = 1;

                // Caution - it could take the Asterisk server more than one socket send to get the all the request parameters sent.
                StringBuilder request = new StringBuilder();
                while (bytesRead > 0)
                {
                    bytesRead = fastAGISocket.Receive(buffer, 0, 2048, SocketFlags.None);
                    request.Append(Encoding.ASCII.GetString(buffer, 0, bytesRead));
                    logger.Debug(Encoding.ASCII.GetString(buffer, 0, bytesRead));

                    if (request.ToString() != null && (Regex.Match(request.ToString(), @"\n\n", RegexOptions.Singleline).Success || Regex.Match(request.ToString(), @"\r\n\r\n", RegexOptions.Singleline).Success))
                    {
                        break;
                    }
                }

                FastAGIRequest fastAGIRequest = new FastAGIRequest();
                return fastAGIRequest.Run(fastAGISocket, request.ToString());
            }
            catch (Exception excp)
            {
                logger.Error("Exception FastAGIWorker ProcessJobEvent. " + excp.Message);
                ExceptionAlert(excp.Message);
                return new AppTimeMetric();
            }
            finally
            {
                if (fastAGISocket != null)
                {
                    try
                    {
                        logger.Debug("connection closed.");
                        fastAGISocket.Close();
                    }
                    catch(Exception sockCkoseExcp)
                    {
                        logger.Error("Exception FastAGIQueueDaemon ProceesJob (closing AGI socket). " + sockCkoseExcp);
                    }
                }
            }
        }