예제 #1
0
 public void Init(LoopHandle loopHandle, Action <Action <object>, object> asyncSendUserPostAction)
 {
     base.Init(loopHandle.LibUv, loopHandle.LibUv.TcpHandleSize, loopHandle.LoopRunThreadId);
     LibUv.TcpInit(loopHandle, this);
     Loop       = loopHandle;
     _postAsync = asyncSendUserPostAction;
 }
예제 #2
0
 public void PipeConnect(UvPipeHandle uvPipeHandle, string text, Action <UvPipeStream, int, Exception, object> callback, object state)
 {
     _callback = callback;
     _state    = state;
     Alloc();
     LibUv.PipeConnect(this, uvPipeHandle, text, _uv_shutdown_cb);
 }
예제 #3
0
 public void Read(Func <UvStreamHandle, int, object, LibUv.BufferStruct> allocCallback, Action <UvStreamHandle, int, Exception, object> readCallback, object obj)
 {
     if (_GCHandle.IsAllocated)
     {
         throw new InvalidOperationException("UvStreamHandle.cs: ReadStop must be called before ReadStart may be called again");
     }
     try
     {
         _allocCallback = allocCallback;
         _readCallback  = readCallback;
         _readState     = obj;
         _GCHandle      = GCHandle.Alloc(this, GCHandleType.Normal);
         LibUv.ReadStart(this, _Alloc_Callback, _Read_Callback);
     }
     catch
     {
         _allocCallback = null;
         _readCallback  = null;
         _readState     = null;
         if (_GCHandle.IsAllocated)
         {
             _GCHandle.Free();
         }
         throw;
     }
 }
예제 #4
0
 protected unsafe void Init(LibUv libuv, int hdle, int point)
 {
     LibUv           = libuv;
     LoopRunThreadId = point;
     //Starting with libuv v1.0, users should allocate the memory for the loops before initializing it with uv_loop_init(uv_loop_t *). This allows you to plug in custom memory management
     handle = Marshal.AllocCoTaskMem(hdle);//申请内存
     *(IntPtr *)((void *)handle) = GCHandle.ToIntPtr(GCHandle.Alloc(this, GCHandleType.Weak));
 }
예제 #5
0
 public void ReadStop()
 {
     if (!_GCHandle.IsAllocated)
     {
         return;
     }
     LibUv.ReadStop(this);
     LibUv.BufferStruct bufferStruct = LibUv.CreateBufferStruct(IntPtr.Zero, 0);
     UbReadCb(handle, 0, ref bufferStruct);
 }
예제 #6
0
 public void Stop()
 {
     if (!_GCHandle.IsAllocated)
     {
         return;
     }
     _allocCallback = null;
     _readCallback  = null;
     _readState     = null;
     if (_GCHandle.IsAllocated)
     {
         _GCHandle.Free();
     }
     LibUv.ReadStop(this);
 }
예제 #7
0
 public void ShutDown(UvStreamHandle uvStreamHandle, Action <int, object> callBack, object state)
 {
     _callBack = callBack;
     _state    = state;
     Alloc();
     try
     {
         LibUv.ShutDown(this, uvStreamHandle, _ShutDown_Callback);
     }
     catch (Exception ex)
     {
         DoDispose();
         throw ex;
     }
 }
예제 #8
0
        public void TcpBind(IPEndPoint iPEndPoint)
        {
            string text = iPEndPoint.Address.ToString();

            LibUv.Addr addr;
            Exception  ex;

            LibUv.Ip4Address(text, iPEndPoint.Port, out addr, out ex);
            if (ex != null)
            {
                Exception ex2;
                LibUv.Ip6Address(text, iPEndPoint.Port, out addr, out ex2);
                if (ex2 != null)
                {
                    throw ex;
                }
            }
            LibUv.TcpBind(this, ref addr, 0);
        }
예제 #9
0
        protected override unsafe bool ReleaseHandle()
        {
            IntPtr ptr = handle;

            if (ptr != IntPtr.Zero)
            {
                IntPtr intPtr = *(IntPtr *)((void *)ptr);
                try
                {
                    LibUv.LoopClose(this);
                }
                catch
                {
                    //ignore
                }
                handle = IntPtr.Zero;
                FreeHandle(ptr, intPtr);
            }
            return(true);
        }
예제 #10
0
 public unsafe void Free(UvStreamHandle uvPipeHandle, UvStreamHandle sendHandle, Action <int, Exception, object> callBack, object state)
 {
     _callback = callBack;
     _state    = state;
     try
     {
         _pins.Add(GCHandle.Alloc(this, GCHandleType.Normal));
         LibUv.BufferStruct *ptr = (LibUv.BufferStruct *)((void *)_bufs);
         *ptr = LibUv.CreateBufferStruct(_bufferPoint, 4);
         LibUv.Write2(this, uvPipeHandle, ptr, 1, sendHandle, _Write_Callback);
     }
     catch
     {
         HandleFree(this);
         sendHandle.Dispose();
         _callback = null;
         _state    = null;
         throw;
     }
 }
예제 #11
0
 private unsafe void Write(UvStreamHandle uvStreamHandle, byte[] buffer1, int buffer1StartIndex, int buffer1Offset, byte[] buffer2, int buffer2StartIndex, int buffer2Offset, Action <int, Exception, object> callBack, object state)
 {
     if (buffer1 == null || buffer1Offset < 1 || buffer1.Length < buffer1Offset + buffer1StartIndex)
     {
         throw new ArgumentNullException("buffer1");
     }
     _callback = callBack;
     _state    = state;
     try
     {
         _pins.Add(GCHandle.Alloc(this, GCHandleType.Normal));
         LibUv.BufferStruct *ptr = (LibUv.BufferStruct *)((void *)_bufs);
         int      nbufs          = 0;
         GCHandle item           = GCHandle.Alloc(buffer1, GCHandleType.Pinned);
         _pins.Add(item);
         //返回的是对象的“数据区”的起始地址
         *ptr = LibUv.CreateBufferStruct(item.AddrOfPinnedObject() + buffer1StartIndex, buffer1Offset);
         nbufs++;
         if (buffer2 != null && buffer2Offset > 0)
         {
             if (buffer2.Length < buffer2Offset + buffer2StartIndex)
             {
                 throw new ArgumentException("buffer2");
             }
             item = GCHandle.Alloc(buffer2, GCHandleType.Pinned);
             _pins.Add(item);
             ptr[1] = LibUv.CreateBufferStruct(item.AddrOfPinnedObject() + buffer2StartIndex, buffer2Offset);
             nbufs++;
         }
         //int uv_write(uv_write_t* req, uv_stream_t* handle,uv_buf_t bufs[], int bufcnt, uv_write_cb cb);
         LibUv.Write(this, uvStreamHandle, ptr, nbufs, _Write_Callback);
     }
     catch
     {
         HandleFree(this);
         _callback = null;
         _state    = null;
         throw;
     }
 }
예제 #12
0
        protected override bool ReleaseHandle()
        {
            IntPtr intPtr = Interlocked.Exchange(ref handle, IntPtr.Zero);

            if (intPtr != IntPtr.Zero)
            {
                if (Thread.CurrentThread.ManagedThreadId != LoopRunThreadId)
                {
                    if (_postAsync != null)
                    {
                        HandleRelease handleRelease = new HandleRelease();
                        handleRelease.LibUv = LibUv;
                        _postAsync(new Action <object>(handleRelease.Release), intPtr);
                    }
                }
                else
                {
                    LibUv.UvClose(intPtr, _Close_Callback);
                }
            }
            return(true);
        }
예제 #13
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="hdl">队列长度</param>
 /// <param name="connectionCallBack"></param>
 /// <param name="state"></param>
 public void Listen(int hdl, Action <UvStreamHandle, int, Exception, object> connectionCallBack, object state)
 {
     if (_listenVitality.IsAllocated)//如果分配了句柄,则为 true;否则为 false
     {
         throw new InvalidOperationException("TODO: Listen may not be called more than once");
     }
     try
     {
         _connectionCB   = connectionCallBack;
         _listenState    = state;
         _listenVitality = GCHandle.Alloc(this, GCHandleType.Normal);
         LibUv.Listen(this, hdl, _Listen_Callback);
     }
     catch
     {
         _connectionCB = null;
         _listenState  = null;
         if (_listenVitality.IsAllocated)
         {
             _listenVitality.Free();
         }
         throw;
     }
 }
예제 #14
0
 public int Start(int num = 0 /*UV_RUN_DEFAULT*/)
 {
     return(LibUv.Run(this, num));
 }
예제 #15
0
 public void TcpNodealy(bool flag)
 {
     LibUv.TcpNodealy(this, flag);
 }
예제 #16
0
 public void Stop()
 {
     LibUv.Stop(this);
 }
예제 #17
0
 public void Release(object obj)
 {
     LibUv.UvClose((IntPtr)obj, _Close_Callback);
 }
예제 #18
0
 public void UvUnRef()
 {
     LibUv.UvUnRef(this);
 }
예제 #19
0
 public void Accept(UvStreamHandle uvStreamHandle)
 {
     LibUv.Accept(this, uvStreamHandle);
 }
예제 #20
0
 /// <summary>
 /// 把监视器和loop联系起来
 /// </summary>
 /// <param name="libUv"></param>
 public void Init(LibUv libUv)
 {
     base.Init(libUv, libUv.GetUvLoopSize(), Thread.CurrentThread.ManagedThreadId);
     LibUv.Init(this);//loop init
 }
예제 #21
0
 /// <summary>
 /// uv_asyncがやってくれるのは、async_cbを呼ぶことだけなので、
 /// なにかデータを渡したいときは別途、
 /// pthread_mutexなどを使用して共有データをわたすように自分でよしなにやるかんじですね
 /// </summary>
 public void AsyncSend()
 {
     LibUv.AsyncSend(this);
 }
예제 #22
0
 /// <summary>
 /// 为loop注册了一个异步消息监听器 其他线程就可以通过async监视器给主线程发送消息
 /// </summary>
 /// <param name="loopHandle"></param>
 /// <param name="cb"></param>
 public void Init(LoopHandle loopHandle, Action cb)
 {
     base.Init(loopHandle.LibUv, loopHandle.LibUv.HandSize(LibUv.HandleType.UV_ASYNC), loopHandle.LoopRunThreadId);
     _callback = cb;
     LibUv.AsyncInit(loopHandle, this, _uv_async_cb);
 }