C#: Implementando o NetworkStream.peek?
-
25-09-2019 - |
Pergunta
Atualmente, não há um NetworkStream.Peek
Método em C#. Qual é a melhor maneira de implementar um método que funcione exatamente como NetworkStream.ReadByte
exceto que o retorno byte
não é realmente removido do Stream
?
Solução
Se você não precisa realmente recuperar o byte, pode se referir ao DataAvailable
propriedade.
Caso contrário, você pode embrulhá -lo com um StreamReader
e invocar seu Peek
método.
Observe que nenhum deles é particularmente confiável para a leitura de um fluxo de rede, devido a problemas de latência. Os dados podem ficar disponíveis (presente no buffer de leitura) no instante depois Você espia.
Não tenho certeza do que você pretende fazer com isso, mas o Read
método em NetworkStream
é uma chamada de bloqueio, então você realmente não precisa verificar o status, mesmo se estiver recebendo pedaços. Se você estiver tentando manter o aplicativo responsivo ao ler o fluxo, use um thread ou chamada assíncrona para receber os dados.
Editar: de acordo com esta postagem, StreamReader.Peek
está buggy em um NetworkStream
, ou pelo menos tem um comportamento sem documentos; portanto, tenha cuidado se optar por seguir esse caminho.
Atualizado - Resposta aos comentários
A noção de uma "espiada" na própria corrente é realmente impossível; É apenas um fluxo e, uma vez que o byte é recebido, não está mais no fluxo. Alguns fluxos suportam a busca de busca para que você possa reler tecnicamente esse byte, mas NetworkStream
não é um deles.
Espreitar apenas se aplica quando está lendo o fluxo em um buffer; Depois que os dados estão em um buffer, espreitar é fácil, porque você apenas verifica o que estiver na posição atual no buffer. É por isso que um StreamReader
é capaz de fazer isso; não Stream
A classe geralmente tem seu próprio Peek
método.
Agora, para esse problema especificamente, questiono se essa é realmente a resposta certa. Eu entendo a idéia de selecionar dinamicamente um método para processar o fluxo, mas você verdade Precisa fazer isso no fluxo bruto? Você não pode ler o fluxo em uma matriz de bytes primeiro, ou mesmo copiá -lo em um MemoryStream
, e processar a partir desse ponto?
A principal questão que vejo é que, se algo ruim acontecer quando você está lendo um fluxo de rede, os dados desapareceram. Mas se você o ler em um local temporário primeiro, poderá depurar isso. Você pode descobrir quais eram os dados e por que o objeto que estava tentando processar os dados falhou no meio do caminho.
Em geral, a primeira coisa que você quer fazer com um NetworkStream
é lido em um buffer local. A única razão pela qual posso pensar em não fazer isso é se você estiver lendo uma quantidade enorme de dados - e mesmo assim, posso considerar o uso do sistema de arquivos como um buffer intermediário se não se encaixar na memória.
Não conheço seus requisitos exatos, mas pelo que aprendi até agora, meu conselho seria: não tente processar seus dados diretamente do NetworkStream
A menos que haja uma razão convincente para fazê -lo. Considere ler os dados na memória ou no disco primeiro e depois processando a cópia.
Outras dicas
Encontrei o mesmo número de magia e depois decido qual processador de stream enviar o fluxo para 'requisito e, infelizmente, não consigo se safar desse problema - como sugerido nos comentários da resposta de Aaronught - passando os bytes já consumidos nos métodos de processamento de fluxo em parâmetros separados, pois esses métodos são um dado e esperam sistemas.io.stream e nada mais.
Eu resolvi isso criando um mais ou menos universal Fream peekableSt classe que envolve um fluxo. Funciona para o Networkstreams, mas também para qualquer outro fluxo, desde que você Stream.CanRead isto.
Editar
Como alternativa, você pode usar o novo ReadSeekableStream
e fazer
var readSeekableStream = new ReadSeekableStream(networkStream, /* >= */ count);
...
readSeekableStream.Read(..., count);
readSeekableStream.Seek(-count, SeekOrigin.Current);
De qualquer forma, aqui vem PeekableStream
:
/// <summary>
/// PeekableStream wraps a Stream and can be used to peek ahead in the underlying stream,
/// without consuming the bytes. In other words, doing Peek() will allow you to look ahead in the stream,
/// but it won't affect the result of subsequent Read() calls.
///
/// This is sometimes necessary, e.g. for peeking at the magic number of a stream of bytes and decide which
/// stream processor to hand over the stream.
/// </summary>
public class PeekableStream : Stream
{
private readonly Stream underlyingStream;
private readonly byte[] lookAheadBuffer;
private int lookAheadIndex;
public PeekableStream(Stream underlyingStream, int maxPeekBytes)
{
this.underlyingStream = underlyingStream;
lookAheadBuffer = new byte[maxPeekBytes];
}
protected override void Dispose(bool disposing)
{
if (disposing)
underlyingStream.Dispose();
base.Dispose(disposing);
}
/// <summary>
/// Peeks at a maximum of count bytes, or less if the stream ends before that number of bytes can be read.
///
/// Calls to this method do not influence subsequent calls to Read() and Peek().
///
/// Please note that this method will always peek count bytes unless the end of the stream is reached before that - in contrast to the Read()
/// method, which might read less than count bytes, even though the end of the stream has not been reached.
/// </summary>
/// <param name="buffer">An array of bytes. When this method returns, the buffer contains the specified byte array with the values between offset and
/// (offset + number-of-peeked-bytes - 1) replaced by the bytes peeked from the current source.</param>
/// <param name="offset">The zero-based byte offset in buffer at which to begin storing the data peeked from the current stream.</param>
/// <param name="count">The maximum number of bytes to be peeked from the current stream.</param>
/// <returns>The total number of bytes peeked into the buffer. If it is less than the number of bytes requested then the end of the stream has been reached.</returns>
public virtual int Peek(byte[] buffer, int offset, int count)
{
if (count > lookAheadBuffer.Length)
throw new ArgumentOutOfRangeException("count", "must be smaller than peekable size, which is " + lookAheadBuffer.Length);
while (lookAheadIndex < count)
{
int bytesRead = underlyingStream.Read(lookAheadBuffer, lookAheadIndex, count - lookAheadIndex);
if (bytesRead == 0) // end of stream reached
break;
lookAheadIndex += bytesRead;
}
int peeked = Math.Min(count, lookAheadIndex);
Array.Copy(lookAheadBuffer, 0, buffer, offset, peeked);
return peeked;
}
public override bool CanRead { get { return true; } }
public override long Position
{
get
{
return underlyingStream.Position - lookAheadIndex;
}
set
{
underlyingStream.Position = value;
lookAheadIndex = 0; // this needs to be done AFTER the call to underlyingStream.Position, as that might throw NotSupportedException,
// in which case we don't want to change the lookAhead status
}
}
public override int Read(byte[] buffer, int offset, int count)
{
int bytesTakenFromLookAheadBuffer = 0;
if (count > 0 && lookAheadIndex > 0)
{
bytesTakenFromLookAheadBuffer = Math.Min(count, lookAheadIndex);
Array.Copy(lookAheadBuffer, 0, buffer, offset, bytesTakenFromLookAheadBuffer);
count -= bytesTakenFromLookAheadBuffer;
offset += bytesTakenFromLookAheadBuffer;
lookAheadIndex -= bytesTakenFromLookAheadBuffer;
if (lookAheadIndex > 0) // move remaining bytes in lookAheadBuffer to front
// copying into same array should be fine, according to http://msdn.microsoft.com/en-us/library/z50k9bft(v=VS.90).aspx :
// "If sourceArray and destinationArray overlap, this method behaves as if the original values of sourceArray were preserved
// in a temporary location before destinationArray is overwritten."
Array.Copy(lookAheadBuffer, lookAheadBuffer.Length - bytesTakenFromLookAheadBuffer + 1, lookAheadBuffer, 0, lookAheadIndex);
}
return count > 0
? bytesTakenFromLookAheadBuffer + underlyingStream.Read(buffer, offset, count)
: bytesTakenFromLookAheadBuffer;
}
public override int ReadByte()
{
if (lookAheadIndex > 0)
{
lookAheadIndex--;
byte firstByte = lookAheadBuffer[0];
if (lookAheadIndex > 0) // move remaining bytes in lookAheadBuffer to front
Array.Copy(lookAheadBuffer, 1, lookAheadBuffer, 0, lookAheadIndex);
return firstByte;
}
else
{
return underlyingStream.ReadByte();
}
}
public override long Seek(long offset, SeekOrigin origin)
{
long ret = underlyingStream.Seek(offset, origin);
lookAheadIndex = 0; // this needs to be done AFTER the call to underlyingStream.Seek(), as that might throw NotSupportedException,
// in which case we don't want to change the lookAhead status
return ret;
}
// from here on, only simple delegations to underlyingStream
public override bool CanSeek { get { return underlyingStream.CanSeek; } }
public override bool CanWrite { get { return underlyingStream.CanWrite; } }
public override bool CanTimeout { get { return underlyingStream.CanTimeout; } }
public override int ReadTimeout { get { return underlyingStream.ReadTimeout; } set { underlyingStream.ReadTimeout = value; } }
public override int WriteTimeout { get { return underlyingStream.WriteTimeout; } set { underlyingStream.WriteTimeout = value; } }
public override void Flush() { underlyingStream.Flush(); }
public override long Length { get { return underlyingStream.Length; } }
public override void SetLength(long value) { underlyingStream.SetLength(value); }
public override void Write(byte[] buffer, int offset, int count) { underlyingStream.Write(buffer, offset, count); }
public override void WriteByte(byte value) { underlyingStream.WriteByte(value); }
}
Se você tem acesso ao Socket
objeto, você pode tentar o Método de recebimento, passando SocketFlags.Peek
. Isso é análogo ao MSG_PEEK
bandeira que pode ser passada para o recv
Ligue para soquetes BSD ou Winsock.
Aqui está um muito simples PeekStream
A implementação que permite espiar um certo número de bytes no início do fluxo (em vez de poder espiar a qualquer momento). Os bytes espalhados são devolvidos como um Stream
eles mesmos, para minimizar as alterações no código existente.
Veja como você o usa:
Stream nonSeekableStream = ...;
PeekStream peekStream = new PeekStream(nonSeekableStream, 30); // Peek max 30 bytes
Stream initialBytesStream = peekStream.GetInitialBytesStream();
ParseHeaders(initialBytesStream); // Work on initial bytes of nonSeekableStream
peekStream.Read(...) // Read normally, the read will start from the beginning
GetInitialBytesStream()
retorna um fluxo buscável que contém até peekSize
bytes iniciais do fluxo subjacente (menos se o fluxo for mais curto que peekSize
).
Devido à sua simplicidade, a leitura Peekstream deve ser apenas marginalmente mais lenta (se é que existe) do que a leitura diretamente do fluxo subjacente.
public class PeekStream : Stream
{
private Stream m_stream;
private byte[] m_buffer;
private int m_start;
private int m_end;
public PeekStream(Stream stream, int peekSize)
{
if (stream == null)
{
throw new ArgumentNullException("stream");
}
if (!stream.CanRead)
{
throw new ArgumentException("Stream is not readable.");
}
if (peekSize < 0)
{
throw new ArgumentOutOfRangeException("peekSize");
}
m_stream = stream;
m_buffer = new byte[peekSize];
m_end = stream.Read(m_buffer, 0, peekSize);
}
public override bool CanRead
{
get
{
return true;
}
}
public override bool CanWrite
{
get
{
return false;
}
}
public override bool CanSeek
{
get
{
return false;
}
}
public override long Length
{
get
{
throw new NotSupportedException();
}
}
public override long Position
{
get
{
throw new NotSupportedException();
}
set
{
throw new NotSupportedException();
}
}
public MemoryStream GetInitialBytesStream()
{
return new MemoryStream(m_buffer, 0, m_end, false);
}
public override long Seek(long offset, SeekOrigin origin)
{
throw new NotSupportedException();
}
public override void SetLength(long value)
{
throw new NotSupportedException();
}
public override int Read(byte[] buffer, int offset, int count)
{
// Validate arguments
if (buffer == null)
{
throw new ArgumentNullException("buffer");
}
if (offset < 0)
{
throw new ArgumentOutOfRangeException("offset");
}
if (offset + count > buffer.Length)
{
throw new ArgumentOutOfRangeException("count");
}
int totalRead = 0;
// Read from buffer
if (m_start < m_end)
{
int toRead = Math.Min(m_end - m_start, count);
Array.Copy(m_buffer, m_start, buffer, offset, toRead);
m_start += toRead;
offset += toRead;
count -= toRead;
totalRead += toRead;
}
// Read from stream
if (count > 0)
{
totalRead += m_stream.Read(buffer, offset, count);
}
// Return total bytes read
return totalRead;
}
public override void Write(byte[] buffer, int offset, int count)
{
throw new NotImplementedException();
}
public override int ReadByte()
{
if (m_start < m_end)
{
return m_buffer[m_start++];
}
else
{
return m_stream.ReadByte();
}
}
public override void Flush()
{
m_stream.Flush();
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
m_stream.Dispose();
}
base.Dispose(disposing);
}
}
Isenção de responsabilidade: Peekstream acima é retirado de um programa de trabalho, mas não é testado de forma abrangente, portanto, pode conter erros. Funciona para mim, mas você pode descobrir alguns casos de canto em que falha.