Ejemplo n.º 1
0
 public void Shutdown(UvStreamHandle handle, Action<UvShutdownReq, int, object> callback, object state)
 {
     _callback = callback;
     _state = state;
     Pin();
     _uv.shutdown(this, handle, _uv_shutdown_cb);
 }
Ejemplo n.º 2
0
        private static void OnConnectionCallback(UvStreamHandle listenSocket, int status, Exception error, object state)
        {
            var listener = (UvTcpListener)state;

            var acceptSocket = new UvTcpHandle();

            try
            {
                acceptSocket.Init(listener._thread.Loop, null);
                acceptSocket.NoDelay(true);
                listenSocket.Accept(acceptSocket);
                var connection = new UvTcpConnection(listener._thread, acceptSocket);
                ExecuteCallback(listener, connection);
            }
            catch (UvException)
            {
                acceptSocket.Dispose();
            }
        }
Ejemplo n.º 3
0
 public void accept(UvStreamHandle server, UvStreamHandle client)
 {
     server.Validate();
     client.Validate();
     ThrowIfErrored(_uv_accept(server, client));
 }
Ejemplo n.º 4
0
 public static extern int uv_shutdown(UvShutdownReq req, UvStreamHandle handle, uv_shutdown_cb cb);
Ejemplo n.º 5
0
        private unsafe void Write(
            UvStreamHandle handle,
            ReadOnlySequence <byte> buffer,
            Action <UvWriteReq, int, object> callback,
            object state)
        {
            try
            {
                int nBuffers = 0;
                if (buffer.IsSingleSegment)
                {
                    nBuffers = 1;
                }
                else
                {
                    foreach (var span in buffer)
                    {
                        nBuffers++;
                    }
                }

                // add GCHandle to keeps this SafeHandle alive while request processing
                _pins.Add(GCHandle.Alloc(this, GCHandleType.Normal));

                var pBuffers = (Uv.uv_buf_t *)_bufs;
                if (nBuffers > BUFFER_COUNT)
                {
                    // create and pin buffer array when it's larger than the pre-allocated one
                    var bufArray = new Uv.uv_buf_t[nBuffers];
                    var gcHandle = GCHandle.Alloc(bufArray, GCHandleType.Pinned);
                    _pins.Add(gcHandle);
                    pBuffers = (Uv.uv_buf_t *)gcHandle.AddrOfPinnedObject();
                }

                if (nBuffers == 1)
                {
                    var memory       = buffer.First;
                    var memoryHandle = memory.Pin();
                    _handles.Add(memoryHandle);
                    pBuffers[0] = Libuv.buf_init((IntPtr)memoryHandle.Pointer, memory.Length);
                }
                else
                {
                    int i = 0;
                    foreach (var memory in buffer)
                    {
                        var memoryHandle = memory.Pin();
                        _handles.Add(memoryHandle);
                        pBuffers[i++] = Libuv.buf_init((IntPtr)memoryHandle.Pointer, memory.Length);
                    }
                }

                _callback = callback;
                _state    = state;
                _uv.write(this, handle, pBuffers, nBuffers, _uv_write_cb);
            }
            catch
            {
                _callback = null;
                _state    = null;
                Unpin(this);
                throw;
            }
        }
Ejemplo n.º 6
0
 public extern static int uv_read_start(UvStreamHandle handle, uv_alloc_cb alloc_cb, uv_read_cb read_cb);
Ejemplo n.º 7
0
 public static extern int uv_try_write(UvStreamHandle handle, uv_buf_t[] bufs, int nbufs);
Ejemplo n.º 8
0
 unsafe public void write2(UvRequest req, UvStreamHandle handle, Uv.uv_buf_t* bufs, int nbufs, UvStreamHandle sendHandle, uv_write_cb cb)
 {
     req.Validate();
     handle.Validate();
     ThrowIfErrored(_uv_write2(req, handle, bufs, nbufs, sendHandle, cb));
 }
Ejemplo n.º 9
0
 public static extern int uv_listen(UvStreamHandle handle, int backlog, uv_connection_cb cb);
Ejemplo n.º 10
0
 public static extern int uv_accept(UvStreamHandle server,UvStreamHandle client);
Ejemplo n.º 11
0
 public extern static int uv_read_start(UvStreamHandle handle,uv_alloc_cb alloc_cb,uv_read_cb read_cb);
Ejemplo n.º 12
0
 public void shutdown(UvShutdownReq req, UvStreamHandle handle, uv_shutdown_cb cb)
 {
     req.Validate();
     handle.Validate();
     ThrowIfErrored(_uv_shutdown(req, handle, cb));
 }
Ejemplo n.º 13
0
 public static extern int uv_listen(UvStreamHandle handle,int backlog,uv_connection_cb cb);
Ejemplo n.º 14
0
 unsafe public void write2(UvRequest req, UvStreamHandle handle, Uv.uv_buf_t *bufs, int nbufs, UvStreamHandle sendHandle, uv_write_cb cb)
 {
     req.Validate();
     handle.Validate();
     ThrowIfErrored(_uv_write2(req, handle, bufs, nbufs, sendHandle, cb));
 }
Ejemplo n.º 15
0
 public void read_stop(UvStreamHandle handle)
 {
     handle.Validate();
     ThrowIfErrored(_uv_read_stop(handle));
 }
Ejemplo n.º 16
0
 public void read_start(UvStreamHandle handle, uv_alloc_cb alloc_cb, uv_read_cb read_cb)
 {
     handle.Validate();
     ThrowIfErrored(_uv_read_start(handle, alloc_cb, read_cb));
 }
Ejemplo n.º 17
0
        private unsafe Uv.uv_buf_t OnAlloc(UvStreamHandle handle, int status)
        {
            _inputBuffer = _input.Alloc(2048);

            void* pointer;
            if (!_inputBuffer.Memory.TryGetPointer(out pointer))
            {
                throw new InvalidOperationException("Pointer must be pinned");
            }

            return handle.Libuv.buf_init((IntPtr)pointer, _inputBuffer.Memory.Length);
        }
Ejemplo n.º 18
0
 public static extern int uv_read_stop(UvStreamHandle handle);
Ejemplo n.º 19
0
        public unsafe void Write(
            UvStreamHandle handle,
            ReadableBuffer buffer,
            Action<UvWriteReq, int, object> callback,
            object state)
        {
            try
            {
                // Preserve the buffer for the async call
                _buffer = buffer.Preserve();
                buffer = _buffer.Buffer;

                int nBuffers = 0;
                if (buffer.IsSingleSpan)
                {
                    nBuffers = 1;
                }
                else
                {
                    foreach (var span in buffer)
                    {
                        nBuffers++;
                    }
                }

                // add GCHandle to keeps this SafeHandle alive while request processing
                _pins.Add(GCHandle.Alloc(this, GCHandleType.Normal));

                var pBuffers = (Uv.uv_buf_t*)_bufs;
                if (nBuffers > BUFFER_COUNT)
                {
                    // create and pin buffer array when it's larger than the pre-allocated one
                    var bufArray = new Uv.uv_buf_t[nBuffers];
                    var gcHandle = GCHandle.Alloc(bufArray, GCHandleType.Pinned);
                    _pins.Add(gcHandle);
                    pBuffers = (Uv.uv_buf_t*)gcHandle.AddrOfPinnedObject();
                }

                if (nBuffers == 1)
                {
                    var memory = buffer.First;
                    void* pointer;
                    if (memory.TryGetPointer(out pointer))
                    {
                        pBuffers[0] = Libuv.buf_init((IntPtr)pointer, memory.Length);
                    }
                    else
                    {
                        throw new InvalidOperationException("Memory needs to be pinned");
                    }
                }
                else
                {
                    int i = 0;
                    void* pointer;
                    foreach (var memory in buffer)
                    {
                        if (memory.TryGetPointer(out pointer))
                        {
                            pBuffers[i++] = Libuv.buf_init((IntPtr)pointer, memory.Length);
                        }
                        else
                        {
                            throw new InvalidOperationException("Memory needs to be pinned");
                        }
                    }
                }

                _callback = callback;
                _state = state;
                _uv.write(this, handle, pBuffers, nBuffers, _uv_write_cb);
            }
            catch
            {
                _callback = null;
                _state = null;
                _buffer.Dispose();
                Unpin(this);
                throw;
            }
        }
Ejemplo n.º 20
0
 public static extern int uv_try_write(UvStreamHandle handle,uv_buf_t[] bufs,int nbufs);
Ejemplo n.º 21
0
 public void shutdown(UvShutdownReq req, UvStreamHandle handle, uv_shutdown_cb cb)
 {
     req.Validate();
     handle.Validate();
     ThrowIfErrored(_uv_shutdown(req, handle, cb));
 }
Ejemplo n.º 22
0
 unsafe public static extern int uv_write2(UvRequest req,UvStreamHandle handle,uv_buf_t*bufs,int nbufs,UvStreamHandle sendHandle,uv_write_cb cb);
Ejemplo n.º 23
0
 public static extern int uv_accept(UvStreamHandle server, UvStreamHandle client);
Ejemplo n.º 24
0
 public static extern int uv_shutdown(UvShutdownReq req,UvStreamHandle handle,uv_shutdown_cb cb);
Ejemplo n.º 25
0
 public static extern int uv_read_stop(UvStreamHandle handle);
Ejemplo n.º 26
0
 public void listen(UvStreamHandle handle, int backlog, uv_connection_cb cb)
 {
     handle.Validate();
     ThrowIfErrored(_uv_listen(handle, backlog, cb));
 }
Ejemplo n.º 27
0
 unsafe public static extern int uv_write2(UvRequest req, UvStreamHandle handle, uv_buf_t* bufs, int nbufs, UvStreamHandle sendHandle, uv_write_cb cb);
Ejemplo n.º 28
0
 public void Accept(UvStreamHandle handle)
 {
     _uv.accept(this, handle);
 }
Ejemplo n.º 29
0
        public unsafe void Write(
            UvStreamHandle handle,
            ReadableBuffer buffer,
            Action <UvWriteReq, int, object> callback,
            object state)
        {
            try
            {
                // Preserve the buffer for the async call
                _buffer = buffer.Preserve();
                buffer  = _buffer.Buffer;

                int nBuffers = 0;
                if (buffer.IsSingleSpan)
                {
                    nBuffers = 1;
                }
                else
                {
                    foreach (var span in buffer)
                    {
                        nBuffers++;
                    }
                }

                // add GCHandle to keeps this SafeHandle alive while request processing
                _pins.Add(GCHandle.Alloc(this, GCHandleType.Normal));

                var pBuffers = (Uv.uv_buf_t *)_bufs;
                if (nBuffers > BUFFER_COUNT)
                {
                    // create and pin buffer array when it's larger than the pre-allocated one
                    var bufArray = new Uv.uv_buf_t[nBuffers];
                    var gcHandle = GCHandle.Alloc(bufArray, GCHandleType.Pinned);
                    _pins.Add(gcHandle);
                    pBuffers = (Uv.uv_buf_t *)gcHandle.AddrOfPinnedObject();
                }

                if (nBuffers == 1)
                {
                    var   memory = buffer.First;
                    void *pointer;
                    if (memory.TryGetPointer(out pointer))
                    {
                        pBuffers[0] = Libuv.buf_init((IntPtr)pointer, memory.Length);
                    }
                    else
                    {
                        throw new InvalidOperationException("Memory needs to be pinned");
                    }
                }
                else
                {
                    int   i = 0;
                    void *pointer;
                    foreach (var memory in buffer)
                    {
                        if (memory.TryGetPointer(out pointer))
                        {
                            pBuffers[i++] = Libuv.buf_init((IntPtr)pointer, memory.Length);
                        }
                        else
                        {
                            throw new InvalidOperationException("Memory needs to be pinned");
                        }
                    }
                }

                _callback = callback;
                _state    = state;
                _uv.write(this, handle, pBuffers, nBuffers, _uv_write_cb);
            }
            catch
            {
                _callback = null;
                _state    = null;
                _buffer.Dispose();
                Unpin(this);
                throw;
            }
        }
Ejemplo n.º 30
0
        private void OnRead(UvStreamHandle handle, int status)
        {
            if (status == 0)
            {
                // A zero status does not indicate an error or connection end. It indicates
                // there is no data to be read right now.
                // See the note at http://docs.libuv.org/en/v1.x/stream.html#c.uv_read_cb.
                _inputBuffer.Commit();
                return;
            }

            var normalRead = status > 0;
            var normalDone = status == EOF;
            var errorDone = !(normalDone || normalRead);
            var readCount = normalRead ? status : 0;

            if (!normalRead)
            {
                handle.ReadStop();
            }

            IOException error = null;
            if (errorDone)
            {
                Exception uvError;
                handle.Libuv.Check(status, out uvError);
                error = new IOException(uvError.Message, uvError);

                // REVIEW: Should we treat ECONNRESET as an error?
                // Ignore the error for now 
                _input.CompleteWriter();
            }
            else if (readCount == 0 || _input.Writing.IsCompleted)
            {
                _input.CompleteWriter();
            }
            else
            {
                _inputBuffer.Advance(readCount);

                var task = _inputBuffer.FlushAsync();

                if (!task.IsCompleted)
                {
                    // If there's back pressure
                    handle.ReadStop();

                    // Resume reading when task continues
                    task.ContinueWith((t, state) => ((UvTcpConnection)state).StartReading(), this);
                }
            }
        }
Ejemplo n.º 31
0
 public void accept(UvStreamHandle server, UvStreamHandle client)
 {
     server.Validate();
     client.Validate();
     ThrowIfErrored(_uv_accept(server, client));
 }
Ejemplo n.º 32
0
 public void listen(UvStreamHandle handle, int backlog, uv_connection_cb cb)
 {
     handle.Validate();
     ThrowIfErrored(_uv_listen(handle, backlog, cb));
 }
Ejemplo n.º 33
0
 private static void ReadCallback(UvStreamHandle handle, int status, object state)
 {
     ((UvTcpConnection)state).OnRead(handle, status);
 }
Ejemplo n.º 34
0
 public void read_start(UvStreamHandle handle, uv_alloc_cb alloc_cb, uv_read_cb read_cb)
 {
     handle.Validate();
     ThrowIfErrored(_uv_read_start(handle, alloc_cb, read_cb));
 }
Ejemplo n.º 35
0
 private static Uv.uv_buf_t AllocCallback(UvStreamHandle handle, int status, object state)
 {
     return ((UvTcpConnection)state).OnAlloc(handle, status);
 }
Ejemplo n.º 36
0
 public void read_stop(UvStreamHandle handle)
 {
     handle.Validate();
     ThrowIfErrored(_uv_read_stop(handle));
 }
Ejemplo n.º 37
0
 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;
 }
Ejemplo n.º 38
0
 public void Accept(UvStreamHandle handle)
 {
     _uv.accept(this, handle);
 }