Exemplo n.º 1
0
 public void Clear()
 {
     try
     {
         foreach (HttpChannel channel in _channels)
         {
             channel.Close();
         }
     }
     catch (Exception ex)
     {
         ServerInfo.WriteErrorLog("HttpChannelColleciont Clear() 出现异常: " + ExceptionUtil.GetExceptionText(ex));
     }
     finally
     {
         _channels.Clear();
     }
 }
Exemplo n.º 2
0
        public async void Listen()
        {
            this.Listener = new TcpListener(IPAddress.Any, this.Port);
            this.Listener.Start();  // TODO: 要捕获异常

            Console.WriteLine("成功监听于 " + this.Port.ToString());

            while (this.IsActive)
            {
                try
                {
                    TcpClient tcpClient = await this.Listener.AcceptTcpClientAsync();

                    // string ip = ((IPEndPoint)s.Client.RemoteEndPoint).Address.ToString();
                    string ip = GetClientIP(tcpClient);
                    ServerInfo.WriteErrorLog("*** ip [" + ip + "] request");

#if NO
                    Thread thread = new Thread(() =>
                    {
                        // this.Processor.TestHandleClient(s);
                        TestHandleClient(tcpClient, ServerInfo._cancel.Token);
                    });
                    thread.Start();
#endif
                    Task.Run(() => TestHandleClient(tcpClient, ServerInfo._cancel.Token));

                    // TestHandleClient(tcpClient, ServerInfo._cancel.Token);
                }
                catch (Exception ex)
                {
                    if (this.IsActive == false)
                    {
                        break;
                    }
                    ServerInfo.WriteErrorLog("Listen() 出现异常: " + ExceptionUtil.GetExceptionMessage(ex));
                }
                Thread.Sleep(1);
            }
        }
Exemplo n.º 3
0
        public async void TestHandleClient(TcpClient tcpClient,
                                           CancellationToken token)
        {
            HttpChannel channel = ServerInfo._httpChannels.Add(tcpClient);

            try
            {
                List <byte> cache        = new List <byte>();
                string      ip           = "";
                Stream      inputStream  = tcpClient.GetStream();
                Stream      outputStream = tcpClient.GetStream();

                try
                {
                    ip = GetClientIP(tcpClient);
                    channel.Touch();

                    int  i       = 0;
                    bool running = true;
                    while (running)
                    {
                        HttpRequest request = await HttpProcessor.GetIncomingRequest(inputStream,
                                                                                     cache,
                                                                                     (headers) =>
                        {
#if NO
                            StringBuilder text = new StringBuilder();
                            text.Append(string.Join("\r\n", headers.Select(x => string.Format("{0}: {1}", x.Key, x.Value))));
                            Console.WriteLine("=== headers ===\r\n" + text.ToString());
#endif

                            if (headers.ContainsKey("User-Agent") == false)
                            {
                                return(false);
                            }
                            if (headers["User-Agent"] != "dp2LibraryClient")
                            {
                                throw new InvalidRequestException("请求不是来自 dp2LibraryClient");
                            }
                            return(true);
                        },
                                                                                     token);

                        if (request == null)
                        {
                            Console.WriteLine("client close on request " + i);
                            break;
                        }
                        Console.WriteLine("request " + i);

                        TimeSpan timeout = GetTimeout(request);
                        if (timeout != TimeSpan.MinValue)
                        {
                            channel.Touch(timeout);
                        }
                        else
                        {
                            channel.Touch();
                        }

                        // 添加头字段 _dp2router_clientip
                        request.Headers.Add("_dp2router_clientip", ip);

                        // Console.WriteLine("=== request ===\r\n" + request.Dump());
                        // ServerInfo.WriteErrorLog("=== request ===\r\n" + request.Dump());

                        HttpResponse response = await ServerInfo.WebCall(request, "content", timeout);   // content text.utf-7

                        channel.Touch();
                        // string content = response.GetContentString();

                        // Console.WriteLine("=== response ===\r\n" + response.Dump());

                        await HttpProcessor.WriteResponseAsync(outputStream, response, token);

                        channel.Touch();

                        // Console.WriteLine("http version = '"+request.Version+"'");

                        if (request.Headers.ContainsKey("Connection"))
                        {
                            string strValue = request.Headers["Connection"];
                            if (strValue == "Keep-Alive")
                            {
                                running = true;
                            }
                            else
                            {
                                running = false;
                            }
                        }
                        else
                        {
                            if (StringUtil.CompareVersion(request.Version, "1.1") >= 0)
                            {
                                running = true; // HTTP 1.1 默认就是 Connection Keep-Alive。即便没有 Connection 头字段,也是 Keep-Alive 状态
                            }
                            else
                            {
                                running = false;
                            }
                        }

                        i++;
                    }
                }
                catch (InvalidRequestException ex)
                {
                    // 2016/11/20
                    ServerInfo.WriteErrorLog("ip:" + ip + " : " + ex.Message);
                }
                catch (Exception ex)
                {
                    // 2016/11/14
                    ServerInfo.WriteErrorLog("ip:" + ip + " TestHandleClient() 异常: " + ExceptionUtil.GetExceptionText(ex));
                }
                finally
                {
                    outputStream.Flush();
                    outputStream.Close();
                    outputStream = null;

                    inputStream.Close();
                    inputStream = null;
                }
            }
            finally
            {
                ServerInfo._httpChannels.Remove(channel);
            }
        }
Exemplo n.º 4
0
 static void UnhandledExceptionTrapper(object sender, UnhandledExceptionEventArgs e)
 {
     ServerInfo.WriteErrorLog("全局异常: e.IsTerminating(" + e.IsTerminating + ")\r\n" + ExceptionUtil.GetExceptionText(e.ExceptionObject as Exception));
 }