本文整理汇总了C#中Stream.ReadBytesAsync方法的典型用法代码示例。如果您正苦于以下问题:C# Stream.ReadBytesAsync方法的具体用法?C# Stream.ReadBytesAsync怎么用?C# Stream.ReadBytesAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Stream
的用法示例。
在下文中一共展示了Stream.ReadBytesAsync方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ReadUIntAsync
public static async Task<VInt> ReadUIntAsync(Stream s, CancellationToken cancel_token)
{
int first = await s.ReadByteAsync();
if (first<0) throw new EndOfStreamException();
int len = CheckLength(first);
var bin = new byte[len];
bin[0] = (byte)first;
await s.ReadBytesAsync(bin, 1, len-1, cancel_token);
long res = bin.Aggregate(0L, (r, b) => (r<<8) | b);
res &= (1<<(7*len))-1;
return new VInt(res, bin);
}
示例2: ProcessInternalAsync
protected override async Task ProcessInternalAsync(Stream stream)
{
var buffer = new byte[ProtocolConstants.RevertBuffer.Length];
try
{
await stream.ReadBytesAsync(buffer, "revert", true);
}
catch (Exception)
{
//a revert was not sent, send is complete
return;
}
var revert = Encoding.Unicode.GetString(buffer);
if (revert == ProtocolConstants.Revert)
{
throw new RevertSendException();
}
}
示例3: BroadcastAsync
/// <summary>
/// Sends binary data from the specified <see cref="Stream"/> asynchronously to
/// every client in the WebSocket service.
/// </summary>
/// <remarks>
/// This method doesn't wait for the send to be complete.
/// </remarks>
/// <param name="stream">
/// A <see cref="Stream"/> from which contains the binary data to send.
/// </param>
/// <param name="length">
/// An <see cref="int"/> that represents the number of bytes to send.
/// </param>
/// <param name="completed">
/// An <see cref="Action"/> delegate that references the method(s) called when
/// the send is complete.
/// </param>
public void BroadcastAsync(Stream stream, int length, Action completed)
{
var msg = _state.CheckIfAvailable (false, true, false) ??
stream.CheckIfCanRead () ??
(length < 1 ? "'length' is less than 1." : null);
if (msg != null) {
_logger.Error (msg);
return;
}
stream.ReadBytesAsync (
length,
data => {
var len = data.Length;
if (len == 0) {
_logger.Error ("The data cannot be read from 'stream'.");
return;
}
if (len < length)
_logger.Warn (
String.Format (
"The data with 'length' cannot be read from 'stream':\n expected: {0}\n actual: {1}",
length,
len));
if (len <= WebSocket.FragmentLength)
broadcast (Opcode.Binary, data, completed);
else
broadcast (Opcode.Binary, new MemoryStream (data), completed);
},
ex => _logger.Fatal (ex.ToString ()));
}
示例4: Send
/// <summary>
/// Sends a binary data from the specified <see cref="Stream"/>
/// using the WebSocket connection.
/// </summary>
/// <remarks>
/// This method does not wait for the send to be complete.
/// </remarks>
/// <param name="stream">
/// A <see cref="Stream"/> object from which contains a binary data to send.
/// </param>
/// <param name="length">
/// An <see cref="int"/> that contains the number of bytes to send.
/// </param>
/// <param name="completed">
/// An Action<bool> delegate that references the method(s) called when
/// the send is complete.
/// A <see cref="bool"/> passed to this delegate is <c>true</c> if the send is
/// complete successfully; otherwise, <c>false</c>.
/// </param>
public void Send(Stream stream, int length, Action<bool> completed)
{
var msg = _readyState.CheckIfOpen () ??
stream.CheckIfCanRead () ??
(length < 1 ? "'length' must be greater than 0." : null);
if (msg != null)
{
_logger.Error (msg);
error (msg);
return;
}
stream.ReadBytesAsync (
length,
data =>
{
var len = data.Length;
if (len == 0)
{
var err = "A data cannot be read from 'stream'.";
_logger.Error (err);
error (err);
return;
}
if (len < length)
_logger.Warn (String.Format (
"A data with 'length' cannot be read from 'stream'.\nexpected: {0} actual: {1}",
length,
len));
var sent = len <= FragmentLength
? send (Opcode.BINARY, data)
: send (Opcode.BINARY, new MemoryStream (data));
if (completed != null)
completed (sent);
},
ex =>
{
_logger.Fatal (ex.ToString ());
error ("An exception has occurred.");
});
}
示例5: SendAsync
/// <summary>
/// Sends binary data from the specified <see cref="Stream"/> asynchronously using
/// the WebSocket connection.
/// </summary>
/// <remarks>
/// This method doesn't wait for the send to be complete.
/// </remarks>
/// <param name="stream">
/// A <see cref="Stream"/> from which contains the binary data to send.
/// </param>
/// <param name="length">
/// An <see cref="int"/> that represents the number of bytes to send.
/// </param>
/// <param name="completed">
/// An <c>Action<bool></c> delegate that references the method(s) called when
/// the send is complete. A <see cref="bool"/> passed to this delegate is <c>true</c>
/// if the send is complete successfully.
/// </param>
public void SendAsync (Stream stream, int length, Action<bool> completed)
{
var msg = _readyState.CheckIfAvailable (false, true, false, false) ??
CheckSendParameters (stream, length);
if (msg != null) {
_logger.Error (msg);
error ("An error has occurred in sending the data.", null);
return;
}
stream.ReadBytesAsync (
length,
data => {
var len = data.Length;
if (len == 0) {
_logger.Error ("The data cannot be read from 'stream'.");
error ("An error has occurred in sending the data.", null);
return;
}
if (len < length)
_logger.Warn (
String.Format (
"The data with 'length' cannot be read from 'stream':\n expected: {0}\n actual: {1}",
length,
len));
var sent = send (Opcode.Binary, new MemoryStream (data));
if (completed != null)
completed (sent);
},
ex => {
_logger.Fatal (ex.ToString ());
error ("An exception has occurred while sending the data.", ex);
});
}
示例6: SendAsync
/// <summary>
/// Sends a binary data from the specified <see cref="Stream"/> asynchronously
/// using the WebSocket connection.
/// </summary>
/// <remarks>
/// This method doesn't wait for the send to be complete.
/// </remarks>
/// <param name="stream">
/// A <see cref="Stream"/> from which contains the binary data to send.
/// </param>
/// <param name="length">
/// An <see cref="int"/> that represents the number of bytes to send.
/// </param>
/// <param name="completed">
/// An <c>Action<bool></c> delegate that references the method(s) called when
/// the send is complete. A <see cref="bool"/> passed to this delegate is <c>true</c>
/// if the send is complete successfully.
/// </param>
public void SendAsync (Stream stream, int length, Action<bool> completed)
{
var msg = _readyState.CheckIfOpen () ??
stream.CheckIfCanRead () ??
(length < 1 ? "'length' is less than 1." : null);
if (msg != null) {
_logger.Error (msg);
error ("An error has occurred in sending the data.", null);
return;
}
stream.ReadBytesAsync (
length,
data => {
var len = data.Length;
if (len == 0) {
_logger.Error ("The data cannot be read from 'stream'.");
error ("An error has occurred in sending the data.", null);
return;
}
if (len < length)
_logger.Warn (
String.Format (
"The data with 'length' cannot be read from 'stream'.\nexpected: {0} actual: {1}",
length,
len));
var sent = send (Opcode.Binary, new MemoryStream (data));
if (completed != null)
completed (sent);
},
ex => {
_logger.Fatal (ex.ToString ());
error ("An exception has occurred while sending the data.", ex);
});
}
示例7: Broadcast
/// <summary>
/// Broadcasts a binary data from the specified <see cref="Stream"/>
/// to all clients of a WebSocket service.
/// </summary>
/// <remarks>
/// This method does not wait for the broadcast to be complete.
/// </remarks>
/// <param name="stream">
/// A <see cref="Stream"/> object from which contains a binary data to broadcast.
/// </param>
/// <param name="length">
/// An <see cref="int"/> that contains the number of bytes to broadcast.
/// </param>
/// <param name="completed">
/// A <see cref="Action"/> delegate that references the method(s) called when
/// the broadcast is complete.
/// </param>
public void Broadcast (Stream stream, int length, Action completed)
{
var msg = _state.CheckIfStarted () ??
stream.CheckIfCanRead () ??
(length < 1 ? "'length' must be greater than 0." : null);
if (msg != null)
{
_logger.Error (msg);
return;
}
stream.ReadBytesAsync (
length,
data =>
{
var len = data.Length;
if (len == 0)
{
_logger.Error ("A data cannot be read from 'stream'.");
return;
}
if (len < length)
_logger.Warn (String.Format (
"A data with 'length' cannot be read from 'stream'.\nexpected: {0} actual: {1}",
length,
len));
if (len <= WebSocket.FragmentLength)
Broadcast (Opcode.BINARY, data, completed);
else
Broadcast (Opcode.BINARY, new MemoryStream (data), completed);
},
ex =>
{
_logger.Fatal (ex.ToString ());
});
}
示例8: ReadBodyAsync
public async Task ReadBodyAsync(Stream s, CancellationToken cancel_token)
{
if (this.Size.IsUnknown) return;
this.Data = await s.ReadBytesAsync((int)this.Size.Value, cancel_token);
}
示例9: SendAsync
/// <summary>
/// Sends binary data from the specified <see cref="Stream"/> asynchronously using
/// the WebSocket connection.
/// </summary>
/// <remarks>
/// This method doesn't wait for the send to be complete.
/// </remarks>
/// <param name="stream">
/// A <see cref="Stream"/> from which contains the binary data to send.
/// </param>
/// <param name="length">
/// An <see cref="int"/> that represents the number of bytes to send.
/// </param>
/// <param name="completed">
/// An <c>Action<bool></c> delegate that references the method(s) called when
/// the send is complete. A <see cref="bool"/> passed to this delegate is <c>true</c>
/// if the send is complete successfully.
/// </param>
public void SendAsync(Stream stream, int length, Action<bool> completed)
{
var msg = CheckIfAvailable(_readyState, false, true, false, false) ??
CheckSendParameters(stream, length);
if (msg != null)
{
#if COMPAT
Log.Error(msg);
#else
msg.Error();
#endif
Error("An error has occurred in sending data.", null);
return;
}
stream.ReadBytesAsync(
length,
data =>
{
var len = data.Length;
if (len == 0)
{
#if COMPAT
Log.Error("The data cannot be read from 'stream'.");
#endif
Error("An error has occurred in sending data.", null);
return;
}
#if COMPAT
if (len < length)
Log.InfoFormat(
"The length of the data is less than 'length':\n expected: {0}\n actual: {1}",
length,
len);
#endif
var sent = send(Opcode.Binary, new MemoryStream(data));
completed?.Invoke(sent);
},
ex =>
{
Error("An exception has occurred while sending data.", ex);
}
);
}
示例10: ReadAsync
public async Task ReadAsync(
Stream stream,
IRTMPContentSink sink,
CancellationToken cancel_token)
{
int len = 0;
var bin = new byte[13];
try {
len += await stream.ReadBytesAsync(bin, len, 13-len, cancel_token);
}
catch (EndOfStreamException) {
return;
}
var header = new FileHeader(bin);
if (!header.IsValid) throw new BadDataException();
sink.OnFLVHeader();
len = 0;
bool eos = false;
while (!eos) {
try {
len += await stream.ReadBytesAsync(bin, len, 11-len, cancel_token);
var read_valid = false;
var body = new FLVTag(this, bin);
if (body.IsValidHeader) {
if (await body.ReadTagBodyAsync(stream, cancel_token)) {
len = 0;
read_valid = true;
switch (body.Type) {
case FLVTag.TagType.Audio:
sink.OnAudio(body.ToRTMPMessage());
break;
case FLVTag.TagType.Video:
sink.OnVideo(body.ToRTMPMessage());
break;
case FLVTag.TagType.Script:
sink.OnData(new DataAMF0Message(body.ToRTMPMessage()));
break;
}
}
}
else {
len += await stream.ReadBytesAsync(bin, len, 13-len, cancel_token);
var new_header = new FileHeader(bin);
if (new_header.IsValid) {
read_valid = true;
sink.OnFLVHeader();
}
}
if (!read_valid) {
int pos = 1;
for (; pos<len; pos++) {
var b = bin[pos];
if ((b & 0xC0)==0 && ((b & 0x1F)==8 || (b & 0x1F)==9 || (b & 0x1F)==18)) {
break;
}
}
if (pos==len) {
len = 0;
}
else {
Array.Copy(bin, pos, bin, 0, len-pos);
len -= pos;
}
}
}
catch (EndOfStreamException) {
eos = true;
}
}
}
示例11: ReadTagBodyAsync
public async Task<bool> ReadTagBodyAsync(Stream stream, CancellationToken cancel_token)
{
this.Body = await stream.ReadBytesAsync(this.DataSize, cancel_token);
this.Footer = await stream.ReadBytesAsync(4, cancel_token);
return IsValidFooter;
}