internal ChunkingWriter(Message originalMessage,TimeoutHelper chunkingTimeout, IOutputChannel outputChannel)
     : base()
 {
     this.version = originalMessage.Version;
     this.originalMessage = originalMessage;
     this.chunkingTimeout = chunkingTimeout;
     this.outputChannel = outputChannel;
     this.startState = new StartChunkState();
     chunkNum = 1;
 }
 internal ChunkingReader(Message startMessage, int maxBufferedChunks, TimeoutHelper receiveTimeout)
 {
     //set innerReader
     this.innerReader = startMessage.GetReaderAtBodyContents();
     this.lockobject = new object();
     this.messageId = ChunkingUtils.GetMessageHeader<Guid>(startMessage,
         ChunkingUtils.MessageIdHeader, ChunkingUtils.ChunkNs);
     this.bufferedChunks = new SynchronizedQueue<Message>(maxBufferedChunks);
     this.receiveTimeout = receiveTimeout;
     this.nextChunkNum = 1;
 }
        private void GetReaderFromNextChunk(TimeoutHelper timeouthelper)
        {
            Message chunk = null;
            if(this.bufferedChunks.TryDequeue(timeouthelper,out chunk))
            {
                int chunkNum = chunk.Headers.GetHeader<int>(ChunkingUtils.ChunkNumberHeader, ChunkingUtils.ChunkNs);
                Console.WriteLine(" < Received chunk {0} of message {1}", chunkNum, this.messageId);
                if (chunkNum != nextChunkNum)
                {
                    throw new CommunicationException(String.Format("Received chunk number {0} but expected chunk number {1}", chunkNum, nextChunkNum));
                }
                this.isLastChunk = (chunk.Headers.FindHeader(ChunkingUtils.ChunkingEndHeader, ChunkingUtils.ChunkNs) > -1);
                this.innerReader = chunk.GetReaderAtBodyContents();
                if (this.isLastChunk)
                {
                    this.innerReader.ReadStartElement(); //operation
                    this.innerReader.ReadStartElement(); //parameter
                }
                else
                {
                    //position reader at Base64 content
                    this.innerReader.ReadStartElement(ChunkingUtils.ChunkElement, ChunkingUtils.ChunkNs);
                }
                lock (lockobject)
                {
                    nextChunkNum++;
                }

            }
            else
            {
                throw new TimeoutException(String.Format("ChunkingReader timed out while waiting for chunk message number {0}", nextChunkNum));
            }
        }