Ejemplo n.º 1
0
        public unsafe StreamHandle CreatePendingType()
        {
            Validate();

            StreamHandle handle = null;
            int          count  = PendingCount();

            if (count > 0)
            {
                IntPtr         loopHandle = ((uv_stream_t *)InternalHandle)->loop;
                var            loop       = HandleContext.GetTarget <LoopContext>(loopHandle);
                uv_handle_type handleType = NativeMethods.PipePendingType(InternalHandle);

                switch (handleType)
                {
                case uv_handle_type.UV_TCP:
                    handle = new Tcp(loop);
                    break;

                case uv_handle_type.UV_NAMED_PIPE:
                    handle = new Pipe(loop);
                    break;

                default:
                    throw ThrowHelper.GetInvalidOperationException_uv_handle_type_not_supported_or_IPC_over_Pipe_is_disabled(handleType);
                }

                NativeMethods.StreamAccept(InternalHandle, handle.InternalHandle);
                handle.ReadStart();
            }

            return(handle);
        }
Ejemplo n.º 2
0
        private static void OnReadCallback(IntPtr handle, IntPtr nread, ref uv_buf_t buf)
        {
            var         stream     = HandleContext.GetTarget <StreamHandle>(handle);
            IByteBuffer byteBuffer = stream._pipeline.GetBuffer(ref buf);

            stream.OnReadCallback(byteBuffer, (int)nread.ToInt64());
        }
Ejemplo n.º 3
0
        private static void OnConnectionCallback(IntPtr handle, int status)
        {
            var server = HandleContext.GetTarget <ServerStream>(handle);

            if (server is null)
            {
                return;
            }

            StreamHandle client = null;
            Exception    error  = null;

            try
            {
                if ((uint)status > SharedConstants.TooBigOrNegative) // < 0
                {
                    error = NativeMethods.CreateError((uv_err_code)status);
                }
                else
                {
                    client = server.NewStream();
                }

                server._connectionHandler(client, error);
            }
            catch
            {
                client?.Dispose();
                throw;
            }
        }
Ejemplo n.º 4
0
        internal ScheduleHandle(
            LoopContext loop,
            uv_handle_type handleType,
            object[] args = null)
        {
            if (loop is null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.loop);
            }

            HandleContext initialHandle = NativeMethods.Initialize(loop.Handle, handleType, this, args);

            Debug.Assert(initialHandle is object);

            _handle    = initialHandle;
            HandleType = handleType;
        }
Ejemplo n.º 5
0
        // 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);
        }
Ejemplo n.º 6
0
        protected internal override unsafe StreamHandle NewStream()
        {
            IntPtr loopHandle = ((uv_stream_t *)InternalHandle)->loop;
            var    loop       = HandleContext.GetTarget <LoopContext>(loopHandle);

            var client = new Tcp(loop);

            NativeMethods.StreamAccept(InternalHandle, client.InternalHandle);
            client.ReadStart();

#if DEBUG
            if (Log.DebugEnabled)
            {
                Log.Debug("{} {} client {} accepted",
                          HandleType, InternalHandle, client.InternalHandle);
            }
#endif

            return(client);
        }
Ejemplo n.º 7
0
        private static void OnWalkCallback(IntPtr handle, IntPtr loopHandle)
        {
            if (handle == IntPtr.Zero)
            {
                return;
            }

            try
            {
                var target = HandleContext.GetTarget <IDisposable>(handle);
                if (Log.InfoEnabled)
                {
                    Log.LoopWalkCallbackDisposed(loopHandle, handle, target);
                }
                target?.Dispose();
            }
            catch (Exception exception)
            {
                Log.Loop_Walk_callback_attempt_to_close_handle_failed(loopHandle, handle, exception);
            }
        }
Ejemplo n.º 8
0
        protected internal override unsafe StreamHandle NewStream()
        {
            IntPtr         loopHandle = ((uv_stream_t *)InternalHandle)->loop;
            var            loop       = HandleContext.GetTarget <LoopContext>(loopHandle);
            uv_handle_type type       = ((uv_stream_t *)InternalHandle)->type;

            StreamHandle client;

            switch (type)
            {
            case uv_handle_type.UV_NAMED_PIPE:
                client = new Pipe(loop, _ipc);
                break;

            case uv_handle_type.UV_TCP:
                client = new Tcp(loop);
                break;

            default:
                throw ThrowHelper.GetInvalidOperationException_Pipe_IPC_handle_not_supported(type);
            }

            NativeMethods.StreamAccept(InternalHandle, client.InternalHandle);
            if (!_ipc)
            {
                client.ReadStart();
            }

#if DEBUG
            if (Log.DebugEnabled)
            {
                Log.Debug("{} {1} client {} accepted. (IPC : {})", HandleType, InternalHandle, client.InternalHandle, _ipc);
            }
#endif

            return(client);
        }
Ejemplo n.º 9
0
        private static void OnWorkCallback(IntPtr handle)
        {
            var workHandle = HandleContext.GetTarget <WorkHandle>(handle);

            workHandle?.OnWorkCallback();
        }
Ejemplo n.º 10
0
        private static void OnFSEventCallback(IntPtr handle, string fileName, int events, int status)
        {
            var fsEvent = HandleContext.GetTarget <FSEvent>(handle);

            fsEvent?.OnFSEventCallback(fileName, events, status);
        }
Ejemplo n.º 11
0
        private static void OnAllocateCallback(IntPtr handle, IntPtr suggestedSize, out uv_buf_t buf)
        {
            var udp = HandleContext.GetTarget <Udp>(handle);

            udp.OnAllocateCallback(out buf);
        }
Ejemplo n.º 12
0
        private static void OnPollCallback(IntPtr handle, int status, int events)
        {
            var poll = HandleContext.GetTarget <Poll>(handle);

            poll?.OnPollCallback(status, events);
        }
Ejemplo n.º 13
0
        private static void OnSignalCallback(IntPtr handle, int signum)
        {
            var signal = HandleContext.GetTarget <Signal>(handle);

            signal?.OnSignalCallback(signum);
        }
Ejemplo n.º 14
0
        private 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);
        }
Ejemplo n.º 15
0
        private static void OnAllocateCallback(IntPtr handle, IntPtr suggestedSize, out uv_buf_t buf)
        {
            var stream = HandleContext.GetTarget <StreamHandle>(handle);

            stream.OnAllocateCallback(out buf);
        }