TcpReplyChannel.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //
  2. // TcpReplyChannel.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2009 Novell, Inc. http://www.novell.com
  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. using System;
  29. using System.Collections.Generic;
  30. using System.IO;
  31. using System.Net;
  32. using System.Net.Sockets;
  33. using System.ServiceModel;
  34. using System.Text;
  35. using System.Threading;
  36. namespace System.ServiceModel.Channels.NetTcp
  37. {
  38. internal class TcpReplyChannel : InternalReplyChannelBase
  39. {
  40. TcpClient client;
  41. TcpChannelInfo info;
  42. TcpBinaryFrameManager frame;
  43. public TcpReplyChannel (ChannelListenerBase listener, TcpChannelInfo info, TcpClient client)
  44. : base (listener)
  45. {
  46. this.client = client;
  47. this.info = info;
  48. }
  49. public MessageEncoder Encoder {
  50. get { return info.MessageEncoder; }
  51. }
  52. public override RequestContext ReceiveRequest (TimeSpan timeout)
  53. {
  54. if (timeout <= TimeSpan.Zero)
  55. throw new ArgumentException (String.Format ("Timeout value must be positive value. It was {0}", timeout));
  56. // FIXME: use timeout
  57. if (client == null)
  58. client = ((TcpChannelListener<IReplyChannel>) Manager).AcceptTcpClient (timeout);
  59. NetworkStream ns = client.GetStream ();
  60. frame = new TcpBinaryFrameManager (TcpBinaryFrameManager.SingletonUnsizedMode, ns, true) { Encoder = this.Encoder };
  61. // FIXME: use timeout
  62. if (!frame.ProcessPreambleRecipient ())
  63. return null;
  64. frame.ProcessPreambleAckRecipient ();
  65. var msg = frame.ReadUnsizedMessage (timeout);
  66. Logger.LogMessage (MessageLogSourceKind.TransportReceive, ref msg, info.BindingElement.MaxReceivedMessageSize);
  67. // LAMESPEC: it contradicts the protocol explanation at section 3.1.1.1.1 in [MC-NMF].
  68. // Moving ReadEndRecord() after context's WriteUnsizedMessage() causes TCP connection blocking.
  69. frame.ReadEndRecord ();
  70. return new TcpRequestContext (this, msg);
  71. }
  72. class TcpRequestContext : InternalRequestContext
  73. {
  74. public TcpRequestContext (TcpReplyChannel owner, Message request)
  75. : base (owner.Manager)
  76. {
  77. this.owner = owner;
  78. this.request = request;
  79. }
  80. TcpReplyChannel owner;
  81. Message request;
  82. public override Message RequestMessage {
  83. get { return request; }
  84. }
  85. public override void Abort ()
  86. {
  87. Close (TimeSpan.Zero);
  88. }
  89. public override void Close (TimeSpan timeout)
  90. {
  91. }
  92. public override void Reply (Message message, TimeSpan timeout)
  93. {
  94. Logger.LogMessage (MessageLogSourceKind.TransportSend, ref message, owner.info.BindingElement.MaxReceivedMessageSize);
  95. owner.frame.WriteUnsizedMessage (message, timeout);
  96. // FIXME: consider timeout here too.
  97. owner.frame.WriteEndRecord ();
  98. }
  99. }
  100. public override bool TryReceiveRequest (TimeSpan timeout, out RequestContext context)
  101. {
  102. try {
  103. context = ReceiveRequest (timeout);
  104. return context != null;
  105. } catch (Exception ex) {
  106. // FIXME: log it?
  107. // Console.WriteLine (ex);
  108. context = null;
  109. return false;
  110. }
  111. }
  112. public override bool WaitForRequest (TimeSpan timeout)
  113. {
  114. throw new NotImplementedException ();
  115. }
  116. bool close_started;
  117. object close_lock = new object ();
  118. protected override void OnClose (TimeSpan timeout)
  119. {
  120. lock (close_lock) {
  121. if (close_started)
  122. return;
  123. close_started = true;
  124. }
  125. client.Close ();
  126. client = null;
  127. base.OnClose (timeout);
  128. }
  129. protected override void OnOpen (TimeSpan timeout)
  130. {
  131. }
  132. }
  133. }