Beispiel #1
0
 internal ReadOperation(INativeUnsafe nativeUnsafe, IByteBuffer buffer)
 {
     this.nativeUnsafe = nativeUnsafe;
     this.Buffer       = buffer;
     this.Status       = 0;
     this.EndOfStream  = false;
 }
Beispiel #2
0
        protected override void OnClosed()
        {
            base.OnClosed();

            _pendingRead.Dispose();
            _nativeUnsafe = null;
        }
Beispiel #3
0
        internal void DoWrite(INativeUnsafe channelUnsafe, ChannelOutboundBuffer input)
        {
            Debug.Assert(_nativeUnsafe is null);

            _nativeUnsafe = channelUnsafe;
            input.ForEachFlushedMessage(this);
            DoWrite();
        }
Beispiel #4
0
        internal void Release()
        {
            this.ReleaseHandles();

            this.nativeUnsafe = null;
            this.Error        = null;
            this.recyclerHandle.Release(this);
        }
Beispiel #5
0
        internal void ReadStart(INativeUnsafe channel)
        {
            Debug.Assert(channel is object);

            Validate();
            int result = NativeMethods.uv_read_start(Handle, AllocateCallback, ReadCallback);

            NativeMethods.ThrowIfError(result);
            _nativeUnsafe = channel;
        }
Beispiel #6
0
        public void ReadStart(INativeUnsafe channel)
        {
            Contract.Requires(channel != null);

            this.Validate();
            int result = NativeMethods.uv_read_start(this.Handle, AllocateCallback, ReadCallback);

            if (result < 0)
            {
                throw NativeMethods.CreateError((uv_err_code)result);
            }

            this.unsafeChannel = channel;
        }
Beispiel #7
0
        void OnWriteCallback(int status)
        {
            INativeUnsafe @unsafe      = _nativeUnsafe;
            int           bytesWritten = _size;

            Release();

            OperationException error = null;

            if (status < 0)
            {
                error = NativeMethods.CreateError((uv_err_code)status);
            }
            @unsafe.FinishWrite(bytesWritten, error);
        }
Beispiel #8
0
        public TcpConnect(INativeUnsafe nativeUnsafe, IPEndPoint remoteEndPoint)
        {
            Debug.Assert(nativeUnsafe is object);
            Debug.Assert(remoteEndPoint is object);

            NativeMethods.GetSocketAddress(remoteEndPoint, out sockaddr addr);
            int result = NativeMethods.uv_tcp_connect(
                Handle,
                nativeUnsafe.UnsafeHandle,
                ref addr,
                WatcherCallback);

            NativeMethods.ThrowIfError(result);

            _nativeUnsafe = nativeUnsafe;
        }
Beispiel #9
0
        protected override void OnClosed()
        {
            base.OnClosed();

            while (true)
            {
                ReadOperation operation = this.pendingReads.Poll();
                if (operation == null)
                {
                    break;
                }

                operation.Dispose();
            }

            this.unsafeChannel = null;
        }
Beispiel #10
0
        void Release()
        {
            var handleCount = _handles.Count;

            if (handleCount > 0)
            {
                for (int i = 0; i < handleCount; i++)
                {
                    _handles[i].Dispose();
                }
                _handles.Clear();
            }

            _nativeUnsafe = null;
            _count        = 0;
            _size         = 0;
            _recyclerHandle.Release(this);
        }
Beispiel #11
0
        public TcpConnect(INativeUnsafe nativeUnsafe, IPEndPoint remoteEndPoint)
        {
            Contract.Requires(nativeUnsafe != null);
            Contract.Requires(remoteEndPoint != null);

            NativeMethods.GetSocketAddress(remoteEndPoint, out sockaddr addr);
            int result = NativeMethods.uv_tcp_connect(
                this.Handle,
                nativeUnsafe.UnsafeHandle,
                ref addr,
                WatcherCallback);

            if (result < 0)
            {
                throw NativeMethods.CreateError((uv_err_code)result);
            }

            this.nativeUnsafe = nativeUnsafe;
        }
Beispiel #12
0
        internal void Prepare(INativeUnsafe channelUnsafe, List <ArraySegment <byte> > nioBuffers)
        {
            if (nioBuffers.Count == 1)
            {
                ArraySegment <byte> buffer = nioBuffers[0];
                GCHandle            handle = GCHandle.Alloc(buffer.Array, GCHandleType.Pinned);
                IntPtr arrayHandle         = handle.AddrOfPinnedObject();

                this.bufs[0] = new uv_buf_t(arrayHandle + buffer.Offset, buffer.Count);
                this.handles.Add(handle);
            }
            else
            {
                if (this.bufs.Length < nioBuffers.Count)
                {
                    if (this.bufsPin.IsAllocated)
                    {
                        this.bufsPin.Free();
                    }
                    this.bufs    = new uv_buf_t[nioBuffers.Count];
                    this.bufsPin = GCHandle.Alloc(this.bufs, GCHandleType.Pinned);
                }

                for (int i = 0; i < nioBuffers.Count; i++)
                {
                    GCHandle handle      = GCHandle.Alloc(nioBuffers[i].Array, GCHandleType.Pinned);
                    IntPtr   arrayHandle = handle.AddrOfPinnedObject();

                    this.bufs[i] = new uv_buf_t(arrayHandle + nioBuffers[i].Offset, nioBuffers[i].Count);
                    this.handles.Add(handle);
                }
            }

            this.bufferCount  = nioBuffers.Count;
            this.nativeUnsafe = channelUnsafe;
        }