处理 XmlWriter 而不发送未完成的结束元素
我正在使用 XmlWriter
发送 XMPP (Jingle) 流。 XMPP 协议在协商 TLS 时替换了 XML 流,这意味着 XML 最终的结果如下:
<stream>
<features>
...
</features>
...TLS gets negotiated...
<stream>
...
</stream>
XML 最终格式不正确,因为有两个流开始标记。
我想做的是在 TLS 协商之前扔掉我正在使用的 XmlWriter 并创建一个全新的,它可以让我更好地模块化我的代码。但是,当我调用 myXmlWriter.Close() 来确保它被处理时,它将发送关闭流结束元素,这会破坏 XMPP 协议。
无论如何,我是否可以关闭 XmlWriter
而不发送未完成的结束元素?
I'm using an XmlWriter
to send an XMPP (Jingle) stream. The XMPP protocol replaces the an XML stream when it negotiates TLS which means the XML ends up like:
<stream>
<features>
...
</features>
...TLS gets negotiated...
<stream>
...
</stream>
The XML end up being not well formed because there are two stream start tags.
What I want to do is throw away the XmlWriter
I am using prior to the TLS negotiation and create a brand new one, it allows me to better modularise my code. However when I call myXmlWriter.Close()
to make sure it gets disposed it will send the closing stream end element which breaks the XMPP protocol.
Is there anyway I can close the XmlWriter
without it sending the outstanding end element?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
创建一个中间流,可用于断开 XmlWriter 与基本流的连接。
这不是最优雅的解决方案,下面的代码需要工作,因此在将其投入生产之前对其进行测试,但是这是关于想法的。
此类旨在用作
XmlWriter
和XmlWriter
输出到的流之间的流。该类只是将所有来自XmlWriter
的调用转发到基本流,但是一旦调用DisconnectBaseStream
,它就会停止转发它们,并且XmlWriter
无法控制不再是基础流了。您可以像这样使用此类:
同样,
DummyStream
是一个起点,需要一些工作。例如,您需要确保XmlWriter
在断开连接后不会进行会崩溃的调用,因此您需要使用Write
方法进行一些检查BaseStream
是否为null
,如果是,则跳过调用。Create an intermediate stream which you can use to disconnect the XmlWriter from the base stream.
This is not the most elegant solution, and the code below needs work, so test it before you put this into production, but it's about the idea.
This class is meant to be used as a stream between the
XmlWriter
and the stream theXmlWriter
is outputting to. This class simply forwards all calls from theXmlWriter
to the base stream, but once you callDisconnectBaseStream
, it stops forwarding them and theXmlWriter
cannot control the base stream anymore.You can use this class like this:
Again, the
DummyStream
is a starting point and will need some work. You will for example want to make sure that theXmlWriter
isn't making calls after the disconnect that will crash, so you will want to to some checking with e.g. theWrite
method whetherBaseStream
isnull
and if yes, just skip the call.