TcpReplyChannel.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. DateTime start = DateTime.Now;
  57. // FIXME: use timeout
  58. if (client == null)
  59. client = ((TcpChannelListener<IReplyChannel>) Manager).AcceptTcpClient (timeout);
  60. NetworkStream ns = client.GetStream ();
  61. frame = new TcpBinaryFrameManager (TcpBinaryFrameManager.SingletonUnsizedMode, ns, true) { Encoder = this.Encoder };
  62. // FIXME: use timeout
  63. if (!frame.ProcessPreambleRecipient ())
  64. return null;
  65. frame.ProcessPreambleAckRecipient ();
  66. var msg = frame.ReadUnsizedMessage (timeout);
  67. Logger.LogMessage (MessageLogSourceKind.TransportReceive, ref msg, info.BindingElement.MaxReceivedMessageSize);
  68. // LAMESPEC: it contradicts the protocol explanation at section 3.1.1.1.1 in [MC-NMF].
  69. // Moving ReadEndRecord() after context's WriteUnsizedMessage() causes TCP connection blocking.
  70. frame.ReadEndRecord ();
  71. return new TcpRequestContext (this, msg);
  72. }
  73. class TcpRequestContext : InternalRequestContext
  74. {
  75. public TcpRequestContext (TcpReplyChannel owner, Message request)
  76. : base (owner.Manager)
  77. {
  78. this.owner = owner;
  79. this.request = request;
  80. }
  81. TcpReplyChannel owner;
  82. Message request;
  83. public override Message RequestMessage {
  84. get { return request; }
  85. }
  86. public override void Abort ()
  87. {
  88. Close (TimeSpan.Zero);
  89. }
  90. public override void Close (TimeSpan timeout)
  91. {
  92. }
  93. public override void Reply (Message message, TimeSpan timeout)
  94. {
  95. Logger.LogMessage (MessageLogSourceKind.TransportSend, ref message, owner.info.BindingElement.MaxReceivedMessageSize);
  96. DateTime start = DateTime.Now;
  97. owner.frame.WriteUnsizedMessage (message, timeout);
  98. // FIXME: consider timeout here too.
  99. owner.frame.WriteEndRecord ();
  100. }
  101. }
  102. public override bool TryReceiveRequest (TimeSpan timeout, out RequestContext context)
  103. {
  104. try {
  105. DateTime start = DateTime.Now;
  106. context = ReceiveRequest (timeout);
  107. return context != null;
  108. } catch (Exception ex) {
  109. // FIXME: log it?
  110. // Console.WriteLine (ex);
  111. context = null;
  112. return false;
  113. }
  114. }
  115. public override bool WaitForRequest (TimeSpan timeout)
  116. {
  117. throw new NotImplementedException ();
  118. }
  119. protected override void OnClose (TimeSpan timeout)
  120. {
  121. client.Close ();
  122. client = null;
  123. base.OnClose (timeout);
  124. }
  125. protected override void OnOpen (TimeSpan timeout)
  126. {
  127. }
  128. }
  129. }