处理 XmlWriter 而不发送未完成的结束元素

发布于 2024-10-02 12:44:37 字数 469 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凡尘雨 2024-10-09 12:44:37

创建一个中间流,可用于断开 XmlWriter 与基本流的连接。

这不是最优雅的解决方案,下面的代码需要工作,因此在将其投入生产之前对其进行测试,但是这是关于想法的。

public class DummyStream : Stream
{
    public DummyStream(Stream baseStream)
    {
        if (baseStream == null)
            throw new ArgumentNullException("baseStream");

        BaseStream = baseStream;
    }

    public Stream BaseStream { get; private set; }

    public void DisconnectBaseStream()
    {
        BaseStream = null;
    }

    private Stream GetBaseStream()
    {
        return BaseStream ?? Stream.Null;
    }

    public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
    {
        return GetBaseStream().BeginRead(buffer, offset, count, callback, state);
    }

    public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
    {
        return GetBaseStream().BeginWrite(buffer, offset, count, callback, state);
    }

    public override bool CanRead
    {
        get { return GetBaseStream().CanRead; }
    }

    public override bool CanSeek
    {
        get { return GetBaseStream().CanSeek; }
    }

    public override bool CanTimeout
    {
        get { return GetBaseStream().CanTimeout; }
    }

    public override bool CanWrite
    {
        get { return GetBaseStream().CanWrite; }
    }

    public override void Close()
    {
        // We do not close the BaseStream because this stream
        // is just a wrapper.

        // GetBaseStream().Close();
    }

    public override ObjRef CreateObjRef(Type requestedType)
    {
        return GetBaseStream().CreateObjRef(requestedType);
    }

    protected override void Dispose(bool disposing)
    {
        base.Dispose(disposing);

        // We do not dispose the BaseStream because this stream
        // is just a wrapper.
    }

    public override int EndRead(IAsyncResult asyncResult)
    {
        return GetBaseStream().EndRead(asyncResult);
    }

    public override void EndWrite(IAsyncResult asyncResult)
    {
        GetBaseStream().EndWrite(asyncResult);
    }

    public override bool Equals(object obj)
    {
        return GetBaseStream().Equals(obj);
    }

    public override void Flush()
    {
        GetBaseStream().Flush();
    }

    public override int GetHashCode()
    {
        return GetBaseStream().GetHashCode();
    }

    public override object InitializeLifetimeService()
    {
        return GetBaseStream().InitializeLifetimeService();
    }

    public override long Length
    {
        get { return GetBaseStream().Length; }
    }

    public override long Position
    {
        get { return GetBaseStream().Position; }
        set { GetBaseStream().Position = value; }
    }

    public override int Read(byte[] buffer, int offset, int count)
    {
        return GetBaseStream().Read(buffer, offset, count);
    }

    public override int ReadByte()
    {
        return GetBaseStream().ReadByte();
    }

    public override int ReadTimeout
    {
        get { return GetBaseStream().ReadTimeout; }
        set { GetBaseStream().ReadTimeout = value; }
    }

    public override long Seek(long offset, SeekOrigin origin)
    {
        return GetBaseStream().Seek(offset, origin);
    }

    public override void SetLength(long value)
    {
        GetBaseStream().SetLength(value);
    }

    public override string ToString()
    {
        return GetBaseStream().ToString();
    }

    public override void Write(byte[] buffer, int offset, int count)
    {
        GetBaseStream().Write(buffer, offset, count);
    }

    public override void WriteByte(byte value)
    {
        GetBaseStream().WriteByte(value);
    }

    public override int WriteTimeout
    {
        get { return GetBaseStream().WriteTimeout; }
        set { GetBaseStream().WriteTimeout = value; }
    }
}

此类旨在用作 XmlWriterXmlWriter 输出到的流之间的流。该类只是将所有来自 XmlWriter 的调用转发到基本流,但是一旦调用 DisconnectBaseStream,它就会停止转发它们,并且 XmlWriter 无法控制不再是基础流了。

您可以像这样使用此类:

using (var stream = /* stream used to communicate with */)
{
    using (var wrapperStream = new DummyStream(stream))
    using (var writer = XmlWriter.Create(wrapperStream))
    {
        // Do you work here.

        // Now, disconnect the dummy stream so that the XML writer
        // cannot send more data.

        wrapperStream.DisconnectBaseStream();

        // End of the using block will close the XmlWriter and it
        // cannot send more data to the base stream.
    }

    // Perform TLS negotiation etc...
}

同样,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.

public class DummyStream : Stream
{
    public DummyStream(Stream baseStream)
    {
        if (baseStream == null)
            throw new ArgumentNullException("baseStream");

        BaseStream = baseStream;
    }

    public Stream BaseStream { get; private set; }

    public void DisconnectBaseStream()
    {
        BaseStream = null;
    }

    private Stream GetBaseStream()
    {
        return BaseStream ?? Stream.Null;
    }

    public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
    {
        return GetBaseStream().BeginRead(buffer, offset, count, callback, state);
    }

    public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
    {
        return GetBaseStream().BeginWrite(buffer, offset, count, callback, state);
    }

    public override bool CanRead
    {
        get { return GetBaseStream().CanRead; }
    }

    public override bool CanSeek
    {
        get { return GetBaseStream().CanSeek; }
    }

    public override bool CanTimeout
    {
        get { return GetBaseStream().CanTimeout; }
    }

    public override bool CanWrite
    {
        get { return GetBaseStream().CanWrite; }
    }

    public override void Close()
    {
        // We do not close the BaseStream because this stream
        // is just a wrapper.

        // GetBaseStream().Close();
    }

    public override ObjRef CreateObjRef(Type requestedType)
    {
        return GetBaseStream().CreateObjRef(requestedType);
    }

    protected override void Dispose(bool disposing)
    {
        base.Dispose(disposing);

        // We do not dispose the BaseStream because this stream
        // is just a wrapper.
    }

    public override int EndRead(IAsyncResult asyncResult)
    {
        return GetBaseStream().EndRead(asyncResult);
    }

    public override void EndWrite(IAsyncResult asyncResult)
    {
        GetBaseStream().EndWrite(asyncResult);
    }

    public override bool Equals(object obj)
    {
        return GetBaseStream().Equals(obj);
    }

    public override void Flush()
    {
        GetBaseStream().Flush();
    }

    public override int GetHashCode()
    {
        return GetBaseStream().GetHashCode();
    }

    public override object InitializeLifetimeService()
    {
        return GetBaseStream().InitializeLifetimeService();
    }

    public override long Length
    {
        get { return GetBaseStream().Length; }
    }

    public override long Position
    {
        get { return GetBaseStream().Position; }
        set { GetBaseStream().Position = value; }
    }

    public override int Read(byte[] buffer, int offset, int count)
    {
        return GetBaseStream().Read(buffer, offset, count);
    }

    public override int ReadByte()
    {
        return GetBaseStream().ReadByte();
    }

    public override int ReadTimeout
    {
        get { return GetBaseStream().ReadTimeout; }
        set { GetBaseStream().ReadTimeout = value; }
    }

    public override long Seek(long offset, SeekOrigin origin)
    {
        return GetBaseStream().Seek(offset, origin);
    }

    public override void SetLength(long value)
    {
        GetBaseStream().SetLength(value);
    }

    public override string ToString()
    {
        return GetBaseStream().ToString();
    }

    public override void Write(byte[] buffer, int offset, int count)
    {
        GetBaseStream().Write(buffer, offset, count);
    }

    public override void WriteByte(byte value)
    {
        GetBaseStream().WriteByte(value);
    }

    public override int WriteTimeout
    {
        get { return GetBaseStream().WriteTimeout; }
        set { GetBaseStream().WriteTimeout = value; }
    }
}

This class is meant to be used as a stream between the XmlWriter and the stream the XmlWriter is outputting to. This class simply forwards all calls from the XmlWriter to the base stream, but once you call DisconnectBaseStream, it stops forwarding them and the XmlWriter cannot control the base stream anymore.

You can use this class like this:

using (var stream = /* stream used to communicate with */)
{
    using (var wrapperStream = new DummyStream(stream))
    using (var writer = XmlWriter.Create(wrapperStream))
    {
        // Do you work here.

        // Now, disconnect the dummy stream so that the XML writer
        // cannot send more data.

        wrapperStream.DisconnectBaseStream();

        // End of the using block will close the XmlWriter and it
        // cannot send more data to the base stream.
    }

    // Perform TLS negotiation etc...
}

Again, the DummyStream is a starting point and will need some work. You will for example want to make sure that the XmlWriter isn't making calls after the disconnect that will crash, so you will want to to some checking with e.g. the Write method whether BaseStream is null and if yes, just skip the call.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文