using System; using Microsoft.SPOT; namespace miniCPF { class ByteQueue { private static byte[][] buffer; private static byte[] returnValue; private static int head; private static int tail; private static int count; private static int capacity; private const int defaultCapacity = 512; private const int defaultMessageLength = 256; public ByteQueue() { buffer = new byte[defaultCapacity][]; for (int i = 0; i < defaultCapacity; i++) { buffer[i] = new byte[defaultMessageLength]; } returnValue = new byte[defaultMessageLength]; capacity = defaultCapacity; head = 0; tail = 0; count = 0; } public int Count { get { return count; } } public void Clear() { count = 0; tail = head; } public void Enqueue(byte[] value) { //if (count == capacity) //{ // Grow(); //} //buffer[head] = value; if (count >= capacity) { throw new InvalidOperationException("Queue is full"); } else { Array.Copy(value, buffer[head], value.Length); head = (head + 1) % capacity; count++; } } public byte[] Dequeue() { Array.Clear(returnValue, 0, returnValue.Length); if (count == 0) { throw new InvalidOperationException("Queue is empty"); } else { // byte value = buffer[tail]; Array.Copy(buffer[tail], returnValue, buffer[tail].Length); tail = (tail + 1) % capacity; count--; return returnValue; } } //public int Dequeue(byte[] bytes) //{ // int maxBytes = System.Math.Min(count, bytes.Length); // for (int i = 0; i < maxBytes; i++) // { // bytes[i] = Dequeue(); // } // return maxBytes; //} //private void Grow() //{ // int newCapacity = capacity << 1; // byte[] newBuffer = new byte[newCapacity]; // if (tail < head) // { // Array.Copy(buffer, tail, newBuffer, 0, count); // } // else // { // Array.Copy(buffer, tail, newBuffer, 0, capacity - tail); // Array.Copy(buffer, 0, newBuffer, capacity - tail, head); // } // buffer = newBuffer; // head = count; // tail = 0; // capacity = newCapacity; //} } }