Beispiel #1
0
        private void NewTcpClient(object obj)
        {
            var client      = obj as TcpClient;
            var stream      = client.GetStream();
            var handler     = new ParserHandler();
            var requ        = new Request();
            var context     = new Context();
            var response    = new Response();

            var parser      = new HttpParser(handler);
            handler.setData(requ);
            int bytesRead = 0 ;
            var buffer = new byte[1024 /* or whatever you like */];

            try
            {

                while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    if (bytesRead == parser.Execute(new ArraySegment<byte>(buffer, 0, bytesRead)))
                        break;
                }
                // ensure you get the last callbacks.
                parser.Execute(default(ArraySegment<byte>));

                logger.InfoFormat("[{0}] URI : {1}", requ.method,requ.uri);
                var svc = this.router.route(requ.uri);
                switch (requ.method)
                {
                   case "GET" :
                     svc._get(context, requ, response);
                     break;

                    case "POST":
                        svc._post(context, requ, response);
                        break;
                }
                response.send(stream);
            }
            catch (Exception ex)
            {
                logger.ErrorFormat("exception: {0} ", ex.Message);
            }
            finally
            {
                stream.Close();
                client.Close();

            }
        }