C# 从 SslStream 连续读取(长连接,持续长达数天)并且有效地没有无限循环

标签 c# sockets ssl sslstream

我对 C# 完全陌生,需要加密客户端和服务器之间发送和接收的数据,在谷歌上搜索了两天后,学会了最好的方法是使用 SslStream,我找到的一些答案给出了很好的例子,但他们都以某种方式假设我们只需要阅读一条消息然后关闭连接,这完全不是我的情况,每当用户触发他的设备通过持久连接发送消息时,我都必须阅读。
Microsoft 文档中的一个示例:

static string ReadMessage(SslStream sslStream)
    {
        // Read the  message sent by the client.
        // The client signals the end of the message using the
        // "<EOF>" marker.
        byte [] buffer = new byte[2048];
        StringBuilder messageData = new StringBuilder();
        int bytes = -1;
        do
        {
            // Read the client's test message.
            bytes = sslStream.Read(buffer, 0, buffer.Length);

            // Use Decoder class to convert from bytes to UTF8
            // in case a character spans two buffers.
            Decoder decoder = Encoding.UTF8.GetDecoder();
            char[] chars = new char[decoder.GetCharCount(buffer,0,bytes)];
            decoder.GetChars(buffer, 0, bytes, chars,0);
            messageData.Append (chars);
            // Check for EOF or an empty message. <------   In my case,I don't have EOF
            if (messageData.ToString().IndexOf("<EOF>") != -1)
            {
                break;
            }
        } while (bytes !=0);

        return messageData.ToString();
    }
和其他答案实际上告诉我如何从 SslStream 连续读取,但他们正在使用无限循环来做到这一点,在服务器端,可能有数千个客户端连接到它,所以可能的性能不佳让我担心,就像这个:
Read SslStream continuously in C# Web MVC 5 project
所以我想知道是否有更好的方法可以从持久的 SslStream 连接中连续读取。
我知道裸 socket 可以使用 SocketAsyncEventArgs 要知道什么时候准备好新数据,我希望我可以用 SslStream 做到这一点,可能我误解了一些东西,任何想法都将不胜感激,在此先感谢。

最佳答案

这是我的镜头。我选择了递归,而不是永远循环。此方法将立即返回,但会在 EOF 时触发事件。被击中,继续阅读:

public static void ReadFromSSLStreamAsync(
    SslStream sslStream,
    Action<string> result,
    Action<Exception> error,
    StringBuilder stringBuilder = null)
{
    const string EOFToken = "<EOF>";

    stringBuilder = stringBuilder ?? new StringBuilder();
    var buffer = new byte[4096];

    try
    {
        sslStream.BeginRead(buffer, 0, buffer.Length, asyncResult =>
        {
            // Read all bytes avaliable from stream and then
            // add them to string builder
            {
                int bytesRead;
                try
                {
                    bytesRead = sslStream.EndRead(asyncResult);
                }
                catch (Exception ex)
                {
                    error?.Invoke(ex);
                    return;
                }

                // Use Decoder class to convert from bytes to
                // UTF8 in case a character spans two buffers.
                var decoder = Encoding.UTF8.GetDecoder();
                var buf = new char[decoder.GetCharCount(buffer, 0, bytesRead)];
                decoder.GetChars(buffer, 0, bytesRead, buf, 0);
                stringBuilder.Append(buf);
            }

            // Find the EOFToken, if found copy all data before the token
            // and send it to event, then remove it from string builder
            {
                int tokenIndex;
                while((tokenIndex = stringBuilder.ToString().IndexOf(EOFToken)) != -1)
                {
                    var buf = new char[tokenIndex];
                    stringBuilder.CopyTo(0, buf, 0, tokenIndex);
                    result?.Invoke(new string(buf));
                    stringBuilder.Remove(0, tokenIndex + EOFToken.Length);
                }
            }

            // Continue reading...
            ReadFromSSLStreamAsync(sslStream, result, error, stringBuilder);
        }, null);
    }
    catch(Exception ex)
    {
        error?.Invoke(ex);
    }
}
你可以这样称呼它:
ReadFromSSLStreamAsync(sslStream, sslData =>
{
    Console.WriteLine($"Finished: {sslData}");
}, error =>
{
    Console.WriteLine($"Errored: {error}");
});
这不是 TaskAsync , 所以你不必 await在上面。但它是异步的,因此您的线程可以继续执行其他操作。

关于C# 从 SslStream 连续读取(长连接,持续长达数天)并且有效地没有无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63534749/

相关文章:

c# - 无法将 T 与 K 相加,因为它返回 T(或数字的通用)

javascript - 在 node.js 中安装 Socket.io 包

javascript - 实时多人游戏-区域广播

linux - 从 linux 中的特定 ip 地址连接

java - 具有自己的证书颁发机构的 Cassandra SSL

apache - HTTPS 请求忽略重写规则

ssl - 使用 TLS V1.0 而非 SSL3.0 连接的 Web 开发软件

C# - 排序 datagridview 时出现问题

c# - 拦截 Controller

c# - 在本地主机上测试 UDP