コード例 #1
0
ファイル: PhpCgiTransfer.cs プロジェクト: chaosannals/pws
        public IEnumerable <FastCgiMessage> Transfer(TcpClient target)
        {
            byte[]        buffer    = new byte[8192];
            NetworkStream requester = Source.GetStream();
            NetworkStream responser = target.GetStream();
            FastCgiParser sfcp      = new FastCgiParser();
            FastCgiParser tfcp      = new FastCgiParser();

            while (true)
            {
                // 请求数据传递
                if (requester.DataAvailable)
                {
                    int count = requester.Read(buffer, 0, buffer.Length);
                    responser.Write(buffer, 0, count);
                    sfcp.Gain(buffer, count);
                    while (true)
                    {
                        FastCgiMessage m = sfcp.Pop();
                        if (m == null)
                        {
                            break;
                        }
                        yield return(m);
                    }
                }

                // 响应数据传递
                if (responser.DataAvailable)
                {
                    int count = responser.Read(buffer, 0, buffer.Length);
                    requester.Write(buffer, 0, count);
                    tfcp.Gain(buffer, count);
                    while (true)
                    {
                        FastCgiMessage m = tfcp.Pop();
                        if (m == null)
                        {
                            break;
                        }
                        yield return(m);
                    }
                }
                yield return(null);
            }
        }
コード例 #2
0
        /// <summary>
        /// 传输
        /// </summary>
        public void Transfer()
        {
            NetworkStream requester = Source.GetStream();
            NetworkStream responser = Target.GetStream();

            byte[]        buffer = new byte[8192];
            DateTime      begin  = DateTime.Now;
            FastCgiParser fcms   = new FastCgiParser();
            FastCgiParser fcmt   = new FastCgiParser();

            try
            {
                bool able = true;
                while (able)
                {
                    // 转发请求信息。
                    if (requester.DataAvailable)
                    {
                        DateTime start = DateTime.Now;
                        int      count = requester.Read(buffer, 0, buffer.Length);
                        responser.Write(buffer, 0, count);
                        fcms.Gain(buffer, count);
                        while (true)
                        {
                            FastCgiMessage m = fcms.Pop();
                            if (m == null)
                            {
                                break;
                            }
                            if (m.Header.Type == FastCgiType.AbortRequest)
                            {
                                able = false;
                            }
                            if (m.Header.Type == FastCgiType.BeginRequest)
                            {
                                FastCgiBeginRequestBody body = m.AsBeginBody();
                                "{0} => {1}".Log(m, body);
                            }
                            else
                            {
                                m.ToString().Log();
                            }
                        }
                    }

                    // 接收响应内容。
                    if (responser.DataAvailable)
                    {
                        DateTime start = DateTime.Now;
                        int      count = responser.Read(buffer, 0, buffer.Length);
                        requester.Write(buffer, 0, count);
                        fcmt.Gain(buffer, count);

                        // 分析 FastCGI
                        while (true)
                        {
                            FastCgiMessage m = fcmt.Pop();
                            if (m == null)
                            {
                                break;
                            }
                            if (m.Header.Type == FastCgiType.EndRequest)
                            {
                                able = false;
                                FastCgiEndRequestBody body = m.AsEndBody();
                                "{0} => {1}".Log(m, body);
                            }
                            else
                            {
                                m.ToString().Log();
                            }
                        }
                    }
                }
            }
            finally
            {
                TimeSpan d = DateTime.Now.Subtract(begin);
                "转发耗时 {0:N} ms".Log(d.TotalMilliseconds);
                // 结束时回收资源。
                requester.Dispose();
                responser.Dispose();
            }
        }