IpcTransport.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // System.Runtime.Remoting.Channels.Ipc.Win32.IpcTransport.cs
  3. //
  4. // Author: Robert Jordan ([email protected])
  5. //
  6. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  7. //
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. #if NET_2_0
  29. using System;
  30. using System.IO;
  31. using System.Runtime.Remoting;
  32. using System.Runtime.Remoting.Channels;
  33. using System.Runtime.Remoting.Messaging;
  34. using System.Runtime.Serialization.Formatters.Binary;
  35. namespace System.Runtime.Remoting.Channels.Ipc.Win32
  36. {
  37. /// <summary>
  38. /// IPC transport helper
  39. /// </summary>
  40. internal class IpcTransport
  41. {
  42. readonly NamedPipeSocket socket;
  43. readonly BinaryFormatter formatter = new BinaryFormatter();
  44. /// <summary>
  45. /// Creates a new object.
  46. /// </summary>
  47. /// <param name="socket">The named pipe.</param>
  48. public IpcTransport(NamedPipeSocket socket)
  49. {
  50. this.socket = socket;
  51. }
  52. /// <summary>
  53. /// Writes a request.
  54. /// </summary>
  55. /// <param name="header"></param>
  56. /// <param name="requestStream"></param>
  57. public void Write(ITransportHeaders header, Stream requestStream)
  58. {
  59. Stream bs = socket.GetStream();
  60. MemoryStream m = new MemoryStream();
  61. formatter.Serialize(m, header);
  62. m.Position = 0;
  63. byte[] bytes = BitConverter.GetBytes((int)m.Length);
  64. bs.Write(bytes, 0, bytes.Length);
  65. m.WriteTo(bs);
  66. try
  67. {
  68. bytes = BitConverter.GetBytes((int)requestStream.Length);
  69. bs.Write(bytes, 0, bytes.Length);
  70. IpcChannelHelper.Copy(requestStream, bs);
  71. }
  72. catch
  73. {
  74. bs.Write(bytes, 0, bytes.Length);
  75. }
  76. bs.Flush();
  77. }
  78. /// <summary>
  79. /// Reads a response.
  80. /// </summary>
  81. /// <param name="headers"></param>
  82. /// <param name="responseStream"></param>
  83. public void Read(out ITransportHeaders headers, out Stream responseStream)
  84. {
  85. byte[] bytes;
  86. long length;
  87. bytes = BitConverter.GetBytes(int.MaxValue);
  88. socket.Receive(bytes, 0, bytes.Length);
  89. length = BitConverter.ToInt32(bytes, 0);
  90. if (length != int.MaxValue && length > 0)
  91. {
  92. bytes = new byte[length];
  93. socket.Receive(bytes, 0, (int)length);
  94. MemoryStream m = new MemoryStream(bytes);
  95. headers = (ITransportHeaders) formatter.Deserialize(m);
  96. }
  97. else
  98. {
  99. headers = new TransportHeaders();
  100. }
  101. bytes = BitConverter.GetBytes(int.MaxValue);
  102. socket.Receive(bytes, 0, bytes.Length);
  103. length = BitConverter.ToInt32(bytes, 0);
  104. if (length != int.MaxValue && length > 0)
  105. {
  106. bytes = new byte[length];
  107. socket.Receive(bytes, 0, (int)length);
  108. responseStream = new MemoryStream(bytes);
  109. }
  110. else
  111. {
  112. responseStream = new MemoryStream(new byte[0]);
  113. }
  114. }
  115. /// <summary>
  116. /// Closes the unterlying named pipe socket.
  117. /// </summary>
  118. public void Close()
  119. {
  120. socket.Close();
  121. }
  122. }
  123. }
  124. #endif