public void Resize()
        {
            const int initialLength = 8;
            var       shared        = new SharedArray <Vector3>(initialLength);

            Vector3[] asManaged = shared;
            for (int i = 0; i < asManaged.Length; i++)
            {
                asManaged[i] = Vector3.one * i;
            }

            Assert.AreEqual(initialLength, shared.Length);
            Assert.AreEqual(initialLength, ((NativeArray <Vector3>)shared).Length);

            const int resizedLength = 16;

            shared.Resize(resizedLength);

            Vector3[] managedAfterResize = shared;
            // did data from before resize get preserved ?
            for (int i = 0; i < initialLength; i++)
            {
                Assert.AreEqual(Vector3.one * i, managedAfterResize[i]);
            }

            Assert.AreEqual(resizedLength, managedAfterResize.Length);
            Assert.AreEqual(resizedLength, ((NativeArray <Vector3>)shared).Length);
        }
Beispiel #2
0
        //private static byte[] ConvertToVarBinary(long value, out byte length)
        //{
        //	var buff = SharedArray.Get();
        //	buff[0] = (byte)value;
        //	buff[1] = unchecked((byte)(value >> 8));
        //	buff[2] = unchecked((byte)(value >> 16));
        //	buff[3] = unchecked((byte)(value >> 24));
        //	buff[4] = unchecked((byte)(value >> 32));
        //	buff[5] = unchecked((byte)(value >> 40));
        //	buff[6] = unchecked((byte)(value >> 48));
        //	buff[7] = unchecked((byte)(value >> 56));

        //	for (int i = 8 - 1; i >= 0; i--)
        //	{
        //		if (buff[i] > 0)
        //		{
        //			length = (byte)(i + 1);
        //			//if (length != 8)
        //			//	Array.Resize(ref buff, length);
        //			return buff;
        //		}
        //	}

        //	length = 1;
        //	return new byte[] { 0 };
        //}

        private static byte[] ConvertToVarBinary(ulong value, out byte length)
        {
            var buff = SharedArray.Get();

            buff[0] = (byte)value;
            buff[1] = unchecked ((byte)(value >> 8));
            buff[2] = unchecked ((byte)(value >> 16));
            buff[3] = unchecked ((byte)(value >> 24));
            buff[4] = unchecked ((byte)(value >> 32));
            buff[5] = unchecked ((byte)(value >> 40));
            buff[6] = unchecked ((byte)(value >> 48));
            buff[7] = unchecked ((byte)(value >> 56));

            for (int i = 8 - 1; i >= 0; i--)
            {
                if (buff[i] > 0)
                {
                    length = (byte)(i + 1);
                    return(buff);
                }
            }

            length = 1;
            return(new byte[] { 0 });
        }
Beispiel #3
0
        public void UpdateTexture()
        {
            if (Initialized)
            {
                UpdateInitialized();

                //execute run-once functions
                if (_needToRunOnce)
                {
                    SendExecuteJSEvent(_runOnceJS);
                    _needToRunOnce = false;
                }
            }
            else
            {
                //  yield return new WaitForSeconds(2);
                if (_connected)
                {
                    //Thread.Sleep(200); //give it some time to initialize
                    try
                    {
                        //init memory file
                        _mainTexArray = new SharedArray <byte>(_sharedFileName);

                        Initialized = true;
                    }
                    catch (Exception ex)
                    {
                        //SharedMem and TCP exceptions
                        Debug.Log("Exception on init:" + ex.Message + ".Waiting for plugin server");
                    }
                }
            }
        }
Beispiel #4
0
        internal static float ReadVarSingle(BinaryReader reader)
        {
            var input = reader.ReadByte();

            var buff = SharedArray.Get();

            var embedded = (input & FlagEmbedded) == FlagEmbedded;

            if (embedded)
            {
                SharedArray.ClearArray4();

                // last byte
                buff[3] = (byte)(input & MaskEmbedded);
                return(BitConverter.ToSingle(buff, 0));
            }

            int length = input;

            if (length < 4)
            {
                SharedArray.ClearArray4();
            }

            reader.Read(buff, 4 - length, length);

            return(BitConverter.ToSingle(buff, 0));
        }
Beispiel #5
0
        public void AcquireReadWriteLocks_ReadWrite_Blocks()
        {
            var name = Guid.NewGuid().ToString();

            using (var sma = new SharedArray <byte>(name, 10))
            {
                using (var smr = new SharedArray <byte>(name))
                {
                    // Acquire write lock
                    sma.AcquireWriteLock();

                    // Should block (and fail to reset write signal)
                    Assert.IsFalse(smr.AcquireReadLock(10));

                    sma.ReleaseWriteLock();

                    smr.AcquireReadLock();

                    // Should block (and fail to reset read signal)
                    Assert.IsFalse(sma.AcquireWriteLock(10));

                    smr.ReleaseReadLock();
                }
            }
        }
        public void BothRepresentations_ReferToSameData()
        {
            var shared = new SharedArray <float>(8);

            float[] asManaged = shared;
            for (int i = 0; i < asManaged.Length; i++)
            {
                asManaged[i] = i;
            }

            NativeArray <float> asNative = shared;

            // does the native representation reflect changes made in the managed one ?
            for (int i = 0; i < asNative.Length; i++)
            {
                Assert.IsTrue(asManaged[i].Equals(asNative[i]));
            }

            for (int i = 0; i < asNative.Length; i++)
            {
                asNative[i] = i * 2f;
            }

            // does the managed representation reflect changes made in the native one ?
            for (int i = 0; i < asManaged.Length; i++)
            {
                var nativeElement = asNative[i];
                Assert.IsTrue((i * 2f).Equals(nativeElement));
                Assert.IsTrue(asManaged[i].Equals(nativeElement));
            }
        }
Beispiel #7
0
        internal static double ReadVarDouble(BinaryReader reader)
        {
            var input = reader.ReadByte();

            var buff = SharedArray.Get();

            var embedded = (input & FlagEmbedded) == FlagEmbedded;

            if (embedded)
            {
                SharedArray.ClearArray8();

                // last byte
                buff[7] = (byte)(input & MaskEmbedded);
                return(BitConverter.ToDouble(buff, 0));
            }

            int length = input;

            if (length < 8)
            {
                SharedArray.ClearArray8();
            }

            reader.Read(buff, 8 - length, length);

            return(BitConverter.ToDouble(buff, 0));
        }
        public unsafe void GetPinnableReference_ReturnsCorrectPointer()
        {
            var shared = new SharedArray <Vector4>(4);

            fixed(Vector4 *arrayPtr = (Vector4[])shared)
            fixed(Vector4 * pinRefPtr = shared)
            Assert.IsTrue(arrayPtr == pinRefPtr);
        }
Beispiel #9
0
        public bool Init(int size)
        {
            _sharedBuffer = new SharedArray <byte>(_name, size);
            _size         = size;
            _isOpen       = true;

            return(_isOpen);
        }
 public void Resize(int newSize)
 {
     if (_sharedBuf.Length != newSize)
     {
         _sharedBuf.Close();
         _sharedBuf = new SharedArray <byte>(Filename, newSize);
     }
 }
Beispiel #11
0
        private static byte[] ConvertToVarBinary(int value, out byte length)
        {
            if (value < 0)
            {
                length = 4;
                var buff = SharedArray.Get();
                buff[0] = (byte)value;
                buff[1] = unchecked ((byte)(value >> 8));
                buff[2] = unchecked ((byte)(value >> 16));
                buff[3] = unchecked ((byte)(value >> 24));
                return(buff);
            }
            else
            {
                var buff = SharedArray.Get();
                SharedArray.ClearArray4();

                var num1 = (byte)value;
                var num2 = unchecked ((byte)(value >> 8));
                var num3 = unchecked ((byte)(value >> 16));
                var num4 = unchecked ((byte)(value >> 24));


                buff[0] = num1;

                if (num2 > 0)
                {
                    buff[1] = num2;
                }
                else if (num3 == 0 && num4 == 0)
                {
                    length = 1;
                    return(buff);
                }

                if (num3 > 0)
                {
                    buff[2] = num3;
                }
                else if (num4 == 0)
                {
                    length = 2;
                    return(buff);
                }

                if (num4 > 0)
                {
                    buff[3] = num4;
                }
                else
                {
                    length = 3;
                    return(buff);
                }
                length = 4;
                return(buff);
            }
        }
Beispiel #12
0
 public void Rent(int length)
 {
     using var buffer = SharedArray.Rent(length);
     Assert.True(buffer.Position == 0);
     Assert.True(buffer.Length % 4096 == 0);
     buffer.Write(10);
     Assert.Equal(sizeof(int), buffer.AsSpan().Length);
     Assert.Equal(sizeof(int), buffer.ToArray().Length);
 }
Beispiel #13
0
        public void Indexer_OutOfRange_ThrowsException()
        {
            var name = Guid.NewGuid().ToString();

            using (var sma = new SharedArray <int>(name, 10))
            {
                bool exceptionThrown = false;
                try
                {
                    sma[-1] = 0;
                }
                catch (ArgumentOutOfRangeException)
                {
                    exceptionThrown = true;
                }

                Assert.IsTrue(exceptionThrown, "Index of -1 should result in ArgumentOutOfRangeException");

                exceptionThrown = false;
                IList <int> a = sma;
                try
                {
                    a[-1] = 0;
                }
                catch (ArgumentOutOfRangeException)
                {
                    exceptionThrown = true;
                }

                Assert.IsTrue(exceptionThrown, "Index of -1 should result in ArgumentOutOfRangeException");

                try
                {
                    exceptionThrown = false;
                    sma[sma.Length] = 0;
                }
                catch (ArgumentOutOfRangeException)
                {
                    exceptionThrown = true;
                }

                Assert.IsTrue(exceptionThrown, "Index of Length should result in ArgumentOutOfRangeException");


                try
                {
                    exceptionThrown = false;
                    a[a.Count]      = 0;
                }
                catch (ArgumentOutOfRangeException)
                {
                    exceptionThrown = true;
                }

                Assert.IsTrue(exceptionThrown, "Index of Length should result in ArgumentOutOfRangeException");
            }
        }
Beispiel #14
0
        public static SharedArray ConvertToBytes(this EventMeta metaInfo)
        {
            var buffer = SharedArray.Rent();

            buffer.Write(metaInfo.Version);
            buffer.Write(metaInfo.Timestamp);
            buffer.WriteUtf8String(metaInfo.FlowId);
            return(buffer);
        }
Beispiel #15
0
 public void Resize(int size)
 {
     if (_sharedBuffer.Length != size)
     {
         _sharedBuffer.Close();
         Thread.Sleep(100);
         _sharedBuffer = new SharedArray <byte>(_name, size);
     }
 }
Beispiel #16
0
        public void WriteShort(int count)
        {
            using var buffer = SharedArray.Rent();
            for (int i = 0; i < count; i++)
            {
                buffer.Write((short)i);
            }

            Assert.Equal(sizeof(short) * count, buffer.AsSpan().Length);
            Assert.Equal(sizeof(short) * count, buffer.ToArray().Length);
        }
        public unsafe void ManagedAndNativeHaveSamePointers()
        {
            var shared = new SharedArray <Vector4>(4);

            fixed(void *managedPtr = (Vector4[])shared)
            {
                var nativePtr = ((NativeArray <Vector4>)shared).GetUnsafeReadOnlyPtr();

                Assert.IsTrue(managedPtr == nativePtr);
            }
        }
        public void Clear_BasicSuccess()
        {
            var managed = new[] { 10, 8, 30, 20 };
            var shared  = new SharedArray <int>(managed);

            shared.Clear();

            for (int i = 0; i < managed.Length; i++)
            {
                Assert.Zero(managed[i]);
            }
        }
        public void GetEnumerator_ThrowsIf_AnyJobsAreWriting()
        {
            var          shared          = new SharedArray <int>(4);
            TestDelegate enumerateAction = () => { foreach (var e in shared)
                                                   {
                                                   }
            };

            AssertSafety.ThrowsIfAnyScheduledWriters(shared, enumerateAction);
            // it's fine if other things are reading while we enumerate, because enumerating returns copies of structs,
            // so the source data won't be changed by the enumerator
            AssertSafety.DoesNotThrowIfAnyScheduledReaders(shared, enumerateAction);
        }
Beispiel #20
0
        /// <summary>
        /// 关闭浏览器
        /// </summary>
        public void Shutdown()
        {
            SendShutdownEvent();

            Initialized = false;
            _connected  = false;

            _mainTexArray  = null;
            _inCommServer  = null;
            _outCommServer = null;
            _pluginProcess = null;
            sPixelLock     = null;
        }
 public void Constructor_ThrowsIf_TypesAreNotEqualSize()
 {
     try
     {
         var shared = new SharedArray <Vector4, float>(8);
         throw new Exception("Shouldn't get here, the above constructor should throw!");
     }
     catch (InvalidOperationException e)
     {
         Assert.NotNull(e.Message);
         Assert.IsTrue(e.Message.Contains("size"));  // is the exception message the intended one ?
     }
 }
Beispiel #22
0
        public void IList_IsReadOnly()
        {
            var name = Guid.NewGuid().ToString();

            using (var sma = new SharedArray <int>(name, 10))
            {
                sma[0] = 3;
                sma[4] = 10;

                IList <int> a = sma;
                Assert.IsTrue(a.IsReadOnly);
            }
        }
 public void Connect(string filename)
 {
     try
     {
         _sharedBuf = new SharedArray <byte>(filename);
         Filename   = filename;
         _isOpen    = true;
     }
     catch (Exception ex)
     {
         _isOpen = false;
     }
 }
        void InitializeIndex(Vector3 center, Vector3 scale, int count, int index)
        {
            var matrices = RandomUtils.Matrices(center, scale, count, index * 18f + 20f);

            Matrices[index] = new SharedArray <Matrix4x4, float4x4>(matrices);
            var colors = new SharedArray <Vector4, float4>(RandomUtils.Colors(count));

            Colors[index] = colors;

            var block = new MaterialPropertyBlock();

            block.SetVectorArray(ColorShaderProperty, colors);
            PropertyBlocks[index] = block;
        }
 public void Connect(string filename)
 {
     try
     {
         _sharedBuf = new SharedArray <byte>(filename);
         Filename   = filename;
         _isOpen    = true;
         Console.Write("Server connected:" + filename);
     }
     catch (Exception ex)
     {
         _isOpen = false;
     }
 }
Beispiel #26
0
        public void WriteChars(int count)
        {
            using var buffer = SharedArray.Rent();
            var length = 0;

            for (int i = 0; i < count; i++)
            {
                buffer.WriteUtf8String(i.ToString().AsSpan());
                length += Encoding.UTF8.GetByteCount(i.ToString());
            }

            Assert.Equal(length, buffer.AsSpan().Length);
            Assert.Equal(length, buffer.ToArray().Length);
        }
Beispiel #27
0
        public bool Connect()
        {
            try
            {
                _sharedBuffer = new SharedArray <byte>(_name);
                _isOpen       = true;
            }
            catch (Exception ex)
            {
                _isOpen = false;
            }

            return(_isOpen);
        }
Beispiel #28
0
        public void IList_IndexOf()
        {
            var name = Guid.NewGuid().ToString();

            using (var sma = new SharedArray <int>(name, 10))
            {
                sma[0] = 3;
                sma[4] = 10;

                IList <int> a = sma;

                Assert.AreEqual(4, a.IndexOf(10));
                Assert.AreEqual(-1, a.IndexOf(11));
            }
        }
Beispiel #29
0
        public void IList_Contains()
        {
            var name = Guid.NewGuid().ToString();

            using (var sma = new SharedArray <int>(name, 10))
            {
                sma[0] = 3;
                sma[4] = 10;

                IList <int> a = sma;

                Assert.IsTrue(a.Contains(10));
                Assert.IsFalse(a.Contains(11));
            }
        }
        public void GetEnumerator_BasicSuccess()
        {
            var shared    = new SharedArray <float>(4);
            var asManaged = (float[])shared;

            for (int i = 0; i < asManaged.Length; i++)
            {
                asManaged[i] = i;
            }

            // try enumerating, which is just a wrapper around normal array enumeration
            foreach (var floatEntry in shared)
            {
                Assert.IsTrue(asManaged.Contains(floatEntry));
            }
        }