我们来了;这是从Socket 或Stream 等源读取标记分隔消息列表的基本 过程。棘手的一点是跟踪您在 incoming 缓冲区中使用的内容,以及来自早期缓冲区的未使用数据的任何积压。请注意,在 Socket 和 Stream 之间更改此代码实质上是将 Receive 更改为 Read - 除了方法相同。
以下内容基本上应该可以满足您的需求。您可以使用ReadNext() API,直到获得null(表示流结束),也可以使用ReadAll(),它为您提供IEnumerable<string> 序列。编码和缓冲区大小可供您通过构造函数进行调整,但默认为合理的值。
foreach (var s in reader.ReadAll())
Console.WriteLine(s);
代码:
class EtxReader : IDisposable
{
public IEnumerable<string> ReadAll()
{
string s;
while ((s = ReadNext()) != null) yield return s;
}
public void Dispose()
{
if (socket != null) socket.Dispose();
socket = null;
if (backlog != null) backlog.Dispose();
backlog = null;
buffer = null;
encoding = null;
}
public EtxReader(Socket socket, Encoding encoding = null, int bufferSize = 4096)
{
this.socket = socket;
this.encoding = encoding ?? Encoding.UTF8;
this.buffer = new byte[bufferSize];
}
private Encoding encoding;
private Socket socket;
int index, count;
byte[] buffer;
private bool ReadMore()
{
index = count = 0;
int bytes = socket.Receive(buffer);
if (bytes > 0)
{
count = bytes;
return true;
}
return false;
}
public const byte ETX = 3;
private MemoryStream backlog = new MemoryStream();
public string ReadNext()
{
string s;
if (count == 0)
{
if (!ReadMore()) return null;
}
// at this point, we expect there to be *some* data;
// this may or may not include the ETX terminator
var etxIndex = Array.IndexOf(buffer, ETX, index);
if (etxIndex >= 0)
{
// found another message in the existing buffer
int len = etxIndex - index;
s = encoding.GetString(buffer, index, len);
index = etxIndex + 1;
count -= (len + 1);
return s;
}
// no ETX in the buffer, so we'll need to fetch more data;
// buffer the unconsumed data that we have
backlog.SetLength(0);
backlog.Write(buffer, index, count);
bool haveEtx;
do
{
if (!ReadMore())
{
// we had unused data; this must signal an error
throw new EndOfStreamException();
}
etxIndex = Array.IndexOf(buffer, ETX, index);
haveEtx = etxIndex >= 0;
if (!haveEtx)
{
// keep buffering
backlog.Write(buffer, index, count);
}
} while (!haveEtx);
// now we have some data in the backlog, and the ETX in the buffer;
// for convenience, copy the rest of the next message into
// the backlog
backlog.Write(buffer, 0, etxIndex);
s = encoding.GetString(backlog.GetBuffer(), 0, (int)backlog.Length);
index = etxIndex + 1;
count -= (etxIndex + 1);
return s;
}
}