示例#1
0
        /// <summary>
        /// Stops the client thread
        /// </summary>
        public void Stop()
        {
            try
            {
                if (this.wThread.IsAlive)
                {
                    this.bRunning = false;
                    this.wThread.Join(200);
                }
                if (this.wThread.IsAlive)
                {
                    this.wThread.Abort();
                }
                if (this.ClientSocket.Connected)
                {
                    this.CloseSockets("");
                }

                if (this.activeOper != null)
                {
                    this.activeOper = null;
                    // this.sessione = null;
                }
            }
            catch (ThreadAbortException)
            {
                // Do thread deinit here..
            }

            this.wThread = null;
            if (this.ClientStopped != null)
            {
                this.ClientStopped(this, this.remoteAddress);
            }
        }
示例#2
0
        /// <summary>
        /// Main ClientManager loop
        /// </summary>
        private void Manager()
        {
            string reason = "";

            bRunning = true;


            char[] splitparms = { '\n' };
            byte[] bytes;               // Incoming data from the client.

            networkStream     = new NetworkStream(this.ClientSocket);
            ReceiveBufferSize = (int)this.ClientSocket.GetSocketOption(
                SocketOptionLevel.Socket,
                SocketOptionName.ReceiveBuffer);

            Server.logger.WriteLine(LogType.Notice, string.Format("ClientManager started for {0}", this.ClientSocket.RemoteEndPoint.ToString()));
            if (this.ClientStarted != null)
            {
                this.ClientStarted(this, this.remoteAddress);
            }
            this.SendData("100 CTISERVER WELCOME", "\n");

            bytes = new byte[ReceiveBufferSize];
            while (bRunning)
            {
                string data       = null;
                string command    = "";
                string parameters = "";

                try
                {
                    if (!this.IsSocketConnected(this.ClientSocket))
                    {
                        reason        = "Remote client has closed the socket";
                        this.bRunning = false;
                    }
                    int BytesRead = networkStream.Read(bytes, 0, ReceiveBufferSize);
                    data = Encoding.ASCII.GetString(bytes, 0, BytesRead);


                    string[] datas = data.Split(splitparms);
                    if (datas.Length > 0)
                    {
                        for (int pos = 0; pos < datas.Length; pos++)
                        {
                            parameters = "";
                            command    = "";
                            command    = ProtocolParser.Parse(datas[pos], ref parameters);
                            if (command != null)
                            {
                                bool bPresent = false;
                                if (this.activeOper != null)
                                {
                                    bPresent = true;
                                }
                                if (!bPresent)
                                {
                                    if ((!command.ToUpper().Equals("USER")) &
                                        (!command.ToUpper().Equals("PASS")) &
                                        (!command.ToUpper().Equals("HELO")) &
                                        (!command.ToUpper().Equals("NOOP")) &
                                        (!command.ToUpper().Equals("ORIG")) &
                                        (!command.ToUpper().Equals("QUIT")))
                                    {
                                        command = "";
                                    }
                                }

                                switch (command.ToUpper().Trim())
                                {
                                    #region USER
                                case "USER":
                                    if (!checkParameters(parameters))
                                    {
                                        this.SendData("101 ERROR", "\n");
                                        break;
                                    }
                                    bool   bAlreadyConnected  = false;
                                    string ipAlreadyConnected = "";
                                    lock (Server.shared.clients)
                                    {
                                        if (Server.shared.clients.Count > 0)
                                        {
                                            for (int i = 0; i < Server.shared.clients.Count; i++)
                                            {
                                                IEnumerator cls = Server.shared.clients.Values.GetEnumerator();
                                                while (cls.MoveNext())
                                                {
                                                    ClientManager cl = (ClientManager)cls.Current;
                                                    if (cl.activeOper != null)
                                                    {
                                                        if (cl.activeOper.USERNAME.Equals(parameters))
                                                        {
                                                            bAlreadyConnected = true;
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    if (bAlreadyConnected)
                                    {
                                        this.SendData("101 KO", "\n");
                                        Server.logger.WriteLine(LogType.Notice, String.Format("Client already connected from {0} with username: {1}", ipAlreadyConnected, parameters));
                                    }
                                    else
                                    {
                                        this.activeOper          = new CTIOperator();
                                        this.activeOper.USERNAME = parameters;
                                        this.SendData("100 OK", "\n");
                                    }
                                    break;
                                    #endregion

                                    #region PASS
                                case "PASS":
                                    if (this.activeOper == null)
                                    {
                                        this.SendData("101 KO", "\n");
                                        break;
                                    }
                                    if (!checkParameters(parameters))
                                    {
                                        this.SendData("101 ERROR", "\n");
                                        break;
                                    }

                                    if (this.activeOper.LoadFromCredential(this.activeOper.USERNAME, parameters))
                                    {
                                        Server.shared.clients.Add(this.activeOper.EXT, this);
                                        this.SendData("102 OK", "\n");
                                    }
                                    else
                                    {
                                        this.SendData("101 KO", "\n");
                                        this.activeOper = null;
                                    }
                                    break;
                                    #endregion

                                    #region QUIT
                                case "QUIT":
                                    if (this.activeOper != null)
                                    {
                                        Server.shared.clients.Remove(this.activeOper.EXT);
                                        this.activeOper = null;
                                    }
                                    this.SendData("900 BYE", "\n");
                                    reason        = "Remote client closed connection";
                                    this.bRunning = false;
                                    break;
                                    #endregion

                                    #region ORIGINATE
                                case "ORIG":
                                    // exten,context,prio
                                    if (!checkParameters(parameters))
                                    {
                                        this.SendData("101 ERROR", "\n");
                                        break;
                                    }
                                    string[] parms = parameters.Split('|');
                                    if (parms.Length < 3)
                                    {
                                        this.SendData("101 ERROR", "\n");
                                        break;
                                    }
                                    string orig_extn = parms[0];
                                    string orig_cntx = parms[1];
                                    string orig_prio = parms[2];
                                    string ast_cmd   = this.BuildOriginateCommand(orig_extn, orig_cntx, orig_prio);
                                    Server.AstCTI.SendDataToAsterisk(ast_cmd);
                                    this.SendData("100 OK", "\n");
                                    break;
                                    #endregion

                                    #region HELO and NOOP
                                case "NOOP":
                                    this.SendData("100 OK", "\n");
                                    break;

                                case "HELO":
                                    this.SendData("100 OK", "\n");
                                    break;
                                    #endregion
                                }
                            }
                        }
                    }
                }
                catch (IOException)
                {
                    reason = "Client Socket timeout";
                    Server.logger.WriteLine(LogType.Notice, reason);
                    bRunning = false;
                    break;
                } // Timeout
                catch (SocketException)
                {
                    reason = "Socket is broken";
                    Server.logger.WriteLine(LogType.Notice, reason);
                    bRunning = false;
                    break;
                }
                catch (ObjectDisposedException ex)
                {
                    Server.logger.WriteLine(LogType.Debug, string.Format("ObjectDisposedException: {0}", ex.Source));
                }
                catch (Exception ex)
                {
                    Server.logger.WriteLine(LogType.Error, ex.ToString());
                }
            }

            this.CloseSockets(reason);
            if (this.activeOper != null)
            {
                Server.shared.clients.Remove(this.activeOper.EXT);
            }

            Interlocked.Decrement(ref Server.shared.NumberOfClients);
            Server.logger.WriteLine(LogType.Notice, String.Format("Number of clients connected: {0}", Server.shared.NumberOfClients));
        }
示例#3
0
        /// <summary>
        /// Main ClientManager loop
        /// </summary>
        private void Manager()
        {
            string reason = "";
            bRunning = true;

            char[] splitparms = { '\n' };
            byte[] bytes;		// Incoming data from the client.

            networkStream = new NetworkStream(this.ClientSocket);
            ReceiveBufferSize = (int)this.ClientSocket.GetSocketOption(
                                    SocketOptionLevel.Socket,
                                    SocketOptionName.ReceiveBuffer);

            Server.logger.WriteLine(LogType.Notice, string.Format("ClientManager started for {0}", this.ClientSocket.RemoteEndPoint.ToString()));
            if (this.ClientStarted != null) this.ClientStarted(this, this.remoteAddress);
            this.SendData("100 CTISERVER WELCOME", "\n");

            bytes = new byte[ReceiveBufferSize];
            while (bRunning)
            {
                string data = null;
                string command = "";
                string parameters = "";

                try
                {
                    if (!this.IsSocketConnected(this.ClientSocket))
                    {
                        reason = "Remote client has closed the socket";
                        this.bRunning = false;
                    }
                    int BytesRead = networkStream.Read(bytes, 0, ReceiveBufferSize);
                    data = Encoding.ASCII.GetString(bytes, 0, BytesRead);

                    string[] datas = data.Split(splitparms);
                    if (datas.Length > 0)
                    {
                        for (int pos = 0; pos < datas.Length; pos++)
                        {
                            parameters = "";
                            command = "";
                            command = ProtocolParser.Parse(datas[pos], ref parameters);
                            if (command != null)
                            {
                                bool bPresent = false;
                                if (this.activeOper != null)
                                {
                                    bPresent = true;

                                }
                                if (!bPresent)
                                {
                                    if ((!command.ToUpper().Equals("USER")) &
                                         (!command.ToUpper().Equals("PASS")) &
                                         (!command.ToUpper().Equals("HELO")) &
                                         (!command.ToUpper().Equals("NOOP")) &
                                         (!command.ToUpper().Equals("ORIG")) &
                                         (!command.ToUpper().Equals("QUIT")))
                                    {
                                        command = "";

                                    }
                                }

                                switch (command.ToUpper().Trim())
                                {
                                    #region USER
                                    case "USER":
                                        if (!checkParameters(parameters))
                                        {
                                            this.SendData("101 ERROR", "\n");
                                            break;
                                        }
                                        bool bAlreadyConnected = false;
                                        string ipAlreadyConnected = "";
                                        lock (Server.shared.clients)
                                        {
                                            if (Server.shared.clients.Count > 0)
                                            {
                                                for (int i = 0; i < Server.shared.clients.Count; i++)
                                                {
                                                    IEnumerator cls = Server.shared.clients.Values.GetEnumerator();
                                                    while(cls.MoveNext())
                                                    {
                                                        ClientManager cl = (ClientManager)cls.Current;
                                                        if (cl.activeOper != null)
                                                        {
                                                            if (cl.activeOper.USERNAME.Equals(parameters))
                                                            {
                                                                bAlreadyConnected = true;
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        if (bAlreadyConnected)
                                        {
                                            this.SendData("101 KO", "\n");
                                            Server.logger.WriteLine(LogType.Notice, String.Format("Client already connected from {0} with username: {1}", ipAlreadyConnected, parameters));
                                        }
                                        else
                                        {
                                            this.activeOper = new CTIOperator();
                                            this.activeOper.USERNAME = parameters;
                                            this.SendData("100 OK", "\n");
                                        }
                                        break;
                                    #endregion

                                    #region PASS
                                    case "PASS":
                                        if (this.activeOper == null)
                                        {
                                            this.SendData("101 KO", "\n");
                                            break;
                                        }
                                        if (!checkParameters(parameters))
                                        {
                                            this.SendData("101 ERROR", "\n");
                                            break;
                                        }

                                        if (this.activeOper.LoadFromCredential(this.activeOper.USERNAME, parameters))
                                        {
                                            Server.shared.clients.Add(this.activeOper.EXT, this);
                                            this.SendData("102 OK", "\n");
                                        }
                                        else
                                        {
                                            this.SendData("101 KO", "\n");
                                            this.activeOper = null;
                                        }
                                        break;
                                    #endregion

                                    #region QUIT
                                    case "QUIT":
                                        if (this.activeOper != null)
                                        {
                                            Server.shared.clients.Remove(this.activeOper.EXT);
                                            this.activeOper = null;
                                        }
                                        this.SendData("900 BYE", "\n");
                                        reason = "Remote client closed connection";
                                        this.bRunning = false;
                                        break;
                                    #endregion

                                    #region ORIGINATE
                                    case "ORIG":
                                        // exten,context,prio
                                        if (!checkParameters(parameters))
                                        {
                                            this.SendData("101 ERROR", "\n");
                                            break;
                                        }
                                        string[] parms = parameters.Split('|');
                                        if (parms.Length < 3)
                                        {
                                            this.SendData("101 ERROR", "\n");
                                            break;
                                        }
                                        string orig_extn = parms[0];
                                        string orig_cntx = parms[1];
                                        string orig_prio = parms[2];
                                        string ast_cmd = this.BuildOriginateCommand(orig_extn, orig_cntx, orig_prio);
                                        Server.AstCTI.SendDataToAsterisk(ast_cmd);
                                        this.SendData("100 OK", "\n");
                                        break;
                                    #endregion

                                    #region HELO and NOOP
                                    case "NOOP":
                                        this.SendData("100 OK", "\n");
                                        break;
                                    case "HELO":
                                        this.SendData("100 OK", "\n");
                                        break;
                                    #endregion
                                }
                            }
                        }
                    }
                }
                catch (IOException)
                {

                    reason = "Client Socket timeout";
                    Server.logger.WriteLine(LogType.Notice, reason);
                    bRunning = false;
                    break;

                } // Timeout
                catch (SocketException)
                {
                    reason = "Socket is broken";
                    Server.logger.WriteLine(LogType.Notice, reason);
                    bRunning = false;
                    break;

                }
                catch (ObjectDisposedException ex)
                {
                    Server.logger.WriteLine(LogType.Debug, string.Format("ObjectDisposedException: {0}",ex.Source));
                }
                catch (Exception ex)
                {
                    Server.logger.WriteLine(LogType.Error, ex.ToString());
                }
            }

            this.CloseSockets(reason);
            if (this.activeOper != null)
                Server.shared.clients.Remove(this.activeOper.EXT);

            Interlocked.Decrement(ref Server.shared.NumberOfClients);
            Server.logger.WriteLine(LogType.Notice, String.Format("Number of clients connected: {0}",Server.shared.NumberOfClients));
        }
示例#4
0
        /// <summary>
        /// Stops the client thread
        /// </summary>
        public void Stop()
        {
            try
            {
                if (this.wThread.IsAlive)
                {
                    this.bRunning = false;
                    this.wThread.Join(200);
                }
                if (this.wThread.IsAlive)
                {
                    this.wThread.Abort();
                }
                if (this.ClientSocket.Connected)
                {
                    this.CloseSockets("");
                }

                if (this.activeOper != null)
                {
                    this.activeOper = null;
                    // this.sessione = null;
                }

            }
            catch (ThreadAbortException)
            {
                // Do thread deinit here..
            }

            this.wThread = null;
            if (this.ClientStopped != null) this.ClientStopped(this, this.remoteAddress);
        }