public RpcTcpBufferPool(int capacity, Func <T> allocator) { // // 所有的capacity都是2的幂 IICAssert.IsTrue(capacity < 1024 * 1024); _capacity = NumberUtils.NextPower2(capacity); _nodes = new CachedNode[_capacity]; _capacityMask = _capacity - 1; _count = _capacity; // // 填充满所有对象 for (int i = 0; i < _capacity; i++) { var node = new CachedNode() { Value = allocator(), Fetched = 0, Index = i, }; node.Value.CachedIndex = i; _nodes[i] = node; } }
public void FillNext(byte[] buffer, int offset, int count) { IICAssert.IsTrue(IdentityNeed == 0 && count <= NextRecvSize); // // 如果没接受完Header, copy到Header Buffer, 否则copy到Body Buffer // // 包头剩余长度 int headerLeft = NextRecvSize - BodyLength; if (headerLeft > 0) { int copySize = count >= headerLeft ? headerLeft : count; int copyOffset = HeaderLength - headerLeft; Array.Copy(buffer, offset, _headerBuffer, copyOffset, copySize); offset += headerLeft; count -= headerLeft; NextRecvSize -= copySize; } // // 如果有包体需要接收, 并且还有剩余缓冲区 if (BodyLength > 0 && count > 0) { int bodyLeft = NextRecvSize; int copySize = count >= bodyLeft ? bodyLeft : count; int copyOffset = BodyLength - bodyLeft; Array.Copy(buffer, offset, _bodyBuffer, copyOffset, copySize); NextRecvSize -= copySize; } }
public ObserverDelegate(string name, Func <List <ObserverItem> > obsereProc, Action clearProc) { IICAssert.IsTrue(obsereProc != null); IICAssert.IsTrue(clearProc != null); _name = name; _obsereProc = obsereProc; _clearProc = clearProc; _clearTime = DateTime.Now; }
public void FillIdentity(byte[] buffer, int offset, int count) { IICAssert.IsTrue(count <= IdentityNeed); int copySize = count == IdentityNeed ? IdentityNeed : count; int copyOffset = IdentityLength - IdentityNeed; Array.Copy(buffer, offset, _identityBuffer, copyOffset, copySize); IdentityNeed -= copySize; if (IdentityNeed == 0) { ParseIdentity(_identityBuffer, offset); } }
/// <summary> /// 创建一个数据包, 首次调用的buffer长度应当小于等于IdentityLength /// </summary> /// <param name="buffer"></param> /// <param name="offset"></param> /// <param name="count">小于等于IdentityLength</param> public RpcTcpPacket(byte[] buffer, int offset, int count) { IICAssert.IsTrue(count <= IdentityLength); if (count == IdentityLength) { IdentityNeed = 0; ParseIdentity(buffer, offset); } else { _identityBuffer = new byte[IdentityLength]; Array.Copy(buffer, offset, _identityBuffer, 0, count); IdentityNeed = IdentityLength - count; } }