public void Shutdown(UvStreamHandle handle, Action<UvShutdownReq, int, object> callback, object state) { _callback = callback; _state = state; Pin(); _uv.shutdown(this, handle, _uv_shutdown_cb); }
private static void OnConnectionCallback(UvStreamHandle listenSocket, int status, Exception error, object state) { var listener = (UvTcpListener)state; var acceptSocket = new UvTcpHandle(); try { acceptSocket.Init(listener._thread.Loop, null); acceptSocket.NoDelay(true); listenSocket.Accept(acceptSocket); var connection = new UvTcpConnection(listener._thread, acceptSocket); ExecuteCallback(listener, connection); } catch (UvException) { acceptSocket.Dispose(); } }
public void accept(UvStreamHandle server, UvStreamHandle client) { server.Validate(); client.Validate(); ThrowIfErrored(_uv_accept(server, client)); }
public static extern int uv_shutdown(UvShutdownReq req, UvStreamHandle handle, uv_shutdown_cb cb);
private unsafe void Write( UvStreamHandle handle, ReadOnlySequence <byte> buffer, Action <UvWriteReq, int, object> callback, object state) { try { int nBuffers = 0; if (buffer.IsSingleSegment) { 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; var memoryHandle = memory.Pin(); _handles.Add(memoryHandle); pBuffers[0] = Libuv.buf_init((IntPtr)memoryHandle.Pointer, memory.Length); } else { int i = 0; foreach (var memory in buffer) { var memoryHandle = memory.Pin(); _handles.Add(memoryHandle); pBuffers[i++] = Libuv.buf_init((IntPtr)memoryHandle.Pointer, memory.Length); } } _callback = callback; _state = state; _uv.write(this, handle, pBuffers, nBuffers, _uv_write_cb); } catch { _callback = null; _state = null; Unpin(this); throw; } }
public extern static int uv_read_start(UvStreamHandle handle, uv_alloc_cb alloc_cb, uv_read_cb read_cb);
public static extern int uv_try_write(UvStreamHandle handle, uv_buf_t[] bufs, int nbufs);
unsafe public void write2(UvRequest req, UvStreamHandle handle, Uv.uv_buf_t* bufs, int nbufs, UvStreamHandle sendHandle, uv_write_cb cb) { req.Validate(); handle.Validate(); ThrowIfErrored(_uv_write2(req, handle, bufs, nbufs, sendHandle, cb)); }
public static extern int uv_listen(UvStreamHandle handle, int backlog, uv_connection_cb cb);
public static extern int uv_accept(UvStreamHandle server,UvStreamHandle client);
public extern static int uv_read_start(UvStreamHandle handle,uv_alloc_cb alloc_cb,uv_read_cb read_cb);
public void shutdown(UvShutdownReq req, UvStreamHandle handle, uv_shutdown_cb cb) { req.Validate(); handle.Validate(); ThrowIfErrored(_uv_shutdown(req, handle, cb)); }
public static extern int uv_listen(UvStreamHandle handle,int backlog,uv_connection_cb cb);
unsafe public void write2(UvRequest req, UvStreamHandle handle, Uv.uv_buf_t *bufs, int nbufs, UvStreamHandle sendHandle, uv_write_cb cb) { req.Validate(); handle.Validate(); ThrowIfErrored(_uv_write2(req, handle, bufs, nbufs, sendHandle, cb)); }
public void read_stop(UvStreamHandle handle) { handle.Validate(); ThrowIfErrored(_uv_read_stop(handle)); }
public void read_start(UvStreamHandle handle, uv_alloc_cb alloc_cb, uv_read_cb read_cb) { handle.Validate(); ThrowIfErrored(_uv_read_start(handle, alloc_cb, read_cb)); }
private unsafe Uv.uv_buf_t OnAlloc(UvStreamHandle handle, int status) { _inputBuffer = _input.Alloc(2048); void* pointer; if (!_inputBuffer.Memory.TryGetPointer(out pointer)) { throw new InvalidOperationException("Pointer must be pinned"); } return handle.Libuv.buf_init((IntPtr)pointer, _inputBuffer.Memory.Length); }
public static extern int uv_read_stop(UvStreamHandle handle);
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; } }
public static extern int uv_try_write(UvStreamHandle handle,uv_buf_t[] bufs,int nbufs);
unsafe public static extern int uv_write2(UvRequest req,UvStreamHandle handle,uv_buf_t*bufs,int nbufs,UvStreamHandle sendHandle,uv_write_cb cb);
public static extern int uv_accept(UvStreamHandle server, UvStreamHandle client);
public static extern int uv_shutdown(UvShutdownReq req,UvStreamHandle handle,uv_shutdown_cb cb);
public void listen(UvStreamHandle handle, int backlog, uv_connection_cb cb) { handle.Validate(); ThrowIfErrored(_uv_listen(handle, backlog, cb)); }
unsafe public static extern int uv_write2(UvRequest req, UvStreamHandle handle, uv_buf_t* bufs, int nbufs, UvStreamHandle sendHandle, uv_write_cb cb);
public void Accept(UvStreamHandle handle) { _uv.accept(this, handle); }
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 void OnRead(UvStreamHandle handle, int status) { if (status == 0) { // A zero status does not indicate an error or connection end. It indicates // there is no data to be read right now. // See the note at http://docs.libuv.org/en/v1.x/stream.html#c.uv_read_cb. _inputBuffer.Commit(); return; } var normalRead = status > 0; var normalDone = status == EOF; var errorDone = !(normalDone || normalRead); var readCount = normalRead ? status : 0; if (!normalRead) { handle.ReadStop(); } IOException error = null; if (errorDone) { Exception uvError; handle.Libuv.Check(status, out uvError); error = new IOException(uvError.Message, uvError); // REVIEW: Should we treat ECONNRESET as an error? // Ignore the error for now _input.CompleteWriter(); } else if (readCount == 0 || _input.Writing.IsCompleted) { _input.CompleteWriter(); } else { _inputBuffer.Advance(readCount); var task = _inputBuffer.FlushAsync(); if (!task.IsCompleted) { // If there's back pressure handle.ReadStop(); // Resume reading when task continues task.ContinueWith((t, state) => ((UvTcpConnection)state).StartReading(), this); } } }
private static void ReadCallback(UvStreamHandle handle, int status, object state) { ((UvTcpConnection)state).OnRead(handle, status); }
private static Uv.uv_buf_t AllocCallback(UvStreamHandle handle, int status, object state) { return ((UvTcpConnection)state).OnAlloc(handle, status); }
public int try_write(UvStreamHandle handle, uv_buf_t[] bufs, int nbufs) { handle.Validate(); var count = _uv_try_write(handle, bufs, nbufs); ThrowIfErrored(count); return count; }