Example #1
0
        //public int DataSize => index;

        #region ====Ctor====
        EntityStoreWriter(int size)
        {
            if (Runtime.RuntimeContext.Current.RuntimeId == 0)
            {
                nativeStringPtr = NativeApi.NewNativeString(size, out dataPtr);
            }
            else
            {
                //注意:在子进程创建的是NativeBytes
                nativeStringPtr = NativeBytes.MakeRaw(size);
                dataPtr         = (byte *)(nativeStringPtr + 4).ToPointer();
            }
            index = 0;
        }
Example #2
0
        //TODO: 获取分区对应的RaftGroupId时考虑在Native层缓存

        /// <summary>
        /// Serializes the model.
        /// </summary>
        /// <returns>在主进程内返回的是NativeString,在子进程内返回NativeBytes</returns>
        internal static unsafe IntPtr SerializeModel(object obj, out int size, int offset = 0)
        {
            //TODO:暂使用内存Copy
            byte[] data = null;
            using (MemoryStream ms = new MemoryStream(1024))
            {
                BinSerializer cf = new BinSerializer(ms);
                try { cf.Serialize(obj); }
                catch (Exception) { throw; }
                finally { cf.Clear(); }

                ms.Close();
                data = ms.ToArray();
            }

            size = data.Length + offset;
            IntPtr nativeDataPtr = IntPtr.Zero;
            byte * dp;

            if (Runtime.RuntimeContext.Current.RuntimeId == 0)
            {
                nativeDataPtr = NativeApi.NewNativeString(size, out dp);
            }
            else
            {
                nativeDataPtr = NativeBytes.MakeRaw(size);
                dp            = (byte *)nativeDataPtr + 4;
            }

            //Log.Debug($"序列化模型: {obj.GetType().Name} arraySize={size} stringSize={NativeString.GetSize(dataString)}");
            fixed(byte *pv = data)
            {
                for (int i = 0; i < offset; i++)
                {
                    dp[i] = 0;
                }
                Buffer.MemoryCopy(pv, dp + offset, data.Length, data.Length);
            }

            return(nativeDataPtr);
        }