Exemplo n.º 1
0
        void ConnectTogether(UdpNetworkDriver server, UdpNetworkDriver client, int maxIterations, out NetworkConnection serverConnection, out NetworkConnection clientConnection)
        {
            int servers = 0, clients = 0, iterations = 0;

            serverConnection = default(NetworkConnection);
            clientConnection = default(NetworkConnection);

            DataStreamReader reader;

            NetworkConnection poppedConnection = default(NetworkConnection);

            while (clients != 1 || servers != 1)
            {
                Assert.Less(iterations++, maxIterations);

                server.ScheduleUpdate().Complete();

                var newConnection = server.Accept();
                if (newConnection != default(NetworkConnection))
                {
                    clients++;
                    clientConnection = newConnection;
                }

                if (client.PopEvent(out poppedConnection, out reader) == ExperimentalEventType.Connect)
                {
                    serverConnection = poppedConnection;
                    servers++;
                }

                client.ScheduleUpdate().Complete();
                Assert.AreNotEqual(clientConnection, default(NetworkConnection));
            }
        }
Exemplo n.º 2
0
        ExperimentalEventType PollEvent(ExperimentalEventType ev, int maxIterations, UdpNetworkDriver socket, ref byte[] buffer, out int size, out NetworkConnection connection)
        {
            int iterator = 0;

            size       = 0;
            connection = default(NetworkConnection);

            while (iterator++ < maxIterations)
            {
                DataStreamReader reader;
                var context = default(DataStreamReader.Context);
                ExperimentalEventType e;
                if ((e = socket.PopEvent(out connection, out reader)) == ev)
                {
                    if (reader.IsCreated)
                    {
                        reader.ReadBytesIntoArray(ref context, ref buffer, reader.Length);
                        size = reader.Length;
                    }
                    return(e);
                }
                socket.ScheduleUpdate().Complete();
            }
            return(ExperimentalEventType.Empty);
        }
Exemplo n.º 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;
                }
            }
        }