Exemple #1
0
        public void AcceptClient(TcpClient tclient)
        {
            Socket        socket = tclient.Client;
            NetworkStream stream = new NetworkStream(socket, true);

            stream.ReadTimeout = 90000;
            Sockets sks = new Sockets(tclient.Client.RemoteEndPoint as IPEndPoint, tclient.Client.LocalEndPoint as IPEndPoint, tclient, stream);

            sks.NewClientFlag = true;
            //客户端异步接收
            try
            {
                sks.nStream.BeginRead(sks.RecBuffer, 0, sks.RecBuffer.Length, new AsyncCallback(EndReader), sks);
            }
            catch
            {
                sks.Dispose();
            }
        }
Exemple #2
0
        private void EndReader(IAsyncResult ir)
        {
            Sockets     sks = ir.AsyncState as Sockets;
            RequestInfo req = null;

            if (sks != null)
            {
                if (sks.NewClientFlag || sks.Offset != 0)
                {
                    sks.NewClientFlag = false;
                    try
                    {
                        sks.Offset = sks.nStream.EndRead(ir);
                    }
                    catch
                    {
                        sks.Dispose();
                        sks.Offset = 0;
                    }
                    if (sks.Offset > 0)
                    {
                        string response = string.Empty;
                        int    status   = 200;
                        sks.ms.Position = sks.ms.Length;
                        sks.ms.Write(sks.RecBuffer, 0, sks.Offset);
                        if (sks.Total == 0 || sks.Total == sks.ms.Length)
                        {
                            try
                            {
                                req = MyIIS.Analyze(sks.ms, ref sks.Total);
                            }
                            catch (MyException me)
                            {
                                response = me.Message;
                                status   = 504;
                            }
                        }
                        if (req != null)
                        {
                            HttpProcessor processor = new HttpProcessor(_host, sks.Client.Client);
                            if (status != 200)
                            {
                                processor.SendResponse(status, Encoding.UTF8.GetBytes(response), null, req.KeepAlive);
                            }
                            else
                            {
                                string staticContentType = HttpProcessor.GetStaticContentType(req);
                                if (!string.IsNullOrEmpty(staticContentType)) //静态内容
                                {
                                    processor.WriteFileResponse(req.FilePath, staticContentType, req.GZip, req.KeepAlive);
                                }
                                else if (req.FilePath.EndsWith("/", StringComparison.Ordinal))  //目录
                                {
                                    string file = defaultDocument(_host.PhysicalDir, new string[] { "default.htm", "default.html", "index.htm", "index.html", "default.aspx", "index.aspx" });
                                    if (string.IsNullOrEmpty(file))
                                    {
                                        processor.WriteDirResponse(req.FilePath, req.KeepAlive);
                                    }
                                    else if (file.EndsWith(".aspx", StringComparison.Ordinal))
                                    {
                                        req.RawUrl         = req.RawUrl + file;
                                        req.FilePath       = req.FilePath + file;
                                        req.RemoteEndPoint = sks.Ip;
                                        req.LocalEndPoint  = sks.Lp;
                                        _host.ProcessRequest(ref processor, ref req);
                                    }
                                    else
                                    {
                                        processor.WriteFileResponse(req.FilePath + file, "text/html", req.GZip, req.KeepAlive);
                                    }
                                }
                                else
                                {
                                    req.RemoteEndPoint = sks.Ip;
                                    req.LocalEndPoint  = sks.Lp;
                                    _host.ProcessRequest(ref processor, ref req);
                                }
                            }
                            if (req.KeepAlive)
                            {
                                if (sks.Client != null && sks.Client.Client != null && sks.Client.Connected)
                                {
                                    if (sks.ms.CanWrite && sks.ms.Length > 0)
                                    {
                                        sks.ms.SetLength(0);
                                        sks.ms.Capacity = 0;
                                        sks.Total       = 0;
                                        GC.Collect(0, GCCollectionMode.Optimized);
                                    }
                                    try
                                    {
                                        sks.nStream.BeginRead(sks.RecBuffer, 0, sks.RecBuffer.Length, new AsyncCallback(EndReader), sks);
                                    }
                                    catch
                                    {
                                        sks.Dispose();
                                    }
                                }
                                else
                                {
                                    sks.Dispose();
                                }
                            }
                            else
                            {
                                sks.Dispose();
                            }
                        }
                        else
                        {
                            if (sks.Client.Connected)
                            {
                                try
                                {
                                    sks.nStream.BeginRead(sks.RecBuffer, 0, sks.RecBuffer.Length, new AsyncCallback(EndReader), sks);
                                }
                                catch
                                {
                                    sks.Dispose();
                                }
                            }
                            else
                            {
                                sks.Dispose();
                            }
                        }
                    }
                }
            }
        }