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(); } }
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); }
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); }
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); } }
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); } }
protected override bool ReleaseHandle() { IntPtr memory = handle; if (memory != IntPtr.Zero) { UVIntrop.close(memory, mDestroyMemory); handle = IntPtr.Zero; } return(true); }
/// <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); }
/// <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; } }
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); }
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); }
//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; } }
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; } }
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); }
public UVLoopHandle() { CreateHandle(UVIntrop.loop_size()); UVIntrop.loop_init(this); }
public void Stop() { UVIntrop.idle_stop(this); }
public void Start(Action <UVIdleHandle> callback) { this.mCallback = callback; UVIntrop.idle_start(this, mIdleCallback); }
public UVIdleHandle(UVLoopHandle loop) { this.CreateHandle(UVHandleType.IDLE); UVIntrop.idle_init(loop, this); }
protected void CreateHandle(UVHandleType type) { CreateMemory(UVIntrop.handle_size(type)); }
protected void CreateRequest(UVRequestType type) { CreateMemory(UVIntrop.req_size(type)); }
/// <summary> /// 接收 /// </summary> /// <param name="handle"></param> public void Accept(UVTCPHandle handle) { UVIntrop.accept(this, handle); }
/// <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); }
public void ReadStop() { UVIntrop.read_stop(this); }
/// <summary> /// 停止 /// </summary> public void Stop() { UVIntrop.stop(this); }
public void NoDelay(bool enable) { UVIntrop.tcp_nodelay(this, enable); }
/// <summary> /// 循环关闭 /// </summary> public void LoopClose() { UVIntrop.loop_close(this); }
public UVTCPHandle(UVLoopHandle loop) { CreateHandle(UVHandleType.TCP); UVIntrop.tcp_init(loop, this); }