public unsafe bool TryGetLoop(out Loop loop) { loop = null; try { IntPtr nativeHandle = this.InternalHandle; if (nativeHandle == IntPtr.Zero) { return(false); } IntPtr loopHandle = ((uv_handle_t *)nativeHandle)->loop; if (loopHandle != IntPtr.Zero) { loop = HandleContext.GetTarget <Loop>(loopHandle); } return(loop != null); } catch (Exception exception) { Log.Warn($"{this.HandleType} Failed to get loop.", exception); return(false); } }
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()); }
static void OnConnectionCallback(IntPtr handle, int status) { var server = HandleContext.GetTarget <ServerStream>(handle); if (server == null) { return; } StreamHandle client = null; Exception error = null; try { if (status < 0) { error = NativeMethods.CreateError((uv_err_code)status); } else { client = server.NewStream(); } server.connectionHandler(client, error); } catch { client?.Dispose(); throw; } }
public unsafe StreamHandle CreatePendingType() { this.Validate(); StreamHandle handle = null; int count = this.PendingCount(); if (count > 0) { IntPtr loopHandle = ((uv_stream_t *)this.InternalHandle)->loop; var loop = HandleContext.GetTarget <LoopContext>(loopHandle); uv_handle_type handleType = NativeMethods.PipePendingType(this.InternalHandle); if (handleType == uv_handle_type.UV_TCP) { handle = new Tcp(loop); } else if (handleType == uv_handle_type.UV_NAMED_PIPE) { handle = new Pipe(loop); } else { throw new InvalidOperationException($"{handleType} not supported or IPC over Pipe is disabled."); } NativeMethods.StreamAccept(this.InternalHandle, handle.InternalHandle); } return(handle); }
protected internal override unsafe StreamHandle NewStream() { IntPtr loopHandle = ((uv_stream_t *)this.InternalHandle)->loop; var loop = HandleContext.GetTarget <LoopContext>(loopHandle); uv_handle_type type = ((uv_stream_t *)this.InternalHandle)->type; StreamHandle client; if (type == uv_handle_type.UV_NAMED_PIPE) { client = new Pipe(loop, this.ipc); } else if (type == uv_handle_type.UV_TCP) { client = new Tcp(loop); } else { throw new InvalidOperationException($"Pipe IPC handle {type} not supported"); } NativeMethods.StreamAccept(this.InternalHandle, client.InternalHandle); if (!this.ipc) { client.ReadStart(); } if (Log.IsDebugEnabled) { Log.DebugFormat("{0} {1} client {2} accepted. (IPC : {3})", this.HandleType, this.InternalHandle, client.InternalHandle, this.ipc); } return(client); }
protected internal override unsafe StreamHandle NewStream() { IntPtr loopHandle = ((uv_stream_t *)this.InternalHandle)->loop; var loop = HandleContext.GetTarget <LoopContext>(loopHandle); return(new Pipe(loop)); }
internal ScheduleHandle( LoopContext loop, uv_handle_type handleType, object[] args = null) { Contract.Requires(loop != null); HandleContext initialHandle = NativeMethods.Initialize(loop.Handle, handleType, this, args); Debug.Assert(initialHandle != null); this.handle = initialHandle; this.HandleType = handleType; }
protected internal override unsafe StreamHandle NewStream() { IntPtr loopHandle = ((uv_stream_t *)this.InternalHandle)->loop; var loop = HandleContext.GetTarget <LoopContext>(loopHandle); var client = new Tcp(loop); NativeMethods.StreamAccept(this.InternalHandle, client.InternalHandle); client.ReadStart(); if (Log.IsDebugEnabled) { Log.DebugFormat("{0} {1} client {2} accepted", this.HandleType, this.InternalHandle, client.InternalHandle); } return(client); }
static void OnWalkCallback(IntPtr handle, IntPtr loopHandle) { if (handle == IntPtr.Zero) { return; } try { var target = HandleContext.GetTarget <IDisposable>(handle); target?.Dispose(); Log.Info($"Loop {loopHandle} walk callback disposed {handle} {target?.GetType()}"); } catch (Exception exception) { Log.Warn($"Loop {loopHandle} Walk callback attempt to close handle {handle} failed. {exception}"); } }
// 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 OnConnectionCallback(IntPtr handle, int status) { var server = HandleContext.GetTarget <ServerStream>(handle); if (server == null) { return; } StreamHandle client = null; Exception error = null; try { if (status < 0) { error = NativeMethods.CreateError((uv_err_code)status); } else { client = server.NewStream(); if (client == null) { throw new InvalidOperationException( $"{server.HandleType} {server.InternalHandle} failed to create new client stream."); } NativeMethods.StreamAccept(server.InternalHandle, client.InternalHandle); client.ReadStart(); Log.DebugFormat("{0} {1} client {2} accepted", server.HandleType, handle, client.InternalHandle); } server.connectionHandler(client, error); } catch { client?.Dispose(); throw; } }
static void OnPollCallback(IntPtr handle, int status, int events) { var poll = HandleContext.GetTarget <Poll>(handle); poll?.OnPollCallback(status, events); }
static void OnAllocateCallback(IntPtr handle, IntPtr suggestedSize, out uv_buf_t buf) { var udp = HandleContext.GetTarget <Udp>(handle); udp.OnAllocateCallback(out buf); }
static void OnFSEventCallback(IntPtr handle, string fileName, int events, int status) { var fsEvent = HandleContext.GetTarget <FSEvent>(handle); fsEvent?.OnFSEventCallback(fileName, events, status); }
static void OnWorkCallback(IntPtr handle) { var workHandle = HandleContext.GetTarget <WorkHandle>(handle); workHandle?.OnWorkCallback(); }
static void OnSignalCallback(IntPtr handle, int signum) { var signal = HandleContext.GetTarget <Signal>(handle); signal?.OnSignalCallback(signum); }
static void OnAllocateCallback(IntPtr handle, IntPtr suggestedSize, out uv_buf_t buf) { var stream = HandleContext.GetTarget <StreamHandle>(handle); stream.OnAllocateCallback(out buf); }
static void OnFSPollCallback(IntPtr handle, int status, ref uv_stat_t prev, ref uv_stat_t curr) { var fsPoll = HandleContext.GetTarget <FSPoll>(handle); fsPoll?.OnFSPollCallback(status, ref prev, ref curr); }