TcpReplyChannel.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //
  2. // HttpReplyChannel.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2006 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
  37. {
  38. internal class TcpReplyChannel : ReplyChannelBase
  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. var msg = frame.ReadUnsizedMessage (timeout);
  57. // It somehow receives EndRecord now ...
  58. frame.ProcessEndRecordRecipient ();
  59. return new TcpRequestContext (this, msg);
  60. }
  61. class TcpRequestContext : InternalRequestContext
  62. {
  63. public TcpRequestContext (TcpReplyChannel owner, Message request)
  64. : base (owner.Manager)
  65. {
  66. this.owner = owner;
  67. this.request = request;
  68. }
  69. TcpReplyChannel owner;
  70. Message request;
  71. public override Message RequestMessage {
  72. get { return request; }
  73. }
  74. public override void Abort ()
  75. {
  76. Close (TimeSpan.Zero);
  77. }
  78. public override void Close (TimeSpan timeout)
  79. {
  80. }
  81. public override void Reply (Message message, TimeSpan timeout)
  82. {
  83. if (message.Headers.RelatesTo == null)
  84. message.Headers.RelatesTo = request.Headers.MessageId;
  85. DateTime start = DateTime.Now;
  86. owner.frame.WriteUnsizedMessage (message, timeout);
  87. owner.frame.ProcessEndRecordInitiator ();
  88. }
  89. }
  90. public override bool TryReceiveRequest (TimeSpan timeout, out RequestContext context)
  91. {
  92. try {
  93. DateTime start = DateTime.Now;
  94. context = ReceiveRequest (timeout);
  95. if (context != null)
  96. return true;
  97. // received EndRecord, so close the session and return false instead.
  98. // (Closing channel here might not be a good idea, but right now I have no better way.)
  99. Close (timeout - (DateTime.Now - start));
  100. return false;
  101. } catch (TimeoutException) {
  102. context = null;
  103. return false;
  104. }
  105. }
  106. public override bool WaitForRequest (TimeSpan timeout)
  107. {
  108. throw new NotImplementedException ();
  109. }
  110. public override EndpointAddress LocalAddress {
  111. get { throw new NotImplementedException (); }
  112. }
  113. protected override void OnClose (TimeSpan timeout)
  114. {
  115. if (client != null)
  116. client.Close ();
  117. }
  118. protected override void OnOpen (TimeSpan timeout)
  119. {
  120. NetworkStream ns = client.GetStream ();
  121. frame = new TcpBinaryFrameManager (TcpBinaryFrameManager.SingletonUnsizedMode, ns, true) { Encoder = this.Encoder, EncodingRecord = TcpBinaryFrameManager.EncodingBinary };
  122. frame.ProcessPreambleRecipient ();
  123. frame.ProcessPreambleAckRecipient ();
  124. }
  125. }
  126. }