public void ValidConnectionMessage() { var responseStream = new MemoryStream(); var stream = new TestStream(new MemoryStream(TestingTools.CreateConnectionRequest(Type.Stream, clientId.ToByteArray())), responseStream); // Create mock byte server and client var mockByteServer = new Mock <IServer <byte, byte> > (); var byteServer = mockByteServer.Object; var byteClient = new TestClient(stream); var server = new StreamServer(byteServer); server.OnClientRequestingConnection += (sender, e) => e.Request.Allow(); server.Start(); // Fire a client connection event var eventArgs = new ClientRequestingConnectionEventArgs <byte, byte> (byteClient); mockByteServer.Raise(m => m.OnClientRequestingConnection += null, eventArgs); Assert.IsTrue(eventArgs.Request.ShouldAllow); Assert.IsFalse(eventArgs.Request.ShouldDeny); server.Update(); Assert.AreEqual(1, server.Clients.Count()); Assert.AreEqual(clientId, server.Clients.First().Guid); TestingTools.CheckConnectionResponse(responseStream.ToArray(), 19, Status.Ok, string.Empty, 16); }
public void ValidHelloMessage() { var responseStream = new MemoryStream(); var stream = new TestStream(new MemoryStream(helloMessage), responseStream); // Create mock byte server and client var mockByteServer = new Mock <IServer <byte, byte> > (); var byteServer = mockByteServer.Object; var byteClient = new TestClient(stream); var server = new StreamServer(byteServer); server.OnClientRequestingConnection += (sender, e) => e.Request.Allow(); server.Start(); // Fire a client connection event var eventArgs = new ClientRequestingConnectionArgs <byte, byte> (byteClient); mockByteServer.Raise(m => m.OnClientRequestingConnection += null, eventArgs); Assert.IsTrue(eventArgs.Request.ShouldAllow); Assert.IsFalse(eventArgs.Request.ShouldDeny); server.Update(); Assert.AreEqual(1, server.Clients.Count()); Assert.AreEqual(clientGuid, server.Clients.First().Guid); byte[] bytes = responseStream.ToArray(); byte[] expectedBytes = { 0x4F, 0x4B }; Assert.IsTrue(expectedBytes.SequenceEqual(bytes)); }
/// <summary> /// Update the Stream server. Executes all streaming RPCs and sends the results to clients (if they have changed). /// </summary> void StreamServerUpdate() { Stopwatch timer = Stopwatch.StartNew(); uint rpcsExecuted = 0; streamServer.Update(); // Run streaming requests foreach (var entry in streamRequests) { var streamClient = entry.Key; var requests = entry.Value; if (!requests.Any()) { continue; } var streamMessage = new StreamMessage(); foreach (var request in requests) { // Run the RPC Response response; try { response = KRPC.Service.Services.Instance.HandleRequest(request.Procedure, request.Arguments); } catch (Exception e) { response = new Response(); response.HasError = true; response.Error = e.ToString(); } rpcsExecuted++; // Don't send an update if it is the previous one if (response.ReturnValue == streamResultCache [request.Identifier]) { continue; } // Add the update to the response message streamResultCache [request.Identifier] = response.ReturnValue; response.Time = GetUniversalTime(); var streamResponse = request.Response; streamResponse.Response = response; streamMessage.Responses.Add(streamResponse); } streamClient.Stream.Write(streamMessage); } timer.Stop(); StreamRPCs = rpcsExecuted; StreamRPCsExecuted += rpcsExecuted; TimePerStreamUpdate = (float)timer.ElapsedSeconds(); }
public void ValidConnectionRequest() { var clientGuid = new Guid("1234567890abcdef1234567890abcdef".ToBytes()); var clientGuidBase64 = Convert.ToBase64String(clientGuid.ToByteArray()); var request = Encoding.ASCII.GetBytes( "GET /?id=" + clientGuidBase64 + " HTTP/1.1\r\n" + "Host: localhost\r\n" + "Upgrade: websocket\r\n" + "Connection: Upgrade\r\n" + "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" + "Sec-WebSocket-Version: 13\r\n\r\n" ); var responseStream = new MemoryStream(); var stream = new TestStream(new MemoryStream(request), responseStream); // Create mock byte server and client var mockByteServer = new Mock <IServer <byte, byte> > (); var byteServer = mockByteServer.Object; var byteClient = new TestClient(stream); var server = new StreamServer(byteServer); server.OnClientRequestingConnection += (sender, e) => e.Request.Allow(); server.Start(); // Fire a client connection event var eventArgs = new ClientRequestingConnectionEventArgs <byte, byte> (byteClient); mockByteServer.Raise(m => m.OnClientRequestingConnection += null, eventArgs); Assert.IsTrue(eventArgs.Request.ShouldAllow); Assert.IsFalse(eventArgs.Request.ShouldDeny); server.Update(); Assert.AreEqual(1, server.Clients.Count()); Assert.AreEqual(clientGuid, server.Clients.First().Guid); var response = Encoding.ASCII.GetString(responseStream.ToArray()); Assert.AreEqual( "HTTP/1.1 101 Switching Protocols\r\n" + "Upgrade: websocket\r\n" + "Connection: Upgrade\r\n" + "Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n\r\n", response ); }
public void InvalidConnectionRequestNoGuid() { var ascii = Encoding.ASCII; var request = ascii.GetBytes( "GET / HTTP/1.1\r\n" + "Host: localhost\r\n" + "Upgrade: websocket\r\n" + "Connection: Upgrade\r\n" + "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" + "Sec-WebSocket-Version: 13\r\n\r\n" ); var responseStream = new MemoryStream(); var stream = new TestStream(new MemoryStream(request), responseStream); // Create mock byte server and client var mockByteServer = new Mock <IServer <byte, byte> > (); var byteServer = mockByteServer.Object; var byteClient = new TestClient(stream); var server = new StreamServer(byteServer); server.OnClientRequestingConnection += (sender, e) => e.Request.Allow(); server.Start(); // Fire a client connection event var eventArgs = new ClientRequestingConnectionEventArgs <byte, byte> (byteClient); mockByteServer.Raise(m => m.OnClientRequestingConnection += null, eventArgs); Assert.IsFalse(eventArgs.Request.ShouldAllow); Assert.IsTrue(eventArgs.Request.ShouldDeny); server.Update(); Assert.AreEqual(0, server.Clients.Count()); var response = ascii.GetString(responseStream.ToArray()); Assert.AreEqual( "HTTP/1.1 400 Bad Request\r\n\r\n", response ); }
public void ValidHelloMessage() { var responseStream = new MemoryStream (); var stream = new TestStream (new MemoryStream (helloMessage), responseStream); // Create mock byte server and client var mockByteServer = new Mock<IServer<byte,byte>> (); var byteServer = mockByteServer.Object; var byteClient = new TestClient (stream); var server = new StreamServer (byteServer); server.OnClientRequestingConnection += (sender, e) => e.Request.Allow (); server.Start (); // Fire a client connection event var eventArgs = new ClientRequestingConnectionArgs<byte,byte> (byteClient); mockByteServer.Raise (m => m.OnClientRequestingConnection += null, eventArgs); Assert.IsTrue (eventArgs.Request.ShouldAllow); Assert.IsFalse (eventArgs.Request.ShouldDeny); server.Update (); Assert.AreEqual (1, server.Clients.Count ()); Assert.AreEqual (clientGuid, server.Clients.First ().Guid); byte[] bytes = responseStream.ToArray (); byte[] expectedBytes = { 0x4F, 0x4B }; Assert.IsTrue (expectedBytes.SequenceEqual (bytes)); }