Beispiel #1
0
        unsafe protected void CreateMemory(Libuv uv, int threadId, int size)
        {
            _uv      = uv;
            ThreadId = threadId;

            handle            = Marshal.AllocCoTaskMem(size);
            *(IntPtr *)handle = GCHandle.ToIntPtr(GCHandle.Alloc(this, GCHandleType.Weak));
        }
Beispiel #2
0
        public void Init(Libuv uv)
        {
            CreateMemory(
                uv,
                Thread.CurrentThread.ManagedThreadId,
                uv.loop_size());

            _uv.loop_init(this);
        }
Beispiel #3
0
 unsafe protected void CreateHandle(
     Libuv uv,
     int threadId,
     int size,
     Action <Action <IntPtr>, IntPtr> queueCloseHandle)
 {
     _queueCloseHandle = queueCloseHandle;
     CreateMemory(uv, threadId, size);
 }
Beispiel #4
0
        public unsafe void Write(
            UvStreamHandle handle,
            MemoryPoolIterator2 start,
            MemoryPoolIterator2 end,
            int nBuffers,
            Action <UvWriteReq, int, Exception, object> callback,
            object state)
        {
            try
            {
                // add GCHandle to keeps this SafeHandle alive while request processing
                _pins.Add(GCHandle.Alloc(this, GCHandleType.Normal));

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

                var block = start.Block;
                for (var index = 0; index < nBuffers; index++)
                {
                    var blockStart = block == start.Block ? start.Index : block.Data.Offset;
                    var blockEnd   = block == end.Block ? end.Index : block.Data.Offset + block.Data.Count;

                    // create and pin each segment being written
                    pBuffers[index] = Libuv.buf_init(
                        block.Pin() + blockStart,
                        blockEnd - blockStart);

                    block = block.Next;
                }

                _callback = callback;
                _state    = state;
                _uv.write(this, handle, pBuffers, nBuffers, _uv_write_cb);
            }
            catch
            {
                _callback = null;
                _state    = null;
                Unpin(this);

                var block = start.Block;
                for (var index = 0; index < nBuffers; index++)
                {
                    block.Unpin();
                    block = block.Next;
                }

                throw;
            }
        }
Beispiel #5
0
 protected unsafe void CreateHandle(
     Libuv uv,
     int threadId,
     int size,
     Action<Action<IntPtr>, IntPtr> queueCloseHandle)
 {
     _queueCloseHandle = queueCloseHandle;
     CreateMemory(uv, threadId, size);
 }
        public void Init(Libuv uv)
        {
            CreateMemory(
                uv,
                Thread.CurrentThread.ManagedThreadId,
                uv.loop_size());

            _uv.loop_init(this);
        }
 public static void Apply(Libuv libuv)
 {
     if (libuv.IsWindows)
     {
         WindowsApis.Apply(libuv);
     }
     else
     {
         LinuxApis.Apply(libuv);
     }
 }
 public static void Apply(Libuv libuv)
 {
     if (libuv.IsWindows)
     {
         WindowsApis.Apply(libuv);
     }
     else
     {
         LinuxApis.Apply(libuv);
     }
 }
        public void Connect(
            UvPipeHandle pipe,
            string name,
            Action <UvConnectRequest, int, Exception, object> callback,
            object state)
        {
            _callback = callback;
            _state    = state;

            Pin();
            Libuv.pipe_connect(this, pipe, name, _uv_connect_cb);
        }
        public unsafe void Write2(
            UvStreamHandle handle,
            ArraySegment <ArraySegment <byte> > bufs,
            UvStreamHandle sendHandle,
            Action <UvWriteReq, int, Exception, object> callback,
            object state)
        {
            try
            {
                // add GCHandle to keeps this SafeHandle alive while request processing
                _pins.Add(GCHandle.Alloc(this, GCHandleType.Normal));

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

                for (var index = 0; index != nBuffers; ++index)
                {
                    // create and pin each segment being written
                    var buf = bufs.Array[bufs.Offset + index];

                    var gcHandle = GCHandle.Alloc(buf.Array, GCHandleType.Pinned);
                    _pins.Add(gcHandle);
                    pBuffers[index] = Libuv.buf_init(
                        gcHandle.AddrOfPinnedObject() + buf.Offset,
                        buf.Count);
                }

                _callback = callback;
                _state    = state;
                _uv.write2(this, handle, pBuffers, nBuffers, sendHandle, _uv_write_cb);
            }
            catch
            {
                _callback = null;
                _state    = null;
                Unpin(this);
                throw;
            }
        }
        public KestrelEngine(ILibraryManager libraryManager, IApplicationShutdown appShutdownService)
        {
            AppShutdown = appShutdownService;
            Threads = new List<KestrelThread>();
            Listeners = new List<Listener>();
            Memory = new MemoryPool();
            Libuv = new Libuv();

            var libraryPath = default(string);

            if (libraryManager != null)
            {
                var library = libraryManager.GetLibraryInformation("Microsoft.AspNet.Server.Kestrel");
                libraryPath = library.Path;
                if (library.Type == "Project")
                {
                    libraryPath = Path.GetDirectoryName(libraryPath);
                }
                if (Libuv.IsWindows)
                {
                    var architecture = IntPtr.Size == 4
                        ? "x86"
                        : "amd64";

                    libraryPath = Path.Combine(
                        libraryPath,
                        "native",
                        "windows",
                        architecture,
                        "libuv.dll");
                }
                else if (Libuv.IsDarwin)
                {
                    libraryPath = Path.Combine(
                        libraryPath,
                        "native",
                        "darwin",
                        "universal",
                        "libuv.dylib");
                }
                else
                {
                    libraryPath = "libuv.so.1";
                }
            }
            Libuv.Load(libraryPath);
        }
        private static void UvReadCb(IntPtr handle, int nread, ref Libuv.uv_buf_t buf)
        {
            var stream = FromIntPtr<UvStreamHandle>(handle);

            try
            {
                if (nread < 0)
                {
                    Exception error;
                    stream._uv.Check(nread, out error);
                    stream._readCallback(stream, 0, error, stream._readState);
                }
                else
                {
                    stream._readCallback(stream, nread, null, stream._readState);
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine("UbReadCb " + ex.ToString());
            }
        }
 public MultipleLoopTests()
 {
     var engine = new KestrelEngine(LibraryManager, new ShutdownNotImplemented());
     _uv = engine.Libuv;
 }
 public static void Apply(Libuv libuv)
 {
     libuv.LoadLibrary = LoadLibrary;
     libuv.FreeLibrary = FreeLibrary;
     libuv.GetProcAddress = GetProcAddress;
 }
 private static void UvAllocCb(IntPtr handle, int suggested_size, out Libuv.uv_buf_t buf)
 {
     var stream = FromIntPtr<UvStreamHandle>(handle);
     try
     {
         buf = stream._allocCallback(stream, suggested_size, stream._readState);
     }
     catch (Exception ex)
     {
         stream._log.LogError("UvAllocCb", ex);
         buf = stream.Libuv.buf_init(IntPtr.Zero, 0);
         throw;
     }
 }
        private static void UvReadCb(IntPtr handle, int nread, ref Libuv.uv_buf_t buf)
        {
            var stream = FromIntPtr<UvStreamHandle>(handle);

            try
            {
                if (nread < 0)
                {
                    Exception error;
                    stream._uv.Check(nread, out error);
                    stream._readCallback(stream, 0, nread, error, stream._readState);
                }
                else
                {
                    stream._readCallback(stream, nread, 0, null, stream._readState);
                }
            }
            catch (Exception ex)
            {
                stream._log.LogError("UbReadCb", ex);
                throw;
            }
        }
        private static void UvReadCb(IntPtr handle, int status, ref Libuv.uv_buf_t buf)
        {
            var stream = FromIntPtr<UvStreamHandle>(handle);

            try
            {
                stream._readCallback(stream, status, stream._readState);
            }
            catch (Exception ex)
            {
                stream._log.LogError("UbReadCb", ex);
                throw;
            }
        }
 public int TryWrite(Libuv.uv_buf_t buf)
 {
     return _uv.try_write(this, new[] { buf }, 1);
 }
 // For testing
 internal KestrelEngine(Libuv uv, IApplicationShutdown appShutdownService)
     : this(appShutdownService)
 {
     Libuv = uv;
 }
Beispiel #20
0
 public unsafe void write(UvWriteReq req, UvStreamHandle handle, Libuv.uv_buf_t* bufs, int nbufs, uv_write_cb cb)
 {
     req.Validate();
     handle.Validate();
     Check(_uv_write(req, handle, bufs, nbufs, cb));
 }
Beispiel #21
0
 unsafe public void write2(UvRequest req, UvStreamHandle handle, Libuv.uv_buf_t* bufs, int nbufs, UvStreamHandle sendHandle, uv_write_cb cb)
 {
     req.Validate();
     handle.Validate();
     Check(_uv_write2(req, handle, bufs, nbufs, sendHandle, cb));
 }
 // For testing
 internal KestrelEngine(Libuv uv, ServiceContext context)
    : base(context)
 {
     Libuv = uv;
     Threads = new List<KestrelThread>();
 }
 public NetworkingTests()
 {
     var engine = new KestrelEngine(new TestServiceContext());
     _uv = engine.Libuv;
     _logger = engine.Log;
 }
 public NetworkingTests()
 {
     var engine = new KestrelEngine(LibraryManager);
     _uv = engine.Libuv;
 }
 public MultipleLoopTests()
 {
     var engine = new KestrelEngine(new TestServiceContext());
     _uv = engine.Libuv;
     _logger = engine.Log;
 }
 public NetworkingTests()
 {
     var engine = new KestrelEngine(LibraryManager, new ShutdownNotImplemented());
     _uv = engine.Libuv;
 }
Beispiel #27
0
 public int try_write(UvStreamHandle handle, Libuv.uv_buf_t[] bufs, int nbufs)
 {
     handle.Validate();
     return Check(_uv_try_write(handle, bufs, nbufs));
 }
 private static void UvAllocCb(IntPtr handle, int suggested_size, out Libuv.uv_buf_t buf)
 {
     var stream = FromIntPtr<UvStreamHandle>(handle);
     try
     {
         buf = stream._allocCallback(stream, suggested_size, stream._readState);
     }
     catch (Exception ex)
     {
         Trace.WriteLine("UvAllocCb " + ex.ToString());
         buf = stream.Libuv.buf_init(IntPtr.Zero, 0);
         throw;
     }
 }
 public static void Apply(Libuv libuv)
 {
     libuv.LoadLibrary    = LoadLibrary;
     libuv.FreeLibrary    = FreeLibrary;
     libuv.GetProcAddress = GetProcAddress;
 }