public void TestDequeue() { Node testNode1 = new Node(100); Node testNode2 = new Node(999); SQueue queue = new SQueue(testNode1); queue.Enqueue(testNode2); Assert.True(queue.Dequeue() == testNode1 && queue.Peek() == testNode2); }
static void Main(string[] args) { SQueue queue = MakeQueue(5, 50, 5); Console.WriteLine("Initial queue"); ViewQueue(queue); queue.Enqueue(queue.Dequeue()); Console.WriteLine("\nQueue after moving the front to the rear"); ViewQueue(queue); Console.ReadKey(); }
private void SpreadReadyPackage(int codec, byte[] data) { qrpe.Enqueue(new RPE() { Codec = codec, Data = data }); lock (lock_threadIsStarted) { if (threadIsStarted) { return; } threadIsStarted = true; } Action spreadAction = () => { RPE rpe = null; while (true) { lock (lock_threadIsStarted) { rpe = qrpe.Dequeue(); if (rpe == null) { threadIsStarted = false; return; } } this.packetParser(rpe.Codec, rpe.Data); } }; Task.Run(spreadAction); }
public void PacketAnalizator(bool recursiveSelfCall) { //declaring finalization function, //result of function shows either we should stay in procedure or go out Func <bool> aFinalization = () => { bool leftInQueue = false; //finalization lock (lockObjPacketAnalizatorIsSleeping) { if (MessageQueue.Count > 0) { leftInQueue = true; } else { this.packetAnalizatorIsSleeping = true; return(true); } } if (leftInQueue) { this.PacketAnalizator(true); return(true); } return(false); }; //Sending from packet analizator to parser Action <int, byte[]> aParsePackage = (codec, data) => { if (this.packetParser != null) { this.SpreadReadyPackage(codec, data); } }; if (!recursiveSelfCall) { lock (lockObjPacketAnalizatorIsSleeping) { if (!this.packetAnalizatorIsSleeping) { return; } this.packetAnalizatorIsSleeping = false; } } bool toExit = false; byte[] dequeued = null; if (MessageQueue.Count > 0) { dequeued = MessageQueue.Dequeue(); //special hack for those who wants to get extra full byte incoming visible. First will be called Action, then it will be EndInvoked, then we could call Callback. but in this case //our Action makes everything, so - no callBacks. if (PublishDequeued != null) { Task.Run(() => PublishDequeued(dequeued)); } if (workingArray == null) { this.package = this.package.Concat(dequeued); } else { if (workingArrayFilled + dequeued.Length <= workingArray.Length) { workingArray.CopyInside(workingArrayFilled, dequeued, 0, dequeued.Length); workingArrayFilled += dequeued.Length; } else { workingArray.CopyInside(workingArrayFilled, dequeued, 0, workingArray.Length - workingArrayFilled); this.package = this.package.Concat(dequeued.Substring(workingArray.Length - workingArrayFilled)); workingArrayFilled += workingArray.Length - workingArrayFilled; } } //normally this package can't be null and length must be always more than 1 if ((this.package != null && this.package.Length > this._maxPayLoad)) { this.DestroySelf?.Invoke(); return; } } else { if (this.package == null && this.workingArray == null) { toExit = true; } else { if (this.package == null || this.package.Length < 1) { if (this.workingArray == null || this.workingArray.Length < 1) { toExit = true; } } } } //security check if (toExit) { if (aFinalization.Invoke()) { return; } } //{0;0}{0;0}{0;0;0;0}{....} //pId - 2 byte device authentificator; codec - 2 bytes; codec length - 4 bytes; data; code; codec length; data... //package binder if (this.newPacket) { if (this.package.Length < 2) { //not enough length go on collect if (aFinalization.Invoke()) { return; } } else { if (dId == -1 && DeviceShouldSendAuthorisationBytesBeforeProceedCodec) { if (UseBigEndian) { dId = this.package.Substring(0, 2).To_UInt16_BigEndian(); } else { dId = this.package.Substring(0, 2).To_UInt16_LittleEndian(); } if (ToSendToParserAuthenticationBytes) { //sending authentification codec to parser aParsePackage(dId, null); } this.package = this.package.Substring(2, this.package.Length); if (this.package == null) { if (aFinalization.Invoke()) { return; } } } } if (this.package.Length < 6) { //not enough length go on collect if (aFinalization.Invoke()) { return; } } //Console.WriteLine("> " + this.package.Substring(0, 2).ToBytesString(" ") + " " + this.package.Substring(2, 4).ToBytesString(" ")); //getting codec 2 bytes if (UseBigEndian) { codec = this.package.Substring(0, 2).To_UInt16_BigEndian(); this.newPacketSize = ((int)this.package.Substring(2, 4).To_UInt32_BigEndian()) + ProtocolExtensionLengthV1; } else { codec = this.package.Substring(0, 2).To_UInt16_LittleEndian(); this.newPacketSize = ((int)this.package.Substring(2, 4).To_UInt32_LittleEndian()) + ProtocolExtensionLengthV1; } this.newPacket = false; if (this.newPacketSize > this._maxPayLoad || this.newPacketSize < 0) { //if (this.DestroySelf != null) // this.DestroySelf(); this.DestroySelf?.Invoke(); return; } //this.cutOff = this.newPacketSize + 6; workingArray = new byte[this.newPacketSize]; if (this.package.Length >= 6) { if (this.package.Length - 6 < this.newPacketSize) { workingArray.CopyInside(0, this.package, 6, this.package.Length - 6); workingArrayFilled = this.package.Length - 6; this.package = null; } else { workingArray.CopyInside(0, this.package, 6, this.newPacketSize); workingArrayFilled = this.newPacketSize; this.package = this.package.Substring(this.newPacketSize + 6); } } } if (workingArrayFilled >= this.newPacketSize) { aParsePackage(codec, workingArray.Substring(0, this.newPacketSize)); workingArray = null; workingArrayFilled = 0; this.newPacket = true; if (MessageQueue.Count > 0) { this.PacketAnalizator(true); return; } else if (this.package != null) { if (this.package.Length > 0) { this.PacketAnalizator(true); return; } } if (aFinalization.Invoke()) { return; } } if (aFinalization.Invoke()) { return; } } //eo function