PageRenderTime 52ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/sipsorcery-xmpp/SIPSorcery.XMPP/WrappedStream.cs

https://github.com/thecc4re/sipsorcery-mono
C# | 156 lines | 101 code | 20 blank | 35 comment | 6 complexity | 7b0a6c56172fff747d8601fbc574ec5c MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. //-----------------------------------------------------------------------------
  2. // Filename: WrappedStream.cs
  3. //
  4. // Description: A class that wraps a stream so that an XmlWriter and XmlReader can be used on top
  5. // of an XMPP network stream. The wrapper is needed because the .Net XmlWriter does not like the root
  6. // element being left open when it gets closed. XMPP requires that the root element is not closed when
  7. // transitioning the stream to TLS.
  8. //
  9. // History:
  10. // 13 Dec 2010 Aaron Clauson Created.
  11. //
  12. // License:
  13. // This software is licensed under the BSD License http://www.opensource.org/licenses/bsd-license.php
  14. //
  15. // Copyright (c) 2010 Aaron Clauson (aaron@sipsorcery.com), Hobart, Tasmania, Australia (www.sipsorcery.com)
  16. // All rights reserved.
  17. //
  18. // Redistribution and use in source and binary forms, with or without modification, are permitted provided that
  19. // the following conditions are met:
  20. //
  21. // Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  22. // Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
  23. // disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of SIP Sorcery.
  24. // nor the names of its contributors may be used to endorse or promote products derived from this software without specific
  25. // prior written permission.
  26. //
  27. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
  28. // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  29. // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  30. // OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  31. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  32. // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. // POSSIBILITY OF SUCH DAMAGE.
  34. //-----------------------------------------------------------------------------
  35. using System;
  36. using System.Collections.Generic;
  37. using System.IO;
  38. using System.Linq;
  39. using System.Text;
  40. using System.Xml;
  41. namespace SIPSorcery.XMPP
  42. {
  43. public class WrappedStream : Stream
  44. {
  45. private Stream m_wrappedStream;
  46. private StreamWriter m_traceStream;
  47. private Boolean m_blockIO;
  48. public WrappedStream(Stream stream, StreamWriter traceStream)
  49. {
  50. m_wrappedStream = stream;
  51. m_traceStream = traceStream;
  52. }
  53. public override bool CanRead
  54. {
  55. get { return m_wrappedStream.CanRead; }
  56. }
  57. public override bool CanSeek
  58. {
  59. get { return m_wrappedStream.CanSeek; }
  60. }
  61. public override bool CanWrite
  62. {
  63. get { return m_wrappedStream.CanWrite; }
  64. }
  65. public override void Flush()
  66. {
  67. m_wrappedStream.Flush();
  68. }
  69. public override long Length
  70. {
  71. get { return m_wrappedStream.Length; }
  72. }
  73. public override long Position
  74. {
  75. get
  76. {
  77. return m_wrappedStream.Position;
  78. }
  79. set
  80. {
  81. m_wrappedStream.Position = value;
  82. }
  83. }
  84. public override int Read(byte[] buffer, int offset, int count)
  85. {
  86. if (!m_blockIO)
  87. {
  88. int bytesRead = m_wrappedStream.Read(buffer, offset, count);
  89. if (m_traceStream != null)
  90. {
  91. m_traceStream.WriteLine();
  92. m_traceStream.WriteLine("Receive=>");
  93. m_traceStream.Flush();
  94. m_traceStream.BaseStream.Write(buffer, offset, bytesRead);
  95. m_traceStream.Flush();
  96. }
  97. return bytesRead;
  98. }
  99. else
  100. {
  101. return 0;
  102. }
  103. }
  104. public override long Seek(long offset, SeekOrigin origin)
  105. {
  106. return m_wrappedStream.Seek(offset, origin);
  107. }
  108. public override void SetLength(long value)
  109. {
  110. m_wrappedStream.SetLength(value);
  111. }
  112. public override void Write(byte[] buffer, int offset, int count)
  113. {
  114. if (!m_blockIO)
  115. {
  116. //Console.WriteLine("Send => " + Encoding.UTF8.GetString(buffer, offset, count));
  117. if (m_traceStream != null)
  118. {
  119. m_traceStream.WriteLine();
  120. m_traceStream.WriteLine("Send=>");
  121. m_traceStream.Flush();
  122. m_traceStream.BaseStream.Write(buffer, offset, count);
  123. m_traceStream.Flush();
  124. }
  125. m_wrappedStream.Write(buffer, offset, count);
  126. }
  127. }
  128. public void BlockIO()
  129. {
  130. m_blockIO = true;
  131. }
  132. public override void Close()
  133. {
  134. }
  135. }
  136. }