本文整理汇总了C#中System.ServiceModel.Channels.BufferManager.ReturnBuffer方法的典型用法代码示例。如果您正苦于以下问题:C# BufferManager.ReturnBuffer方法的具体用法?C# BufferManager.ReturnBuffer怎么用?C# BufferManager.ReturnBuffer使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.ServiceModel.Channels.BufferManager
的用法示例。
在下文中一共展示了BufferManager.ReturnBuffer方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ToFrameStreamObservable
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);
}
});
}
示例2: Encode
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);
}
示例3: ReadMessage
/// <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);
}
示例4: ReadMessage
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);
}
示例5: ReadMessage
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);
}
示例6: ReadMessage
/// <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);
}
示例7: DecompressBuffer
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);
}
}
示例8: ReadMessage
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);
}
示例9: CompressBuffer
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);
}
}
示例10: ReadMessage
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;
}
示例11: WriteMessage
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);
}
}
示例12: CompressBuffer
/// <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;
}
示例13: Write
public void Write(byte[] buffer, int offset, int size, bool immediate, TimeSpan timeout, BufferManager bufferManager)
{
Write(buffer, offset, size, immediate, timeout);
bufferManager.ReturnBuffer(buffer);
}
示例14: ReadMessage
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;
}
示例15: WriteNow
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);
}
}
}