public async Task <bool> Connect(HostEndPoint endPoint, int timeout = 1000) { // TODO: remove this from here. FloodNetwork.NetworkInitialize(); var client = new HostClient(); host = client; var tcs = new TaskCompletionSource <bool>(); var session = client.Session; session.StateChange += state => { if (state == SessionState.Open) { tcs.SetResult(true); } }; var details = new HostConnectionDetails() { Address = endPoint.Address, Port = endPoint.Port, ChannelCount = ChannelCount }; client.Connect(details); // Wait for task somewhere else if (await Task.WhenAny(tcs.Task, Task.Delay(timeout)) != tcs.Task) { return(false); } client.SessionPacket += OnPacket; Log.Info("Client connected with session!"); return(true); }
public async Task<bool> Connect(HostEndPoint endPoint, int timeout = 1000) { // TODO: remove this from here. //Flood.Network.NetworkInitialize(); var client = new HostClient(); host = client; var tcs = new TaskCompletionSource<bool>(); var session = client.Session; session.StateChange += state => { if (state == SessionState.Open) tcs.SetResult(true); }; var details = new HostConnectionDetails() { Address = endPoint.Address, Port = endPoint.Port, ChannelCount = ChannelCount }; client.Connect(details); // Wait for task somewhere else if (await Task.WhenAny(tcs.Task, Task.Delay(timeout)) != tcs.Task) return false; client.SessionPacket += OnPacket; Log.Info("Client connected with session!"); return true; }
public void TestPeerConnection() { Flood.Network.NetworkInitialize(); var conn = new HostConnectionDetails {Address = "", Port = 13131}; var hostServer = new HostServer(); var hostClient = new HostClient(); hostServer.CreateSocket(conn); hostClient.Connect(conn); //Test Connections { //bool clientConnected = false; //bool serverConnected = false; //hostClient.PeerConnect += peer => clientConnected = true; //hostServer.PeerConnect += peer => serverConnected = true; var watch = Stopwatch.StartNew(); while (watch.ElapsedMilliseconds < 20) { hostClient.ProcessEvents(1); hostServer.ProcessEvents(1); } //Assert.IsTrue(clientConnected); //Assert.IsTrue(serverConnected); } //Test QueuePacket { const ushort packetId = 13; const string packetString = "message"; var packetReceived = false; var packetReceivedId = -1; var packetReceiveString = ""; hostServer.SessionPacket += (session, packet, channel) => { packetReceived = true; packetReceivedId = packet.Id; var bytes = packet.Read(); packetReceiveString = Encoding.UTF8.GetString(bytes.ToArray(),0,bytes.Count); }; var p = new Packet(packetId); p.Write(new List<byte>(Encoding.ASCII.GetBytes(packetString))); hostClient.Peer.QueuePacket(p, 0); var watch = Stopwatch.StartNew(); while (watch.ElapsedMilliseconds < 20) { hostClient.ProcessEvents(1); hostServer.ProcessEvents(1); } Assert.IsTrue(packetReceived); Assert.AreEqual(packetId,packetReceivedId); Assert.AreEqual(packetString,packetReceiveString); } //Test Encrypted Packet { const ushort packetId = 13; const string packetString = "message"; var packetReceived = false; var packetReceivedId = -1; var packetReceiveString = ""; hostServer.SessionPacket += (session, packet, channel) => { packetReceived = true; packetReceivedId = packet.Id; var bytes = packet.Read(); packetReceiveString = Encoding.UTF8.GetString(bytes.ToArray(),0,bytes.Count); }; var p = new Packet(packetId) {Flags = PacketFlags.Encrypted}; p.Write(new List<byte>(Encoding.ASCII.GetBytes(packetString))); hostClient.Peer.QueuePacket(p, 0); var watch = Stopwatch.StartNew(); while (watch.ElapsedMilliseconds < 20) { hostClient.ProcessEvents(1); hostServer.ProcessEvents(1); } Assert.IsTrue(packetReceived); Assert.AreEqual(packetId,packetReceivedId); Assert.AreEqual(packetString,packetReceiveString); } }
public void TestPeerConnection() { FloodNetwork.NetworkInitialize(); var conn = new HostConnectionDetails { Address = "", Port = 13131 }; var hostServer = new HostServer(); var hostClient = new HostClient(); hostServer.CreateSocket(conn); hostClient.Connect(conn); //Test Connections { //bool clientConnected = false; //bool serverConnected = false; //hostClient.PeerConnect += peer => clientConnected = true; //hostServer.PeerConnect += peer => serverConnected = true; var watch = Stopwatch.StartNew(); while (watch.ElapsedMilliseconds < 20) { hostClient.ProcessEvents(1); hostServer.ProcessEvents(1); } //Assert.IsTrue(clientConnected); //Assert.IsTrue(serverConnected); } //Test QueuePacket { const ushort packetId = 13; const string packetString = "message"; var packetReceived = false; var packetReceivedId = -1; var packetReceiveString = ""; hostServer.SessionPacket += (session, packet, channel) => { packetReceived = true; packetReceivedId = packet.Id; var bytes = packet.Read(); packetReceiveString = Encoding.UTF8.GetString(bytes.ToArray(), 0, bytes.Count); }; var p = new Packet(packetId); p.Write(new List <byte>(Encoding.ASCII.GetBytes(packetString))); hostClient.Peer.QueuePacket(p, 0); var watch = Stopwatch.StartNew(); while (watch.ElapsedMilliseconds < 20) { hostClient.ProcessEvents(1); hostServer.ProcessEvents(1); } Assert.IsTrue(packetReceived); Assert.AreEqual(packetId, packetReceivedId); Assert.AreEqual(packetString, packetReceiveString); } //Test Encrypted Packet { const ushort packetId = 13; const string packetString = "message"; var packetReceived = false; var packetReceivedId = -1; var packetReceiveString = ""; hostServer.SessionPacket += (session, packet, channel) => { packetReceived = true; packetReceivedId = packet.Id; var bytes = packet.Read(); packetReceiveString = Encoding.UTF8.GetString(bytes.ToArray(), 0, bytes.Count); }; var p = new Packet(packetId) { Flags = PacketFlags.Encrypted }; p.Write(new List <byte>(Encoding.ASCII.GetBytes(packetString))); hostClient.Peer.QueuePacket(p, 0); var watch = Stopwatch.StartNew(); while (watch.ElapsedMilliseconds < 20) { hostClient.ProcessEvents(1); hostServer.ProcessEvents(1); } Assert.IsTrue(packetReceived); Assert.AreEqual(packetId, packetReceivedId); Assert.AreEqual(packetString, packetReceiveString); } }