I'm using an XmlWriter
to send an XM开发者_开发百科PP (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?
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.
精彩评论