/// <summary> /// Initializes a new instance of the <see cref="MemoryWriter"/> class. /// </summary> /// <param name="containerInfoPointer">The container information pointer.</param> /// <exception cref="ArgumentNullException">Pointer</exception> /// <exception cref="ArgumentException">Length has to be 8 byte aligned!</exception> /// <exception cref="Exception"> /// There is already a writer connected to the container! /// or /// Multiple writer access is not allowed at memory containers! /// </exception> public MemoryWriter(IntPtr containerInfoPointer) { if (disposedValue) { throw new ObjectDisposedException("MemoryWriter"); } var id = DefaultRNG.UInt32; ContainerInfoPointer = containerInfoPointer; containerInfo = GetContainerInfo(); if (containerInfo.Data == IntPtr.Zero) { throw new ArgumentNullException(nameof(containerInfoPointer)); } if (containerInfo.Length % 8 != 0) { throw new ArgumentException("Length has to be 8 byte aligned!"); } if (containerInfo.WriterID != 0) { throw new Exception("There is already a writer connected to the container!"); } containerInfo.WriterID = id; SetContainerInfo(0); var checkContainerInfo = GetContainerInfo(); if (!Equals(checkContainerInfo, containerInfo)) { throw new Exception("Multiple writer access is not allowed at memory containers!"); } }
/// <summary> /// Initializes a new instance of the <see cref="MemoryWriter"/> class. /// </summary> /// <param name="id">The identifier.</param> /// <param name="name">Name of the container.</param> /// <param name="size">Size of the data.</param> public MemoryWriter(uint id, string name, int size) { containerInfo = new MemoryContainerInfo() { ID = id, Data = Marshal.AllocHGlobal(size), WriterID = DefaultRNG.UInt32, Position = 0, Name = name, Length = size, }; ContainerInfoPointer = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(MemoryContainerInfo))); FreeMemory = true; }