简体   繁体   English

具有GZipStream的C#SslStream

[英]C# SslStream with GZipStream

Is it possible to use GZipStream passing an SslStream in C#? 是否可以使用GZipStream在C#中传递SslStream? ie can you do 那你能做吗


GZipStream stream = new GZipStream(sslStream, CompressionMode.Compress);
stream.Write(...);

...

GZipStream stream = new GZipStream(sslStream, CompressionMode.Decompress);
stream.Read(...);

If this is possible, is the SslStream still in a useable state after this or should it be closed? 如果可能,SslStream在此之后是否仍处于可用状态,还是应该将其关闭?

Thanks. 谢谢。

I can't think why not; 我不知道为什么不这样做。 the SSL stream will just be given bytes, but that is what it expected anyway. SSL流将仅被提供字节,但这还是它所期望的。 The important thing is to close the GZipStream properly, as even Flush() doesn't always really flush (due to having to keep a running buffer for compression). 重要的是正确关闭GZipStream ,因为即使Flush()也不总是真正刷新(由于必须保留运行中的缓冲区进行压缩)。 So: 所以:

using(GZipStream stream = new GZipStream(sslStream, CompressionMode.Compress)) {
    stream.Write(...);
    stream.Close(); // arguably overkill; the Dispose() from "using" should be enough
}

I'm also assuming that you aren't reading and writing to the same SSL stream (ie that is two separate examples, not one example), as that doesn't sound likely to work. 我还假设您不是在读取和写入同一SSL流(即,这是两个单独的示例,而不是一个示例),因为这听起来不太可行。

It is possible and may sometimes work, but it also may sometimes fail. 这是可能的,有时可能会起作用,但有时也可能会失败。 It should work if your protocol always allows you to "cleanly close" the GZipStream before the SSLStream closes. 如果您的协议始终允许您在SSLStream关闭之前“完全关闭” GZipStream,则它应该可以工作。 By "cleanly close" I mean that the GZipStream closure effects (the flushing of the compression buffer) can occur and propogate to the peer. “干净关闭”是指GZipStream的关闭效果(压缩缓冲区的刷新)会发生并传播到同级。 It may fail because Ssl allows for rather drastic closure semantics. 它可能会失败,因为Ssl允许相当严格的闭包语义。 See section 7.2.1 of RFC 2246 . 请参阅RFC 2246的 7.2.1节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM