Пример #1
0
        public void UdpC_Timeout_ShouldDisconnect()
        {
            int customTimeout = 1000;

            using (var server = new UdpNetworkDriver(new NetworkConfigParameter {
                disconnectTimeoutMS = customTimeout
            }))
                using (var client = new UdpNetworkDriver(new NetworkConfigParameter {
                    disconnectTimeoutMS = customTimeout
                }))
                {
                    ushort serverPort = 50006;

                    server.Bind(NetworkEndPoint.Parse(IPAddress.Loopback.ToString(), serverPort));
                    client.Bind(NetworkEndPoint.Parse(IPAddress.Loopback.ToString(), 0));

                    server.Listen();

                    var id = client.Connect(NetworkEndPoint.Parse(IPAddress.Loopback.ToString(), serverPort));

                    NetworkConnection serverConnection, clientConnection;
                    int maxIterations = 100;

                    ConnectTogether(server, client, maxIterations, out serverConnection, out clientConnection);
                    Assert.AreEqual(id, serverConnection);

                    // Force timeout
                    Thread.Sleep(customTimeout + 500);

                    var message = new DataStreamWriter(7, Allocator.Persistent);
                    message.Write((byte)'m');
                    message.Write((byte)'e');
                    message.Write((byte)'s');
                    message.Write((byte)'s');
                    message.Write((byte)'a');
                    message.Write((byte)'g');
                    message.Write((byte)'e');
                    server.Send(NetworkPipeline.Null, clientConnection, message);

                    var data = new byte[1472];
                    int size = -1;
                    NetworkConnection from;
                    Assert.AreEqual(ExperimentalEventType.Disconnect, PollEvent(ExperimentalEventType.Disconnect, maxIterations, server, ref data, out size, out from));
                    Assert.AreEqual(from, clientConnection);
                }
        }
Пример #2
0
        public void UdpC_MultipleConnectTest_ShouldConnect()
        {
            using (var server = new UdpNetworkDriver(new NetworkDataStreamParameter {
            }))
                using (var client0 = new UdpNetworkDriver(new NetworkDataStreamParameter {
                }))
                    using (var client1 = new UdpNetworkDriver(new NetworkDataStreamParameter {
                    }))
                        using (var client2 = new UdpNetworkDriver(new NetworkDataStreamParameter {
                        }))
                            using (var client3 = new UdpNetworkDriver(new NetworkDataStreamParameter {
                            }))
                            {
                                ushort serverPort = 50005;

                                server.Bind(NetworkEndPoint.Parse(IPAddress.Loopback.ToString(), serverPort));
                                client0.Bind(NetworkEndPoint.Parse(IPAddress.Loopback.ToString(), 0));
                                client1.Bind(NetworkEndPoint.Parse(IPAddress.Loopback.ToString(), 0));
                                client2.Bind(NetworkEndPoint.Parse(IPAddress.Loopback.ToString(), 0));
                                client3.Bind(NetworkEndPoint.Parse(IPAddress.Loopback.ToString(), 0));

                                server.Listen();

                                NetworkConnection serverConnection, clientConnection;
                                int maxIterations = 100;


                                var id = client0.Connect(NetworkEndPoint.Parse(IPAddress.Loopback.ToString(), serverPort));
                                ConnectTogether(server, client0, maxIterations, out serverConnection, out clientConnection);
                                Assert.AreEqual(id, serverConnection);

                                id = client1.Connect(NetworkEndPoint.Parse(IPAddress.Loopback.ToString(), serverPort));
                                ConnectTogether(server, client1, maxIterations, out serverConnection, out clientConnection);
                                Assert.AreEqual(id, serverConnection);

                                id = client2.Connect(NetworkEndPoint.Parse(IPAddress.Loopback.ToString(), serverPort));
                                ConnectTogether(server, client2, maxIterations, out serverConnection, out clientConnection);
                                Assert.AreEqual(id, serverConnection);

                                id = client3.Connect(NetworkEndPoint.Parse(IPAddress.Loopback.ToString(), serverPort));
                                ConnectTogether(server, client3, maxIterations, out serverConnection, out clientConnection);
                                Assert.AreEqual(id, serverConnection);
                            }
        }
Пример #3
0
        public void Update()
        {
            if (!m_Socket.Listening && !conn.IsCreated)
            {
                conn = m_Socket.Connect(NetworkEndPoint.Parse(IPAddress.Loopback.ToString(), (ushort)serverPort));
            }
            else if (!m_Socket.Listening && dataCounter == 0 && !conn.IsCreated)
            {
                using (var message = new DataStreamWriter(7, Allocator.Persistent))
                {
                    message.Write((byte)'m');
                    message.Write((byte)'e');
                    message.Write((byte)'s');
                    message.Write((byte)'s');
                    message.Write((byte)'a');
                    message.Write((byte)'g');
                    message.Write((byte)'e');

                    m_Socket.Send(NetworkPipeline.Null, conn, message);
                }
            }
            else if (!m_Socket.Listening && conn.IsCreated &&
                     UnityEngine.Random.Range(0, 1000) < 10)
            {
                m_Socket.Disconnect(conn);
                conn = default(NetworkConnection);
            }

            NetworkConnection connection;
            DataStreamReader  reader;
            var context = default(DataStreamReader.Context);
            var ev      = m_Socket.PopEvent(out connection, out reader);

            if (ev == ExperimentalEventType.Empty)
            {
                return;
            }

            using (var writer = new DataStreamWriter(reader.Length, Allocator.Temp))
            {
                unsafe
                {
                    reader.ReadBytes(ref context, writer.GetUnsafePtr(), reader.Length);
                }
                switch (ev)
                {
                case ExperimentalEventType.Connect:
                    connectCounter++;
                    break;

                case ExperimentalEventType.Disconnect:
                    conn = default(NetworkConnection);
                    disconnectCounter++;
                    break;

                case ExperimentalEventType.Data:
                    dataCounter++;
                    m_Socket.Send(NetworkPipeline.Null, connection, writer);
                    break;
                }
            }
        }