/// <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; } }
private static void _ReadCallback(IntPtr streamPtr, int nread, ref UvBuffer buf) { var stream = FromIntPtr <UvStream>(streamPtr); foreach (var gcHandle in stream._PinnedReadHandles) { gcHandle.Free(); } stream._PinnedReadHandles.Clear(); Exception error; Libuv.CheckStatusCode(nread, out error); stream._UserReadCallback.Invoke(stream, nread, error, stream._UserReadState); }
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; } }