Example #1
0
        private bool TryEnqueue(IList <ArraySegment <byte> > items, out bool conflict)
        {
            conflict = false;
            int oldCurrent = mOffset;

            if (oldCurrent + items.Count >= QUEUE_SIZE)
            {
                return(false);
            }

            if (Interlocked.CompareExchange(ref mOffset, oldCurrent + items.Count, oldCurrent) != oldCurrent)
            {
                conflict = true;
                return(false);
            }

            for (int i = 0; i < items.Count; i++)
            {
                ArraySegment <byte> item = items[i];
                var gcHandle             = GCHandle.Alloc(item.Array, GCHandleType.Pinned);
                mPins[oldCurrent + i]        = gcHandle;
                this.mBuffer[oldCurrent + i] = UVIntrop.buf_init(gcHandle.AddrOfPinnedObject() + item.Offset, item.Count);
            }
            return(true);
        }
        private static void UVConnectCallback(IntPtr reqHandle, Int32 status)
        {
            var req      = FromIntPtr <UVConnectRquest>(reqHandle);
            var callback = req.mCallback;
            var state    = req.mState;


            UVException error = null;

            if (status < 0)
            {
                UVIntrop.Check(status, out error);
            }

            try
            {
                if (callback != null)
                {
                    callback(req, status, error, state);
                }
            }
            catch (Exception ex)
            {
                throw;
            }
            finally
            {
                req.Close();
            }
        }
Example #3
0
 public void ReadStart(AllocCallback allocCallback, ReadCallback readCallback, object allocState, object readState)
 {
     this.mAllocCallback      = allocCallback;
     this.mReadCallback       = readCallback;
     this.mReadCallbackState  = readState;
     this.mAllocCallbackState = allocState;
     UVIntrop.read_start(this, mOnAlloc, mOnRead);
 }
Example #4
0
        public UVWriteRequest()
        {
            Int32 requestSize = UVIntrop.req_size(UVRequestType.WRITE);
            var   bufferSize  = Marshal.SizeOf(typeof(UVIntrop.uv_buf_t)) * QUEUE_SIZE;

            this.mPins = new GCHandle[QUEUE_SIZE];
            CreateMemory(requestSize + bufferSize);
            this.mBuffer = (UVIntrop.uv_buf_t *)(this.handle + requestSize);
        }
Example #5
0
        public unsafe void Write(byte[] datas, Int32 offset, Int32 count)
        {
            fixed(byte *p = datas)
            {
                UVIntrop.uv_buf_t[] mbuf = new UVIntrop.uv_buf_t[] {
                    UVIntrop.buf_init((IntPtr)(p + offset), count)
                };

                UVIntrop.try_write(this, mbuf, 1);
            }
        }
Example #6
0
        private void StartThread(object state)
        {
            Action <UVLoopHandle> cb = state as Action <UVLoopHandle>;

            UVIntrop.run(this, (Int32)UVIntrop.UV_RUN_MODE.UV_RUN_DEFAULT);
            Volatile.Write(ref mStateMutex, 0);
            if (cb != null)
            {
                cb(this);
            }
        }
Example #7
0
        protected override bool ReleaseHandle()
        {
            IntPtr memory = handle;

            if (memory != IntPtr.Zero)
            {
                UVIntrop.close(memory, mDestroyMemory);
                handle = IntPtr.Zero;
            }

            return(true);
        }
Example #8
0
        /// <summary>
        /// 绑定
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        public void Bind(string ip, Int32 port)
        {
            UVException exception;
            SockAddr    addr;

            UVIntrop.ip4_addr(ip, port, out addr, out exception);
            if (exception != null)
            {
                throw exception;
            }

            UVIntrop.tcp_bind(this, ref addr, 0);
        }
Example #9
0
 /// <summary>
 /// 写入数据
 /// </summary>
 /// <param name="request"></param>
 /// <param name="callback"></param>
 /// <param name="state"></param>
 internal unsafe void Write(UVStreamHandle server, Action <UVWriteRequest, Int32, UVException, object> callback, object state)
 {
     try
     {
         this.mWriteCallback      = callback;
         this.mWriteCallbackState = state;
         UVIntrop.write(this, server, this.mBuffer, this.mOffset, mOnWrite);
     }
     catch
     {
         this.UnpinGCHandles();
         throw;
     }
 }
Example #10
0
        private unsafe static void UVReadCb(IntPtr handle, int nread, ref UVIntrop.uv_buf_t buf)
        {
            UVException ex;

            UVIntrop.Check(nread, out ex);

            UVStreamHandle target = FromIntPtr <UVStreamHandle>(handle);

            if (target == null)
            {
                throw new UVException("流已释放");
            }
            target.mReadCallback(target, nread, ex, ref buf, target.mReadCallbackState);
        }
Example #11
0
        public void Connect(UVTCPHandle tcp, string ip, Int32 port, Action <UVConnectRquest, Int32, UVException, object> callback, object state)
        {
            this.mCallback = callback;
            this.mState    = state;
            UVException ex;
            SockAddr    addr;

            UVIntrop.ip4_addr(ip, port, out addr, out ex);
            if (ex != null)
            {
                throw ex;
            }

            UVIntrop.tcp_connect(this, tcp, ref addr, UVConnectcb);
        }
Example #12
0
        //todo:重载各种write

        #region  Callback
        private static void UVConnectionCb(IntPtr server, Int32 status)
        {
            UVException error;

            UVIntrop.Check(status, out error);
            UVTCPHandle handle = UVMemory.FromIntPtr <UVTCPHandle>(server);

            try
            {
                handle.mConnectionCallback(handle, status, error, handle.mConnectionCallbackState);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Example #13
0
        private unsafe static void UVWriteCb(IntPtr reqHandle, Int32 status)
        {
            var req = FromIntPtr <UVWriteRequest>(reqHandle);

            UVException error = null;

            if (status < 0)
            {
                UVIntrop.Check(status, out error);
            }

            try
            {
                req.RaiseSended(status, error);
            }
            catch
            {
                throw;
            }
        }
Example #14
0
        private unsafe bool TryEnqueue(ArraySegment <byte> item, out bool conflict)
        {
            conflict = false;
            int currentCount = mOffset;

            if (currentCount >= QUEUE_SIZE)
            {
                return(false);
            }

            if (Interlocked.CompareExchange(ref mOffset, currentCount + 1, currentCount) != currentCount)
            {
                conflict = true;
                return(false);
            }

            //添加
            var gcHandle = GCHandle.Alloc(item.Array, GCHandleType.Pinned);

            mPins[currentCount]        = gcHandle;
            this.mBuffer[currentCount] = UVIntrop.buf_init(gcHandle.AddrOfPinnedObject() + item.Offset, item.Count);

            return(true);
        }
Example #15
0
 public UVLoopHandle()
 {
     CreateHandle(UVIntrop.loop_size());
     UVIntrop.loop_init(this);
 }
Example #16
0
 public void Stop()
 {
     UVIntrop.idle_stop(this);
 }
Example #17
0
 public void Start(Action <UVIdleHandle> callback)
 {
     this.mCallback = callback;
     UVIntrop.idle_start(this, mIdleCallback);
 }
Example #18
0
 public UVIdleHandle(UVLoopHandle loop)
 {
     this.CreateHandle(UVHandleType.IDLE);
     UVIntrop.idle_init(loop, this);
 }
Example #19
0
 protected void CreateHandle(UVHandleType type)
 {
     CreateMemory(UVIntrop.handle_size(type));
 }
Example #20
0
 protected void CreateRequest(UVRequestType type)
 {
     CreateMemory(UVIntrop.req_size(type));
 }
Example #21
0
 /// <summary>
 /// 接收
 /// </summary>
 /// <param name="handle"></param>
 public void Accept(UVTCPHandle handle)
 {
     UVIntrop.accept(this, handle);
 }
Example #22
0
 /// <summary>
 /// 开始监听
 /// </summary>
 /// <param name="backlog"></param>
 /// <param name="connectionCallback"></param>
 /// <param name="state"></param>
 public void Listen(int backlog, Action <UVTCPHandle, Int32, UVException, Object> connectionCallback, object state)
 {
     mConnectionCallbackState = state;
     mConnectionCallback      = connectionCallback;
     UVIntrop.listen(this, backlog, mOnConnection);
 }
Example #23
0
 public void ReadStop()
 {
     UVIntrop.read_stop(this);
 }
Example #24
0
 /// <summary>
 /// 停止
 /// </summary>
 public void Stop()
 {
     UVIntrop.stop(this);
 }
Example #25
0
 public void NoDelay(bool enable)
 {
     UVIntrop.tcp_nodelay(this, enable);
 }
Example #26
0
 /// <summary>
 /// 循环关闭
 /// </summary>
 public void LoopClose()
 {
     UVIntrop.loop_close(this);
 }
Example #27
0
 public UVTCPHandle(UVLoopHandle loop)
 {
     CreateHandle(UVHandleType.TCP);
     UVIntrop.tcp_init(loop, this);
 }