Пример #1
0
        private unsafe void Write(
            UvStreamHandle handle,
            ReadableBuffer buffer,
            Action <UvWriteReq, int, UvException, object> callback,
            object state)
        {
            try
            {
                var nBuffers = 0;
                if (buffer.IsSingleSpan)
                {
                    nBuffers = 1;
                }
                else
                {
                    foreach (var _ in buffer)
                    {
                        nBuffers++;
                    }
                }

                var pBuffers = (LibuvFunctions.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 LibuvFunctions.uv_buf_t[nBuffers];
                    var gcHandle = GCHandle.Alloc(bufArray, GCHandleType.Pinned);
                    _pins.Add(gcHandle);
                    pBuffers = (LibuvFunctions.uv_buf_t *)gcHandle.AddrOfPinnedObject();
                }

                if (nBuffers == 1)
                {
                    var memory       = buffer.First;
                    var memoryHandle = memory.Retain(true);
                    _handles.Add(memoryHandle);

                    // Fast path for single buffer
                    pBuffers[0] = Libuv.buf_init(
                        (IntPtr)memoryHandle.PinnedPointer,
                        memory.Length);
                }
                else
                {
                    var index = 0;
                    foreach (var memory in buffer)
                    {
                        // This won't actually pin the buffer since we're already using pinned memory
                        var memoryHandle = memory.Retain(true);
                        _handles.Add(memoryHandle);

                        // create and pin each segment being written
                        pBuffers[index] = Libuv.buf_init(
                            (IntPtr)memoryHandle.PinnedPointer,
                            memory.Length);
                        index++;
                    }
                }

                _callback = callback;
                _state    = state;
                _uv.write(this, handle, pBuffers, nBuffers, _uv_write_cb);
            }
            catch
            {
                _callback = null;
                _state    = null;
                UnpinGcHandles();
                throw;
            }
        }
Пример #2
0
 public LibuvAwaitable <UvWriteReq> WriteAsync(UvStreamHandle handle, ReadableBuffer buffer)
 {
     Write(handle, buffer, LibuvAwaitable <UvWriteReq> .Callback, _awaitable);
     return(_awaitable);
 }
Пример #3
0
 public LibuvAwaitable <UvWriteReq> WriteAsync(UvStreamHandle handle, ArraySegment <ArraySegment <byte> > bufs)
 {
     Write(handle, bufs, LibuvAwaitable <UvWriteReq> .Callback, _awaitable);
     return(_awaitable);
 }
Пример #4
0
 public static extern int uv_try_write(UvStreamHandle handle,uv_buf_t[] bufs,int nbufs);
Пример #5
0
 public static extern unsafe int uv_write2(UvRequest req,UvStreamHandle handle,uv_buf_t*bufs,int nbufs,UvStreamHandle sendHandle,uv_write_cb cb);
Пример #6
0
 public static extern int uv_read_start(UvStreamHandle handle,uv_alloc_cb alloc_cb,uv_read_cb read_cb);
Пример #7
0
 public static extern int uv_read_stop(UvStreamHandle handle);
Пример #8
0
 public static extern int uv_listen(UvStreamHandle handle,int backlog,uv_connection_cb cb);
Пример #9
0
 public static extern int uv_accept(UvStreamHandle server,UvStreamHandle client);
Пример #10
0
 public void read_stop(UvStreamHandle handle)
 {
     handle.Validate();
     ThrowIfErrored(_uv_read_stop(handle));
 }
Пример #11
0
 public unsafe void write2(UvRequest req, UvStreamHandle handle, 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));
 }
Пример #12
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));
 }
Пример #13
0
 public void accept(UvStreamHandle server, UvStreamHandle client)
 {
     server.Validate();
     client.Validate();
     ThrowIfErrored(_uv_accept(server, client));
 }
Пример #14
0
 public void listen(UvStreamHandle handle, int backlog, uv_connection_cb cb)
 {
     handle.Validate();
     ThrowIfErrored(_uv_listen(handle, backlog, cb));
 }
Пример #15
0
 public LibuvAwaitable <UvWriteReq> WriteAsync(UvStreamHandle handle, in ReadOnlySequence <byte> buffer)
 public LibuvAwaitable <UvWriteReq> WriteAsync(UvStreamHandle handle, ReadOnlySequence <byte> buffer)
 {
     Write(handle, buffer, LibuvAwaitable <UvWriteReq> .Callback, _awaitable);
     return(_awaitable);
 }