Exemple #1
0
        private Tuple <IHttpHandler, string> GetHttpHandler(Stream inputStream, byte[] prefix, HttpRequest request)
        {
            var handle         = default(Tuple <IHttpHandler, string>);
            var buffer         = new byte[1024 * 1024];
            var byteBag        = new List <byte>();
            var delimiterBytes = Encoding.UTF8.GetBytes(Environment.NewLine + Environment.NewLine);
            var count          = int.MaxValue;
            var index          = -1;

            do
            {
                count = 0;
                SystemHelper.InvokeSilently(delegate { count = inputStream.Read(buffer, 0, buffer.Length); });
                if (count > 0)
                {
                    var buffer1 = buffer.Take(count).ToArray();
                    index = buffer1.IndexOf(delimiterBytes);
                    if (index == -1)
                    {
                        byteBag.AddRange(buffer1);
                        NLogHelper.Info(Encoding.UTF8.GetString(byteBag.ToArray()));
                    }
                    else
                    {
                        byteBag.AddRange(buffer1.Take(index));
                        NLogHelper.Info(Encoding.UTF8.GetString(byteBag.ToArray()));
                        handle = GetHttpHandler(inputStream, request, byteBag.ToArray(), buffer1.Skip(index + delimiterBytes.Length).ToArray());
                    }
                }
            }while (count != 0 && index == -1);
            return(handle);
        }
Exemple #2
0
        public void Start()
        {
            ThreadPool.QueueUserWorkItem(delegate
            {
                var listener = new TcpListener(IPAddress.Any, this.Port);
                listener.Start();
                while (true)
                {
                    var tcpClient = listener.AcceptTcpClient();
                    ThreadPool.QueueUserWorkItem(delegate
                    {
                        try
                        {
                            var socket     = tcpClient.Client;
                            var visitor    = socket.RemoteEndPoint;
                            var connection = new HttpConnection()
                            {
                                Socket = socket
                            };
                            if (!httpSessions.ContainsKey(visitor.ToString()))
                            {
                                httpSessions.Add(visitor.ToString(), new List <HttpConnection>());
                            }

                            if (httpSessions[visitor.ToString()].Count > 10)
                            {
                                //return busy message
                            }
                            else
                            {
                                httpSessions[visitor.ToString()].Add(connection);
                                using (var networkStream = tcpClient.GetStream())
                                {
                                    //var endTime = connection.TimeStamp.AddMilliseconds(connection.ReadTimeout);
                                    //networkStream.ReadTimeout = (int)(endTime-DateTime.Now).TotalMilliseconds;
                                    ProcessRequest(connection, networkStream, null);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            NLogHelper.Error(ex);
                        }
                    });
                }
            });

            ThreadPool.QueueUserWorkItem(delegate
            {
                Thread.Sleep(1000);
                if (httpSessions.Count > 100)
                {
                    for (var i = httpSessions.Count - 1; i >= 0; i--)
                    {
                        var collection = httpSessions.Values.ElementAt(i);
                        if (!collection.Any())
                        {
                            httpSessions.Remove(httpSessions.Keys.ElementAt(i));
                        }
                    }
                }
            });
        }