コード例 #1
0
        void OnReadCallback(ref uv_buf_t buf, int status)
        {
            /*
             *  nread is > 0 if there is data available or < 0 on error.
             *  When we’ve reached EOF, nread will be set to UV_EOF.
             *  When nread < 0, the buf parameter might not point to a valid buffer;
             *  in that case buf.len and buf.base are both set to 0
             */
            // For status = 0 (Nothing to read)
            if (status >= 0)
            {
                Log.Debug($"{this.HandleType} {this.InternalHandle} read, length = {buf.Length} status = {status}.");
                this.Pipeline.OnReadCompleted(buf.Memory, status);
                return;
            }

            Exception exception = null;

            if (status != (int)uv_err_code.UV_EOF) // Stream end is not an error
            {
                exception = NativeMethods.CreateError((uv_err_code)status);
                Log.Error($"{this.HandleType} {this.InternalHandle} read error, status = {status}", exception);
            }

            Log.Debug($"{this.HandleType} {this.InternalHandle} read completed.");
            this.Pipeline.OnReadCompleted(exception);
            this.ReadStop();
        }
コード例 #2
0
ファイル: BufferCollection.cs プロジェクト: PlumpMath/sharpuv
 internal void DeleteBuffer(uv_buf_t buffer)
 {
     if (buffer.data != IntPtr.Zero)
     {
         buffer.data = _loop.BufferManager.Free(buffer.data);
     }
 }
コード例 #3
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());
        }
コード例 #4
0
ファイル: Pipeline.cs プロジェクト: yyjdelete/SpanNetty
        internal IByteBuffer GetBuffer(ref uv_buf_t buf)
        {
            IByteBuffer byteBuffer = _pendingRead.Buffer;

            _pendingRead.Reset();
            return(byteBuffer);
        }
コード例 #5
0
        void OnAllocateCallback(out uv_buf_t buf)
        {
            BufferBlock block = this.Pipeline.GetReadBuffer();

            buf = new uv_buf_t(block.AsPointer(), block.Count);
            Log.Debug($"{this.HandleType} {this.InternalHandle} buffer allocated, size = {buf.Length}.");
        }
コード例 #6
0
ファイル: StreamHandle.cs プロジェクト: PlumpMath/NetUV
        void OnAllocateCallback(out uv_buf_t buf)
        {
            BufferRef bufferRef = this.pipeline.AllocateReadBuffer();

            uv_buf_t[] bufs = bufferRef.GetBuffer();
#if DEBUG
            Contract.Assert(bufs != null && bufs.Length > 0);
#endif
            // ReSharper disable once PossibleNullReferenceException
            buf = bufs[0];
        }
コード例 #7
0
ファイル: Udp.cs プロジェクト: wazazhang/NetUV
        void OnAllocateCallback(out uv_buf_t buf)
        {
            IByteBuffer buffer = this.allocator.Buffer(FixedBufferSize);

            if (Log.IsTraceEnabled)
            {
                Log.TraceFormat("{0} {1} receive buffer allocated size = {2}", this.HandleType, this.InternalHandle, buffer.Capacity);
            }

            buf = this.pendingRead.GetBuffer(buffer);
        }
コード例 #8
0
        internal IArrayBuffer <byte> GetBuffer(ref uv_buf_t buf)
        {
            IArrayBuffer <byte> byteBuffer = null;

            if (this.bufferQueue.TryDequeue(out BufferRef bufferRef))
            {
                byteBuffer = bufferRef.GetByteBuffer();
            }
            bufferRef?.Dispose();

            return(byteBuffer);
        }
コード例 #9
0
ファイル: BufferCollection.cs プロジェクト: PlumpMath/sharpuv
        internal byte[] CopyAndDeleteBuffer(uv_buf_t buf, int size)
        {
            byte[] data = new byte[size > 0 ? size : 0];

            if (size > 0)
            {
                Marshal.Copy(buf.data, data, 0, size);
            }

            this.DeleteBuffer(buf);
            return(data);
        }
コード例 #10
0
        private void OnAllocateCallback(out uv_buf_t buf)
        {
            IByteBuffer buffer = _allocator.Buffer(FixedBufferSize);

#if DEBUG
            if (Log.TraceEnabled)
            {
                Log.Trace("{} {} receive buffer allocated size = {}", HandleType, InternalHandle, buffer.Capacity);
            }
#endif

            buf = _pendingRead.GetBuffer(buffer);
        }
コード例 #11
0
        public WriteTask(Action <WriteTask, Exception> taskCallback)
        {
            Contract.Requires(taskCallback != null);

            this.taskCallback = taskCallback;
            int bufferSize = Marshal.SizeOf <uv_buf_t>();

            this.Request = new WatcherRequest(
                uv_req_type.UV_WRITE,
                this.OnWriteCallback,
                bufferSize);

            this.buf = new uv_buf_t(IntPtr.Zero, 0);
        }
コード例 #12
0
        private IntPtr Create(uv_req_type reqType, uv_buf_t buffer)
        {
            var requestHandle = _loop.Allocs.AllocRequest(reqType);

            uv_req_t request = new uv_req_t()
            {
                type = reqType,
                data = buffer.data
            };

            Marshal.StructureToPtr(request, requestHandle, false);

            _writes.Add(requestHandle, buffer);
            return(requestHandle);
        }
コード例 #13
0
        void OnAllocateCallback(out uv_buf_t buf)
        {
            ByteBuffer buffer = this.allocator.Buffer(FixedBufferSize);

            Log.TraceFormat("{0} {1} receive buffer allocated size = {2}", this.HandleType, this.InternalHandle, buffer.Count);

            var bufferRef = new BufferRef(buffer);

            this.bufferQueue.Enqueue(bufferRef);

            uv_buf_t[] bufs = bufferRef.GetBuffer();
#if DEBUG
            Contract.Assert(bufs != null && bufs.Length > 0);
#endif
            buf = bufs[0];
        }
コード例 #14
0
ファイル: Udp.cs プロジェクト: doruu12/NetUV
        void OnAllocateCallback(out uv_buf_t buf)
        {
            IArrayBuffer <byte> buffer = this.allocator.Buffer(FixedBufferSize);

            if (Log.IsTraceEnabled)
            {
                Log.TraceFormat("{0} {1} receive buffer allocated size = {2}", this.HandleType, this.InternalHandle, buffer.Capacity);
            }

            var bufferRef = new BufferRef(buffer, buffer.WriterIndex, buffer.WritableCount);

            this.bufferQueue.Enqueue(bufferRef);

            uv_buf_t[] bufs = bufferRef.GetBuffer();
            Debug.Assert(bufs != null && bufs.Length > 0);
            buf = bufs[0];
        }
コード例 #15
0
        public unsafe void TrySend(IPEndPoint remoteEndPoint, byte[] array, int offset, int count)
        {
            if (remoteEndPoint is null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.remoteEndPoint);
            }
            if (array is null || 0u >= (uint)array.Length)
            {
                return;
            }
            if ((uint)count > SharedConstants.TooBigOrNegative)
            {
                ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count);
            }
            if ((uint)(offset + count) > (uint)array.Length)
            {
                ThrowHelper.ThrowArgumentException_InvalidOffLen();
            }

            Validate();
            try
            {
                fixed(byte *memory = array)
                {
                    var buf = new uv_buf_t((IntPtr)memory + offset, count);

                    NativeMethods.UdpTrySend(InternalHandle, remoteEndPoint, ref buf);
                }
            }
#if DEBUG
            catch (Exception exception)
            {
                if (Log.DebugEnabled)
                {
                    Log.Debug($"{HandleType} Trying to send data to {remoteEndPoint} failed.", exception);
                }
#else
            catch (Exception)
            {
#endif
                throw;
            }
        }
コード例 #16
0
ファイル: TcpStream.cs プロジェクト: raiskader/TestMirror
 static void OnReadCallback(IntPtr handle, IntPtr nread, ref uv_buf_t buf)
 {
     // look up the C# TcpStream for this native handle
     if (nativeLookup.TryGetValue(handle, out NativeHandle entry))
     {
         if (entry is TcpStream stream)
         {
             stream.OnReadCallback(stream.readBuffer, (int)nread.ToInt64());
         }
         else
         {
             Log.Error($"TcpStream.OnReadCallback: unexpected lookup type: {entry.GetType()}");
         }
     }
     else
     {
         Log.Error($"TcpStream.OnReadCallback: nativeLookup no entry found for handle={handle}");
     }
 }
コード例 #17
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);
        }
コード例 #18
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);
        }
コード例 #19
0
ファイル: TcpStream.cs プロジェクト: raiskader/TestMirror
 static void OnAllocateCallback(IntPtr handle, IntPtr suggestedSize, out uv_buf_t buf)
 {
     // look up the C# TcpStream for this native handle
     buf = default;
     if (nativeLookup.TryGetValue(handle, out NativeHandle entry))
     {
         if (entry is TcpStream stream)
         {
             buf = stream.readBufferStruct;
         }
         else
         {
             Log.Error($"TcpStream.OnReadCallback: unexpected lookup type: {entry.GetType()}");
         }
     }
     else
     {
         Log.Error($"TcpStream.OnReadCallback: nativeLookup no entry found for handle={handle}");
     }
 }
コード例 #20
0
ファイル: StreamHandle.cs プロジェクト: PlumpMath/NetUV
        internal unsafe void TryWrite(byte[] array, int offset, int count)
        {
            Contract.Requires(array != null && array.Length > 0);
            Contract.Requires(offset >= 0 && count > 0);
            Contract.Requires((offset + count) <= array.Length);

            this.Validate();
            try
            {
                fixed(byte *memory = array)
                {
                    var buf = new uv_buf_t((IntPtr)memory + offset, count);

                    NativeMethods.TryWriteStream(this.InternalHandle, ref buf);
                }
            }
            catch (Exception exception)
            {
                Log.Debug($"{this.HandleType} Trying to write data failed.", exception);
                throw;
            }
        }
コード例 #21
0
ファイル: Udp.cs プロジェクト: doruu12/NetUV
        public unsafe void TrySend(IPEndPoint remoteEndPoint, byte[] array, int offset, int count)
        {
            Contract.Requires(remoteEndPoint != null);
            Contract.Requires(array != null && array.Length > 0);
            Contract.Requires(offset >= 0 && count > 0);
            Contract.Requires((offset + count) <= array.Length);

            this.Validate();
            try
            {
                fixed(byte *memory = array)
                {
                    var buf = new uv_buf_t((IntPtr)memory + offset, count);

                    NativeMethods.UdpTrySend(this.InternalHandle, remoteEndPoint, ref buf);
                }
            }
            catch (Exception exception)
            {
                Log.Debug($"{this.HandleType} Trying to send data to {remoteEndPoint} failed.", exception);
                throw;
            }
        }
コード例 #22
0
 public int try_write(UvStreamHandle handle, uv_buf_t[] bufs, int nbufs)
 {
     handle.Validate();
     return Check(_uv_try_write(handle, bufs, nbufs));
 }
コード例 #23
0
 unsafe private int UvWrite(UvRequest req, UvStreamHandle handle, uv_buf_t* bufs, int nbufs, uv_write_cb cb)
 {
     return _onWrite(handle, new ArraySegment<ArraySegment<byte>>(), status => cb(req.InternalGetHandle(), status));
 }
コード例 #24
0
ファイル: StreamHandle.cs プロジェクト: yyjdelete/SpanNetty
 private void OnAllocateCallback(out uv_buf_t buf)
 {
     buf = _pipeline.AllocateReadBuffer();
 }
コード例 #25
0
ファイル: TcpStream.cs プロジェクト: dstoffels/Theia
        static void OnAllocateCallback(IntPtr handle, IntPtr suggestedSize, out uv_buf_t buf)
        {
            TcpStream stream = HandleContext.GetTarget <TcpStream>(handle);

            buf = stream.readBufferStruct;
        }
コード例 #26
0
ファイル: TcpStream.cs プロジェクト: dstoffels/Theia
        static void OnReadCallback(IntPtr handle, IntPtr nread, ref uv_buf_t buf)
        {
            TcpStream stream = HandleContext.GetTarget <TcpStream>(handle);

            stream.OnReadCallback(stream.readBuffer, (int)nread.ToInt64());
        }
コード例 #27
0
ファイル: TcpStream.cs プロジェクト: dstoffels/Theia
 // buffers /////////////////////////////////////////////////////////////
 // create read buffer and pin it so GC doesn't move it while libuv uses
 // it.
 internal void CreateAndPinReadBuffer(int size)
 {
     readBuffer       = new byte[size];
     readBufferStruct = PinReadBuffer();
 }
コード例 #28
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);
        }
コード例 #29
0
        static void OnReadCallback(IntPtr handle, IntPtr nread, ref uv_buf_t buf)
        {
            var stream = HandleContext.GetTarget <StreamHandle>(handle);

            stream.OnReadCallback(ref buf, (int)nread.ToInt64());
        }
コード例 #30
0
ファイル: Uv.cs プロジェクト: AlexGhiondea/corefxlab
 public int try_write(UvStreamHandle handle, uv_buf_t[] bufs, int nbufs)
 {
     handle.Validate();
     var count = _uv_try_write(handle, bufs, nbufs);
     ThrowIfErrored(count);
     return count;
 }
コード例 #31
0
 void OnAllocateCallback(out uv_buf_t buf)
 {
     buf = this.pipeline.AllocateReadBuffer();
 }
コード例 #32
0
 unsafe public static extern int uv_write2(UvRequest req, UvStreamHandle handle, uv_buf_t* bufs, int nbufs, UvStreamHandle sendHandle, uv_write_cb cb);
コード例 #33
0
 public static extern int uv_try_write(UvStreamHandle handle, uv_buf_t[] bufs, int nbufs);
コード例 #34
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);
        }
コード例 #35
0
 unsafe public void write(UvRequest req, UvStreamHandle handle, uv_buf_t* bufs, int nbufs, uv_write_cb cb)
 {
     req.Validate();
     handle.Validate();
     Check(_uv_write(req, handle, bufs, nbufs, cb));
 }