Beispiel #1
0
        static void OnCloseHandle(IntPtr handle)
        {
            if (handle == IntPtr.Zero)
            {
                return;
            }

            NativeHandle nativeHandle = null;

            // Get gc handle first
            IntPtr pHandle = ((uv_handle_t *)handle)->data;

            if (pHandle != IntPtr.Zero)
            {
                GCHandle gcHandle = GCHandle.FromIntPtr(pHandle);
                if (gcHandle.IsAllocated)
                {
                    nativeHandle = gcHandle.Target as NativeHandle;
                    //Console.WriteLine("gcHandle free");
                    gcHandle.Free();

                    ((uv_handle_t *)handle)->data = IntPtr.Zero;
                }
            }


            // Release memory
            Libuv.FreeMemory(handle);
            nativeHandle?.OnClosed();
        }
Beispiel #2
0
 public EventLoop()
 {
     _loop = Libuv.CreateLoop();
     Libuv.uv_loop_init(_loop);
     _async  = new Async(this, doAsyncJob, null);
     PReader = new PacketRead();
 }
Beispiel #3
0
        public void Listen()
        {
            int r = Libuv.uv_listen(_handle, 10, ListenCB);

            if (r != 0)
            {
                Console.WriteLine($"uv_listen error:{r}");
            }
        }
        static void OnWatcherCallback(IntPtr handle, int status)
        {
            var request = GetDataFromHandle <ConnectRequest>(handle);

            if (status < 0)
            {
                request.error = Libuv.CreateError((uv_err_code)status);
            }
            request.OnWatcherCallback();
        }
        protected NativeRequest(uv_req_type requestType, int size)
        {
            IntPtr handle = Libuv.Allocate(requestType);

            GCHandle gcHandle = GCHandle.Alloc(this, GCHandleType.Normal);

            *(IntPtr *)handle = GCHandle.ToIntPtr(gcHandle);

            this.Handle      = handle;
            this.requestType = requestType;
        }
Beispiel #6
0
        public TcpHandle(EventLoop loop) : base()
        {
            _handle    = Libuv.Allocate(uv_handle_type.UV_TCP);
            _eventLoop = loop;

            Libuv.uv_tcp_init(loop.Loop, _handle);

            GCHandle gcHandle = GCHandle.Alloc(this, GCHandleType.Normal);

            ((uv_handle_t *)_handle)->data = GCHandle.ToIntPtr(gcHandle);
        }
Beispiel #7
0
 private bool disposedValue = false; // 要检测冗余调用
 protected virtual void Dispose(bool disposing)
 {
     if (!disposedValue)
     {
         if (disposing)
         {
             // TODO: 释放托管状态(托管对象)。
         }
         // TODO: 释放未托管的资源(未托管的对象)并在以下内容中替代终结器。
         // TODO: 将大型字段设置为 null。
         Libuv.FreeMemory(_loop);
         disposedValue = true;
     }
 }
Beispiel #8
0
        public Timer(EventLoop loop, Action <object> cb, object state)
        {
            _eventLoop = loop;
            _callback  = cb;
            _state     = state;

            _handle = Libuv.Allocate(uv_handle_type.UV_TIMER);

            Libuv.uv_timer_init(_eventLoop.Loop, _handle);


            GCHandle gcHandle = GCHandle.Alloc(this, GCHandleType.Normal);

            ((uv_handle_t *)_handle)->data = GCHandle.ToIntPtr(gcHandle);
        }
Beispiel #9
0
        public void Run(long interval = 0)
        {
            if (interval > 0)
            {
                _timer = new Timer(this, (obj) =>
                {
                    long dt = Libuv.uv_now(_loop);
                    Update(dt);
                }, null);

                _timer.Start(interval, interval);
            }

            Libuv.uv_run(_loop, Libuv.uv_run_mode.UV_RUN_DEFAULT);
        }
        public ConnectRequest(IntPtr handle, IPEndPoint remoteEndPoint) : base(uv_req_type.UV_CONNECT, 0)
        {
            Libuv.GetSocketAddress(remoteEndPoint, out sockaddr addr);
            int result = Libuv.uv_tcp_connect(
                this.Handle,
                handle,
                ref addr,
                WatcherCallback);

            //ThrowIfError(result);
            if (result < 0)
            {
                error = Libuv.CreateError((uv_err_code)result);
                OnWatcherCallback();
            }
        }
Beispiel #11
0
        internal void CloseHandle()
        {
            IntPtr handle = this._handle;

            if (handle == IntPtr.Zero)
            {
                return;
            }

            int result = Libuv.uv_is_closing(handle);

            if (result == 0)
            {
                Libuv.uv_close(handle, CloseCallback);
            }
        }
Beispiel #12
0
        public Async(EventLoop loop, Action <object> cb, object state)
        {
            var handle = Libuv.Allocate(uv_handle_type.UV_ASYNC);

            _handle = handle;

            _eventLoop = loop;

            _callback = cb;
            _state    = state;


            Libuv.uv_async_init(loop.Loop, _handle, WorkCallback);

            GCHandle gcHandle = GCHandle.Alloc(this, GCHandleType.Normal);

            ((uv_handle_t *)_handle)->data = GCHandle.ToIntPtr(gcHandle);
        }
Beispiel #13
0
        public void Bind(IPEndPoint ip)
        {
            sockaddr addr = new sockaddr();

            Libuv.GetSocketAddress(ip, out addr);

            int r = Libuv.uv_tcp_init(_eventLoop.Loop, _handle);

            if (r != 0)
            {
                Console.WriteLine($"uv_tcp_init error:{r}");
            }

            r = Libuv.uv_tcp_bind(_handle, ref addr, 0);
            if (r != 0)
            {
                Console.WriteLine($"uv_tcp_bind error:{r}");
            }



            //Libuv.uv_run(_loop, Libuv.uv_run_mode.UV_RUN_DEFAULT);
        }
Beispiel #14
0
        void OnListenCB(int status)
        {
            Tcp tcp = new Tcp(_eventLoop);
            int ret = Libuv.uv_accept(_handle, tcp.Handle);

            if (ret == 0)
            {
                //Libuv.uv_re
                Channel c = new Channel( );
                tcp.Channel = c;
                c.Handle    = tcp;

                ChannelAddEvent?.Invoke(c);

                Libuv.uv_read_start(tcp.Handle, Tcp.AllocCB, Tcp.ReadCB);
            }
            else
            {
                Console.WriteLine("accept error!!!");

                tcp.Dispose();
            }
        }
Beispiel #15
0
 public void Start(long interval, long repeated)
 {
     Libuv.uv_timer_start(_handle, WorkCallback, interval, repeated);
 }
Beispiel #16
0
 public void Send()
 {
     Libuv.uv_async_send(this.Handle);
 }
Beispiel #17
0
 public void Write(IntPtr dst)
 {
     Libuv.uv_write(Handle, dst, ref _buf, 1, OnWriteCB);
 }