Beispiel #1
0
        public void UdpC_ReconnectAndResend_ShouldReconnectAndResend()
        {
            using (var server = new UdpNetworkDriver(new NetworkDataStreamParameter {
            }))
                using (var client = new UdpNetworkDriver(new NetworkDataStreamParameter {
                }))
                {
                    ushort serverPort = 50007;

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

                    server.Listen();

                    NetworkConnection serverConnection, clientConnection;
                    int maxIterations = 100;

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

                    client.Disconnect(id);

                    server.ScheduleUpdate().Complete();

                    var data = new byte[1472];
                    var size = 1472;
                    NetworkConnection from;

                    Assert.AreEqual(ExperimentalEventType.Disconnect, PollEvent(ExperimentalEventType.Disconnect, maxIterations, server, ref data, out size, out from));

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

                    var message = new byte[]
                    {
                        (byte)'m',
                        (byte)'e',
                        (byte)'s',
                        (byte)'s',
                        (byte)'a',
                        (byte)'g',
                        (byte)'e'
                    };

                    SendReceive(client, server, clientConnection, serverConnection, message, maxIterations);
                }
        }
Beispiel #2
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;
                }
            }
        }