Пример #1
0
        /// <summary>
        /// Handles the received commands of the m_socketServer control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="args">The <see cref="SocketServer.SocketEventArgs"/> instance containing the event data.</param>
        void m_socketServer_NewMessage(object sender, SocketEventArgs e)
        {
            OpResult opResult = new OpResult(OpStatusCode.BadRequest);

            try
            {
                if (e.Message.Length == 0)
                {
                    return;
                }

                if (e.Message.Equals("help", StringComparison.InvariantCultureIgnoreCase))
                {
                    opResult = m_remoteCommands.CommandList(GetPortNumber(m_basePortNumber));
                }
                else if (e.Message.Equals("exit", StringComparison.InvariantCultureIgnoreCase))
                {
                    m_socketServer.CloseClient(e.TcpClient);
                    return;
                }
                else
                {
                    string[] command = e.Message.Split(new char[] { ' ' }, 2);
                    if (command.Length == 0)
                    {
                        return;
                    }

                    opResult = m_remoteCommands.Execute(command[0], (command.Length == 2 ? command[1] : string.Empty));
                }
                m_socketServer.SendMessage(string.Format("{0} {1}\r\n", (int)opResult.StatusCode, opResult.StatusText), e.TcpClient);
                if (opResult.StatusCode == OpStatusCode.Ok)
                {
                    m_socketServer.SendMessage(opResult.ToString(), e.TcpClient);
                    m_socketServer.SendMessage(".\r\n", e.TcpClient);
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError(ex.ToString());
            }
        }
Пример #2
0
        void NewRequestThread(Object o)
        {
            SocketAsyncEventArgs e = (SocketAsyncEventArgs)o;
            DataHolderToken token = (DataHolderToken)e.UserToken;

            OpResult opResult = new OpResult(OpStatusCode.BadRequest);
            string sCommand = "";
            string sParam = "";
            string sBody = "";
            string sTempBody = "";
            try
            {
                // Show error for index
                if (token.httpRequest.Length == 0)
                {
                    sCommand = "<i>No command specified.</i>";
                    sParam = "<i>No parameters specified.</i>";
                }
                else
                {
                    string[] req = token.httpRequest.Split(new char[] { '?' }, 2); //Strip off "?"
                    string[] cmd_stack = req[0].Split(new char[] { '/' });
                    for (int idx = 0; idx < cmd_stack.Length; idx++)
                    {
                        sTempBody = "";
                        string[] command = cmd_stack[idx].Split(new char[] { ' ' }, 2);
                        if (command.Length == 0)
                            return;
                        sCommand = command[0];
                        sParam = (command.Length == 2 ? command[1] : string.Empty);

                        if (sCommand.Equals("help", StringComparison.InvariantCultureIgnoreCase))
                        {
                            opResult = m_remoteCommands.CommandListHTML(AddInModule.GetPortNumber(AddInModule.m_basePortNumber));
                        }
                        else if (sCommand.Equals("format", StringComparison.InvariantCultureIgnoreCase))
                        {
                            ICommand formatter = new customCmd(sBody);
                            opResult = formatter.Execute(sParam);
                            sBody = "";
                        }
                        else if (token.opResult != null)
                        {
                            opResult = token.opResult;
                        }
                        else if (sCommand.Equals("music-list-stats"))
                        {
                            opResult = getLibraryStats();
                        }
                        else
                        {
                            opResult = m_remoteCommands.Execute(sCommand, sParam, token.nowPlaying, token.currentMedia);
                        }

                        //If cache was cleared, start another build
                        if (sCommand.Equals("music-clear-cache") && opResult.StatusCode == OpStatusCode.Ok)
                        {
                            startCacheBuildNow();
                        }

                        sTempBody = opResult.ToString();

                        if (sParam.Length == 0)
                        {
                            sParam = "<i>No parameters specified.</i>";
                        }

                        if (opResult.StatusCode != OpStatusCode.Json)
                        {
                            if (opResult.StatusCode != OpStatusCode.Ok && opResult.StatusCode != OpStatusCode.Success)
                            {
                                sTempBody = string.Format("<h1>ERROR<hr>Command: {0}<br>Params: {1}<br>Returned: {2} - {3}<hr>See <a href='help'>Help</a></h1>", sCommand, sParam, opResult.StatusCode, opResult.ToString());
                            }
                            else if (opResult.StatusCode != OpStatusCode.OkImage)
                            {
                                if (sTempBody.Length > 0)
                                {
                                    if (sTempBody.TrimStart()[0] != '<')
                                    {
                                        sTempBody = "<pre>" + sTempBody + "</pre>";
                                    }
                                }
                                else
                                {
                                    sTempBody = string.Format("<h1>Ok<hr>Last Command: '{0}'<br>Params: {1}<br>Returned: {2}<hr>See <a href='help'>Help</a></h1>", sCommand, sParam, opResult.StatusCode);
                                }
                            }
                            if (sBody.Length > 0)
                            {
                                sBody += "<HR>";
                            }
                            sBody += sTempBody;
                        }
                    }
                }

                //Get bytes to send to browser
                if (opResult.StatusCode == OpStatusCode.Json)
                {
                    token.dataToSend = GetPageJsonDataToSend(opResult.ToString());
                }
                else if (opResult.StatusCode == OpStatusCode.OkImage)
                {
                    token.dataToSend = GetImageDataToSend(opResult.ToString(), opResult.StatusText);
                }
                else
                {
                    token.dataToSend = GetPageDataToSend(string.Format("{0}\r\n", sBody));
                }
            }
            catch (Exception ex)
            {
                token.dataToSend = GetPageDataToSend(string.Format("<html><body>EXCEPTION: {0}<hr></body></html>", ex.Message));
                Trace.TraceError(ex.ToString());
            }

            //Set send operation variables
            token.sendBytesRemainingCount = token.dataToSend.Length;
            token.bytesSentAlreadyCount = 0;

            StartSend(e);
        }
Пример #3
0
        /// <summary>
        /// Handles the received commands of the m_socketServer control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="args">The <see cref="SocketServer.SocketEventArgs"/> instance containing the event data.</param>
        void m_socketServer_NewMessage(object sender, SocketEventArgs e)
        {
            OpResult opResult = new OpResult(OpStatusCode.BadRequest);
            try
            {
                if (e.Message.Length == 0)
                    return;

                if (e.Message.Equals("help", StringComparison.InvariantCultureIgnoreCase))
                {
                    opResult = m_remoteCommands.CommandList(GetPortNumber(m_basePortNumber));
                }
                else if (e.Message.Equals("exit", StringComparison.InvariantCultureIgnoreCase))
                {
                    m_socketServer.CloseClient(e.TcpClient);
                    return;
                }
                else
                {
                    string[] command = e.Message.Split(new char[] { ' ' }, 2);
                    if (command.Length == 0)
                        return;

                    opResult = m_remoteCommands.Execute(command[0], (command.Length == 2 ? command[1] : string.Empty));
                }
                m_socketServer.SendMessage(string.Format("{0} {1}\r\n", (int)opResult.StatusCode, opResult.StatusText), e.TcpClient);
                if (opResult.StatusCode == OpStatusCode.Ok)
                {
                    m_socketServer.SendMessage(opResult.ToString(), e.TcpClient);
                    m_socketServer.SendMessage(".\r\n", e.TcpClient);
                }

            }
            catch (Exception ex)
            {
                Trace.TraceError(ex.ToString());
            }
        }
Пример #4
0
        void m_httpServer_NewRequest_thread(HttpEventArgs e)
        {
            OpResult opResult = new OpResult(OpStatusCode.BadRequest);
            string sCommand = "";
            string sParam = "";
            string sBody = "";
            string sTempBody = "";

            try
            {
                // Show error for index
                if (e.Request.Length == 0)
                {
                    sCommand = "<i>No command specified.</i>";
                    sParam = "<i>No parameters specified.</i>";
                }
                else
                {
                    string[] req = e.Request.Split(new char[] { '?' }, 2); //Strip off "?"
                    string[] cmd_stack = req[0].Split(new char[] { '/' });
                    for (int idx = 0; idx < cmd_stack.Length; idx++)
                    {
                        sTempBody = "";
                        string[] command = cmd_stack[idx].Split(new char[] { ' ' }, 2);
                        if (command.Length == 0)
                            return;
                        sCommand = command[0];
                        sParam = (command.Length == 2 ? command[1] : string.Empty);
                        if (sCommand.Equals("help", StringComparison.InvariantCultureIgnoreCase))
                            opResult = m_remoteCommands.CommandListHTML(GetPortNumber(m_basePortNumber));
                        else if (sCommand.Equals("format", StringComparison.InvariantCultureIgnoreCase))
                        {
                            ICommand formatter = new customCmd(sBody);
                            opResult = formatter.Execute(sParam);
                            sBody = "";
                        }
                        else opResult = m_remoteCommands.Execute(sCommand, sParam);
                        sTempBody = opResult.ToString();
                        if (sParam.Length == 0) sParam = "<i>No parameters specified.</i>";
                        if (opResult.StatusCode != OpStatusCode.Ok && opResult.StatusCode != OpStatusCode.Success)
                        {
                            sTempBody = string.Format("<h1>ERROR<hr>Command: {0}<br>Params: {1}<br>Returned: {2} - {3}<hr>See <a href='help'>Help</a></h1>", sCommand, sParam, opResult.StatusCode, opResult.ToString());
                            //if (sBody.Length > 0) sBody += "<HR>";
                            //sBody += sTempBody;
                            //break;
                        }
                        else if (opResult.StatusCode != OpStatusCode.OkImage)
                        {
                            if (sTempBody.Length > 0)
                            {
                                if (sTempBody.TrimStart()[0] != '<') sTempBody = "<pre>" + sTempBody + "</pre>";
                            }
                            else
                            {
                                sTempBody = string.Format("<h1>Ok<hr>Last Command: '{0}'<br>Params: {1}<br>Returned: {2}<hr>See <a href='help'>Help</a></h1>", sCommand, sParam, opResult.StatusCode);
                            }
                            //if (sBody.Length > 0) sBody += "<HR>";
                            //sBody += sTempBody;
                        }
                        if (sBody.Length > 0) sBody += "<HR>";
                        sBody += sTempBody;
                    }
                }
                if (opResult.StatusCode == OpStatusCode.OkImage) m_httpServer.SendImage(opResult.ToString(), opResult.StatusText, e.HttpSocket);
                else m_httpServer.SendPage(string.Format("{0}\r\n", sBody), e.HttpSocket);
            }
            catch (Exception ex)
            {
                m_httpServer.SendPage(string.Format("<html><body>EXCEPTION: {0}<hr></body></html>",
                        ex.Message), e.HttpSocket);
                Trace.TraceError(ex.ToString());
            }
        }
Пример #5
0
        void m_httpServer_NewRequest_thread(HttpEventArgs e)
        {
            OpResult opResult  = new OpResult(OpStatusCode.BadRequest);
            string   sCommand  = "";
            string   sParam    = "";
            string   sBody     = "";
            string   sTempBody = "";

            try
            {
                // Show error for index
                if (e.Request.Length == 0)
                {
                    sCommand = "<i>No command specified.</i>";
                    sParam   = "<i>No parameters specified.</i>";
                }
                else
                {
                    string[] req       = e.Request.Split(new char[] { '?' }, 2); //Strip off "?"
                    string[] cmd_stack = req[0].Split(new char[] { '/' });
                    for (int idx = 0; idx < cmd_stack.Length; idx++)
                    {
                        sTempBody = "";
                        string[] command = cmd_stack[idx].Split(new char[] { ' ' }, 2);
                        if (command.Length == 0)
                        {
                            return;
                        }
                        sCommand = command[0];
                        sParam   = (command.Length == 2 ? command[1] : string.Empty);
                        if (sCommand.Equals("help", StringComparison.InvariantCultureIgnoreCase))
                        {
                            opResult = m_remoteCommands.CommandListHTML(GetPortNumber(m_basePortNumber));
                        }
                        else if (sCommand.Equals("format", StringComparison.InvariantCultureIgnoreCase))
                        {
                            ICommand formatter = new customCmd(sBody);
                            opResult = formatter.Execute(sParam);
                            sBody    = "";
                        }
                        else
                        {
                            opResult = m_remoteCommands.Execute(sCommand, sParam);
                        }
                        sTempBody = opResult.ToString();
                        if (sParam.Length == 0)
                        {
                            sParam = "<i>No parameters specified.</i>";
                        }
                        if (opResult.StatusCode != OpStatusCode.Ok && opResult.StatusCode != OpStatusCode.Success)
                        {
                            sTempBody = string.Format("<h1>ERROR<hr>Command: {0}<br>Params: {1}<br>Returned: {2} - {3}<hr>See <a href='help'>Help</a></h1>", sCommand, sParam, opResult.StatusCode, opResult.ToString());
                            //if (sBody.Length > 0) sBody += "<HR>";
                            //sBody += sTempBody;
                            //break;
                        }
                        else if (opResult.StatusCode != OpStatusCode.OkImage)
                        {
                            if (sTempBody.Length > 0)
                            {
                                if (sTempBody.TrimStart()[0] != '<')
                                {
                                    sTempBody = "<pre>" + sTempBody + "</pre>";
                                }
                            }
                            else
                            {
                                sTempBody = string.Format("<h1>Ok<hr>Last Command: '{0}'<br>Params: {1}<br>Returned: {2}<hr>See <a href='help'>Help</a></h1>", sCommand, sParam, opResult.StatusCode);
                            }
                            //if (sBody.Length > 0) sBody += "<HR>";
                            //sBody += sTempBody;
                        }
                        if (sBody.Length > 0)
                        {
                            sBody += "<HR>";
                        }
                        sBody += sTempBody;
                    }
                }
                if (opResult.StatusCode == OpStatusCode.OkImage)
                {
                    m_httpServer.SendImage(opResult.ToString(), opResult.StatusText, e.HttpSocket);
                }
                else
                {
                    m_httpServer.SendPage(string.Format("{0}\r\n", sBody), e.HttpSocket);
                }
            }
            catch (Exception ex)
            {
                m_httpServer.SendPage(string.Format("<html><body>EXCEPTION: {0}<hr></body></html>",
                                                    ex.Message), e.HttpSocket);
                Trace.TraceError(ex.ToString());
            }
        }