本文整理汇总了C#中ByteBuffer.GetByteArray方法的典型用法代码示例。如果您正苦于以下问题:C# ByteBuffer.GetByteArray方法的具体用法?C# ByteBuffer.GetByteArray怎么用?C# ByteBuffer.GetByteArray使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ByteBuffer
的用法示例。
在下文中一共展示了ByteBuffer.GetByteArray方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ReadMessageAsync
public static async Task<WebSocketMessage> ReadMessageAsync(WebSocket webSocket, byte[] buffer, int maxMessageSize)
{
ArraySegment<byte> arraySegment = new ArraySegment<byte>(buffer);
WebSocketReceiveResult receiveResult = await webSocket.ReceiveAsync(arraySegment, CancellationToken.None).ConfigureAwait(continueOnCapturedContext: false);
// special-case close messages since they might not have the EOF flag set
if (receiveResult.MessageType == WebSocketMessageType.Close)
{
return new WebSocketMessage(null, WebSocketMessageType.Close);
}
if (receiveResult.EndOfMessage)
{
// we anticipate that single-fragment messages will be common, so we optimize for them
switch (receiveResult.MessageType)
{
case WebSocketMessageType.Binary:
return new WebSocketMessage(BufferSliceToByteArray(buffer, receiveResult.Count), WebSocketMessageType.Binary);
case WebSocketMessageType.Text:
return new WebSocketMessage(BufferSliceToString(buffer, receiveResult.Count), WebSocketMessageType.Text);
default:
throw new Exception("This code path should never be hit.");
}
}
else
{
// for multi-fragment messages, we need to coalesce
ByteBuffer bytebuffer = new ByteBuffer(maxMessageSize);
bytebuffer.Append(BufferSliceToByteArray(buffer, receiveResult.Count));
WebSocketMessageType originalMessageType = receiveResult.MessageType;
while (true)
{
// loop until an error occurs or we see EOF
receiveResult = await webSocket.ReceiveAsync(arraySegment, CancellationToken.None).ConfigureAwait(continueOnCapturedContext: false);
if (receiveResult.MessageType != originalMessageType)
{
throw new InvalidOperationException("Incorrect message type");
}
bytebuffer.Append(BufferSliceToByteArray(buffer, receiveResult.Count));
if (receiveResult.EndOfMessage)
{
switch (receiveResult.MessageType)
{
case WebSocketMessageType.Binary:
return new WebSocketMessage(bytebuffer.GetByteArray(), WebSocketMessageType.Binary);
case WebSocketMessageType.Text:
return new WebSocketMessage(bytebuffer.GetString(), WebSocketMessageType.Text);
default:
throw new Exception("This code path should never be hit.");
}
}
}
}
}
示例2: ReadMessageAsync
public static async Task<WebSocketMessage> ReadMessageAsync(WebSocket webSocket, int bufferSize, int? maxMessageSize, CancellationToken disconnectToken)
{
WebSocketMessage message;
// Read the first time with an empty array
WebSocketReceiveResult receiveResult = await webSocket.ReceiveAsync(_emptyArraySegment, disconnectToken).PreserveCultureNotContext();
if (TryGetMessage(receiveResult, null, out message))
{
return message;
}
var buffer = new byte[bufferSize];
// Now read with the real buffer
var arraySegment = new ArraySegment<byte>(buffer);
receiveResult = await webSocket.ReceiveAsync(arraySegment, disconnectToken).PreserveCultureNotContext();
if (TryGetMessage(receiveResult, buffer, out message))
{
return message;
}
else
{
// for multi-fragment messages, we need to coalesce
ByteBuffer bytebuffer = new ByteBuffer(maxMessageSize);
bytebuffer.Append(BufferSliceToByteArray(buffer, receiveResult.Count));
WebSocketMessageType originalMessageType = receiveResult.MessageType;
while (true)
{
// loop until an error occurs or we see EOF
receiveResult = await webSocket.ReceiveAsync(arraySegment, disconnectToken).PreserveCultureNotContext();
if (receiveResult.MessageType == WebSocketMessageType.Close)
{
return WebSocketMessage.CloseMessage;
}
if (receiveResult.MessageType != originalMessageType)
{
throw new InvalidOperationException("Incorrect message type");
}
bytebuffer.Append(BufferSliceToByteArray(buffer, receiveResult.Count));
if (receiveResult.EndOfMessage)
{
switch (receiveResult.MessageType)
{
case WebSocketMessageType.Binary:
return new WebSocketMessage(bytebuffer.GetByteArray(), WebSocketMessageType.Binary);
case WebSocketMessageType.Text:
return new WebSocketMessage(bytebuffer.GetString(), WebSocketMessageType.Text);
default:
throw new InvalidOperationException("Unknown message type");
}
}
}
}
}
开发者ID:FabianGosebrink,项目名称:ASPNET-Core-Angular2-SignalR-Typescript,代码行数:64,代码来源:WebSocketMessageReader.cs
示例3: ReadMessageAsync
public static async Task<WebSocketMessage> ReadMessageAsync(WebSocket webSocket, byte[] buffer, int maxMessageSize, CancellationToken disconnectToken)
{
var arraySegment = new ArraySegment<byte>(buffer);
WebSocketReceiveResult receiveResult = null;
try
{
receiveResult = await webSocket.ReceiveAsync(arraySegment, disconnectToken).ConfigureAwait(continueOnCapturedContext: false);
}
catch (Exception)
{
// If the websocket is aborted while we're reading then just rethrow
// an operaton cancelled exception so the caller can handle it
// appropriately
if (webSocket.State == WebSocketState.Aborted)
{
throw new OperationCanceledException();
}
else
{
// Otherwise rethrow the original exception
throw;
}
}
// special-case close messages since they might not have the EOF flag set
if (receiveResult.MessageType == WebSocketMessageType.Close)
{
return new WebSocketMessage(null, WebSocketMessageType.Close);
}
if (receiveResult.EndOfMessage)
{
// we anticipate that single-fragment messages will be common, so we optimize for them
switch (receiveResult.MessageType)
{
case WebSocketMessageType.Binary:
return new WebSocketMessage(BufferSliceToByteArray(buffer, receiveResult.Count), WebSocketMessageType.Binary);
case WebSocketMessageType.Text:
return new WebSocketMessage(BufferSliceToString(buffer, receiveResult.Count), WebSocketMessageType.Text);
default:
throw new Exception("This code path should never be hit.");
}
}
else
{
// for multi-fragment messages, we need to coalesce
ByteBuffer bytebuffer = new ByteBuffer(maxMessageSize);
bytebuffer.Append(BufferSliceToByteArray(buffer, receiveResult.Count));
WebSocketMessageType originalMessageType = receiveResult.MessageType;
while (true)
{
// loop until an error occurs or we see EOF
receiveResult = await webSocket.ReceiveAsync(arraySegment, disconnectToken).ConfigureAwait(continueOnCapturedContext: false);
if (receiveResult.MessageType != originalMessageType)
{
throw new InvalidOperationException("Incorrect message type");
}
bytebuffer.Append(BufferSliceToByteArray(buffer, receiveResult.Count));
if (receiveResult.EndOfMessage)
{
switch (receiveResult.MessageType)
{
case WebSocketMessageType.Binary:
return new WebSocketMessage(bytebuffer.GetByteArray(), WebSocketMessageType.Binary);
case WebSocketMessageType.Text:
return new WebSocketMessage(bytebuffer.GetString(), WebSocketMessageType.Text);
default:
throw new Exception("This code path should never be hit.");
}
}
}
}
}
示例4: ReadMessageAsync
public static async Task<WebSocketMessage> ReadMessageAsync(WebSocket webSocket, MemoryPool _memoryPool, int bufferSize, int? maxMessageSize, CancellationToken disconnectToken)
{
WebSocketMessage message;
// Read the first time with an empty array
// Holds hear untill first message comes through. Because its empty, it will go onto the next
// TryGetMessage when the data comes through.
var receiveResult = await webSocket.ReceiveAsync(_emptyArraySegment, disconnectToken).PreserveCultureNotContext();
if (TryGetMessage(receiveResult, null, out message))
{
return message;
}
var arraySegment = _memoryPool.AllocSegment(bufferSize);
// Now read with the real buffer
// We now know there is content, so we we allocate memory. Additionally this is setup in hopes
// that the first message we read will only take one read.
receiveResult = await webSocket.ReceiveAsync(arraySegment, disconnectToken).PreserveCultureNotContext();
if (TryGetMessage(receiveResult, arraySegment.Array, out message))
{
return message;
}
// Lastly read the rest of the message if needed
// For multi-fragment messages, we need to coalesce and use a buffer that can grow in size
// as more of the message comes in.
var bytebuffer = new ByteBuffer(maxMessageSize);
bytebuffer.Append(BufferSliceToByteArray(arraySegment.Array, receiveResult.Count));
var originalMessageType = receiveResult.MessageType;
while (true)
{
// loop until an error occurs or we see EOF
receiveResult = await webSocket.ReceiveAsync(arraySegment, disconnectToken).PreserveCultureNotContext();
if (receiveResult.MessageType == WebSocketMessageType.Close)
{
return WebSocketMessage.CloseMessage;
}
if (receiveResult.MessageType != originalMessageType)
{
throw new InvalidOperationException("Incorrect message type");
}
bytebuffer.Append(BufferSliceToByteArray(arraySegment.Array, receiveResult.Count));
if (receiveResult.EndOfMessage)
{
switch (receiveResult.MessageType)
{
case WebSocketMessageType.Binary:
return new WebSocketMessage(bytebuffer.GetByteArray(), WebSocketMessageType.Binary);
case WebSocketMessageType.Text:
return new WebSocketMessage(bytebuffer.GetString(), WebSocketMessageType.Text);
default:
throw new InvalidOperationException("Unknown message type");
}
}
}
}