public void TestLotsOfMessages() { ObjectPool <byte[]> pool = new ObjectPool <byte[]>(() => { return(new byte[1024]); }); WebSocketEchoServer server = new WebSocketEchoServer(pool); try { server.Start(); BlockingCollection <object> blockingCollection = new BlockingCollection <object>(); ClientSockNetChannel client = (ClientSockNetChannel)SockNetClient.Create(server.Endpoint, ClientSockNetChannel.DefaultNoDelay, ClientSockNetChannel.DefaultTtl, pool) .AddModule(new WebSocketClientSockNetChannelModule("/", "localhost", (ISockNetChannel sockNetClient) => { blockingCollection.Add(true); })); client.Connect().WaitForValue(TimeSpan.FromSeconds(5)); object currentObject; Assert.IsTrue(blockingCollection.TryTake(out currentObject, DEFAULT_ASYNC_TIMEOUT)); Assert.IsTrue((bool)currentObject); client.Pipe.AddIncomingLast <WebSocketFrame>((ISockNetChannel sockNetClient, ref WebSocketFrame data) => { blockingCollection.Add(data); }); byte[] randomData = new byte[2000]; new Random(GetHashCode() ^ DateTime.Now.Millisecond).NextBytes(randomData); for (int i = 0; i < 1000; i++) { client.Send(WebSocketFrame.CreateBinaryFrame(randomData, false)); } int receivedMessages = 0; for (int i = 0; i < 1000; i++) { if (receivedMessages < 1000 && blockingCollection.TryTake(out currentObject, DEFAULT_ASYNC_TIMEOUT)) { receivedMessages++; } else { break; } } Assert.AreEqual(1000, receivedMessages); client.Disconnect().WaitForValue(TimeSpan.FromSeconds(5)); } finally { server.Stop(); } }
public void TestSimpleGetHttps() { ObjectPool <byte[]> pool = new ObjectPool <byte[]>(() => { return(new byte[1024]); }); HttpEchoServer server = new HttpEchoServer(pool); try { server.Start(true); BlockingCollection <HttpResponse> responses = new BlockingCollection <HttpResponse>(); ClientSockNetChannel client = (ClientSockNetChannel)SockNetClient.Create(server.Endpoint, ClientSockNetChannel.DefaultNoDelay, ClientSockNetChannel.DefaultTtl, pool) .AddModule(new HttpSockNetChannelModule(HttpSockNetChannelModule.ParsingMode.Client)); client.Pipe.AddIncomingLast <HttpResponse>((ISockNetChannel channel, ref HttpResponse data) => { responses.Add(data); }); client.ConnectWithTLS((object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) => { return(true); }) .WaitForValue(TimeSpan.FromSeconds(5)); HttpRequest request = new HttpRequest(client.BufferPool) { Action = "GET", Path = "/", Version = "HTTP/1.1" }; request.Header["Host"] = "localhost"; request.Header["Connection"] = "Close"; client.Send(request); HttpResponse response = null; responses.TryTake(out response, 5000); Assert.IsNotNull(response); Assert.IsNotNull(response.Version); Assert.IsNotNull(response.Code); Assert.IsNotNull(response.Reason); MemoryStream stream = new MemoryStream(); response.Write(stream, false); stream.Position = 0; using (StreamReader reader = new StreamReader(stream)) { Console.WriteLine("Got response: " + reader.ReadToEnd()); } } finally { server.Stop(); } }
public void TestChunked() { ObjectPool <byte[]> pool = new ObjectPool <byte[]>(() => { return(new byte[1024]); }); HttpChunkedServer server = new HttpChunkedServer(pool); try { server.Start(false); BlockingCollection <HttpResponse> responses = new BlockingCollection <HttpResponse>(); ClientSockNetChannel client = (ClientSockNetChannel)SockNetClient.Create(server.Endpoint, ClientSockNetChannel.DefaultNoDelay, ClientSockNetChannel.DefaultTtl, pool) .AddModule(new HttpSockNetChannelModule(HttpSockNetChannelModule.ParsingMode.Client)); client.Pipe.AddIncomingLast <HttpResponse>((ISockNetChannel channel, ref HttpResponse data) => { responses.Add(data); }); Assert.IsNotNull(client.Connect().WaitForValue(TimeSpan.FromSeconds(5))); HttpRequest request = new HttpRequest(client.BufferPool) { Action = "GET", Path = "/httpgallery/chunked/chunkedimage.aspx", Version = "HTTP/1.1" }; request.Header["Host"] = "www.httpwatch.com"; client.Send(request); HttpResponse response = null; responses.TryTake(out response, 10000); Assert.IsNotNull(response); Assert.IsNotNull(response.Version); Assert.IsNotNull(response.Code); Assert.IsNotNull(response.Reason); MemoryStream stream = new MemoryStream(); response.Write(stream, false); stream.Position = 0; using (StreamReader reader = new StreamReader(stream)) { Console.WriteLine("Got response: " + reader.ReadToEnd()); } } finally { server.Stop(); } }
public void TestContinuationEchoWithMask() { ObjectPool <byte[]> pool = new ObjectPool <byte[]>(() => { return(new byte[1024]); }); WebSocketEchoServer server = new WebSocketEchoServer(pool); try { server.Start(false, true); BlockingCollection <object> blockingCollection = new BlockingCollection <object>(); ClientSockNetChannel client = (ClientSockNetChannel)SockNetClient.Create(server.Endpoint, ClientSockNetChannel.DefaultNoDelay, ClientSockNetChannel.DefaultTtl, pool) .AddModule(new WebSocketClientSockNetChannelModule("/", "localhost", (ISockNetChannel sockNetClient) => { blockingCollection.Add(true); })); client.Connect().WaitForValue(TimeSpan.FromSeconds(5)); object currentObject; Assert.IsTrue(blockingCollection.TryTake(out currentObject, DEFAULT_ASYNC_TIMEOUT)); Assert.IsTrue((bool)currentObject); client.Pipe.AddIncomingLast <WebSocketFrame>((ISockNetChannel sockNetClient, ref WebSocketFrame data) => { blockingCollection.Add(data); }); Random rand = new Random(this.GetHashCode() ^ DateTime.Now.Millisecond); byte[] rawData = new byte[rand.Next(5000, 10000)]; rand.NextBytes(rawData); client.Send(WebSocketFrame.CreateBinaryFrame(rawData, true)); Assert.IsTrue(blockingCollection.TryTake(out currentObject, DEFAULT_ASYNC_TIMEOUT)); Assert.IsTrue(currentObject is WebSocketFrame); AreArraysEqual(rawData, ((WebSocketFrame)currentObject).Data); client.Disconnect().WaitForValue(TimeSpan.FromSeconds(5)); } finally { server.Stop(); } }
public void TestSimpleSslContent() { ObjectPool <byte[]> pool = new ObjectPool <byte[]>(() => { return(new byte[1024]); }); GdsEchoServer server = new GdsEchoServer(pool); try { server.Start(true); BlockingCollection <object> blockingCollection = new BlockingCollection <object>(); ClientSockNetChannel client = (ClientSockNetChannel)SockNetClient.Create(server.Endpoint, ClientSockNetChannel.DefaultNoDelay, ClientSockNetChannel.DefaultTtl, pool) .AddModule(new GdsSockNetChannelModule(true)); client.ConnectWithTLS((object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) => { return(true); }) .WaitForValue(TimeSpan.FromSeconds(5)); object currentObject; client.Pipe.AddIncomingLast <GdsFrame>((ISockNetChannel sockNetClient, ref GdsFrame data) => { blockingCollection.Add(data); }); ChunkedBuffer body = new ChunkedBuffer(pool); body.OfferRaw(Encoding.UTF8.GetBytes("some test"), 0, Encoding.UTF8.GetByteCount("some test")); client.Send(GdsFrame.NewContentFrame(1, null, false, body, true)); Assert.IsTrue(blockingCollection.TryTake(out currentObject, 5000)); Assert.IsTrue(currentObject is GdsFrame); Assert.AreEqual("some test", ((GdsFrame)currentObject).Body.ToString(Encoding.UTF8)); Console.WriteLine("Got response: \n" + ((GdsFrame)currentObject).Body); client.Disconnect().WaitForValue(TimeSpan.FromSeconds(5)); } finally { server.Stop(); } }
public void TestEchoWithMaskWithSsl() { ObjectPool <byte[]> pool = new ObjectPool <byte[]>(() => { return(new byte[1024]); }); WebSocketEchoServer server = new WebSocketEchoServer(pool); try { server.Start(true); BlockingCollection <object> blockingCollection = new BlockingCollection <object>(); ClientSockNetChannel client = (ClientSockNetChannel)SockNetClient.Create(server.Endpoint, ClientSockNetChannel.DefaultNoDelay, ClientSockNetChannel.DefaultTtl, pool) .AddModule(new WebSocketClientSockNetChannelModule("/", "localhost", (ISockNetChannel sockNetClient) => { blockingCollection.Add(true); })); client.ConnectWithTLS((object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) => { return(true); }) .WaitForValue(TimeSpan.FromSeconds(5)); object currentObject; Assert.IsTrue(blockingCollection.TryTake(out currentObject, DEFAULT_ASYNC_TIMEOUT)); Assert.IsTrue((bool)currentObject); client.Pipe.AddIncomingLast <WebSocketFrame>((ISockNetChannel sockNetClient, ref WebSocketFrame data) => { blockingCollection.Add(data); }); client.Send(WebSocketFrame.CreateTextFrame("some test", true)); Assert.IsTrue(blockingCollection.TryTake(out currentObject, DEFAULT_ASYNC_TIMEOUT)); Assert.IsTrue(currentObject is WebSocketFrame); Assert.AreEqual("some test", ((WebSocketFrame)currentObject).DataAsString); Console.WriteLine("Got response: \n" + ((WebSocketFrame)currentObject).DataAsString); client.Disconnect().WaitForValue(TimeSpan.FromSeconds(5)); } finally { server.Stop(); } }
public void TestBindWithoutSsl() { ObjectPool <byte[]> pool = new ObjectPool <byte[]>(() => { return(new byte[1024]); }); ServerSockNetChannel server = SockNetServer.Create(GetLocalIpAddress(), 0, ServerSockNetChannel.DefaultBacklog, pool); try { server.Bind(); server.Pipe.AddIncomingFirst <ChunkedBuffer>((ISockNetChannel channel, ref ChunkedBuffer data) => { channel.Send(data); }); BlockingCollection <string> incomingData = new BlockingCollection <string>(); ClientSockNetChannel client = SockNetClient.Create(GetLocalIpAddress(), server.LocalEndpoint.Port, ClientSockNetChannel.DefaultNoDelay, ClientSockNetChannel.DefaultTtl, pool); Assert.IsNotNull(client.Connect().WaitForValue(TimeSpan.FromSeconds(5))); client.Pipe.AddIncomingFirst((ISockNetChannel channel, ref ChunkedBuffer data) => { StreamReader reader = new StreamReader(data.Stream); incomingData.Add(reader.ReadToEnd()); }); client.Send(Encoding.UTF8.GetBytes("a test!")); string incomingValue = null; Assert.IsTrue(incomingData.TryTake(out incomingValue, TimeSpan.FromSeconds(5))); Assert.AreEqual("a test!", incomingValue); } finally { server.Close(); } }
public void TestIncompleteBufferParsing() { ObjectPool <byte[]> pool = new ObjectPool <byte[]>(() => { return(new byte[1024]); }); WebSocketEchoServer server = new WebSocketEchoServer(pool); try { server.Start(); BlockingCollection <object> blockingCollection = new BlockingCollection <object>(); ClientSockNetChannel client = (ClientSockNetChannel)SockNetClient.Create(server.Endpoint, ClientSockNetChannel.DefaultNoDelay, ClientSockNetChannel.DefaultTtl, pool) .AddModule(new WebSocketClientSockNetChannelModule("/", "localhost", (ISockNetChannel sockNetClient) => { blockingCollection.Add(true); })); client.Connect().WaitForValue(TimeSpan.FromSeconds(5)); object currentObject; Assert.IsTrue(blockingCollection.TryTake(out currentObject, DEFAULT_ASYNC_TIMEOUT)); Assert.IsTrue((bool)currentObject); client.Pipe.AddIncomingLast <WebSocketFrame>((ISockNetChannel sockNetClient, ref WebSocketFrame data) => { blockingCollection.Add(data); }); string body1 = new string('A', 4913) + "X"; WebSocketFrame frame1 = WebSocketFrame.CreateTextFrame( "STS/1.0 200 OK" + "\r\n" + "s:7R" + "\r\n" + "n:bytes 0-4915/5000" + "\r\n" + "l:4916" + "\r\n" + "" + "\r\n" + body1 + "\r\n", false); client.Send(frame1); string body2 = new string('B', 81) + "Y"; WebSocketFrame frame2 = WebSocketFrame.CreateTextFrame( "STS/1.0 200 OK" + "\r\n" + "s:7R" + "\r\n" + "n:bytes 4916-4999/5000" + "\r\n" + "l:84" + "\r\n" + "" + "\r\n" + body2 + "\r\n", false); client.Send(frame2); Assert.IsTrue(blockingCollection.TryTake(out currentObject, DEFAULT_ASYNC_TIMEOUT)); Assert.IsTrue(currentObject is WebSocketFrame); Assert.AreEqual(frame1.DataAsString, ((WebSocketFrame)currentObject).DataAsString); Console.WriteLine("Got response: \n" + ((WebSocketFrame)currentObject).DataAsString); Assert.IsTrue(blockingCollection.TryTake(out currentObject, DEFAULT_ASYNC_TIMEOUT)); Assert.IsTrue(currentObject is WebSocketFrame); Assert.AreEqual(frame2.DataAsString, ((WebSocketFrame)currentObject).DataAsString); Console.WriteLine("Got response: \n" + ((WebSocketFrame)currentObject).DataAsString); client.Disconnect().WaitForValue(TimeSpan.FromSeconds(5)); } finally { server.Stop(); } }
public void TestSmallMessagesInParallel() { ObjectPool <byte[]> pool = new ObjectPool <byte[]>(() => { return(new byte[1024]); }); Random random = new Random(this.GetHashCode() ^ DateTime.Now.Millisecond); WebSocketEchoServer server = new WebSocketEchoServer(pool); try { server.Start(); BlockingCollection <object> blockingCollection = new BlockingCollection <object>(); ClientSockNetChannel client = (ClientSockNetChannel)SockNetClient.Create(server.Endpoint, ClientSockNetChannel.DefaultNoDelay, ClientSockNetChannel.DefaultTtl, pool) .AddModule(new WebSocketClientSockNetChannelModule("/", "localhost", (ISockNetChannel sockNetClient) => { blockingCollection.Add(true); })); client.Connect().WaitForValue(TimeSpan.FromSeconds(5)); object currentObject; Assert.IsTrue(blockingCollection.TryTake(out currentObject, DEFAULT_ASYNC_TIMEOUT)); Assert.IsTrue((bool)currentObject); client.Pipe.AddIncomingLast <WebSocketFrame>((ISockNetChannel sockNetClient, ref WebSocketFrame data) => { blockingCollection.Add(data); }); int numberOfMessages = 100; byte[][] expectedResults = new byte[numberOfMessages][]; for (int i = 0; i < numberOfMessages; i++) { ThreadPool.QueueUserWorkItem((object state) => { int index = (int)state; byte[] messageData = new byte[random.Next(50, 100)]; random.NextBytes(messageData); messageData[0] = (byte)(index >> 0); messageData[1] = (byte)(index >> 8); messageData[2] = (byte)(index >> 16); messageData[3] = (byte)(index >> 24); expectedResults[index] = messageData; client.Send(WebSocketFrame.CreateBinaryFrame(messageData)); // simulate GC if (index % Math.Max(numberOfMessages / 10, 1) == 0) { GC.Collect(); GC.WaitForPendingFinalizers(); } }, i); } for (int i = 0; i < numberOfMessages; i++) { Assert.IsTrue(blockingCollection.TryTake(out currentObject, DEFAULT_ASYNC_TIMEOUT)); Assert.IsTrue(currentObject is WebSocketFrame); // simulate GC if (i % Math.Max(numberOfMessages / 10, 1) == 0) { GC.Collect(); GC.WaitForPendingFinalizers(); } byte[] incomingData = ((WebSocketFrame)currentObject).Data; int index = 0; index |= incomingData[0] << 0; index |= incomingData[1] << 8; index |= incomingData[2] << 16; index |= incomingData[3] << 24; Console.WriteLine(index); AreArraysEqual(expectedResults[index], incomingData); } client.Disconnect().WaitForValue(TimeSpan.FromSeconds(5)); } finally { server.Stop(); } }