public void Stop()
 {
     if (server != null)
     {
         server.Close();
         server = null;
     }
 }
示例#2
0
        public void TestServer()
        {
            ObjectPool <byte[]> pool = new ObjectPool <byte[]>(() => { return(new byte[1024]); });

            const String testString = "what a great test!";

            ServerSockNetChannel server = (ServerSockNetChannel)SockNetServer.Create(GetLocalIpAddress(), 0, ServerSockNetChannel.DefaultBacklog, pool)
                                          .AddModule(new HttpSockNetChannelModule(HttpSockNetChannelModule.ParsingMode.Server));

            try
            {
                Assert.IsNotNull(server.Bind().WaitForValue(TimeSpan.FromSeconds(5)));

                server.Pipe.AddIncomingLast <HttpRequest>((ISockNetChannel channel, ref HttpRequest data) =>
                {
                    HttpResponse response = new HttpResponse(channel.BufferPool)
                    {
                        Version = data.Version,
                        Code    = "200",
                        Reason  = "OK"
                    };
                    response.Header["Content-Length"] = "" + data.BodySize;
                    Copy(data.Body.Stream, response.Body.Stream, data.BodySize);

                    Promise <ISockNetChannel> sendPromise = channel.Send(response);
                    if (("http/1.0".Equals(data.Version) && !"keep-alive".Equals(data.Header["connection"], StringComparison.CurrentCultureIgnoreCase)) ||
                        "close".Equals(data.Header["connection"], StringComparison.CurrentCultureIgnoreCase))
                    {
                        sendPromise.OnFulfilled = (ISockNetChannel value, Exception e, Promise <ISockNetChannel> promise) =>
                        {
                            value.Close();
                        };
                    }
                });

                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://" + GetLocalIpAddress() + ":" + server.LocalEndpoint.Port);
                request.KeepAlive = false;
                request.Timeout   = 5000;
                request.Method    = "POST";

                using (StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.Default))
                {
                    writer.Write(testString);
                }

                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    Assert.AreEqual(200, (int)response.StatusCode);
                    Assert.AreEqual("OK", response.StatusDescription);

                    Assert.AreEqual(Encoding.Default.GetByteCount(testString), response.ContentLength);

                    using (StreamReader responseReader = new StreamReader(response.GetResponseStream(), Encoding.Default))
                    {
                        Assert.AreEqual(testString, responseReader.ReadToEnd());
                    }
                }
            }
            finally
            {
                server.Close();
            }
        }