public byte[] Next(int maxQuantSize) { dataLeft = (int)(currentStream.Length - currentStream.Position); var actualHeadSize = got1Sended ? DefaultHeadSize : (DefaultHeadSize + 4); var head = new QuantumHead { length = (ushort)Math.Min(actualHeadSize+dataLeft,maxQuantSize), msgId = msgId, type = got1Sended ? QuantumType.Data : QuantumType.Start, }; byte[] Quant = new byte[head.length]; //Head head.SetToArray (Quant, 0, DefaultHeadSize); //Length if start quant if (!got1Sended) Array.Copy(BitConverter.GetBytes(dataLeft),0,Quant,DefaultHeadSize,4); //data currentStream.Read (Quant, actualHeadSize, head.length-actualHeadSize); got1Sended = true; dataLeft -= (Quant.Length - actualHeadSize); return Quant; }
public bool Collect(QuantumHead head, byte[] packetFromAStream, int headStart) { lastTS = DateTime.Now; int bodyStart = headStart + DefaultHeadSize; int bodyLen = head.length - DefaultHeadSize; if(stream == null) { if (head.type == QuantumType.Start) { lenght= BitConverter.ToInt32 (packetFromAStream, bodyStart ); stream = new MemoryStream (lenght); stream.Write (packetFromAStream, bodyStart + 4, bodyLen - 4); } else//Stream is null and its mean Error return true; } else if (head.type == QuantumType.Data) { stream.Write (packetFromAStream, bodyStart, bodyLen); } else { stream = null; return true; } if (stream.Length == lenght) return true; if (stream.Length < lenght) return false; stream = null; return true; }
void handle(QuantumHead head, byte[] msgFromStream, int quantBeginOffset){ LightCollector c = null; if (collectors.ContainsKey (head.msgId)) c = collectors [head.msgId]; else { c = new LightCollector (); collectors.Add (head.msgId, c); } if (c.Collect (head, msgFromStream, quantBeginOffset)) { // we have got a new light message! var stream = c.GetLightMessageStream (); collectors.Remove (head.msgId); if (stream != null) { stream.Position = 0; if (OnLightMessage != null) OnLightMessage (this, head, stream); } else { //Oops. An Error has occured during message collecting. if (OnCollectingError != null) { byte[] badArray = new byte[msgFromStream.Length - quantBeginOffset]; Array.Copy (msgFromStream, quantBeginOffset, badArray, 0, badArray.Length); OnCollectingError (this, head, badArray); } } } }