void OnReadCallback(ref uv_buf_t buf, int status) { /* * nread is > 0 if there is data available or < 0 on error. * When we’ve reached EOF, nread will be set to UV_EOF. * When nread < 0, the buf parameter might not point to a valid buffer; * in that case buf.len and buf.base are both set to 0 */ // For status = 0 (Nothing to read) if (status >= 0) { Log.Debug($"{this.HandleType} {this.InternalHandle} read, length = {buf.Length} status = {status}."); this.Pipeline.OnReadCompleted(buf.Memory, status); return; } Exception exception = null; if (status != (int)uv_err_code.UV_EOF) // Stream end is not an error { exception = NativeMethods.CreateError((uv_err_code)status); Log.Error($"{this.HandleType} {this.InternalHandle} read error, status = {status}", exception); } Log.Debug($"{this.HandleType} {this.InternalHandle} read completed."); this.Pipeline.OnReadCompleted(exception); this.ReadStop(); }
internal void DeleteBuffer(uv_buf_t buffer) { if (buffer.data != IntPtr.Zero) { buffer.data = _loop.BufferManager.Free(buffer.data); } }
static void OnReadCallback(IntPtr handle, IntPtr nread, ref uv_buf_t buf) { var stream = HandleContext.GetTarget <StreamHandle>(handle); ByteBuffer byteBuffer = stream.pipeline.GetBuffer(ref buf); stream.OnReadCallback(byteBuffer, (int)nread.ToInt64()); }
internal IByteBuffer GetBuffer(ref uv_buf_t buf) { IByteBuffer byteBuffer = _pendingRead.Buffer; _pendingRead.Reset(); return(byteBuffer); }
void OnAllocateCallback(out uv_buf_t buf) { BufferBlock block = this.Pipeline.GetReadBuffer(); buf = new uv_buf_t(block.AsPointer(), block.Count); Log.Debug($"{this.HandleType} {this.InternalHandle} buffer allocated, size = {buf.Length}."); }
void OnAllocateCallback(out uv_buf_t buf) { BufferRef bufferRef = this.pipeline.AllocateReadBuffer(); uv_buf_t[] bufs = bufferRef.GetBuffer(); #if DEBUG Contract.Assert(bufs != null && bufs.Length > 0); #endif // ReSharper disable once PossibleNullReferenceException buf = bufs[0]; }
void OnAllocateCallback(out uv_buf_t buf) { IByteBuffer buffer = this.allocator.Buffer(FixedBufferSize); if (Log.IsTraceEnabled) { Log.TraceFormat("{0} {1} receive buffer allocated size = {2}", this.HandleType, this.InternalHandle, buffer.Capacity); } buf = this.pendingRead.GetBuffer(buffer); }
internal IArrayBuffer <byte> GetBuffer(ref uv_buf_t buf) { IArrayBuffer <byte> byteBuffer = null; if (this.bufferQueue.TryDequeue(out BufferRef bufferRef)) { byteBuffer = bufferRef.GetByteBuffer(); } bufferRef?.Dispose(); return(byteBuffer); }
internal byte[] CopyAndDeleteBuffer(uv_buf_t buf, int size) { byte[] data = new byte[size > 0 ? size : 0]; if (size > 0) { Marshal.Copy(buf.data, data, 0, size); } this.DeleteBuffer(buf); return(data); }
private void OnAllocateCallback(out uv_buf_t buf) { IByteBuffer buffer = _allocator.Buffer(FixedBufferSize); #if DEBUG if (Log.TraceEnabled) { Log.Trace("{} {} receive buffer allocated size = {}", HandleType, InternalHandle, buffer.Capacity); } #endif buf = _pendingRead.GetBuffer(buffer); }
public WriteTask(Action <WriteTask, Exception> taskCallback) { Contract.Requires(taskCallback != null); this.taskCallback = taskCallback; int bufferSize = Marshal.SizeOf <uv_buf_t>(); this.Request = new WatcherRequest( uv_req_type.UV_WRITE, this.OnWriteCallback, bufferSize); this.buf = new uv_buf_t(IntPtr.Zero, 0); }
private IntPtr Create(uv_req_type reqType, uv_buf_t buffer) { var requestHandle = _loop.Allocs.AllocRequest(reqType); uv_req_t request = new uv_req_t() { type = reqType, data = buffer.data }; Marshal.StructureToPtr(request, requestHandle, false); _writes.Add(requestHandle, buffer); return(requestHandle); }
void OnAllocateCallback(out uv_buf_t buf) { ByteBuffer buffer = this.allocator.Buffer(FixedBufferSize); Log.TraceFormat("{0} {1} receive buffer allocated size = {2}", this.HandleType, this.InternalHandle, buffer.Count); var bufferRef = new BufferRef(buffer); this.bufferQueue.Enqueue(bufferRef); uv_buf_t[] bufs = bufferRef.GetBuffer(); #if DEBUG Contract.Assert(bufs != null && bufs.Length > 0); #endif buf = bufs[0]; }
void OnAllocateCallback(out uv_buf_t buf) { IArrayBuffer <byte> buffer = this.allocator.Buffer(FixedBufferSize); if (Log.IsTraceEnabled) { Log.TraceFormat("{0} {1} receive buffer allocated size = {2}", this.HandleType, this.InternalHandle, buffer.Capacity); } var bufferRef = new BufferRef(buffer, buffer.WriterIndex, buffer.WritableCount); this.bufferQueue.Enqueue(bufferRef); uv_buf_t[] bufs = bufferRef.GetBuffer(); Debug.Assert(bufs != null && bufs.Length > 0); buf = bufs[0]; }
public unsafe void TrySend(IPEndPoint remoteEndPoint, byte[] array, int offset, int count) { if (remoteEndPoint is null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.remoteEndPoint); } if (array is null || 0u >= (uint)array.Length) { return; } if ((uint)count > SharedConstants.TooBigOrNegative) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count); } if ((uint)(offset + count) > (uint)array.Length) { ThrowHelper.ThrowArgumentException_InvalidOffLen(); } Validate(); try { fixed(byte *memory = array) { var buf = new uv_buf_t((IntPtr)memory + offset, count); NativeMethods.UdpTrySend(InternalHandle, remoteEndPoint, ref buf); } } #if DEBUG catch (Exception exception) { if (Log.DebugEnabled) { Log.Debug($"{HandleType} Trying to send data to {remoteEndPoint} failed.", exception); } #else catch (Exception) { #endif throw; } }
static void OnReadCallback(IntPtr handle, IntPtr nread, ref uv_buf_t buf) { // look up the C# TcpStream for this native handle if (nativeLookup.TryGetValue(handle, out NativeHandle entry)) { if (entry is TcpStream stream) { stream.OnReadCallback(stream.readBuffer, (int)nread.ToInt64()); } else { Log.Error($"TcpStream.OnReadCallback: unexpected lookup type: {entry.GetType()}"); } } else { Log.Error($"TcpStream.OnReadCallback: nativeLookup no entry found for handle={handle}"); } }
// addr: // struct sockaddr ontaining the address of the sender. // Can be NULL. Valid for the duration of the callback only. // // flags: // One or more or’ed UV_UDP_* constants. // Right now only UV_UDP_PARTIAL is used private static void OnReceiveCallback(IntPtr handle, IntPtr nread, ref uv_buf_t buf, ref sockaddr addr, int flags) { var udp = HandleContext.GetTarget <Udp>(handle); IByteBuffer byteBuffer = udp.GetBuffer(); int count = (int)nread.ToInt64(); IPEndPoint remoteEndPoint = count > 0 ? addr.GetIPEndPoint() : null; // // Indicates message was truncated because read buffer was too small. // The remainder was discarded by the OS. Used in uv_udp_recv_cb. // if (flags == (int)uv_udp_flags.UV_UDP_PARTIAL) { Log.Handle_receive_result_truncated(handle, byteBuffer); } udp.OnReceivedCallback(byteBuffer, count, remoteEndPoint); }
// addr: // struct sockaddr ontaining the address of the sender. // Can be NULL. Valid for the duration of the callback only. // // flags: // One or more or’ed UV_UDP_* constants. // Right now only UV_UDP_PARTIAL is used static void OnReceiveCallback(IntPtr handle, IntPtr nread, ref uv_buf_t buf, ref sockaddr addr, int flags) { var udp = HandleContext.GetTarget <Udp>(handle); IArrayBuffer <byte> byteBuffer = udp.GetBuffer(); int count = (int)nread.ToInt64(); IPEndPoint remoteEndPoint = count > 0 ? addr.GetIPEndPoint() : null; // // Indicates message was truncated because read buffer was too small. // The remainder was discarded by the OS. Used in uv_udp_recv_cb. // if (flags == (int)uv_udp_flags.UV_UDP_PARTIAL) { Log.Warn($"{uv_handle_type.UV_UDP} {handle} receive result truncated, buffer size = {byteBuffer.Capacity}"); } udp.OnReceivedCallback(byteBuffer, count, remoteEndPoint); }
static void OnAllocateCallback(IntPtr handle, IntPtr suggestedSize, out uv_buf_t buf) { // look up the C# TcpStream for this native handle buf = default; if (nativeLookup.TryGetValue(handle, out NativeHandle entry)) { if (entry is TcpStream stream) { buf = stream.readBufferStruct; } else { Log.Error($"TcpStream.OnReadCallback: unexpected lookup type: {entry.GetType()}"); } } else { Log.Error($"TcpStream.OnReadCallback: nativeLookup no entry found for handle={handle}"); } }
internal unsafe void TryWrite(byte[] array, int offset, int count) { Contract.Requires(array != null && array.Length > 0); Contract.Requires(offset >= 0 && count > 0); Contract.Requires((offset + count) <= array.Length); this.Validate(); try { fixed(byte *memory = array) { var buf = new uv_buf_t((IntPtr)memory + offset, count); NativeMethods.TryWriteStream(this.InternalHandle, ref buf); } } catch (Exception exception) { Log.Debug($"{this.HandleType} Trying to write data failed.", exception); throw; } }
public unsafe void TrySend(IPEndPoint remoteEndPoint, byte[] array, int offset, int count) { Contract.Requires(remoteEndPoint != null); Contract.Requires(array != null && array.Length > 0); Contract.Requires(offset >= 0 && count > 0); Contract.Requires((offset + count) <= array.Length); this.Validate(); try { fixed(byte *memory = array) { var buf = new uv_buf_t((IntPtr)memory + offset, count); NativeMethods.UdpTrySend(this.InternalHandle, remoteEndPoint, ref buf); } } catch (Exception exception) { Log.Debug($"{this.HandleType} Trying to send data to {remoteEndPoint} failed.", exception); throw; } }
public int try_write(UvStreamHandle handle, uv_buf_t[] bufs, int nbufs) { handle.Validate(); return Check(_uv_try_write(handle, bufs, nbufs)); }
unsafe private int UvWrite(UvRequest req, UvStreamHandle handle, uv_buf_t* bufs, int nbufs, uv_write_cb cb) { return _onWrite(handle, new ArraySegment<ArraySegment<byte>>(), status => cb(req.InternalGetHandle(), status)); }
private void OnAllocateCallback(out uv_buf_t buf) { buf = _pipeline.AllocateReadBuffer(); }
static void OnAllocateCallback(IntPtr handle, IntPtr suggestedSize, out uv_buf_t buf) { TcpStream stream = HandleContext.GetTarget <TcpStream>(handle); buf = stream.readBufferStruct; }
static void OnReadCallback(IntPtr handle, IntPtr nread, ref uv_buf_t buf) { TcpStream stream = HandleContext.GetTarget <TcpStream>(handle); stream.OnReadCallback(stream.readBuffer, (int)nread.ToInt64()); }
// buffers ///////////////////////////////////////////////////////////// // create read buffer and pin it so GC doesn't move it while libuv uses // it. internal void CreateAndPinReadBuffer(int size) { readBuffer = new byte[size]; readBufferStruct = PinReadBuffer(); }
static void OnAllocateCallback(IntPtr handle, IntPtr suggestedSize, out uv_buf_t buf) { var stream = HandleContext.GetTarget <StreamHandle>(handle); stream.OnAllocateCallback(out buf); }
static void OnReadCallback(IntPtr handle, IntPtr nread, ref uv_buf_t buf) { var stream = HandleContext.GetTarget <StreamHandle>(handle); stream.OnReadCallback(ref buf, (int)nread.ToInt64()); }
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; }
void OnAllocateCallback(out uv_buf_t buf) { buf = this.pipeline.AllocateReadBuffer(); }
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_try_write(UvStreamHandle handle, uv_buf_t[] bufs, int nbufs);
static void OnAllocateCallback(IntPtr handle, IntPtr suggestedSize, out uv_buf_t buf) { var udp = HandleContext.GetTarget <Udp>(handle); udp.OnAllocateCallback(out buf); }
unsafe public void write(UvRequest req, UvStreamHandle handle, uv_buf_t* bufs, int nbufs, uv_write_cb cb) { req.Validate(); handle.Validate(); Check(_uv_write(req, handle, bufs, nbufs, cb)); }