コード例 #1
0
            public void Start(bool isTls = false)
            {
                server = SockNetServer.Create(GetLocalIpAddress(), 0, ServerSockNetChannel.DefaultBacklog, pool);

                try
                {
                    server.AddModule(new GdsSockNetChannelModule(true));

                    if (isTls)
                    {
                        byte[] rawCert = CertificateUtil.CreateSelfSignCertificatePfx("CN=\"test\"; C=\"USA\"", DateTime.Today.AddDays(-10), DateTime.Today.AddDays(+10));

                        server.BindWithTLS(new X509Certificate2(rawCert),
                                           (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) => { return(true); }).WaitForValue(TimeSpan.FromSeconds(5));
                    }
                    else
                    {
                        server.Bind().WaitForValue(TimeSpan.FromSeconds(5));
                    }

                    Assert.IsTrue(server.IsActive);

                    server.Pipe.AddIncomingLast <GdsFrame>((ISockNetChannel channel, ref GdsFrame data) =>
                    {
                        channel.Send(data);
                    });
                }
                catch (Exception)
                {
                    Stop();
                }
            }
コード例 #2
0
            public void Start(bool isTls = false)
            {
                server = SockNetServer.Create(GetLocalIpAddress(), 0, ServerSockNetChannel.DefaultBacklog, pool);

                try
                {
                    server.AddModule(new HttpSockNetChannelModule(HttpSockNetChannelModule.ParsingMode.Server));

                    server.Pipe.AddIncomingLast <HttpRequest>((ISockNetChannel channel, ref HttpRequest data) =>
                    {
                        HttpResponse response = new HttpResponse(channel.BufferPool)
                        {
                            Version = data.Version,
                            Code    = "200",
                            Reason  = "OK"
                        };

                        foreach (string headerName in data.Headers.Names)
                        {
                            response.Headers[headerName] = data.Headers[headerName];
                        }

                        response.Body = data.Body;

                        channel.Send(response);
                    });

                    if (isTls)
                    {
                        byte[] rawCert = CertificateUtil.CreateSelfSignCertificatePfx("CN=\"test\"; C=\"USA\"", DateTime.Today.AddDays(-10), DateTime.Today.AddDays(+10));

                        server.BindWithTLS(new X509Certificate2(rawCert),
                                           (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) => { return(true); }).WaitForValue(TimeSpan.FromSeconds(5));
                    }
                    else
                    {
                        server.Bind().WaitForValue(TimeSpan.FromSeconds(5));
                    }

                    Assert.IsTrue(server.IsActive);
                }
                catch (Exception)
                {
                    Stop();
                }
            }
コード例 #3
0
            public void Start(bool isTls = false, bool doContinuations = false)
            {
                server = SockNetServer.Create(GetLocalIpAddress(), 0, ServerSockNetChannel.DefaultBacklog, pool);

                try
                {
                    server.AddModule(new WebSocketServerSockNetChannelModule("/", "localhost"));

                    if (isTls)
                    {
                        byte[] rawCert = CertificateUtil.CreateSelfSignCertificatePfx("CN=\"test\"; C=\"USA\"", DateTime.Today.AddDays(-10), DateTime.Today.AddDays(+10));

                        server.BindWithTLS(new X509Certificate2(rawCert),
                                           (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) => { return(true); }).WaitForValue(TimeSpan.FromSeconds(5));
                    }
                    else
                    {
                        server.Bind().WaitForValue(TimeSpan.FromSeconds(5));
                    }

                    Assert.IsTrue(server.IsActive);

                    server.Pipe.AddIncomingLast <WebSocketFrame>((ISockNetChannel channel, ref WebSocketFrame data) =>
                    {
                        if (doContinuations)
                        {
                            int perFrameSize = data.Data.Length / 3;

                            for (int i = 0; i < 3; i++)
                            {
                                bool isDone = false;
                                byte[] rawData;

                                if (i + 1 < 3)
                                {
                                    rawData = new byte[perFrameSize];
                                    isDone  = false;
                                }
                                else
                                {
                                    rawData = new byte[data.Data.Length - (perFrameSize * 2)];
                                    isDone  = true;
                                }

                                Buffer.BlockCopy(data.Data, i * perFrameSize, rawData, 0, rawData.Length);

                                if (data.Operation == WebSocketFrame.WebSocketFrameOperation.BinaryFrame)
                                {
                                    channel.Send(WebSocketFrame.CreateBinaryFrame(rawData, true, i != 0, isDone));
                                }
                                else
                                {
                                    channel.Send(WebSocketFrame.CreateTextFrame(rawData, true, i != 0, isDone));
                                }
                            }
                        }
                        else
                        {
                            channel.Send(data);
                        }
                    });
                }
                catch (Exception)
                {
                    Stop();
                }
            }
コード例 #4
0
            public void Start(bool isTls = false)
            {
                string sampleContent = "<test><val>hello</val></test>";

                int sampleContentLength = Encoding.UTF8.GetByteCount(sampleContent);

                string chunk1Content = "<test><val>";
                string chunk2Content = "hello</val>";
                string chunk3Content = "</test>";

                int chunk1ContentLength = Encoding.UTF8.GetByteCount(chunk1Content);
                int chunk2ContentLength = Encoding.UTF8.GetByteCount(chunk2Content);
                int chunk3ContentLength = Encoding.UTF8.GetByteCount(chunk3Content);

                string chunk1HttpContent = "HTTP/1.0 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n" + string.Format("{0:X}", chunk1ContentLength) + "\r\n" + chunk1Content + "\r\n";
                string chunk2HttpContent = string.Format("{0:X}", chunk2ContentLength) + "\r\n" + chunk2Content + "\r\n";
                string chunk3HttpContent = string.Format("{0:X}", chunk3ContentLength) + "\r\n" + chunk3Content + "\r\n";
                string chunk4HttpContent = "0\r\n\r\n";

                server = SockNetServer.Create(GetLocalIpAddress(), 0, ServerSockNetChannel.DefaultBacklog, pool);

                try
                {
                    server.AddModule(new HttpSockNetChannelModule(HttpSockNetChannelModule.ParsingMode.Server));

                    server.Pipe.AddIncomingLast <HttpRequest>((ISockNetChannel channel, ref HttpRequest data) =>
                    {
                        ChunkedBuffer buffer1 = new ChunkedBuffer(channel.BufferPool);
                        buffer1.Write(Encoding.ASCII.GetBytes(chunk1HttpContent), 0, Encoding.ASCII.GetByteCount(chunk1HttpContent));
                        channel.Send(buffer1);

                        ChunkedBuffer buffer2 = new ChunkedBuffer(channel.BufferPool);
                        buffer2.Write(Encoding.ASCII.GetBytes(chunk2HttpContent), 0, Encoding.ASCII.GetByteCount(chunk2HttpContent));
                        channel.Send(buffer2);

                        ChunkedBuffer buffer3 = new ChunkedBuffer(channel.BufferPool);
                        buffer3.Write(Encoding.ASCII.GetBytes(chunk3HttpContent), 0, Encoding.ASCII.GetByteCount(chunk3HttpContent));
                        channel.Send(buffer3);

                        ChunkedBuffer buffer4 = new ChunkedBuffer(channel.BufferPool);
                        buffer4.Write(Encoding.ASCII.GetBytes(chunk4HttpContent), 0, Encoding.ASCII.GetByteCount(chunk4HttpContent));
                        channel.Send(buffer4);
                    });

                    if (isTls)
                    {
                        byte[] rawCert = CertificateUtil.CreateSelfSignCertificatePfx("CN=\"test\"; C=\"USA\"", DateTime.Today.AddDays(-10), DateTime.Today.AddDays(+10));

                        server.BindWithTLS(new X509Certificate2(rawCert),
                                           (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) => { return(true); }).WaitForValue(TimeSpan.FromSeconds(5));
                    }
                    else
                    {
                        server.Bind().WaitForValue(TimeSpan.FromSeconds(5));
                    }

                    Assert.IsTrue(server.IsActive);
                }
                catch (Exception)
                {
                    Stop();
                }
            }
コード例 #5
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();
            }
        }