0

我想用 C# 编写一个php 桌面项目,但在 C# 中找不到可用的嵌入式 Web 服务器(我尝试过 Mongoose,但未能将它的文​​件编译成 dll),所以我用 C# 构建了一个简单的 Web 服务器,使用内嵌NET HTTP Server,自己编写php-cgi进程代码。

但是在我完成服务器后,使用 Mongoose 和我的服务器运行相同的 PHP 项目(Laravel)。我发现 Mongoose 服务器的响应速度比我的服务器更快。

我无法弄清楚我错过了哪一点。

HTTP 服务器使用System.Net.Sockets.Socket该类来执行异步网络通信。

部分代码(初始化服务器)如​​下:

ss = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
ss.Bind(new IPEndPoint(IPAddress.Any, port));
ss.Listen(100);
ss.BeginAccept(new AsyncCallback(AcceptCallback), ss);

PHP处理程序方法:

    bool phpHandler(HttpRequest request, HttpResponse response)
    {
        string phpScript = rootScriptPath + "\\index.php";
        Dictionary<string, string> envs = new Dictionary<string, string>();

        setEnvs(envs, request);
        PHPProcess pp = new PHPProcess(phpScript, envs);

        pp.start();
        if (request.Method == "POST")
        {

            pp.process.StandardInput.AutoFlush = true;
            pp.process.StandardInput.Write(request.Content);

            pp.process.StandardInput.Close();
        }
        string outPutContent = pp.readRes();

        //separate header and body in php-cgi output
        string[] outPutSlices = outPutContent.Split(new String[] { "\r\n" }, StringSplitOptions.None);

        bool headerEndFlag = false;

        string bodyContent = "";
        foreach (string outPutSlice in outPutSlices)
        {

            if (outPutSlice.Length > 0)
            {
                if (headerEndFlag)
                {
                    bodyContent += outPutSlice + "\r\n";

                }
                else
                {
                    if (outPutSlice.IndexOf(':') > 0)
                    {
                        string[] headerSingleSlices = outPutSlice.Split(new Char[] { ':' });
                        if (!response.Header.ContainsKey(headerSingleSlices[0]))
                            response.Header.Add(headerSingleSlices[0], headerSingleSlices[1]);
                    }
                }

            }
            else
            {
                headerEndFlag = true;
            }

        }
        if (bodyContent.Length > 2)
        {
            bodyContent = bodyContent.Remove(bodyContent.Length - 2);
        }

        response.Content = bodyContent;
        response.ReturnCode = 200;
        pp.destory();
        return true;
    }

PHP进程类:

class PHPProcess
{
    private string phpDir = System.Environment.CurrentDirectory + "\\" + Properties.HcParams.Default.php_path;
    public Process process;

    public PHPProcess(string entranceScript,Dictionary<string,string> envs){
        process = new Process();
        string scriptName = "\\" + entranceScript.Split(new Char[]{'\\'}).Last();
        Console.WriteLine("ScriptName:" + scriptName);
        process.StartInfo.FileName = phpDir + "\\php-cgi.exe";
        process.StartInfo.Arguments = entranceScript;
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.StandardOutputEncoding = Encoding.UTF8;
        process.StartInfo.RedirectStandardInput = true;
        process.StartInfo.CreateNoWindow = true;
        process.StartInfo.EnvironmentVariables.Add("REDIRECT_STATUS", "200");
        process.StartInfo.EnvironmentVariables.Add("SCRIPT_NAME", scriptName);
        process.StartInfo.EnvironmentVariables.Add("SCRIPT_FILENAME", entranceScript);
        process.StartInfo.EnvironmentVariables.Add("SERVER_INTERFACE", "CGI/1.1");
        process.StartInfo.EnvironmentVariables.Add("GETWAY_INTERFACE", "CGI/1.1");
        process.StartInfo.EnvironmentVariables.Add("SERVER_PROTOCOL", "HTTP/1.1");
        process.StartInfo.EnvironmentVariables.Add("SERVER_PORT", Bmain.StartPort.ToString());
        foreach (var env in envs)
        {
            process.StartInfo.EnvironmentVariables.Add(env.Key, env.Value);
        }
    }

    public void start(){
        process.Start();

    }
    public string readRes()
    {
        string OutputText = process.StandardOutput.ReadToEnd();
        return OutputText;
    }

    public void destory(){
        process.WaitForExit();
        process.Close();
    }
}

我该怎么做才能优化速度?

4

0 回答 0