c# - 实现 TCP 成帧的正确模式是什么?它是一个过滤器堆栈吗?

标签 c# .net tcp

我正在尝试实现一个强大的 TCP 库,它将允许用户选择一个应用程序协议(protocol)或实现他们自己的应用程序协议(protocol)并简单地将它们“插入”到客户端/服务器中。

我所说的协议(protocol)只是指能够定义如何将流构建到消息中的能力。

我正在为堆栈的其余部分使用内置的异步 TCP 库,并开发了一个客户端,它会在建立连接、读取或写入数据或引发异常时引发事件。

我有两种实现框架协议(protocol)的选择。第一个已经在工作,它是扩展客户端类并覆盖数据接收事件,以便仅在接收到完整消息时引发此事件。 (即在后台我缓冲来自套接字的原始数据,并根据协议(protocol)决定何时收到完整消息,然后才引发数据接收事件。)这类似于 Nito.Asynch 的方式。图书馆作品。

这种方法的问题在于它意味着每个新协议(protocol)都需要一个新的客户端实现。我更希望客户端维护一个可以添加或删除的内部过滤器堆栈。

当在套接字上接收到数据时,它被传递到第一个过滤器,该过滤器进行缓冲,直到它决定传递一个完整的消息,并删除了 header 或元数据。然后将其传递给堆栈中的下一个过滤器等。

通过这种方式,可以独立于库定义/开发过滤器,并根据配置(在运行时)将其注入(inject)客户端。

为了实现这一点,我考虑将过滤器定义为 System.IO.Stream(传入和传出)的成对实现,它们由客户端内部保存。

从套接字读取的数据将写入堆栈底部的传入流。然后从该流读取的数据将写入下一个流等,直到最后一个流(堆栈顶部)返回数据,然后由客户端返回。 (我的计划是使用 Stream 的 CopyTo() 函数)。

写入客户端的数据将被写入到顶部的输出流并复制到堆栈中,直到底部的输出流写入底层套接字。

显然有很多需要考虑的问题,我正在努力思考如何以正确的方式将其作为 Stream 对象。 示例:当有人调用 Flush()... 时我该怎么办?

这是实现此目标的好方法还是我在这里重新发明轮子?

Nito.Asynch 库

最佳答案

我正在回答我自己的问题,希望我的解决方案能得到一些好的评论并可能帮助其他人。

我为协议(protocol)过滤器和数据帧定义了两个接口(interface)。 (为了明确术语,我避免使用数据包一词,以免与较低级别协议(protocol)中定义的数据包混淆。)

虽然不是我自己的意图,但我猜这可以在任何传输协议(protocol)(即命名管道、TCP、串行)之上使用。

首先是数据框的定义。这包括“数据”(有效载荷)以及将传输数据构建为原子“消息”的任何字节。

/// <summary>
/// A packet of data with some form of meta data which frames the payload for transport in via a stream.
/// </summary>
public interface IFramedData
{
    /// <summary>
    /// Get the data payload from the framed data (excluding any bytes that are used to frame the data)
    /// i.e. The received data minus protocl specific framing
    /// </summary>
    public readonly byte[] Data { get; }

    /// <summary>
    /// Get the framed data (payload including framing bytes) ready to send
    /// </summary>
    /// <returns>Framed data</returns>
    public byte[] ToBytes();
}

然后是协议(protocol)过滤器,它从某个源(例如 TCP 套接字,或者如果它们在堆栈中使用的话,甚至是另一个过滤器)读取数据并将数据写回。

过滤器应读取数据(包括帧)并为每个完整的帧读取引发 DataReceived 事件。通过 IFramedData 实例的“数据”属性访问有效负载。

当数据被写入过滤器时,它应该适本地“构建”它,然后在每次准备好发送完整的数据帧时引发 DataToSend 事件。 (在我的例子中,这将是立竿见影的,但我试图允许一个协议(protocol),该协议(protocol)可能会发送固定长度的消息或出于其他原因缓冲输入,然后返回一个准备发送的完整帧。

/// <summary>
/// A protocol filter can be used to read and write data from/to a Stream and frame/deframe the messages.
/// </summary>
/// <typeparam name="TFramedData">The data frame that is handled by this filter</typeparam>
public interface IProtocolFilter<TFramedData> where TFramedData : IFramedData
{
    /// <summary>
    /// Should be raised whenever a complete data frame is ready to send.
    /// </summary>
    /// <remarks>
    /// May be raised after a call to <see cref="FlushSend()"/>
    /// </remarks>
    public event Action<TFramedData> DataToSend;

    /// <summary>
    /// Should be raised whenever a complete data frame has been received.
    /// </summary>
    /// <remarks>
    /// May be raised after a call to <see cref="FlushReceive()"/>
    /// </remarks>
    public event Action<TFramedData> DataReceived;

    /// <summary>
    /// Should be raised if any data written or read breaks the protocol.
    /// This could be due to any asynchronous operation that cannot be raised by the calling function.
    /// </summary>
    /// <remarks>
    /// Behaviour may be protocol specific such as flushing the read or write cache or even resetting the connection.
    /// </remarks>
    public event Action<Exception> ProtocolException;

    /// <summary>
    /// Read data into the recieve buffer
    /// </summary>
    /// <remarks>
    /// This may raise the DataReceived event (possibly more than once if multiple complete frames are read)
    /// </remarks>
    /// <param name="buffer">Data buffer</param>
    /// <param name="offset">Position within the buffer where data must start being read.</param>
    /// <param name="count">Number of bytes to read.</param>
    /// <returns></returns>
    public int Read(byte[] buffer, int offset, int count);

    /// <summary>
    /// Write data to the send buffer.
    /// </summary>
    /// <remarks>
    /// This may raise the DataToSend event (possibly more than once if the protocl requires the data is broken into multiple frames)
    /// </remarks>
    /// <param name="buffer">Data buffer</param>
    /// <param name="offset">Position within the buffer where data must start being read.</param>
    /// <param name="count">Number of bytes to read from the buffer</param>
    public void Write(byte[] buffer, int offset, int count);

    /// <summary>
    /// Flush any data from the receive buffer and if appropriate, raise a DataReceived event.
    /// </summary>
    public void FlushReceive();

    /// <summary>
    /// Flush any data from the send buffer and if appropriate, raise a DataToSend event.
    /// </summary>
    public void FlushSend();
}

然后我围绕 TcpClient 编写了一个非常简单的包装器,它执行异步读取和写入,并在协议(protocol)栈顶部的过滤器引发 DataReceived 事件或底部的过滤器引发 DataToSend 事件时引发事件(我也写将数据发送到套接字,但这允许应用程序监视它写入客户端的数据何时实际发送)。

关于c# - 实现 TCP 成帧的正确模式是什么?它是一个过滤器堆栈吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11310455/

相关文章:

c# - Control.Invoke 从运行表单的同一线程

jquery 调用我的 helloworld Web 服务

.net - 测试 TCP 端口是否在未连接的情况下打开

.net - 间歇性 XmlSerializer(部分)空文件

使用缓冲区和线程进行 Java TCP byteArray 传输

python - 监视数据的 TCP/IP 连接

c# - Entity Framework 连接池 : how to inject UserId into SQL Servers session_context?

c# - C#.net 中的 JSON Twitter 列表

python - "app.run(host=' 0.0.0 是什么意思? 0') "表示 Flask

c# - 自定义媒体元素