public static ArraySegment<byte> Encode(Uri uri, ArraySegment<byte> messageBuffer, BufferManager bufferManager) { byte[] uriBuffer = UnicodeEncoding.Unicode.GetBytes(uri.ToString()); byte[] uriLengthBuffer = EncodeInt(uriBuffer.Length); byte[] payloadLengthBuffer = EncodeInt(messageBuffer.Count); // Encode the following fields: // Uri length (4 bytes) // Uri (size specified by uri length) // Payload length (4 bytes) // Payload (size specified by payload length) byte[] buffer = bufferManager.TakeBuffer(uriLengthBuffer.Length + uriBuffer.Length + payloadLengthBuffer.Length + messageBuffer.Count); int destOffset = 0; Buffer.BlockCopy(uriLengthBuffer, 0, buffer, destOffset, uriLengthBuffer.Length); destOffset += uriLengthBuffer.Length; Buffer.BlockCopy(uriBuffer, 0, buffer, destOffset, uriBuffer.Length); destOffset += uriBuffer.Length; Buffer.BlockCopy(payloadLengthBuffer, 0, buffer, destOffset, payloadLengthBuffer.Length); destOffset += payloadLengthBuffer.Length; Buffer.BlockCopy(messageBuffer.Array, messageBuffer.Offset, buffer, destOffset, messageBuffer.Count); bufferManager.ReturnBuffer(messageBuffer.Array); return new ArraySegment<byte>(buffer); }
public static IObservable<DisposableByteBuffer> ToFrameStreamObservable(this Stream stream, BufferManager bufferManager) { return Observable.Create<DisposableByteBuffer>(async (observer, token) => { var headerBuffer = new byte[sizeof(int)]; try { while (!token.IsCancellationRequested) { if (await stream.ReadBytesCompletelyAsync(headerBuffer, headerBuffer.Length, token) != headerBuffer.Length) break; var length = BitConverter.ToInt32(headerBuffer, 0); var buffer = bufferManager.TakeBuffer(length); if (await stream.ReadBytesCompletelyAsync(buffer, length, token) != length) break; observer.OnNext(new DisposableByteBuffer(buffer, length, Disposable.Create(() => bufferManager.ReturnBuffer(buffer)))); } observer.OnCompleted(); } catch (Exception error) { observer.OnError(error); } }); }
/// <inheritdoc /> public override Message ReadMessage(ArraySegment<byte> buffer, BufferManager bufferManager, string contentType) { // Align internal/external types this.contentType = contentType; var msgContents = new byte[buffer.Count]; Array.Copy(buffer.Array, buffer.Offset, msgContents, 0, msgContents.Length); bufferManager.ReturnBuffer(buffer.Array); // Most interoperable to include the xml declaration this.writerSettings.OmitXmlDeclaration = false; // Save the encoding for when we write the response this.writerSettings.Encoding = msgContents.GetEncoding(contentType); var xmlDeclEncoding = msgContents.GetXmlDeclEncoding(this.writerSettings.Encoding); // Check if the two encodings align if (xmlDeclEncoding != null && xmlDeclEncoding.WebName == this.writerSettings.Encoding.WebName) { // Need to recode msgContents = Encoding.Convert(this.writerSettings.Encoding, xmlDeclEncoding, msgContents); } var stream = new MemoryStream(msgContents); return this.ReadMessage(stream, int.MaxValue); }
public override Message ReadMessage(ArraySegment<byte> buffer, BufferManager bufferManager, string contentType) { var msgContents = new byte[buffer.Count]; Array.Copy(buffer.Array, buffer.Offset, msgContents, 0, msgContents.Length); bufferManager.ReturnBuffer(buffer.Array); var stream = new MemoryStream(msgContents); return ReadMessage(stream, int.MaxValue); }
/// <summary> /// This method is used to read a message from a specified buffer. /// </summary> /// <param name="buffer">Specify the buffer.</param> /// <param name="bufferManager">Specify the buffer manager. </param> /// <param name="messageContentType">Specify the content type.</param> /// <returns>Return the System.ServiceModel.Channels.Message that is read from the buffer specified.</returns> public override Message ReadMessage(ArraySegment<byte> buffer, BufferManager bufferManager, string messageContentType) { byte[] msgContents = new byte[buffer.Count]; Array.Copy(buffer.Array, buffer.Offset, msgContents, 0, msgContents.Length); bufferManager.ReturnBuffer(buffer.Array); MemoryStream stream = new MemoryStream(msgContents); stream.Position = 0; return this.ReadMessage(stream, int.MaxValue); }
public override Message ReadMessage(ArraySegment<byte> buffer, BufferManager bufferManager, string contentType) { ArraySegment<byte> data = BuildReply(buffer); bufferManager.ReturnBuffer(buffer.Array); MemoryStream stream = new MemoryStream(data.Array); return ReadMessage(stream, int.MaxValue); }
internal static void DecompressBuffer(ref ArraySegment<byte> buffer, BufferManager bufferManager, CompressionFormat compressionFormat, long maxReceivedMessageSize) { MemoryStream memoryStream = new MemoryStream(buffer.Array, buffer.Offset, buffer.Count); int maxDecompressedSize = (int)Math.Min(maxReceivedMessageSize, int.MaxValue); using (BufferManagerOutputStream bufferedOutStream = new BufferManagerOutputStream(SR.MaxReceivedMessageSizeExceeded, 1024, maxDecompressedSize, bufferManager)) { bufferedOutStream.Write(buffer.Array, 0, buffer.Offset); byte[] tempBuffer = bufferManager.TakeBuffer(DecompressBlockSize); try { using (Stream ds = compressionFormat == CompressionFormat.GZip ? (Stream)new GZipStream(memoryStream, CompressionMode.Decompress) : (Stream)new DeflateStream(memoryStream, CompressionMode.Decompress)) { while (true) { int bytesRead = ds.Read(tempBuffer, 0, DecompressBlockSize); if (bytesRead > 0) { bufferedOutStream.Write(tempBuffer, 0, bytesRead); } else { break; } } } } finally { bufferManager.ReturnBuffer(tempBuffer); } int length = 0; byte[] decompressedBytes = bufferedOutStream.ToArray(out length); bufferManager.ReturnBuffer(buffer.Array); buffer = new ArraySegment<byte>(decompressedBytes, buffer.Offset, length - buffer.Offset); } }
public override Message ReadMessage(ArraySegment<byte> buffer, BufferManager bufferManager, string contentType) { byte[] msgContents = new byte[buffer.Count]; Array.Copy(buffer.Array, buffer.Offset, msgContents, 0, msgContents.Length); bufferManager.ReturnBuffer(buffer.Array); MemoryStream stream = new MemoryStream(msgContents); XmlReader reader = XmlReader.Create(stream); XElement elm = XElement.Load(reader); reader.Close(); var msgElm = elm.Descendants().First(); XmlReader msgReader = msgElm.CreateReader(); return Message.CreateMessage(msgReader, int.MaxValue, this.MessageVersion); }
private ArraySegment <byte> AddSessionInformationToMessage(ArraySegment <byte> messageData, BufferManager bufferManager, int maxMessageSize) { int num = 0; byte[] array = messageData.Array; if (this.writerSession.HasNewStrings) { IList <XmlDictionaryString> newStrings = this.writerSession.GetNewStrings(); for (int i = 0; i < newStrings.Count; i++) { int byteCount = Encoding.UTF8.GetByteCount(newStrings[i].Value); num += IntEncoder.GetEncodedSize(byteCount) + byteCount; } int num4 = messageData.Offset + messageData.Count; int num5 = maxMessageSize - num4; if ((num5 - num) < 0) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new QuotaExceededException(System.ServiceModel.SR.GetString("MaxSentMessageSizeExceeded", new object[] { maxMessageSize }))); } int bufferSize = (messageData.Offset + messageData.Count) + num; if (array.Length < bufferSize) { byte[] dst = bufferManager.TakeBuffer(bufferSize); Buffer.BlockCopy(array, messageData.Offset, dst, messageData.Offset, messageData.Count); bufferManager.ReturnBuffer(array); array = dst; } Buffer.BlockCopy(array, messageData.Offset, array, messageData.Offset + num, messageData.Count); int num7 = messageData.Offset; for (int j = 0; j < newStrings.Count; j++) { string s = newStrings[j].Value; int num9 = Encoding.UTF8.GetByteCount(s); num7 += IntEncoder.Encode(num9, array, num7); num7 += Encoding.UTF8.GetBytes(s, 0, s.Length, array, num7); } this.writerSession.ClearNewStrings(); } int encodedSize = IntEncoder.GetEncodedSize(num); int offset = messageData.Offset - encodedSize; int count = (encodedSize + messageData.Count) + num; IntEncoder.Encode(num, array, offset); return(new ArraySegment <byte>(array, offset, count)); }
public override void Write(byte[] buffer, int offset, int size, bool immediate, TimeSpan timeout, BufferManager bufferManager) { if (size <= 0) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("size", size, SR.ValueMustBePositive)); } ThrowPendingWriteException(); if (immediate || _flushTimeout == TimeSpan.Zero) { WriteNow(buffer, offset, size, timeout, bufferManager); } else { WriteLater(buffer, offset, size, timeout); bufferManager.ReturnBuffer(buffer); } }
internal static void CompressBuffer(ref ArraySegment<byte> buffer, BufferManager bufferManager, CompressionFormat compressionFormat) { using (BufferManagerOutputStream bufferedOutStream = new BufferManagerOutputStream(SR.MaxSentMessageSizeExceeded, 1024, int.MaxValue, bufferManager)) { bufferedOutStream.Write(buffer.Array, 0, buffer.Offset); using (Stream ds = compressionFormat == CompressionFormat.GZip ? (Stream)new GZipStream(bufferedOutStream, CompressionMode.Compress, true) : (Stream)new DeflateStream(bufferedOutStream, CompressionMode.Compress, true)) { ds.Write(buffer.Array, buffer.Offset, buffer.Count); } int length = 0; byte[] compressedBytes = bufferedOutStream.ToArray(out length); bufferManager.ReturnBuffer(buffer.Array); buffer = new ArraySegment<byte>(compressedBytes, buffer.Offset, length - buffer.Offset); } }
internal static void CompressBuffer(ref ArraySegment <byte> buffer, BufferManager bufferManager, CompressionFormat compressionFormat) { using (BufferManagerOutputStream bufferedOutStream = new BufferManagerOutputStream(SR.MaxSentMessageSizeExceeded, 1024, int.MaxValue, bufferManager)) { bufferedOutStream.Write(buffer.Array, 0, buffer.Offset); using (Stream ds = compressionFormat == CompressionFormat.GZip ? (Stream) new GZipStream(bufferedOutStream, CompressionMode.Compress, true) : (Stream) new DeflateStream(bufferedOutStream, CompressionMode.Compress, true)) { ds.Write(buffer.Array, buffer.Offset, buffer.Count); } int length = 0; byte[] compressedBytes = bufferedOutStream.ToArray(out length); bufferManager.ReturnBuffer(buffer.Array); buffer = new ArraySegment <byte>(compressedBytes, buffer.Offset, length - buffer.Offset); } }
public override void Write(byte[] buffer, int offset, int size, bool immediate, TimeSpan timeout, BufferManager bufferManager) { if (size <= 0) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("size", size, System.ServiceModel.SR.GetString("ValueMustBePositive"))); } this.ThrowPendingWriteException(); if (immediate || (this.flushTimeout == 0L)) { ThreadTrace.Trace("BC:Write now"); this.WriteNow(buffer, offset, size, timeout, bufferManager); } else { ThreadTrace.Trace("BC:Write later"); this.WriteLater(buffer, offset, size, timeout); bufferManager.ReturnBuffer(buffer); } ThreadTrace.Trace("BC:Write done"); }
public override Message ReadMessage(ArraySegment<byte> buffer, BufferManager bufferManager, string contentType) { Message response = null; using (MemoryStream ms1 = new MemoryStream(buffer.Array, buffer.Offset, buffer.Count)) using (MemoryStream ms2 = new MemoryStream()) using (XmlTextWriter xml1 = new XmlTextWriter(ms2, Encoding.UTF8)) { bufferManager.ReturnBuffer(buffer.Array); XDocument.Load(ms1).Root.LastNode.WriteTo(xml1); xml1.Flush(); BufferManager bm = BufferManager.CreateBufferManager(3, (int)ms2.Length); byte[] newBuffer = bm.TakeBuffer((int)ms2.Length); Array.Copy(ms2.ToArray(), newBuffer, ms2.Length); response = _encoder.ReadMessage(new ArraySegment<byte>(newBuffer, 0, (int)ms2.Length), bm, contentType); } return response; }
protected override void Dispose(bool disposing) { if (disposing) { try { _stream?.Dispose(); } finally { _stream = null; var tempBuffer = _buffer; _buffer = null; _bufferManager?.ReturnBuffer(tempBuffer); _bufferManager = null; } } // Call base.Dispose(bool) to cleanup async IO resources base.Dispose(disposing); }
public override ArraySegment<byte> WriteMessage(Message msg, int maxMessageSize, BufferManager bufferManager, int messageOffset) { // If the message has the payload as a property, we inject it to the message buffer object property; if (msg.Properties.TryGetValue(OutOfBandPayloadProperty.Name, out property)) { OutOfBandPayloadProperty payloadProperty = (OutOfBandPayloadProperty)property; ArraySegment<byte> origMsgArray = this.InnerEncoder.WriteMessage(msg, maxMessageSize, bufferManager, messageOffset); ArraySegment<byte> payload = payloadProperty.GetPayload(); ArraySegment<byte> msgWithPayload = BinaryFormatHelper.AppendPayloadAsHeader(origMsgArray, payload, bufferManager); bufferManager.ReturnBuffer(origMsgArray.Array); return msgWithPayload; } else { return this.InnerEncoder.WriteMessage(msg, maxMessageSize, bufferManager, messageOffset); } }
void WriteNow(byte[] buffer, int offset, int size, TimeSpan timeout, BufferManager bufferManager) { lock (ThisLock) { if (pendingWriteSize > 0) { int remainingSize = writeBufferSize - pendingWriteSize; CancelFlushTimer(); if (size <= remainingSize) { Buffer.BlockCopy(buffer, offset, writeBuffer, pendingWriteSize, size); if (bufferManager != null) { bufferManager.ReturnBuffer(buffer); } pendingWriteSize += size; FlushCore(timeout); return; } else { TimeoutHelper timeoutHelper = new TimeoutHelper(timeout); FlushCore(timeoutHelper.RemainingTime()); timeout = timeoutHelper.RemainingTime(); } } if (bufferManager == null) { Connection.Write(buffer, offset, size, true, timeout); } else { Connection.Write(buffer, offset, size, true, timeout, bufferManager); } } }
public override void ReturnBuffer(byte[] buffer) { _innerBufferManager.ReturnBuffer(buffer); }
private ArraySegment<byte> AddSessionInformationToMessage(ArraySegment<byte> messageData, BufferManager bufferManager, int maxMessageSize) { int dictionarySize = 0; byte[] buffer = messageData.Array; if (_writerSession.HasNewStrings) { IList<XmlDictionaryString> newStrings = _writerSession.GetNewStrings(); for (int i = 0; i < newStrings.Count; i++) { int utf8ValueSize = Encoding.UTF8.GetByteCount(newStrings[i].Value); dictionarySize += IntEncoder.GetEncodedSize(utf8ValueSize) + utf8ValueSize; } int messageSize = messageData.Offset + messageData.Count; int remainingMessageSize = maxMessageSize - messageSize; if (remainingMessageSize - dictionarySize < 0) { string excMsg = SR.Format(SR.MaxSentMessageSizeExceeded, maxMessageSize); if (WcfEventSource.Instance.MaxSentMessageSizeExceededIsEnabled()) { WcfEventSource.Instance.MaxSentMessageSizeExceeded(excMsg); } throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new QuotaExceededException(excMsg)); } int requiredBufferSize = messageData.Offset + messageData.Count + dictionarySize; if (buffer.Length < requiredBufferSize) { byte[] newBuffer = bufferManager.TakeBuffer(requiredBufferSize); Buffer.BlockCopy(buffer, messageData.Offset, newBuffer, messageData.Offset, messageData.Count); bufferManager.ReturnBuffer(buffer); buffer = newBuffer; } Buffer.BlockCopy(buffer, messageData.Offset, buffer, messageData.Offset + dictionarySize, messageData.Count); int offset = messageData.Offset; for (int i = 0; i < newStrings.Count; i++) { string newString = newStrings[i].Value; int utf8ValueSize = Encoding.UTF8.GetByteCount(newString); offset += IntEncoder.Encode(utf8ValueSize, buffer, offset); offset += Encoding.UTF8.GetBytes(newString, 0, newString.Length, buffer, offset); } _writerSession.ClearNewStrings(); } int headerSize = IntEncoder.GetEncodedSize(dictionarySize); int newOffset = messageData.Offset - headerSize; int newSize = headerSize + messageData.Count + dictionarySize; IntEncoder.Encode(dictionarySize, buffer, newOffset); return new ArraySegment<byte>(buffer, newOffset, newSize); }
public void Write(byte[] buffer, int offset, int size, bool immediate, TimeSpan timeout, BufferManager bufferManager) { Write(buffer, offset, size, immediate, timeout); bufferManager.ReturnBuffer(buffer); }
/// <summary> /// Reads a message from a specified buffer. /// </summary> /// <param name="buffer">Buffer from which the message is deserialized.</param> /// <param name="bufferManager">BufferManager that manages the buffer from which the message is deserialized.</param> /// <param name="contentType">The Multipurpose Internet Mail Extensions (MIME) message-level content-type.</param> /// <returns></returns> public override Message ReadMessage(ArraySegment<byte> buffer, BufferManager bufferManager, string contentType) { //Just write the message to memory, then deserialize it var msgContents = new byte[buffer.Count]; Array.Copy(buffer.Array, buffer.Offset, msgContents, 0, msgContents.Length); bufferManager.ReturnBuffer(buffer.Array); using (var stream = new MemoryStream(msgContents)) { return ReadMessage(stream, int.MaxValue, contentType); } }
/// <summary> /// Compress a buffer /// </summary> /// <param name="buffer"></param> /// <param name="bufferManager"></param> /// <param name="messageOffset"></param> /// <returns></returns> private static ArraySegment<byte> CompressBuffer(ArraySegment<byte> buffer, BufferManager bufferManager, int messageOffset) { // Create a memory stream for the final message MemoryStream memoryStream = new MemoryStream(); // Copy the bytes that should not be compressed into the stream memoryStream.Write(buffer.Array, 0, messageOffset); // Compress the message into the stream using (GZipStream gzStream = new GZipStream(memoryStream, CompressionMode.Compress, true)) { gzStream.Write(buffer.Array, messageOffset, buffer.Count); } // Convert the stream into a bytes array byte[] compressedBytes = memoryStream.ToArray(); // Allocate a new buffer to hold the new bytes array byte[] bufferedBytes = bufferManager.TakeBuffer(compressedBytes.Length); // Copy the compressed data into the allocated buffer Array.Copy(compressedBytes, 0, bufferedBytes, 0, compressedBytes.Length); // Release the original buffer we got as an argument bufferManager.ReturnBuffer(buffer.Array); // Create a new ArraySegment that points to the new message buffer ArraySegment<byte> byteArray = new ArraySegment<byte>(bufferedBytes, messageOffset, compressedBytes.Length - messageOffset); return byteArray; }
public void Write(byte[] buffer, int offset, int size, bool immediate, TimeSpan timeout, BufferManager bufferManager) { bool flag = true; try { if (size > this.writeBufferSize) { this.WriteHelper(buffer, offset, size, immediate, timeout, ref this.writeOverlapped.Holder[0]); } else { this.FinishPendingWrite(timeout); ConnectionUtilities.ValidateBufferBounds(buffer, offset, size); lock (this.writeLock) { this.ValidateEnterWritingState(true); bool flag2 = false; try { flag = false; this.StartSyncWrite(buffer, offset, size); flag2 = true; } finally { if (!this.isWriteOutstanding) { flag = true; } else if (flag2) { this.EnterWritingState(); this.pendingWriteBuffer = buffer; this.pendingWriteBufferManager = bufferManager; } } } } } catch (PipeException exception) { throw DiagnosticUtility.ExceptionUtility.ThrowHelper(this.ConvertPipeException(exception, TransferOperation.Write), this.ExceptionEventType); } finally { if (flag) { bufferManager.ReturnBuffer(buffer); } } }
private void FinishPendingWrite(TimeSpan timeout) { if (this.pendingWriteBuffer != null) { byte[] pendingWriteBuffer; BufferManager pendingWriteBufferManager; lock (this.writeLock) { if (this.pendingWriteBuffer == null) { return; } pendingWriteBuffer = this.pendingWriteBuffer; this.pendingWriteBuffer = null; pendingWriteBufferManager = this.pendingWriteBufferManager; this.pendingWriteBufferManager = null; } try { bool flag = false; try { this.WaitForSyncWrite(timeout, true); flag = true; } finally { lock (this.writeLock) { try { if (flag) { this.FinishSyncWrite(true); } } finally { this.ExitWritingState(); if (!this.isWriteOutstanding) { pendingWriteBufferManager.ReturnBuffer(pendingWriteBuffer); this.WriteIOCompleted(); } } } } } catch (PipeException exception) { throw DiagnosticUtility.ExceptionUtility.ThrowHelper(this.ConvertPipeException(exception, TransferOperation.Write), this.ExceptionEventType); } } }
/// <summary> /// Decodes a WCF buffered message /// </summary> /// <param name="buffer"> /// The message buffer/offset /// </param> /// <param name="bufferManager"> /// The WCF buffer manager for this channel /// </param> /// <param name="contentType"> /// The underlying message content type /// </param> /// <returns> /// The decoded WCF message /// </returns> public override Message ReadMessage( ArraySegment<Byte> buffer, BufferManager bufferManager, String contentType) { try { return ReadMessage( new MemoryStream(buffer.Array, buffer.Offset, buffer.Count, false), 65536, contentType ); } finally { // the streamed read does not access the buffer, // so we must always return it to the manager bufferManager.ReturnBuffer(buffer.Array); } }
//Helper method to decompress an array of bytes private static ArraySegment<byte> DecompressBuffer(ArraySegment<byte> buffer, BufferManager bufferManager, CompressionAlgorithm compressionAlgorithm) { var memoryStream = new MemoryStream(buffer.Array, buffer.Offset, buffer.Count); var decompressedStream = new MemoryStream(); //int totalRead = 0; const int blockSize = 1024; byte[] tempBuffer = bufferManager.TakeBuffer(blockSize); using (Stream compressedStream = compressionAlgorithm == CompressionAlgorithm.GZip ? new GZipStream(memoryStream, CompressionMode.Decompress) : (Stream) new DeflateStream(memoryStream, CompressionMode.Decompress)) { while (true) { int bytesRead = compressedStream.Read(tempBuffer, 0, blockSize); if (bytesRead == 0) break; decompressedStream.Write(tempBuffer, 0, bytesRead); //totalRead += bytesRead; } } bufferManager.ReturnBuffer(tempBuffer); byte[] decompressedBytes = decompressedStream.ToArray(); byte[] bufferManagerBuffer = bufferManager.TakeBuffer(decompressedBytes.Length + buffer.Offset); Array.Copy(buffer.Array, 0, bufferManagerBuffer, 0, buffer.Offset); Array.Copy(decompressedBytes, 0, bufferManagerBuffer, buffer.Offset, decompressedBytes.Length); var byteArray = new ArraySegment<byte>(bufferManagerBuffer, buffer.Offset, decompressedBytes.Length); bufferManager.ReturnBuffer(buffer.Array); return byteArray; }
//Helper method to compress an array of bytes private static ArraySegment<byte> CompressBuffer(ArraySegment<byte> buffer, BufferManager bufferManager, int messageOffset, CompressionAlgorithm compressionAlgorithm) { var memoryStream = new MemoryStream(); using (Stream compressedStream = compressionAlgorithm == CompressionAlgorithm.GZip ? new GZipStream(memoryStream, CompressionMode.Compress, true) : (Stream) new DeflateStream(memoryStream, CompressionMode.Compress, true) ) { compressedStream.Write(buffer.Array, buffer.Offset, buffer.Count); } byte[] compressedBytes = memoryStream.ToArray(); int totalLength = messageOffset + compressedBytes.Length; byte[] bufferedBytes = bufferManager.TakeBuffer(totalLength); Array.Copy(compressedBytes, 0, bufferedBytes, messageOffset, compressedBytes.Length); bufferManager.ReturnBuffer(buffer.Array); var byteArray = new ArraySegment<byte>(bufferedBytes, messageOffset, compressedBytes.Length); return byteArray; }
public override Message ReadMessage(ArraySegment<byte> buffer, BufferManager bufferManager, string contentType) { if (bufferManager == null) { throw new ArgumentNullException("bufferManager"); } byte[] content = new byte[buffer.Count]; Array.Copy(buffer.Array, buffer.Offset, content, 0, content.Length); bufferManager.ReturnBuffer(buffer.Array); HttpRequestMessage request = new HttpRequestMessage(); var httpContent = new ByteArrayContent(content); httpContent.Headers.Clear(); if (contentType != null) { httpContent.Headers.Add("content-type", contentType); } request.Content = httpContent; Message message = request.ToMessage(); message.Properties.Encoder = this; return message; }
//Helper method to compress an array of bytes static ArraySegment<byte> CompressBuffer(ArraySegment<byte> buffer, BufferManager bufferManager, int messageOffset) { MemoryStream memoryStream = new MemoryStream(); using (GZipStream gzStream = new GZipStream(memoryStream, CompressionMode.Compress, true)) { gzStream.Write(buffer.Array, buffer.Offset, buffer.Count); } byte[] compressedBytes = memoryStream.ToArray(); int totalLength = messageOffset + compressedBytes.Length; byte[] bufferedBytes = bufferManager.TakeBuffer(totalLength); Array.Copy(compressedBytes, 0, bufferedBytes, messageOffset, compressedBytes.Length); bufferManager.ReturnBuffer(buffer.Array); ArraySegment<byte> byteArray = new ArraySegment<byte>(bufferedBytes, messageOffset, bufferedBytes.Length - messageOffset); return byteArray; }
/// <summary> /// Decompress a buffer /// </summary> /// <param name="buffer"></param> /// <param name="bufferManager"></param> /// <returns></returns> private static ArraySegment<byte> DecompressBuffer(ArraySegment<byte> buffer, BufferManager bufferManager) { // Create a new memory stream, and copy into it the buffer to decompress MemoryStream memoryStream = new MemoryStream(buffer.Array, buffer.Offset, buffer.Count); // Create a memory stream to store the decompressed data MemoryStream decompressedStream = new MemoryStream(); // The totalRead stores the number of decompressed bytes int totalRead = 0; int blockSize = 1024; // Allocate a temporary buffer to use with the decompression byte[] tempBuffer = bufferManager.TakeBuffer(blockSize); // Uncompress the compressed data using (GZipStream gzStream = new GZipStream(memoryStream, CompressionMode.Decompress)) { while (true) { // Read from the compressed data stream int bytesRead = gzStream.Read(tempBuffer, 0, blockSize); if (bytesRead == 0) break; // Write to the decompressed data stream decompressedStream.Write(tempBuffer, 0, bytesRead); totalRead += bytesRead; } } // Release the temporary buffer bufferManager.ReturnBuffer(tempBuffer); // Convert the decompressed data stream into bytes array byte[] decompressedBytes = decompressedStream.ToArray(); // Allocate a new buffer to store the message byte[] bufferManagerBuffer = bufferManager.TakeBuffer(decompressedBytes.Length + buffer.Offset); // Copy the bytes that comes before the compressed message in the buffer argument Array.Copy(buffer.Array, 0, bufferManagerBuffer, 0, buffer.Offset); // Copy the decompressed data Array.Copy(decompressedBytes, 0, bufferManagerBuffer, buffer.Offset, decompressedBytes.Length); // Create a new ArraySegment that points to the new message buffer ArraySegment<byte> byteArray = new ArraySegment<byte>(bufferManagerBuffer, buffer.Offset, decompressedBytes.Length); // Release the original message buffer bufferManager.ReturnBuffer(buffer.Array); return byteArray; }
//Helper method to decompress an array of bytes static ArraySegment<byte> DecompressBuffer(ArraySegment<byte> buffer, BufferManager bufferManager) { MemoryStream memoryStream = new MemoryStream(buffer.Array, buffer.Offset, buffer.Count); MemoryStream decompressedStream = new MemoryStream(); int totalRead = 0; int blockSize = 1024; byte[] tempBuffer = bufferManager.TakeBuffer(blockSize); using (GZipStream gzStream = new GZipStream(memoryStream, CompressionMode.Decompress)) { while (true) { int bytesRead = gzStream.Read(tempBuffer, 0, blockSize); if (bytesRead == 0) break; decompressedStream.Write(tempBuffer, 0, bytesRead); totalRead += bytesRead; } } bufferManager.ReturnBuffer(tempBuffer); byte[] decompressedBytes = decompressedStream.ToArray(); byte[] bufferManagerBuffer = bufferManager.TakeBuffer(decompressedBytes.Length + buffer.Offset); Array.Copy(buffer.Array, 0, bufferManagerBuffer, 0, buffer.Offset); Array.Copy(decompressedBytes, 0, bufferManagerBuffer, buffer.Offset, decompressedBytes.Length); ArraySegment<byte> byteArray = new ArraySegment<byte>(bufferManagerBuffer, buffer.Offset, decompressedBytes.Length); bufferManager.ReturnBuffer(buffer.Array); return byteArray; }
public override Message ReadMessage(ArraySegment<byte> buffer, BufferManager bufferManager, string contentType) { byte[] msgContents = new byte[buffer.Count]; Array.Copy(buffer.Array, buffer.Offset, msgContents, 0, msgContents.Length); bufferManager.ReturnBuffer(buffer.Array); MemoryStream stream = new MemoryStream(msgContents); Common.CustomTextTraceSource ts = new Common.CustomTextTraceSource("CustomTextMessageEncoder.CustomTextMessageEncoder.ReadMessage", "MyTraceSource", SourceLevels.Information); string xmlstring = string.Empty; try { MemoryStream tmpStream = new MemoryStream(msgContents); var sr = new StreamReader(tmpStream); xmlstring = sr.ReadToEnd(); ts.TraceInformation(xmlstring); //XmlDocument signedDoc = new XmlDocument() { PreserveWhitespace = false }; //signedDoc.LoadXml(xmlstring); //XmlSigner verifier = new XmlSigner(signedDoc); //XmlElement sigElement = GetSignatureElement(signedDoc, @"http://www.w3.org/2000/09/xmldsig#", "Security"); //bool valid = false; //if (sigElement != null) //{ // //valid = verifier.ValidateSignature(sigElement, algorithm); // valid = verifier.ValidateSignature(sigElement); // file.WriteLine("Signature Validation = " + valid.ToString()); //} } catch (Exception e) { ts.TraceInformation("Exception: " + e.Message); if (e.InnerException != null) { ts.TraceInformation("InnerException: " + e.InnerException.Message); } } //file.WriteLine("Request Message: "); //file.WriteLine(xmlstring); //file.Close(); return ReadMessage(stream, int.MaxValue); }
public static IObservable<DisposableByteBuffer> ToFrameClientObservable(this Socket socket, SocketFlags socketFlags, BufferManager bufferManager, Selector selector) { return Observable.Create<DisposableByteBuffer>(observer => { var headerState = new BufferState(new byte[sizeof(int)], 0, sizeof(int)); var contentState = new BufferState(null, 0, -1); var selectMode = socketFlags.HasFlag(SocketFlags.OutOfBand) ? SelectMode.SelectError : SelectMode.SelectRead; selector.AddCallback(selectMode, socket, _ => { try { if (headerState.Length > 0) { headerState.Advance(socket.Receive(headerState.Bytes, headerState.Offset, headerState.Length, socketFlags)); if (headerState.Length == 0) { contentState.Length = BitConverter.ToInt32(headerState.Bytes, 0); contentState.Offset = 0; contentState.Bytes = bufferManager.TakeBuffer(contentState.Length); } } if (contentState.Bytes != null) { contentState.Advance(socket.Receive(contentState.Bytes, contentState.Offset, contentState.Length, socketFlags)); if (contentState.Length == 0) { var managedBuffer = contentState.Bytes; var length = contentState.Offset; observer.OnNext(new DisposableByteBuffer(managedBuffer, length, Disposable.Create(() => bufferManager.ReturnBuffer(managedBuffer)))); contentState.Bytes = null; headerState.Length = headerState.Offset; headerState.Offset = 0; } } } catch (Exception exception) { if (!exception.IsWouldBlock()) observer.OnError(exception); } }); return Disposable.Create(() => selector.RemoveCallback(SelectMode.SelectRead, socket)); }); }
//returns false if the message was dropped because the max pending message count was hit. bool IUdpReceiveHandler.HandleDataReceived(ArraySegment <byte> data, EndPoint remoteEndpoint, int interfaceIndex, Action onMessageDequeuedCallback) { BufferManager localBufferManager = this.bufferManager; bool returnBuffer = true; string messageHash = null; Message message = null; bool continueReceiving = true; try { IPEndPoint remoteIPEndPoint = (IPEndPoint)remoteEndpoint; if (localBufferManager != null) { message = UdpUtility.DecodeMessage(this.duplicateDetector, this.messageEncoderFactory.Encoder, localBufferManager, data, remoteIPEndPoint, interfaceIndex, true, out messageHash); if (message != null) { // We pass in the length of the message buffer instead of the length of the message to keep track of the amount of memory that's been allocated continueReceiving = Dispatch(message, data.Array.Length, onMessageDequeuedCallback); returnBuffer = !continueReceiving; } } else { Fx.Assert(this.State != CommunicationState.Opened, "buffer manager should only be null when closing down and the channel instance has taken control of the receive manager."); IUdpReceiveHandler receiveHandler = (IUdpReceiveHandler)this.channelInstance; if (receiveHandler != null) { returnBuffer = false; //let the channel instance take care of the buffer continueReceiving = receiveHandler.HandleDataReceived(data, remoteEndpoint, interfaceIndex, onMessageDequeuedCallback); } else { //both channel and listener are shutting down, so drop the message and stop the receive loop continueReceiving = false; } } } catch (Exception e) { if (Fx.IsFatal(e)) { returnBuffer = false; throw; } HandleReceiveException(e); } finally { if (returnBuffer) { if (message != null) { if (this.duplicateDetector != null) { Fx.Assert(messageHash != null, "message hash should always be available if duplicate detector is enabled"); this.duplicateDetector.RemoveEntry(messageHash); } message.Close(); // implicitly returns the buffer } else { // CSDMain 238600. Both channel and listener are shutting down. There's a race condition happening here // and the bufferManager is not available at this moment. The data buffer ignored here might introduce // an issue with buffer manager, but given that we are in the shutting down case here, it should not be a // big problem. if (localBufferManager != null) { localBufferManager.ReturnBuffer(data.Array); } } } } return(continueReceiving); }
public override void Write(byte[] buffer, int offset, int size, bool immediate, TimeSpan timeout, BufferManager bufferManager) { if (size <= 0) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("size", size, SR.GetString( SR.ValueMustBePositive))); } ThrowPendingWriteException(); if (immediate || flushTimeout == 0) { ThreadTrace.Trace("BC:Write now"); WriteNow(buffer, offset, size, timeout, bufferManager); } else { ThreadTrace.Trace("BC:Write later"); WriteLater(buffer, offset, size, timeout); bufferManager.ReturnBuffer(buffer); } ThreadTrace.Trace("BC:Write done"); }
private void WriteNow(byte[] buffer, int offset, int size, TimeSpan timeout, BufferManager bufferManager) { lock (this.ThisLock) { if (this.pendingWriteSize > 0) { int num = this.writeBufferSize - this.pendingWriteSize; this.CancelFlushTimer(); if (size <= num) { Buffer.BlockCopy(buffer, offset, this.writeBuffer, this.pendingWriteSize, size); if (bufferManager != null) { bufferManager.ReturnBuffer(buffer); } this.pendingWriteSize += size; this.FlushCore(timeout); goto Label_00BF; } TimeoutHelper helper = new TimeoutHelper(timeout); this.FlushCore(helper.RemainingTime()); timeout = helper.RemainingTime(); } if (bufferManager == null) { base.Connection.Write(buffer, offset, size, true, timeout); } else { base.Connection.Write(buffer, offset, size, true, timeout, bufferManager); } Label_00BF:; } }
protected override async Task OnSendAsync(Message message, TimeSpan timeout) { ThrowIfDisposedOrNotOpen(); TimeoutHelper timeoutHelper = new TimeoutHelper(timeout); // If timeout == TimeSpan.MaxValue, then we want to pass Timeout.Infinite as // SemaphoreSlim doesn't accept timeouts > Int32.MaxValue. // Using TimeoutHelper.RemainingTime() would yield a value less than TimeSpan.MaxValue // and would result in the value Int32.MaxValue so we must use the original timeout specified. if (!await SendLock.WaitAsync(TimeoutHelper.ToMilliseconds(timeout))) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new TimeoutException( SR.Format(SR.SendToViaTimedOut, Via, timeout), TimeoutHelper.CreateEnterTimedOutException(timeout))); } byte[] buffer = null; try { // check again in case the previous send faulted while we were waiting for the lock ThrowIfDisposedOrNotOpen(); ThrowIfOutputSessionClosed(); bool success = false; try { ApplyChannelBinding(message); var tcs = new TaskCompletionSource <bool>(this); AsyncCompletionResult completionResult; if (IsStreamedOutput) { completionResult = StartWritingStreamedMessage(message, timeoutHelper.RemainingTime(), s_onWriteComplete, tcs); } else { bool allowOutputBatching; ArraySegment <byte> messageData; allowOutputBatching = message.Properties.AllowOutputBatching; messageData = EncodeMessage(message); buffer = messageData.Array; completionResult = StartWritingBufferedMessage( message, messageData, allowOutputBatching, timeoutHelper.RemainingTime(), s_onWriteComplete, tcs); } if (completionResult == AsyncCompletionResult.Completed) { tcs.TrySetResult(true); } await tcs.Task; FinishWritingMessage(); success = true; if (WcfEventSource.Instance.MessageSentByTransportIsEnabled()) { EventTraceActivity eventTraceActivity = EventTraceActivityHelper.TryExtractActivity(message); WcfEventSource.Instance.MessageSentByTransport(eventTraceActivity, RemoteAddress.Uri.AbsoluteUri); } } finally { if (!success) { Fault(); } } } finally { SendLock.Release(); } if (buffer != null) { BufferManager.ReturnBuffer(buffer); } }
public void Return(byte[] buffer) { _internalBufferManager.ReturnBuffer(buffer); }