public unsafe void Write( UvStreamHandle handle, ref ReadableBuffer buffer, Action <UvWriteReq, int, Exception, object> callback, object state) { try { int nBuffers = 0; if (buffer.IsSingleSpan) { nBuffers = 1; } else { foreach (var span in buffer) { nBuffers++; } } // add GCHandle to keeps this SafeHandle alive while request processing _pins.Add(GCHandle.Alloc(this, GCHandleType.Normal)); var pBuffers = (Uv.uv_buf_t *)_bufs; if (nBuffers > BUFFER_COUNT) { // create and pin buffer array when it's larger than the pre-allocated one var bufArray = new Uv.uv_buf_t[nBuffers]; var gcHandle = GCHandle.Alloc(bufArray, GCHandleType.Pinned); _pins.Add(gcHandle); pBuffers = (Uv.uv_buf_t *)gcHandle.AddrOfPinnedObject(); } if (nBuffers == 1) { var span = buffer.FirstSpan; pBuffers[0] = Libuv.buf_init((IntPtr)span.UnsafePointer, span.Length); } else { int i = 0; foreach (var span in buffer) { pBuffers[i++] = Libuv.buf_init((IntPtr)span.UnsafePointer, span.Length); } } _callback = callback; _state = state; _uv.write(this, handle, pBuffers, nBuffers, _uv_write_cb); } catch { _callback = null; _state = null; Unpin(this); throw; } }
private static void UvAllocCb(IntPtr handle, int suggested_size, out Uv.uv_buf_t buf) { var stream = FromIntPtr <UvStreamHandle>(handle); try { buf = stream._allocCallback(stream, suggested_size, stream._readState); } catch { buf = stream.Libuv.buf_init(IntPtr.Zero, 0); throw; } }
public unsafe void Write( UvStreamHandle handle, ReadableBuffer buffer, Action <UvWriteReq, int, object> callback, object state) { try { // Preserve the buffer for the async call _buffer = buffer.Preserve(); buffer = _buffer.Buffer; int nBuffers = 0; if (buffer.IsSingleSpan) { nBuffers = 1; } else { foreach (var span in buffer) { nBuffers++; } } // add GCHandle to keeps this SafeHandle alive while request processing _pins.Add(GCHandle.Alloc(this, GCHandleType.Normal)); var pBuffers = (Uv.uv_buf_t *)_bufs; if (nBuffers > BUFFER_COUNT) { // create and pin buffer array when it's larger than the pre-allocated one var bufArray = new Uv.uv_buf_t[nBuffers]; var gcHandle = GCHandle.Alloc(bufArray, GCHandleType.Pinned); _pins.Add(gcHandle); pBuffers = (Uv.uv_buf_t *)gcHandle.AddrOfPinnedObject(); } if (nBuffers == 1) { var memory = buffer.First; void *pointer; if (memory.TryGetPointer(out pointer)) { pBuffers[0] = Libuv.buf_init((IntPtr)pointer, memory.Length); } else { throw new InvalidOperationException("Memory needs to be pinned"); } } else { int i = 0; void *pointer; foreach (var memory in buffer) { if (memory.TryGetPointer(out pointer)) { pBuffers[i++] = Libuv.buf_init((IntPtr)pointer, memory.Length); } else { throw new InvalidOperationException("Memory needs to be pinned"); } } } _callback = callback; _state = state; _uv.write(this, handle, pBuffers, nBuffers, _uv_write_cb); } catch { _callback = null; _state = null; _buffer.Dispose(); Unpin(this); throw; } }
private static void UvReadCb(IntPtr handle, int status, ref Uv.uv_buf_t buf) { var stream = FromIntPtr <UvStreamHandle>(handle); stream._readCallback(stream, status, stream._readState); }
public int TryWrite(Uv.uv_buf_t buf) { return(_uv.try_write(this, new[] { buf }, 1)); }