123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- namespace SuperSocket.Common
- {
-
-
-
- public sealed class SendingQueue : IList<ArraySegment<byte>>
- {
- private readonly int m_Offset;
- private readonly int m_Capacity;
- private int m_CurrentCount = 0;
- private ArraySegment<byte>[] m_GlobalQueue;
- private static ArraySegment<byte> m_Null = default(ArraySegment<byte>);
- private int m_UpdatingCount;
- private bool m_ReadOnly = false;
- private ushort m_TrackID = 1;
- private int m_InnerOffset = 0;
-
-
-
-
-
-
- public ushort TrackID
- {
- get { return m_TrackID; }
- }
-
-
-
-
-
-
- public SendingQueue(ArraySegment<byte>[] globalQueue, int offset, int capacity)
- {
- m_GlobalQueue = globalQueue;
- m_Offset = offset;
- m_Capacity = capacity;
- }
- private bool TryEnqueue(ArraySegment<byte> item, out bool conflict, ushort trackID)
- {
- conflict = false;
- var oldCount = m_CurrentCount;
- if (oldCount >= Capacity)
- return false;
- if (m_ReadOnly)
- return false;
- if (trackID != m_TrackID)
- return false;
- int compareCount = Interlocked.CompareExchange(ref m_CurrentCount, oldCount + 1, oldCount);
-
- if (compareCount != oldCount)
- {
- conflict = true;
- return false;
- }
- m_GlobalQueue[m_Offset + oldCount] = item;
- return true;
- }
-
-
-
-
-
-
- public bool Enqueue(ArraySegment<byte> item, ushort trackID)
- {
- if (m_ReadOnly)
- return false;
- Interlocked.Increment(ref m_UpdatingCount);
- while (!m_ReadOnly)
- {
- bool conflict = false;
- if (TryEnqueue(item, out conflict, trackID))
- {
- Interlocked.Decrement(ref m_UpdatingCount);
- return true;
- }
-
- if (!conflict)
- break;
- }
- Interlocked.Decrement(ref m_UpdatingCount);
- return false;
- }
-
-
-
-
-
-
- public bool Enqueue(IList<ArraySegment<byte>> items, ushort trackID)
- {
- if (m_ReadOnly)
- return false;
- Interlocked.Increment(ref m_UpdatingCount);
- bool conflict;
- while (!m_ReadOnly)
- {
- if (TryEnqueue(items, out conflict, trackID))
- {
- Interlocked.Decrement(ref m_UpdatingCount);
- return true;
- }
- if (!conflict)
- break;
- }
- Interlocked.Decrement(ref m_UpdatingCount);
- return false;
- }
- private bool TryEnqueue(IList<ArraySegment<byte>> items, out bool conflict, ushort trackID)
- {
- conflict = false;
- var oldCount = m_CurrentCount;
- int newItemCount = items.Count;
- int expectedCount = oldCount + newItemCount;
- if (expectedCount > Capacity)
- return false;
- if (m_ReadOnly)
- return false;
- if (m_TrackID != trackID)
- return false;
- int compareCount = Interlocked.CompareExchange(ref m_CurrentCount, expectedCount, oldCount);
- if (compareCount != oldCount)
- {
- conflict = true;
- return false;
- }
- var queue = m_GlobalQueue;
- for (var i = 0; i < items.Count; i++)
- {
- queue[m_Offset + oldCount + i] = items[i];
- }
- return true;
- }
-
-
-
- public void StopEnqueue()
- {
- if (m_ReadOnly)
- return;
- m_ReadOnly = true;
- if (m_UpdatingCount <= 0)
- return;
- var spinWait = new SpinWait();
- spinWait.SpinOnce();
-
- while (m_UpdatingCount > 0)
- {
- spinWait.SpinOnce();
- }
- }
-
-
-
- public void StartEnqueue()
- {
- m_ReadOnly = false;
- }
-
-
-
-
-
-
- public ArraySegment<byte>[] GlobalQueue
- {
- get { return m_GlobalQueue; }
- }
-
-
-
-
-
-
- public int Offset
- {
- get { return m_Offset; }
- }
-
-
-
-
-
-
- public int Capacity
- {
- get { return m_Capacity; }
- }
-
-
-
-
- public int Count
- {
- get { return m_CurrentCount - m_InnerOffset; }
- }
-
-
-
-
-
-
- public int Position { get; set; }
-
-
-
-
-
-
-
-
- public int IndexOf(ArraySegment<byte> item)
- {
- throw new NotSupportedException();
- }
-
-
-
-
-
-
- public void Insert(int index, ArraySegment<byte> item)
- {
- throw new NotSupportedException();
- }
-
-
-
-
-
- public void RemoveAt(int index)
- {
- throw new NotSupportedException();
- }
-
-
-
-
-
-
- public ArraySegment<byte> this[int index]
- {
- get
- {
- var targetIndex = m_Offset + m_InnerOffset + index;
- var value = m_GlobalQueue[targetIndex];
- if (value.Array != null)
- return value;
- var spinWait = new SpinWait();
- while (true)
- {
- spinWait.SpinOnce();
- value = m_GlobalQueue[targetIndex];
- if (value.Array != null)
- return value;
- if (spinWait.Count > 50)
- return value;
- }
- }
- set
- {
- throw new NotSupportedException();
- }
- }
-
-
-
-
-
- public void Add(ArraySegment<byte> item)
- {
- throw new NotSupportedException();
- }
-
-
-
-
- public void Clear()
- {
- if (m_TrackID >= ushort.MaxValue)
- m_TrackID = 1;
- else
- m_TrackID++;
-
- for (var i = 0; i < m_CurrentCount; i++)
- {
- m_GlobalQueue[m_Offset + i] = m_Null;
- }
- m_CurrentCount = 0;
- m_InnerOffset = 0;
- Position = 0;
- }
-
-
-
-
-
-
-
-
- public bool Contains(ArraySegment<byte> item)
- {
- throw new NotSupportedException();
- }
-
-
-
-
-
- public void CopyTo(ArraySegment<byte>[] array, int arrayIndex)
- {
- for (var i = 0; i < Count; i++)
- {
- array[arrayIndex + i] = this[i];
- }
- }
-
-
-
-
- public bool IsReadOnly
- {
- get { return m_ReadOnly; }
- }
-
-
-
-
-
-
-
-
- public bool Remove(ArraySegment<byte> item)
- {
- throw new NotSupportedException();
- }
-
-
-
-
-
-
-
- public IEnumerator<ArraySegment<byte>> GetEnumerator()
- {
- for (var i = 0; i < (m_CurrentCount - m_InnerOffset); i++)
- {
- yield return m_GlobalQueue[m_Offset + m_InnerOffset + i];
- }
- }
-
-
-
-
-
-
-
- System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
-
-
-
-
- public void InternalTrim(int offset)
- {
- var innerCount = m_CurrentCount - m_InnerOffset;
- var subTotal = 0;
- for (var i = m_InnerOffset; i < innerCount; i++)
- {
- var segment = m_GlobalQueue[m_Offset + i];
- subTotal += segment.Count;
- if (subTotal <= offset)
- continue;
- m_InnerOffset = i;
- var rest = subTotal - offset;
- m_GlobalQueue[m_Offset + i] = new ArraySegment<byte>(segment.Array, segment.Offset + segment.Count - rest, rest);
- break;
- }
- }
- }
-
-
-
- public class SendingQueueSourceCreator : ISmartPoolSourceCreator<SendingQueue>
- {
- private int m_SendingQueueSize;
-
-
-
-
- public SendingQueueSourceCreator(int sendingQueueSize)
- {
- m_SendingQueueSize = sendingQueueSize;
- }
-
-
-
-
-
-
- public ISmartPoolSource Create(int size, out SendingQueue[] poolItems)
- {
- var source = new ArraySegment<byte>[size * m_SendingQueueSize];
- poolItems = new SendingQueue[size];
- for (var i = 0; i < size; i++)
- {
- poolItems[i] = new SendingQueue(source, i * m_SendingQueueSize, m_SendingQueueSize);
- }
- return new SmartPoolSource(source, size);
- }
- }
- }
|