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)); }
public void ShortHelloMessageHeader() { var shortHelloMessage = new byte[5]; Array.Copy(helloMessage, shortHelloMessage, shortHelloMessage.Length); var responseStream = new MemoryStream(); var stream = new TestStream(new MemoryStream(shortHelloMessage), responseStream); // Create mock byte server and client var mockByteServer = new Mock <IServer <byte, byte> > (); var byteServer = mockByteServer.Object; var mockByteClient = new Mock <IClient <byte, byte> > (); mockByteClient.Setup(x => x.Stream).Returns(stream); var byteClient = mockByteClient.Object; 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.IsFalse(eventArgs.Request.ShouldAllow); Assert.IsTrue(eventArgs.Request.ShouldDeny); Assert.AreEqual(0, responseStream.Length); }
public void OnClientRequestingConnection(object sender, ClientRequestingConnectionArgs args) { // Not open, so open the dialog if (!Visible) { Logger.WriteLine("Asking player to allow/deny connection attempt..."); this.args = args; Open(); return; } // Already open for a different request, so ignore if (Visible && this.args.Client != args.Client) { return; } // Open, and we have a decision (must be the correct client at this point), to close the dialog if (Visible && !this.args.Request.StillPending) { if (this.args.Request.ShouldAllow) { args.Request.Allow(); } else { args.Request.Deny(); } Close(); } }
public void DefaultBehaviour() { var attempt = new ClientRequestingConnectionArgs <byte, byte> (null); Assert.IsFalse(attempt.Request.ShouldDeny); Assert.IsFalse(attempt.Request.ShouldAllow); Assert.IsTrue(attempt.Request.StillPending); }
public void AllowAndDeny() { var attempt = new ClientRequestingConnectionArgs <byte, byte> (null); attempt.Request.Allow(); attempt.Request.Deny(); Assert.IsTrue(attempt.Request.ShouldDeny); Assert.IsFalse(attempt.Request.ShouldAllow); Assert.IsFalse(attempt.Request.StillPending); }
/// <summary> /// When a client requests a connection, check and parse the hello message (which should /// consist of a header and a client name), then trigger RPCServer.OnClientRequestingConnection /// to get response of delegates /// </summary> public void HandleClientRequestingConnection(object sender, ClientRequestingConnectionArgs <byte, byte> args) { if (!pendingClients.ContainsKey(args.Client)) { // A new client connection attempt. Verify the hello message. string clientName = CheckHelloMessage(args.Client); if (clientName != null) { // Hello message OK, add it to the pending clients var client = new RPCClient(clientName, args.Client); pendingClients [args.Client] = client; } else { // Deny the connection, don't add it to pending clients args.Request.Deny(); return; } } // Client is in pending clients and passed hello message verification. // Invoke connection request events. if (OnClientRequestingConnection != null) { var client = pendingClients [args.Client]; var subArgs = new ClientRequestingConnectionArgs <Request, Response> (client); OnClientRequestingConnection(this, subArgs); if (subArgs.Request.ShouldAllow) { args.Request.Allow(); clients [args.Client] = client; args.Client.Stream.Write(client.Guid.ToByteArray()); Logger.WriteLine("RPCServer: client connection allowed"); } if (subArgs.Request.ShouldDeny) { args.Request.Deny(); Logger.WriteLine("RPCServer: client connection denied", Logger.Severity.Warning); } if (!subArgs.Request.StillPending) { pendingClients.Remove(args.Client); } } else { // No events configured, so allow the connection args.Request.Allow(); clients [args.Client] = pendingClients [args.Client]; args.Client.Stream.Write(args.Client.Guid.ToByteArray()); Logger.WriteLine("RPCServer: client connection allowed"); pendingClients.Remove(args.Client); } }
/// <summary> /// When a client requests a connection, check the hello message, /// then trigger RPCServer.OnClientRequestingConnection to get response of delegates /// </summary> public void HandleClientRequestingConnection(object sender, ClientRequestingConnectionArgs <byte, byte> args) { if (!pendingClients.ContainsKey(args.Client)) { // A new client connection attempt. Verify the hello message. var guid = CheckHelloMessage(args.Client); if (guid != Guid.Empty) { // Hello message OK, add it to the pending clients var client = new StreamClient(args.Client, guid); pendingClients [args.Client] = client; } else { // Deny the connection, don't add it to pending clients args.Request.Deny(); return; } } // Client is in pending clients and passed hello message verification. // Invoke connection request events. if (OnClientRequestingConnection != null) { var client = pendingClients [args.Client]; var subArgs = new ClientRequestingConnectionArgs <byte, StreamMessage> (client); OnClientRequestingConnection(this, subArgs); if (subArgs.Request.ShouldAllow) { args.Request.Allow(); clients [args.Client] = client; Logger.WriteLine("StreamServer: Client connection allowed."); args.Client.Stream.Write(okMessage); } if (subArgs.Request.ShouldDeny) { args.Request.Deny(); Logger.WriteLine("StreamServer: Client connection denied.", Logger.Severity.Warning); } if (!subArgs.Request.StillPending) { pendingClients.Remove(args.Client); } } else { args.Request.Allow(); clients [args.Client] = pendingClients [args.Client]; Logger.WriteLine("StreamServer: Client connection allowed."); args.Client.Stream.Write(okMessage); pendingClients.Remove(args.Client); } }
public void Update() { // Remove disconnected clients foreach (var client in clients.Where(x => !x.Connected).ToList()) { clients.Remove(client); DisconnectClient(client); } // Process pending clients lock (pendingClientsLock) { var stillPendingClients = new List <TCPClient> (); foreach (var client in pendingClients) { // Trigger OnClientRequestingConnection events to verify the connection var args = new ClientRequestingConnectionArgs <byte, byte> (client); if (OnClientRequestingConnection != null) { OnClientRequestingConnection(this, args); } // Deny the connection if (args.Request.ShouldDeny) { Logger.WriteLine("TCPServer(" + name + "): client connection denied (" + client.Address + ")", Logger.Severity.Warning); DisconnectClient(client, true); } // Allow the connection if (args.Request.ShouldAllow) { Logger.WriteLine("TCPServer(" + name + "): client connection accepted (" + client.Address + ")"); clients.Add(client); if (OnClientConnected != null) { OnClientConnected(this, new ClientConnectedArgs <byte, byte> (client)); } } // Still pending, will either be denied or allowed on a subsequent called to Update if (args.Request.StillPending) { stillPendingClients.Add(client); } } pendingClients = stillPendingClients; } }
public void ValidHelloMessageWithNoName() { for (int i = 12; i < helloMessage.Length; i++) { helloMessage [i] = 0x00; } 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 RPCServer(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("", server.Clients.First().Name); byte[] bytes = responseStream.ToArray(); byte[] responseBytes = byteClient.Guid.ToByteArray(); Assert.IsTrue(responseBytes.SequenceEqual(bytes)); }
protected override void Closed() { args = null; }