private bool CheckControl(ref SocketControlClient client, string[] cmd)
 {
     if (cmd.Length > 1 & cmd[0].ToUpper() == "ADMIN")
     {
         UserData admin = App.checkAdmin(cmd[1]);
         if (admin != null)
         {
             client.type = TYPE.ADMIN;
         }
         else
         {
             client.type = TYPE.NONE;
         }
     }
     else if (cmd.Length > 1)
     {
         bool flag = App.checkControl(cmd[1]);
         if (flag)
         {
             client.type = TYPE.USER;
         }
         else
         {
             client.type = TYPE.NONE;
         }
     }
     return(client.type != TYPE.NONE);
 }
 private void RunCMD(SocketControlClient client, string cmd)
 {
     if (client.type != TYPE.NONE && cmd.Length > 0)
     {
         if (cmd.ToUpper() == "PLAY")
         {
             if (this.PlayVlc != null)
             {
                 this.PlayVlc(this, client.Sock.RemoteEndPoint);
             }
         }
         else if (cmd.ToUpper() == "STOP")
         {
             if (this.StopVlc != null)
             {
                 this.StopVlc(this, client.Sock.RemoteEndPoint);
             }
         }
         else if (cmd.ToUpper() == "TURNOFF" && client.type == TYPE.ADMIN)
         {
             if (this.TurnOffApp != null)
             {
                 TurnOffApp(this, client.Sock.RemoteEndPoint);
             }
         }
         else if (cmd.ToUpper() == "SCREEN")
         {
             if (this.SendPlaying != null)
             {
                 this.SendPlaying(this, client.Sock.RemoteEndPoint);
             }
         }
     }
 }
        /// <summary>
        /// Add the given connection to our list of clients
        /// Note we have a new friend
        /// Send a welcome to the new client
        /// Setup a callback to recieve data
        /// </summary>
        /// <param name="sockClient">Connection to keep</param>
        //public void NewConnection( TcpListener listener )
        public void NewConnection(Socket sockClient)
        {
            // Program blocks on Accept() until a client connects.
            //SocketChatClient client = new SocketChatClient( listener.AcceptSocket() );
            SocketControlClient client = new SocketControlClient(sockClient);

            m_aryClients.Add(client);
            Console.WriteLine("Client {0}, joined", client.Sock.RemoteEndPoint);
            // Get current date and time.
            String strDateLine = "NOONCE|" + this.keySerect;

            // Convert to byte array and send.
            Byte[] byteDateLine = System.Text.Encoding.ASCII.GetBytes(strDateLine.ToCharArray());
            client.Sock.Send(byteDateLine, byteDateLine.Length, 0);
            client.SetupRecieveCallback(this);
        }
        public void SendMsg(String Msg, EndPoint ip)
        {
            int count = this.m_aryClients.Count;

            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    SocketControlClient client = this.m_aryClients[i] as SocketControlClient;
                    if (client.Sock.RemoteEndPoint == ip)
                    {
                        client.Sock.Send(getByteText(Msg));
                        return;
                    }
                }
            }
        }
        /// <summary>
        /// Gui tin nhan den tat ca cac may
        /// </summary>
        /// <param name="Msg"></param>
        public void SendMsg(String Msg)
        {
            int count = this.m_aryClients.Count;

            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    SocketControlClient client = this.m_aryClients[i] as SocketControlClient;
                    try
                    {
                        client.Sock.Send(getByteText(Msg));
                    }
                    catch (Exception ex)
                    {
#if DEBUG
                        MessageBox.Show(ex.Message);
#endif
                    }
                }
            }
        }
        /// <summary>
        /// Get the new data and send it out to all other connections.
        /// Note: If not data was recieved the connection has probably
        /// died.
        /// </summary>
        /// <param name="ar"></param>
        public void OnRecievedData(IAsyncResult ar)
        {
            SocketControlClient client = (SocketControlClient)ar.AsyncState;

            byte[] aryRet = client.GetRecievedData(ar);
            if (aryRet.Length < 1)
            {
                Console.WriteLine("Client {0}, disconnected", client.Sock.RemoteEndPoint);
                client.Sock.Close();
                m_aryClients.Remove(client);
                return;
            }
            string str = System.Text.Encoding.Default.GetString(aryRet);

            Console.WriteLine(str);
            if (client.type == TYPE.NONE)
            {
                string[] dataStr = str.Split('_');
                bool     flag    = this.CheckControl(ref client, dataStr);
                if (!flag)
                {
                    client.Sock.Send(getByteText("FAIL|200|CONTROL"));
                    client.Sock.Close();
                    m_aryClients.Remove(client);
                }
                else
                {
                    client.Sock.Send(getByteText("OK|200|CONTROL"));
                    client.SetupRecieveCallback(this);
                }
            }
            else
            {
                #region Comment

                //    if (dataStr.Length > 1 & dataStr[0].ToUpper() == "ADMIN")
                //    {
                //        if (CommonUtilities.checkAdminColtrol(dataStr[1]))
                //        {
                //            client.flag_admin_Control = true;
                //            client.flag_can_Coltrol = true;
                //            client.Sock.Send(getByteText("OK|200|ADMIN"));
                //            client.SetupRecieveCallback(this);
                //        }
                //        else
                //        {
                //            client.flag_can_Coltrol = false;
                //            client.Sock.Send(getByteText("FAIL|205|ADMIN"));
                //            client.Sock.Close();
                //            m_aryClients.Remove(client);
                //        }
                //    }
                //    else
                //    {
                //        if (CommonUtilities.userColtrol.checkLoginNetWork(CommonUtilities.keySerect,str) && flag_login_user)
                //        {
                //            client.flag_admin_Control = false;
                //            client.flag_can_Coltrol = true;
                //            client.Sock.Send(getByteText("OK|200"));
                //            client.SetupRecieveCallback(this);

                //        }
                //        else
                //        {
                //            client.flag_admin_Control = false;
                //            client.flag_can_Coltrol = false;
                //            client.Sock.Send(getByteText("FAIL|205"));
                //            client.Sock.Close();
                //            m_aryClients.Remove(client);
                //        }

                //    }
                //}
                //else
                //{
                //    if (client.flag_admin_Control)
                //    {
                //        if (str.ToUpper() == "PLAY")
                //        {
                //            this.adminControl = _controlVLC._CONTROL_PLAY;
                //            this.PlayVlc(this, client.Sock.RemoteEndPoint);
                //        }
                //        else if (str.ToUpper() == "PAUSE")
                //        {
                //            this.adminControl = _controlVLC._CONTROL_PAUSE;
                //        }
                //        else if (str.ToUpper() == "STOP")
                //        {
                //            this.adminControl = _controlVLC._CONTROL_STOP;
                //            this.StopVlc(this, client.Sock.RemoteEndPoint);

                //        }
                //        else if (str.ToUpper() == "NEXT")
                //        {
                //            this.adminControl = _controlVLC._CONTROL_NEXT;
                //        }
                //        else if (str.ToUpper() == "BACK")
                //        {
                //            this.adminControl = _controlVLC._CONTROL_BACK;
                //        }
                //        else if (str.ToUpper() == "STREAM")
                //        {
                //            this.adminControl = _controlVLC._CONTROL_STREAM;
                //            this.ipHostStream = parseIP(client.Sock.RemoteEndPoint);
                //            if (PlayStreaming != null)
                //            {
                //                PlayStreaming(this, this.ipHostStream);
                //            }
                //        }
                //        else if(str.ToUpper()=="ABORT")
                //        {
                //            this.adminControl = _controlVLC._CONTROL_ABORT_USER;
                //        }
                //        else if (str.ToUpper() == "ACCEPT")
                //        {
                //            this.adminControl = _controlVLC._CONTROL_ACCEPT_USER;
                //        }
                //        else if (str.ToUpper() == "SCREEN")
                //        {
                //            if (SendPlaying != null)
                //            {
                //                SendPlaying(this, client.Sock.RemoteEndPoint);
                //            }
                //            this.adminControl = _controlVLC._CONTROL_SCREEN;
                //        }
                //        else if (str.ToUpper() == "TURN_OFF")
                //        {
                //            this.adminControl = _controlVLC._CONTROL_OFF;
                //            if(this.TurnOffApp!=null)
                //                TurnOffApp(this, client.Sock.RemoteEndPoint);
                //        }
                //        else if (str.ToUpper() == "STOPSTREAM")
                //        {
                //            this.dataControl = _controlVLC._CONTROL_STREAM_STOP;
                //            if (StopStreaming != null)
                //                StopStreaming(this, new EventArgs());

                //        }
                //        else
                //        {
                //            this.adminControl = _controlVLC._CONTROL_FREE;
                //        }
                //    }
                //    else
                //    {
                //        if (str.ToUpper() == "PLAY")
                //        {
                //            this.dataControl = _controlVLC._CONTROL_PLAY;
                //            if (this.PlayVlc!=null)
                //            {
                //                this.PlayVlc(this, client.Sock.RemoteEndPoint);
                //            }
                //        }
                //        else if (str.ToUpper() == "PAUSE")
                //        {
                //            this.dataControl = _controlVLC._CONTROL_PAUSE;
                //        }
                //        else if (str.ToUpper() == "STOP")
                //        {
                //            this.dataControl = _controlVLC._CONTROL_STOP;
                //            if (this.StopVlc != null)
                //            {
                //                this.StopVlc(this, client.Sock.RemoteEndPoint);
                //            }
                //        }
                //        else if (str.ToUpper() == "NEXT")
                //        {
                //            this.dataControl = _controlVLC._CONTROL_NEXT;
                //        }
                //        else if (str.ToUpper() == "BACK")
                //        {
                //            this.dataControl = _controlVLC._CONTROL_BACK;
                //        }
                //        else if (str.ToUpper() == "STREAM")
                //        {
                //            this.dataControl = _controlVLC._CONTROL_STREAM;
                //            this.ipHostStream = parseIP(client.Sock.RemoteEndPoint);
                //            if (PlayStreaming != null)
                //            {
                //                PlayStreaming(this, this.ipHostStream);
                //            }

                //        }
                //        else if (str.ToUpper() == "SCREEN"){
                //            if (SendPlaying != null)
                //            {
                //                SendPlaying(this, client.Sock.RemoteEndPoint);
                //            }
                //            this.dataControl = _controlVLC._CONTROL_SCREEN;
                //        }
                //        else if (str.ToUpper() == "STOPSTREAM")
                //        {
                //            this.dataControl = _controlVLC._CONTROL_STREAM_STOP;
                //            if (StopStreaming != null)
                //                StopStreaming(this, new EventArgs());

                //        }
                //        else
                //        {
                //            this.dataControl = _controlVLC._CONTROL_FREE;
                //        }
                //    }
                #endregion Comment

                this.RunCMD(client, str);
                client.Sock.Send(getByteText("OK|200|" + str.ToUpper()));
                client.SetupRecieveCallback(this);
            }
        }
 private void RunCMD(SocketControlClient client, string cmd)
 {
     if (client.type != TYPE.NONE && cmd.Length>0) 
     {
         if (cmd.ToUpper() == "PLAY")
         {
             if (this.PlayVlc != null)
             {
                 this.PlayVlc(this, client.Sock.RemoteEndPoint);
             }
         }
         else if (cmd.ToUpper() == "STOP")
         {
             if (this.StopVlc != null)
             {
                 this.StopVlc(this, client.Sock.RemoteEndPoint);
             }
         }
         else if (cmd.ToUpper() == "TURNOFF" && client.type== TYPE.ADMIN)
         {
               if(this.TurnOffApp!=null)
                     TurnOffApp(this, client.Sock.RemoteEndPoint);
         }
         else if (cmd.ToUpper() == "SCREEN")
         {
             if (this.SendPlaying != null)
             {
                 this.SendPlaying(this, client.Sock.RemoteEndPoint);
             }
         }
     }
 }
 private bool CheckControl(ref SocketControlClient client, string[] cmd)
 {
     if (cmd.Length > 1 & cmd[0].ToUpper() == "ADMIN")
     {
         UserData admin = App.checkAdmin(cmd[1]);
         if (admin != null)
         {
             client.type = TYPE.ADMIN;
         }
         else
         {
             client.type = TYPE.NONE;
         }
     }
     else if(cmd.Length>1)
     {
         bool flag = App.checkControl(cmd[1]);
         if (flag)
         {
             client.type = TYPE.USER;
         }
         else
         {
             client.type = TYPE.NONE;
         }
     }
     return client.type != TYPE.NONE;
 }
        /// <summary>
        /// Add the given connection to our list of clients
        /// Note we have a new friend
        /// Send a welcome to the new client
        /// Setup a callback to recieve data
        /// </summary>
        /// <param name="sockClient">Connection to keep</param>
        //public void NewConnection( TcpListener listener )
        public void NewConnection(Socket sockClient)
        {
            // Program blocks on Accept() until a client connects.
            //SocketChatClient client = new SocketChatClient( listener.AcceptSocket() );
            SocketControlClient client = new SocketControlClient(sockClient);
            m_aryClients.Add(client);
            Console.WriteLine("Client {0}, joined", client.Sock.RemoteEndPoint);
            // Get current date and time.           
            String strDateLine = "NOONCE|" + this.keySerect;

            // Convert to byte array and send.
            Byte[] byteDateLine = System.Text.Encoding.ASCII.GetBytes(strDateLine.ToCharArray());
            client.Sock.Send(byteDateLine, byteDateLine.Length, 0);
            client.SetupRecieveCallback(this);
        }