Ejemplo n.º 1
0
        public unsafe void Assert_GotDataRequest(NetworkEndPoint from, byte[] dataToCompare)
        {
            NetworkInterfaceEndPoint remote = default;

            network_iovec[] iovecs = new network_iovec[2];
            iovecs[0].buf = m_LocalData.GetUnsafePtr();
            iovecs[0].len = sizeof(UdpCHeader);
            iovecs[1].buf = (byte *)m_LocalData.GetUnsafePtr() + sizeof(UdpCHeader);
            iovecs[1].len = NetworkParameterConstants.MTU;
            int dataLen = 0;

            Assert.True(EndPoint.IsLoopback || EndPoint.IsAny);
            Assert.True(from.IsLoopback || from.IsAny);
            var localEndPoint = IPCManager.Instance.CreateEndPoint(EndPoint.Port);
            var fromEndPoint  = IPCManager.Instance.CreateEndPoint(from.Port);

            fixed(network_iovec *iovptr = &iovecs[0])
            {
                dataLen = IPCManager.Instance.ReceiveMessageEx(localEndPoint, iovptr, 2, ref remote);
            }

            if (dataLen <= 0)
            {
                iovecs[0].len = iovecs[1].len = 0;
            }

            Assert.True(iovecs[0].len + iovecs[1].len == dataLen);
            Assert.True(iovecs[0].len == sizeof(UdpCHeader));

            UdpCHeader header = new UdpCHeader();
            var        reader = new DataStreamReader(m_LocalData.GetSubArray(0, sizeof(UdpCHeader)));

            Assert.True(reader.IsCreated);
            reader.ReadBytes(header.Data, sizeof(UdpCHeader));
            Assert.True(header.Type == (int)UdpCProtocol.Data);

            Assert.True(remote == fromEndPoint);

            Assert.True(iovecs[1].len == dataToCompare.Length);

            reader = new DataStreamReader(m_LocalData.GetSubArray(iovecs[0].len, dataToCompare.Length));
            var received = new NativeArray <byte>(dataToCompare.Length, Allocator.Temp);

            reader.ReadBytes(received);

            for (int i = 0, n = dataToCompare.Length; i < n; ++i)
            {
                Assert.True(received[i] == dataToCompare[i]);
            }
        }
        public unsafe void Assert_GotDataRequest(NetworkEndPoint from, byte[] dataToCompare)
        {
            NetworkEndPoint remote = default(NetworkEndPoint);

            m_LocalDataStream.Clear();
            network_iovec[] iovecs = new network_iovec[2];
            iovecs[0].buf = m_LocalDataStream.GetUnsafePtr();
            iovecs[0].len = sizeof(UdpCHeader);
            iovecs[1].buf = m_LocalDataStream.GetUnsafePtr() + sizeof(UdpCHeader);
            iovecs[1].len = NetworkParameterConstants.MTU;
            int dataLen = 0;

            fixed(network_iovec *iovptr = &iovecs[0])
            {
                dataLen = IPCManager.Instance.ReceiveMessageEx(Address, iovptr, 2, ref remote);
            }

            if (dataLen <= 0)
            {
                iovecs[0].len = iovecs[1].len = 0;
            }

            Assert.True(iovecs[0].len + iovecs[1].len == dataLen);
            Assert.True(iovecs[0].len == sizeof(UdpCHeader));
            m_LocalDataStream.WriteBytesWithUnsafePointer(iovecs[0].len);

            UdpCHeader header    = new UdpCHeader();
            var        reader    = new DataStreamReader(m_LocalDataStream, 0, sizeof(UdpCHeader));
            var        readerCtx = default(DataStreamReader.Context);

            Assert.True(reader.IsCreated);
            reader.ReadBytes(ref readerCtx, header.Data, sizeof(UdpCHeader));
            Assert.True(header.Type == (int)UdpCProtocol.Data);

            Assert.True(remote.Family == NetworkFamily.IPC);
            //Assert.True(remote.ipc_handle == from.ipc_handle);
            Assert.True(remote.Port == from.Port);

            Assert.True(iovecs[1].len == dataToCompare.Length);
            m_LocalDataStream.WriteBytesWithUnsafePointer(iovecs[1].len);

            reader    = new DataStreamReader(m_LocalDataStream, iovecs[0].len, dataToCompare.Length);
            readerCtx = default(DataStreamReader.Context);
            var received = reader.ReadBytesAsArray(ref readerCtx, dataToCompare.Length);

            for (int i = 0, n = dataToCompare.Length; i < n; ++i)
            {
                Assert.True(received[i] == dataToCompare[i]);
            }
        }