Beispiel #1
0
        public void CreateAndConnect_NetworkConnection_ToRemoteEndPoint()
        {
            var connection = Driver.Connect(RemoteDriver.LocalEndPoint());

            Assert.That(connection.IsCreated);
            Driver.ScheduleUpdate().Complete();

            RemoteDriver.ScheduleUpdate().Complete();
            Assert.That(RemoteDriver.Accept().IsCreated);

            Driver.ScheduleUpdate().Complete();
            DataStreamReader reader;

            Assert.That(connection.PopEvent(Driver, out reader) == NetworkEvent.Type.Connect);
        }
Beispiel #2
0
        public void DisconnectFromARemoteEndPoint()
        {
            using (var host = new LocalDriverHelper(default(NetworkEndPoint)))
            {
                host.Host();
                var driver = new NetworkDriver(new IPCNetworkInterface(), new NetworkDataStreamParameter {
                    size = 64
                });

                // Need to be connected in order to be able to send a disconnect packet.
                NetworkConnection connectionId = driver.Connect(host.EndPoint);
                Assert.True(connectionId != default(NetworkConnection));
                driver.ScheduleUpdate().Complete();

                var local = driver.LocalEndPoint();
                host.Assert_GotConnectionRequest(local, true);

                NetworkConnection con;
                DataStreamReader  slice;
                // Pump so we get the accept message back.
                driver.ScheduleUpdate().Complete();
                Assert.AreEqual(NetworkEvent.Type.Connect, driver.PopEvent(out con, out slice));
                driver.Disconnect(connectionId);
                driver.ScheduleUpdate().Complete();

                host.Assert_GotDisconnectionRequest(local);

                driver.Dispose();
            }
        }
Beispiel #3
0
        public void Accept()
        {
            Driver.Bind(NetworkEndPoint.LoopbackIpv4);
            Driver.Listen();
            Assert.That(Driver.Listening);

            // create connection to test to connect.
            /*var remote =*/ RemoteDriver.Connect(Driver.LocalEndPoint());

            NetworkConnection id;
            DataStreamReader  reader;
            const int         maximumIterations = 10;
            int  count     = 0;
            bool connected = false;

            while (count++ < maximumIterations)
            {
                // Clear pending events
                Driver.PopEvent(out id, out reader);
                RemoteDriver.PopEvent(out id, out reader);

                Driver.ScheduleUpdate().Complete();
                RemoteDriver.ScheduleUpdate().Complete();
                var connection = Driver.Accept();
                if (connection != default(NetworkConnection))
                {
                    connected = true;
                }
            }

            Assert.That(connected);
        }
Beispiel #4
0
        public void ShouldAcceptConnection()
        {
            Server.Instance.Init(new ushort[] { 9099 }, 100, true, false);
            var serverDriver = new NetworkDriver(new IPCNetworkInterface(), new NetworkDataStreamParameter {
                size = 64
            });

            Server.Instance.Start(serverDriver);

            // send out connection request
            var clientDriver = new NetworkDriver(new IPCNetworkInterface(), new NetworkDataStreamParameter {
                size = 64
            });
            NetworkConnection networkConnection = clientDriver.Connect(serverDriver.LocalEndPoint());

            Assert.True(networkConnection != default);
            clientDriver.ScheduleUpdate().Complete();

            var numberOfConnectedClients = 0;
            var onClientConnected        = new Server.ConnectionDelegate(id => numberOfConnectedClients++);

            Server.Instance.ClientConnected += onClientConnected;
            Server.Instance.Tick();
            Assert.AreEqual(1, numberOfConnectedClients);
            Server.Instance.ClientConnected -= onClientConnected;

            if (clientDriver.IsCreated)
            {
                clientDriver.Dispose();
            }
        }
Beispiel #5
0
        public void DisconnectByRemote()
        {
            using (var host = new LocalDriverHelper(default(NetworkEndPoint)))
                using (var client = new NetworkDriver(new IPCNetworkInterface()))
                {
                    host.Host();
                    var popEvent = NetworkEvent.Type.Empty;
                    var c        = client.Connect(host.EndPoint);

                    client.ScheduleUpdate().Complete();
                    host.Assert_GotConnectionRequest(client.LocalEndPoint(), true);

                    byte             reason = 0;
                    DataStreamReader reader;
                    for (int frm = 0; frm < 10; ++frm)
                    {
                        if (c.GetState(client) == NetworkConnection.State.Connected)
                        {
                            c.Disconnect(client);
                        }

                        if ((popEvent = host.m_LocalDriver.PopEvent(out var id, out reader)) != NetworkEvent.Type.Empty)
                        {
                            reason = (reader.IsCreated && reader.Length > 0) ? reason = reader.ReadByte() : (byte)0;
                            break;
                        }
                        host.Update();
                        client.ScheduleUpdate().Complete();
                    }
                    Assert.AreEqual(NetworkEvent.Type.Disconnect, popEvent);
                    Assert.AreEqual((byte)DisconnectReason.ClosedByRemote, reason);
                }
        }
Beispiel #6
0
        public void NetworkPipeline_Fragmentation_SendRecvOnce()
        {
            var clientPipe = m_ClientDriver.CreatePipeline(typeof(FragmentationPipelineStage));
            var serverPipe = m_ServerDriver.CreatePipeline(typeof(FragmentationPipelineStage));

            // Connect to server
            var clientToServer = m_ClientDriver.Connect(m_ServerDriver.LocalEndPoint());

            Assert.AreNotEqual(default(NetworkConnection), clientToServer);
            m_ClientDriver.ScheduleUpdate().Complete();

            // Handle incoming connection from client
            m_ServerDriver.ScheduleUpdate().Complete();
            var serverToClient = m_ServerDriver.Accept();

            Assert.AreNotEqual(default(NetworkConnection), serverToClient);

            // Send message to client
            if (m_ServerDriver.BeginSend(serverPipe, serverToClient, out var strm) == 0)
            {
                strm.WriteInt(42);
                m_ServerDriver.EndSend(strm);
            }
            m_ServerDriver.ScheduleUpdate().Complete();

            // Receive incoming message from server
            m_ClientDriver.ScheduleUpdate().Complete();
            DataStreamReader readStrm;

            Assert.AreEqual(NetworkEvent.Type.Connect, clientToServer.PopEvent(m_ClientDriver, out readStrm));
            Assert.AreEqual(NetworkEvent.Type.Data, clientToServer.PopEvent(m_ClientDriver, out readStrm));
            Assert.AreEqual(sizeof(int), readStrm.Length);
            Assert.AreEqual(42, readStrm.ReadInt());
        }
        public void IPC_Setup()
        {
            Driver = TestNetworkDriver.Create(new NetworkDataStreamParameter {
                size = 64
            });
            RemoteDriver = TestNetworkDriver.Create(new NetworkDataStreamParameter {
                size = 64
            });

            RemoteDriver.Bind(NetworkEndPoint.LoopbackIpv4);
            RemoteDriver.Listen();
            ToRemoteConnection = Driver.Connect(RemoteDriver.LocalEndPoint());
            Driver.ScheduleFlushSend(default).Complete();
Beispiel #8
0
        public void ConnectAttemptWithRetriesToARemoteEndPoint()
        {
            NetworkConnection connection;

            NetworkEvent.Type eventType = 0;
            DataStreamReader  reader;

            // Tiny connect timeout for this test to be quicker
            using (var client = new NetworkDriver(new IPCNetworkInterface(), new NetworkDataStreamParameter {
                size = 64
            },
                                                  new NetworkConfigParameter {
                connectTimeoutMS = 15, maxConnectAttempts = 10, fixedFrameTimeMS = 10
            }))
            {
                var hostAddress = NetworkEndPoint.LoopbackIpv4.WithPort(1);
                client.Connect(hostAddress);

                // Wait past the connect timeout so there will be unanswered connect requests
                client.ScheduleUpdate().Complete();
                client.ScheduleUpdate().Complete();

                using (var host = new LocalDriverHelper(hostAddress))
                {
                    host.Host();

                    // Now give the next connect attempt time to happen
                    // TODO: Would be better to be able to see internal state here and explicitly wait until next connect attempt happens
                    //client.ScheduleUpdate().Complete();

                    host.Assert_GotConnectionRequest(client.LocalEndPoint(), true);

                    // Wait for the client to get the connect event back
                    for (int i = 0; i < 2; ++i)
                    {
                        client.ScheduleUpdate().Complete();
                        eventType = client.PopEvent(out connection, out reader);
                        if (eventType != NetworkEvent.Type.Empty)
                        {
                            break;
                        }
                    }

                    Assert.AreEqual(NetworkEvent.Type.Connect, eventType);
                }
            }
        }
Beispiel #9
0
        public void DisconnectTimeoutOnServer()
        {
            using (var host = new LocalDriverHelper(default(NetworkEndPoint),
                                                    new NetworkConfigParameter {
                disconnectTimeoutMS = 40, fixedFrameTimeMS = 10
            }))
                using (var client = new NetworkDriver(new IPCNetworkInterface(), new NetworkConfigParameter {
                    disconnectTimeoutMS = 40, fixedFrameTimeMS = 10
                }))
                {
                    NetworkConnection id;
                    NetworkEvent.Type popEvent = NetworkEvent.Type.Empty;
                    DataStreamReader  reader;
                    byte reason = 0;

                    host.Host();

                    client.Connect(host.EndPoint);
                    client.ScheduleUpdate().Complete();
                    host.Assert_GotConnectionRequest(client.LocalEndPoint(), true);

                    // Host sends stuff but gets nothing back, until disconnect timeout happens
                    for (int frm = 0; frm < 10; ++frm)
                    {
                        if (host.m_LocalDriver.BeginSend(NetworkPipeline.Null, host.ClientConnections[0], out var stream) == 0)
                        {
                            for (int i = 0; i < 100; i++)
                            {
                                stream.WriteByte((byte)i);
                            }

                            host.m_LocalDriver.EndSend(stream);
                        }
                        if ((popEvent = host.m_LocalDriver.PopEvent(out id, out reader)) != NetworkEvent.Type.Empty)
                        {
                            reason = (reader.IsCreated && reader.Length > 0) ? reason = reader.ReadByte() : (byte)0;
                            break;
                        }
                        host.Update();
                    }
                    Assert.AreEqual(NetworkEvent.Type.Disconnect, popEvent);
                    Assert.AreEqual((byte)DisconnectReason.Timeout, reason);
                }
        }
Beispiel #10
0
        public void ConnectToARemoteEndPoint()
        {
            using (var host = new LocalDriverHelper(default(NetworkEndPoint)))
            {
                host.Host();
                var driver = new NetworkDriver(new IPCNetworkInterface(), new NetworkDataStreamParameter {
                    size = 64
                });

                NetworkConnection connectionId = driver.Connect(host.EndPoint);
                Assert.True(connectionId != default(NetworkConnection));
                driver.ScheduleUpdate().Complete();

                var local = driver.LocalEndPoint();
                host.Assert_GotConnectionRequest(local);

                driver.Dispose();
            }
        }
Beispiel #11
0
 public void NetworkPipeline_CreatePipelineAfterConnectFails()
 {
     m_ClientDriver.Connect(m_ServerDriver.LocalEndPoint());
     Assert.Throws <InvalidOperationException>(() => { m_ClientDriver.CreatePipeline(typeof(TestPipelineStageWithHeader)); });
 }