本文整理汇总了C#中IConnection.ReadAsync方法的典型用法代码示例。如果您正苦于以下问题:C# IConnection.ReadAsync方法的具体用法?C# IConnection.ReadAsync怎么用?C# IConnection.ReadAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IConnection
的用法示例。
在下文中一共展示了IConnection.ReadAsync方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ReadInternal
private static async Task<ReadResult<IRedisValue>> ReadInternal(byte[] initialBuffer, IConnection connection)
{
if (initialBuffer.Length == 0)
{
var bytes = new byte[connection.ReadBufferSize];
var bytesRead = await connection.ReadAsync(bytes);
if (bytesRead == 0)
throw ExceptionBecause.Connection.DidNotReturnDataOnRead(connection);
initialBuffer = new byte[bytesRead];
Array.Copy(bytes, 0, initialBuffer, 0, bytesRead);
}
var dataType = initialBuffer[0];
var startBuffer = new byte[initialBuffer.Length - 1];
Array.Copy(initialBuffer, 1, startBuffer, 0, initialBuffer.Length - 1);
switch (dataType)
{
case DataPrefixes.StringPrefix:
case DataPrefixes.IntPrefix:
return await ReadSimpleString(startBuffer, connection);
case DataPrefixes.BytesPrefix:
return await ReadBulkString(startBuffer, connection);
case DataPrefixes.ArrayPrefix:
return await ReadArray(startBuffer, connection);
case DataPrefixes.ErrorPrefix:
return await ReadSimpleErrorString(startBuffer, connection);
}
throw ExceptionBecause.MalformedData.InDataType(dataType);
}
示例2: ReadBytes
public static async Task<ReadResult<byte[]>> ReadBytes(byte[] initialBuffer, IConnection connection)
{
var expectedLength = await DelimitedReader.ReadInt(initialBuffer, connection);
if (expectedLength.Value == -1)
return new ReadResult<byte[]>(expectedLength.LeftoverBuffer, null);
var lengthPrefixOverReadBytes = expectedLength.LeftoverBuffer;
using (var output = new MemoryStream())
{
var buffer = new byte[connection.ReadBufferSize];
var bufferLength = lengthPrefixOverReadBytes.Length;
lengthPrefixOverReadBytes.CopyTo(buffer, 0);
var totalExpectedBytes = expectedLength.Value + Delimiter.Length;
var bytesProcessed = 0;
var bytesNotProcessed = 0;
while (bytesProcessed < totalExpectedBytes)
{
if (bufferLength > 0)
{
if (bytesProcessed + bufferLength >= totalExpectedBytes)
{
var bytesToProcess = (int)totalExpectedBytes - bytesProcessed;
var bytesToCopy = Math.Max(0, bytesToProcess - Delimiter.Length);
output.Write(buffer, 0, bytesToCopy);
bytesProcessed += bytesToProcess;
bytesNotProcessed = bufferLength - bytesToProcess;
}
else
{
var bytesToCopy = (int)Math.Min(bufferLength, totalExpectedBytes - bytesProcessed - Delimiter.Length);
output.Write(buffer, 0, bytesToCopy);
bytesProcessed += bufferLength;
}
}
if (bytesProcessed < totalExpectedBytes)
bufferLength = await connection.ReadAsync(buffer);
}
var unprocessed = new byte[bytesNotProcessed];
Array.Copy(buffer, bufferLength - bytesNotProcessed, unprocessed, 0, bytesNotProcessed);
return new ReadResult<byte[]>(unprocessed, output.ToArray());
}
}
示例3: ReadBytes
public static async Task<ReadResult<byte[]>> ReadBytes(byte[] initialBuffer, IConnection connection)
{
using (var output = new MemoryStream())
{
var buffer = new byte[connection.ReadBufferSize];
var bufferLength = initialBuffer.Length;
initialBuffer.CopyTo(buffer, 0);
var foundDelimiterByte1 = false;
var foundDelimiterByte2 = false;
var delimiterEndPosition = 0;
while (!foundDelimiterByte2)
{
if (bufferLength > 0)
{
var dataEndPosition = 0;
for (var i = 0; i < bufferLength; i++)
{
if (foundDelimiterByte1)
{
if (buffer[i] != Delimiter.Byte2)
throw ExceptionBecause.MalformedData.InMessageDelimiter(Delimiter.Byte2, buffer[i]);
foundDelimiterByte2 = true;
delimiterEndPosition = i;
break;
}
if (buffer[i] == Delimiter.Byte1)
foundDelimiterByte1 = true;
else
dataEndPosition = i;
}
output.Write(buffer, 0, dataEndPosition + 1);
}
if (!foundDelimiterByte2)
bufferLength = await connection.ReadAsync(buffer);
}
var unprocessedBytes = new byte[bufferLength - delimiterEndPosition - 1];
Array.Copy(buffer, delimiterEndPosition + 1, unprocessedBytes, 0, unprocessedBytes.Length);
return new ReadResult<byte[]>(unprocessedBytes, output.ToArray());
}
}
示例4: SendPreambleAsync
internal async Task<IConnection> SendPreambleAsync(IConnection connection, TimeoutHelper timeoutHelper, ClientFramingDecoder decoder)
{
await connection.WriteAsync(Preamble, 0, Preamble.Length, true, timeoutHelper.RemainingTime());
if (_upgrade != null)
{
StreamUpgradeInitiator upgradeInitiator = _upgrade.CreateUpgradeInitiator(this.RemoteAddress, this.Via);
await upgradeInitiator.OpenAsync(timeoutHelper.RemainingTime());
var connectionWrapper = new OutWrapper<IConnection>();
connectionWrapper.Value = connection;
bool upgradeInitiated = await ConnectionUpgradeHelper.InitiateUpgradeAsync(upgradeInitiator, connectionWrapper, decoder, this, timeoutHelper.RemainingTime());
connection = connectionWrapper.Value;
if (!upgradeInitiated)
{
await ConnectionUpgradeHelper.DecodeFramingFaultAsync(decoder, connection, this.Via, _messageEncoder.ContentType, timeoutHelper.RemainingTime());
}
await upgradeInitiator.CloseAsync(timeoutHelper.RemainingTime());
await connection.WriteAsync(ClientSingletonEncoder.PreambleEndBytes, 0, ClientSingletonEncoder.PreambleEndBytes.Length, true, timeoutHelper.RemainingTime());
}
byte[] ackBuffer = new byte[1];
int ackBytesRead = await connection.ReadAsync(ackBuffer, 0, ackBuffer.Length, timeoutHelper.RemainingTime());
if (!ConnectionUpgradeHelper.ValidatePreambleResponse(ackBuffer, ackBytesRead, decoder, Via))
{
await ConnectionUpgradeHelper.DecodeFramingFaultAsync(decoder, connection, Via,
_messageEncoder.ContentType, timeoutHelper.RemainingTime());
}
return connection;
}