Exemplo n.º 1
0
        /// <summary>
        /// Executes the write request on the base handle.
        /// </summary>
        public void Write()
        {
            this.EnsureCallingThread();
            try
            {
                // Build UvBuffers to write
                var buffers   = this.Buffers;
                var count     = buffers.Count;
                var uvBuffers = new UvBuffer[count];
                for (int i = 0; i < count; i++)
                {
                    var buffer             = buffers[i];
                    var pinnedBufferHandle = GCHandle.Alloc(buffer.Array, GCHandleType.Pinned);
                    this._PinnedWriteHandles.Add(pinnedBufferHandle);

                    uvBuffers[i] = Libuv.uv_buf_init(Marshal.UnsafeAddrOfPinnedArrayElement(buffer.Array, buffer.Offset), buffer.Count);
                }

                this.Write(uvBuffers);
            }
            catch (Exception)
            {
                foreach (var gcHandle in this._PinnedWriteHandles)
                {
                    gcHandle.Free();
                }
                this._PinnedWriteHandles.Clear();
                throw;
            }
        }
Exemplo n.º 2
0
        private static void _AllocCallback(IntPtr streamPtr, int suggestedSize, out UvBuffer buf)
        {
            var stream = FromIntPtr <UvStream>(streamPtr);

            try
            {
                var buffer             = stream._UserAllocCallback.Invoke(stream, suggestedSize, stream._UserReadState);
                var pinnedBufferHandle = GCHandle.Alloc(buffer.Array, GCHandleType.Pinned);
                stream._PinnedReadHandles.Add(pinnedBufferHandle);

                buf = Libuv.uv_buf_init(Marshal.UnsafeAddrOfPinnedArrayElement(buffer.Array, buffer.Offset), buffer.Count);
            }
            catch (Exception)
            {
                foreach (var gcHandle in stream._PinnedReadHandles)
                {
                    gcHandle.Free();
                }
                stream._PinnedReadHandles.Clear();
                buf = Libuv.uv_buf_init(IntPtr.Zero, 0);
                throw;
            }
        }