Example #1
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);
        }
Example #2
0
        public void QueueWriteStream(byte[] array, StreamHandle sendHandle,
                                     Action <StreamHandle, Exception> completion)
        {
            Contract.Requires(array != null);

            this.QueueWriteStream(array, 0, array.Length, sendHandle, completion);
        }
Example #3
0
        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;
            }
        }
Example #4
0
        internal Pipeline(StreamHandle streamHandle, ByteBufferAllocator allocator)
        {
            Contract.Requires(streamHandle != null);
            Contract.Requires(allocator != null);

            this.streamHandle = streamHandle;
            this.allocator    = allocator;
            this.receiveBufferSizeEstimate = new ReceiveBufferSizeEstimate();
            this.bufferQueue = new BufferQueue();
        }
Example #5
0
        internal Pipeline(StreamHandle streamHandle, PooledByteBufferAllocator allocator)
        {
            Contract.Requires(streamHandle != null);
            Contract.Requires(allocator != null);

            this.streamHandle = streamHandle;
            this.allocator    = allocator;
            this.receiveBufferSizeEstimate = new ReceiveBufferSizeEstimate();
            this.pendingRead = new PendingRead();
        }
Example #6
0
        public void QueueWriteStream(byte[] array, int offset, int count,
                                     StreamHandle sendHandle,
                                     Action <StreamHandle, Exception> completion)
        {
            Contract.Requires(array != null && array.Length > 0);
            Contract.Requires(offset >= 0 && count > 0);
            Contract.Requires((offset + count) <= array.Length);

            IByteBuffer buffer    = Unpooled.WrappedBuffer(array, offset, count);
            var         bufferRef = new WriteBufferRef(buffer);

            this.pipeline.QueueWrite(bufferRef, sendHandle, completion);
        }
Example #7
0
        public void QueueWriteStream(WritableBuffer writableBuffer, StreamHandle sendHandle,
                                     Action <StreamHandle, Exception> completion)
        {
            Contract.Requires(completion != null);
            Contract.Requires(sendHandle != null);

            IByteBuffer buffer = writableBuffer.GetBuffer();

            if (buffer == null || !buffer.IsReadable())
            {
                return;
            }

            var bufferRef = new WriteBufferRef(buffer);

            this.pipeline.QueueWrite(bufferRef, sendHandle, completion);
        }
Example #8
0
        public void QueueWriteStream(WritableBuffer writableBuffer, StreamHandle sendHandle,
                                     Action <StreamHandle, Exception> completion)
        {
            Contract.Requires(completion != null);
            Contract.Requires(sendHandle != null);

            IArrayBuffer <byte> buffer = writableBuffer.ArrayBuffer;

            if (buffer == null ||
                !buffer.IsReadable())
            {
                return;
            }

            var bufferRef = new BufferRef(buffer, buffer.ReaderIndex, buffer.ReadableCount);

            this.pipeline.QueueWrite(bufferRef, sendHandle, completion);
        }
Example #9
0
        internal void QueueWrite(BufferRef bufferRef, StreamHandle sendHandle, Action <StreamHandle, Exception> completion)
        {
            Contract.Requires(bufferRef != null);
            Contract.Requires(sendHandle != null);

            try
            {
                WriteRequest request = Recycler.Take();
                request.Prepare(bufferRef,
                                (writeRequest, exception) => completion?.Invoke(this.streamHandle, exception));

                this.streamHandle.WriteStream(request, sendHandle);
            }
            catch (Exception exception)
            {
                Log.Error($"{nameof(Pipeline)} {this.streamHandle.HandleType} faulted.", exception);
                throw;
            }
        }
Example #10
0
        internal void WriteStream(WriteRequest request, StreamHandle sendHandle)
        {
            Contract.Requires(request != null);
            Contract.Requires(sendHandle != null);

            this.Validate();
            try
            {
                NativeMethods.WriteStream(
                    request.InternalHandle,
                    this.InternalHandle,
                    ref request.Bufs,
                    ref request.Size,
                    sendHandle.InternalHandle);
            }
            catch (Exception exception)
            {
                Log.Error($"{this.HandleType} Failed to write data {request}.", exception);
                throw;
            }
        }
Example #11
0
        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;
            }
        }
Example #12
0
 internal Pipeline(StreamHandle streamHandle)
     : this(streamHandle, ByteBufferAllocator.Pooled)
 {
 }
Example #13
0
 internal Pipeline(StreamHandle streamHandle)
     : this(streamHandle, ByteBufferAllocator.Default)
 {
 }