本文整理汇总了C#中System.ServiceModel.Channels.BufferManager.TakeBuffer方法的典型用法代码示例。如果您正苦于以下问题:C# BufferManager.TakeBuffer方法的具体用法?C# BufferManager.TakeBuffer怎么用?C# BufferManager.TakeBuffer使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.ServiceModel.Channels.BufferManager
的用法示例。
在下文中一共展示了BufferManager.TakeBuffer方法的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: LidgrenNetChannel
public LidgrenNetChannel(NetConnection connection, BufferManager bufferManager)
{
_connection = connection;
_bufferManager = bufferManager;
_buffer = _bufferManager.TakeBuffer(1024*1024);
_stream = new MemoryStream(_buffer);
_writer = new BinaryWriter(_stream);
}
示例4: ManagedBuffer
/// <summary>
/// Initializes a new buffer instance
/// </summary>
/// <param name="manager">
/// The manager for this buffer
/// </param>
/// <param name="size">
/// The size of the buffer to allocate, in bytes
/// </param>
public ManagedBuffer(BufferManager manager, Int32 size)
{
if (manager == null)
throw new ArgumentNullException("manager");
if (size <= 0)
throw new ArgumentException("data");
this.manager = manager;
this.data = new ArraySegment<Byte>(manager.TakeBuffer(size));
}
示例5: BufferedMemoryStream
public BufferedMemoryStream(BufferManager bufferManager, int capacity)
{
if (bufferManager == null)
throw new ArgumentNullException("bufferManager");
if (capacity < 0)
throw new ArgumentOutOfRangeException("capacity", "Must be positive.");
_bufferManager = bufferManager;
_isWriter = true;
_buffer = _bufferManager.TakeBuffer(capacity);
_capacity = _buffer.Length;
_length = 0;
_isOpen = true;
}
示例6: WriteMessage
public override ArraySegment<byte> WriteMessage(Message message, int maxMessageSize, BufferManager bufferManager, int messageOffset)
{
MemoryStream stream = new MemoryStream();
XmlWriter writer = XmlWriter.Create(stream, this.writerSettings);
message.WriteMessage(writer);
writer.Close();
byte[] messageBytes = stream.GetBuffer();
int messageLength = (int)stream.Position;
stream.Close();
int totalLength = messageLength + messageOffset;
byte[] totalBytes = bufferManager.TakeBuffer(totalLength);
Array.Copy(messageBytes, 0, totalBytes, messageOffset, messageLength);
ArraySegment<byte> byteArray = new ArraySegment<byte>(totalBytes, messageOffset, messageLength);
return byteArray;
}
示例7: WriteMessage
public ArraySegment<byte> WriteMessage(MessageEncoder encoder, Message message, int maxMessageSize, BufferManager bufferManager, int messageOffset)
{
MemoryStream stream = new MemoryStream();
var writer = new StreamWriter(stream);
writer = xmlToCvsTranslator.Translate(message.GetReaderAtBodyContents(), writer);
writer.Flush();
byte[] messageBytes = stream.GetBuffer();
int messageLength = (int)stream.Position;
int totalLength = messageLength + messageOffset;
byte[] totalBytes = bufferManager.TakeBuffer(totalLength);
Array.Copy(messageBytes, 0, totalBytes, messageOffset, messageLength);
ArraySegment<byte> byteArray = new ArraySegment<byte>(totalBytes, messageOffset, messageLength);
writer.Close();
return byteArray;
}
示例8: Reinitialize
public void Reinitialize(int initialSize, int maxSizeQuota, int effectiveMaxSize, BufferManager bufferManager)
{
Contract.Assert(!_initialized, "Clear must be called before re-initializing stream");
if (bufferManager == null)
{
throw Error.ArgumentNull("bufferManager");
}
_theMaxSizeQuota = maxSizeQuota;
_maxSize = effectiveMaxSize;
_bufferManager = bufferManager;
_currentChunk = bufferManager.TakeBuffer(initialSize);
_currentChunkSize = 0;
_totalSize = 0;
_chunkCount = 1;
_chunks[0] = _currentChunk;
_initialized = true;
}
示例9: ReadMessage
public override Message ReadMessage(ArraySegment<byte> buffer, BufferManager bufferManager, string contentType)
{
var bytes = new byte[buffer.Count];
Array.Copy(buffer.Array, buffer.Offset, bytes, 0, bytes.Length);
var message = new UTF8Encoding(true).GetString(bytes);
// Сохраняем ответ в файл содержащий actor
File.WriteAllText("D:\\TrueClientResponseActor.xml", message);
var m = message.Replace("s:actor=\"" + SMEVActor + "\"", "");
var bytes2 = new UTF8Encoding().GetBytes(m);
// Сохраняем ответ в файл уже без actor
File.WriteAllBytes("D:\\TrueClientResponse.xml", bytes2);
var length = bytes2.Length;
const int zero = 0;
var num = length + zero;
var array = bufferManager.TakeBuffer(num);
Array.Copy(bytes2, 0, array, zero, num);
buffer = new ArraySegment<byte>(array, zero, num);
return innerEncoder.ReadMessage(buffer, bufferManager, contentType);
}
示例10: 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);
}
}
示例11: TestSendUDPToService
public string TestSendUDPToService(string ipaddress,int port)
{
Socket socket = new Socket(SocketType.Dgram, ProtocolType.Udp);
m_bufferManager = BufferManager.CreateBufferManager(100 * 1024, 1024);
for(int i =0 ;i<=50;i++)
{
AsyncClientToken asyncClientToken = new AsyncClientToken();
//asyncClientToken.HandlerReturnData = handlerReturnData;
asyncClientToken.Socket = socket;
//asyncClientToken.MessageID = messageID;
asyncClientToken.IP = ipaddress;
asyncClientToken.Port = port;
asyncClientToken.Buffer = m_bufferManager.TakeBuffer(1024);
IPEndPoint ipe = new IPEndPoint(IPAddress.Parse(asyncClientToken.IP), asyncClientToken.Port);
int sendCount = Encoding.UTF8.GetBytes("TestSendUDPToService" + i.ToString(), 0, ("TestSendUDPToService" + i.ToString()).Length, asyncClientToken.Buffer, 0);
CommonVariables.LogTool.Log(DateTime.Now.ToString(CommonFlag.F_DateTimeFormat) + " Send:" + asyncClientToken.IP + asyncClientToken.Port.ToString() + "TestSendUDPToService" + i.ToString());
socket.BeginSendTo(asyncClientToken.Buffer, 0, sendCount, SocketFlags.None, ipe, new AsyncCallback(SendCallback), asyncClientToken);
}
return null;
}
示例12: WriteMessage
public ArraySegment<byte> WriteMessage(MessageEncoder encoder, Message message, int maxMessageSize, BufferManager bufferManager, int messageOffset)
{
MemoryStream stream = new MemoryStream();
StreamWriter sw = new StreamWriter(stream);
string methodName = null;
if (message.Properties.ContainsKey(JSONPBehavior.Name))
{
methodName = ((JSONPMessageProperty) (message.Properties[JSONPBehavior.Name])).MethodName;
}
if (methodName != null)
{
sw.Write(methodName + "( ");
sw.Flush();
}
XmlWriter writer = JsonReaderWriterFactory.CreateJsonWriter(stream);
message.WriteMessage(writer);
writer.Flush();
if (methodName != null)
{
sw.Write(" );");
sw.Flush();
}
byte[] messageBytes = stream.GetBuffer();
int messageLength = (int) stream.Position;
int totalLength = messageLength + messageOffset;
byte[] totalBytes = bufferManager.TakeBuffer(totalLength);
Array.Copy(messageBytes, 0, totalBytes, messageOffset, messageLength);
ArraySegment<byte> byteArray = new ArraySegment<byte>(totalBytes, messageOffset, messageLength);
writer.Close();
return byteArray;
}
示例13: WriteMessage
public override ArraySegment<byte> WriteMessage (
Message message, int maxMessageSize,
BufferManager bufferManager, int messageOffset)
{
VerifyMessageVersion (message);
ArraySegment<byte> seg = new ArraySegment<byte> (
bufferManager.TakeBuffer (maxMessageSize),
messageOffset, maxMessageSize);
XmlWriterSettings s = new XmlWriterSettings ();
s.Encoding = encoding;
s.CheckCharacters = false;
using (XmlWriter w = XmlWriter.Create (
new MemoryStream (seg.Array, seg.Offset, seg.Count), s)) {
message.WriteMessage (
XmlDictionaryWriter.CreateDictionaryWriter (w));
}
return seg;
}
示例14: 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;
}
示例15: DecompressBuffer
/// <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;
}