コード例 #1
0
ファイル: ScheduleHandle.cs プロジェクト: PlumpMath/NetUV
        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);
            }
        }
コード例 #2
0
ファイル: StreamHandle.cs プロジェクト: PlumpMath/NetUV
        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());
        }
コード例 #3
0
ファイル: ServerStream.cs プロジェクト: wazazhang/NetUV
        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;
            }
        }
コード例 #4
0
        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);
        }
コード例 #5
0
        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);
        }
コード例 #6
0
        protected internal override unsafe StreamHandle NewStream()
        {
            IntPtr loopHandle = ((uv_stream_t *)this.InternalHandle)->loop;
            var    loop       = HandleContext.GetTarget <LoopContext>(loopHandle);

            return(new Pipe(loop));
        }
コード例 #7
0
ファイル: ScheduleHandle.cs プロジェクト: heng83/NetUV
        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;
        }
コード例 #8
0
        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);
        }
コード例 #9
0
        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}");
            }
        }
コード例 #10
0
ファイル: Udp.cs プロジェクト: doruu12/NetUV
        // 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);
        }
コード例 #11
0
ファイル: ServerStream.cs プロジェクト: PlumpMath/NetUV
        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;
            }
        }
コード例 #12
0
        static void OnPollCallback(IntPtr handle, int status, int events)
        {
            var poll = HandleContext.GetTarget <Poll>(handle);

            poll?.OnPollCallback(status, events);
        }
コード例 #13
0
ファイル: Udp.cs プロジェクト: doruu12/NetUV
        static void OnAllocateCallback(IntPtr handle, IntPtr suggestedSize, out uv_buf_t buf)
        {
            var udp = HandleContext.GetTarget <Udp>(handle);

            udp.OnAllocateCallback(out buf);
        }
コード例 #14
0
ファイル: FSEvent.cs プロジェクト: wazazhang/NetUV
        static void OnFSEventCallback(IntPtr handle, string fileName, int events, int status)
        {
            var fsEvent = HandleContext.GetTarget <FSEvent>(handle);

            fsEvent?.OnFSEventCallback(fileName, events, status);
        }
コード例 #15
0
ファイル: WorkHandle.cs プロジェクト: PlumpMath/NetUV
        static void OnWorkCallback(IntPtr handle)
        {
            var workHandle = HandleContext.GetTarget <WorkHandle>(handle);

            workHandle?.OnWorkCallback();
        }
コード例 #16
0
ファイル: Signal.cs プロジェクト: wazazhang/NetUV
        static void OnSignalCallback(IntPtr handle, int signum)
        {
            var signal = HandleContext.GetTarget <Signal>(handle);

            signal?.OnSignalCallback(signum);
        }
コード例 #17
0
ファイル: StreamHandle.cs プロジェクト: PlumpMath/NetUV
        static void OnAllocateCallback(IntPtr handle, IntPtr suggestedSize, out uv_buf_t buf)
        {
            var stream = HandleContext.GetTarget <StreamHandle>(handle);

            stream.OnAllocateCallback(out buf);
        }
コード例 #18
0
        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);
        }