void HandleData(NetIncomingMessage message) { ushort id = ushort.MaxValue; ByteBuffer byteBuffer = null; try { id = message.ReadUInt16(); ushort len = message.ReadUInt16(); byteBuffer = ByteBufferPool.Alloc(len); message.ReadBytes(byteBuffer.Data, 0, len); var result = dispatcher.Fire(message.SenderConnection, (MessageID)id, byteBuffer, message); if (result != MessageHandleResult.Processing) { ByteBufferPool.Dealloc(ref byteBuffer); } } catch (Exception e) { ByteBufferPool.Dealloc(ref byteBuffer); NetLog.Exception("HandleData throws exception", e); if (id != ushort.MaxValue) { NetLog.Error("Caught exception when processing message " + (MessageID)id); } else { NetLog.Error("Caught exception when processing message"); } } }
static void ByteBufferPoolTest() { for (int i = 0; i < ByteBufferPool.sPool.Length; ++i) { var p = ByteBufferPool.sPool[i]; Debug.Assert(p.Count == ByteBufferPool.initialArraySize); for (int j = 0; j < p.Count; ++j) { Debug.Assert(p[j].Data.Length == 1 << (i + ByteBufferPool.minSizePOT)); } } int min = 1 << ByteBufferPool.minSizePOT; List <ByteBuffer> buffers = new List <ByteBuffer>(); buffers.Add(ByteBufferPool.Alloc(0)); buffers.Add(ByteBufferPool.Alloc(1)); buffers.Add(ByteBufferPool.Alloc(min - 1)); buffers.Add(ByteBufferPool.Alloc(min)); buffers.Add(ByteBufferPool.Alloc(min + 1)); var pool_64 = ByteBufferPool.sPool[ByteBufferPool.sIndexLookup[1 << ByteBufferPool.minSizePOT]]; Debug.Assert(ByteBufferPool.AvailableSlots(pool_64) == 0); var pool_128 = ByteBufferPool.sPool[ByteBufferPool.sIndexLookup[1 << (ByteBufferPool.minSizePOT + 1)]]; Debug.Assert(ByteBufferPool.AvailableSlots(pool_128) == ByteBufferPool.initialArraySize - 1); buffers.Add(ByteBufferPool.Alloc(min)); Debug.Assert(ByteBufferPool.AvailableSlots(pool_64) == ByteBufferPool.initialArraySize - 1); for (int i = 0; i < buffers.Count; ++i) { ByteBuffer bb = buffers[i]; ByteBufferPool.Dealloc(ref bb); Debug.Assert(null == bb); } Debug.Assert(ByteBufferPool.AvailableSlots(pool_64) == ByteBufferPool.initialArraySize * 2); Debug.Assert(ByteBufferPool.AvailableSlots(pool_128) == ByteBufferPool.initialArraySize); try { ByteBufferPool.Alloc((1 << ByteBufferPool.maxSizePOT) + 1); Debug.Assert(false); } catch (ArgumentOutOfRangeException) { } ByteBuffer wrongsize = new ByteBuffer(new byte[123]); ByteBufferPool.Dealloc(ref wrongsize); Debug.Assert(null != wrongsize); ByteBuffer exceed = new ByteBuffer(new byte[1 << ByteBufferPool.minSizePOT]); ByteBufferPool.Dealloc(ref exceed); Debug.Assert(null != exceed); Console.WriteLine("ByteBuffer Pool Checked"); }
public void FullUpdate(Msg_SC_Snapshot snapshot) { mServerTick = snapshot.TickNow; mSimulateTicks = 0; mTickObjects.FullUpdate(new TickObjectDictionary.TickObjectEnumerator(snapshot)); mHasFullUpdated = true; if (null != mProcessing) { ByteBufferPool.Dealloc(ref mProcessing); } while (mCachedSnapshots.Count > 0) { ByteBuffer bb = mCachedSnapshots.Dequeue(); ByteBufferPool.Dealloc(ref bb); } }
void Simulate() { if (null == mProcessing && mCachedSnapshots.Count < cacheSnapshots) { return; } if (null == mProcessing) { mProcessing = mCachedSnapshots.Dequeue(); } int simulateCount = 1; uint sot = Game.Instance.snapshotOverTick; if (mCachedSnapshots.Count > cacheSnapshots - 1) { uint k = (uint)mCachedSnapshots.Count; uint ticksToSimulate = (k + 1) * sot - mSimulateTicks; uint ticksSupposeToSimulate = cacheSnapshots * sot - mSimulateTicks; simulateCount = Mathf.Max(1, Mathf.FloorToInt((float)ticksToSimulate / ticksSupposeToSimulate)); } Msg_SC_Snapshot ss = InstancePool.Get <Msg_SC_Snapshot>(); Msg_SC_Snapshot.GetRootAsMsg_SC_Snapshot(mProcessing, ss); for (int i = 0; i < simulateCount; ++i) { mServerTick = ss.TickNow - sot + mSimulateTicks; float nt = (float)(mSimulateTicks + 1) / sot; mTickObjects.Simulate(nt, new TickObjectDictionary.TickObjectEnumerator(ss)); ++mSimulateTicks; ++mServerTick; if (mSimulateTicks >= sot) { mSimulateTicks = 0; ByteBufferPool.Dealloc(ref mProcessing); if (mCachedSnapshots.Count <= 0) { break; } mProcessing = mCachedSnapshots.Dequeue(); Msg_SC_Snapshot.GetRootAsMsg_SC_Snapshot(mProcessing, ss); } } }
void HandleData(NetIncomingMessage message) { MessageID id = (MessageID)message.ReadUInt16(); ushort len = message.ReadUInt16(); ByteBuffer byteBuffer = ByteBufferPool.Alloc(len); try { message.ReadBytes(byteBuffer.Data, 0, len); var result = dispatcher.Fire(message.SenderConnection, id, byteBuffer, message); if (result != MessageHandleResult.Processing) { ByteBufferPool.Dealloc(ref byteBuffer); } } catch (Exception e) { ByteBufferPool.Dealloc(ref byteBuffer); NetLog.Exception("HandleData throws exception", e); } }