ConnectionModeReader.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Channels
  5. {
  6. using System.Diagnostics;
  7. using System.Runtime;
  8. using System.ServiceModel;
  9. using System.Threading;
  10. delegate void ConnectionModeCallback(ConnectionModeReader connectionModeReader);
  11. sealed class ConnectionModeReader : InitialServerConnectionReader
  12. {
  13. Exception readException;
  14. ServerModeDecoder decoder;
  15. byte[] buffer;
  16. int offset;
  17. int size;
  18. ConnectionModeCallback callback;
  19. static WaitCallback readCallback;
  20. TimeoutHelper receiveTimeoutHelper;
  21. public ConnectionModeReader(IConnection connection, ConnectionModeCallback callback, ConnectionClosedCallback closedCallback)
  22. : base(connection, closedCallback)
  23. {
  24. this.callback = callback;
  25. }
  26. public int BufferOffset
  27. {
  28. get { return offset; }
  29. }
  30. public int BufferSize
  31. {
  32. get { return size; }
  33. }
  34. public long StreamPosition
  35. {
  36. get { return decoder.StreamPosition; }
  37. }
  38. public TimeSpan GetRemainingTimeout()
  39. {
  40. return this.receiveTimeoutHelper.RemainingTime();
  41. }
  42. void Complete(Exception e)
  43. {
  44. // exception will be logged by the caller
  45. readException = e;
  46. Complete();
  47. }
  48. void Complete()
  49. {
  50. callback(this);
  51. }
  52. bool ContinueReading()
  53. {
  54. for (;;)
  55. {
  56. if (size == 0)
  57. {
  58. if (readCallback == null)
  59. {
  60. readCallback = new WaitCallback(ReadCallback);
  61. }
  62. if (Connection.BeginRead(0, Connection.AsyncReadBufferSize, GetRemainingTimeout(),
  63. readCallback, this) == AsyncCompletionResult.Queued)
  64. {
  65. break;
  66. }
  67. if (!GetReadResult()) // we're at EOF, bail
  68. {
  69. return false;
  70. }
  71. }
  72. for (;;)
  73. {
  74. int bytesDecoded;
  75. try
  76. {
  77. bytesDecoded = decoder.Decode(buffer, offset, size);
  78. }
  79. catch (CommunicationException e)
  80. {
  81. // see if we need to send back a framing fault
  82. string framingFault;
  83. if (FramingEncodingString.TryGetFaultString(e, out framingFault))
  84. {
  85. byte[] drainBuffer = new byte[128];
  86. InitialServerConnectionReader.SendFault(
  87. Connection, framingFault, drainBuffer, GetRemainingTimeout(),
  88. MaxViaSize + MaxContentTypeSize);
  89. base.Close(GetRemainingTimeout());
  90. }
  91. throw;
  92. }
  93. if (bytesDecoded > 0)
  94. {
  95. offset += bytesDecoded;
  96. size -= bytesDecoded;
  97. }
  98. if (decoder.CurrentState == ServerModeDecoder.State.Done)
  99. {
  100. return true;
  101. }
  102. if (size == 0)
  103. {
  104. break;
  105. }
  106. }
  107. }
  108. return false;
  109. }
  110. static void ReadCallback(object state)
  111. {
  112. ConnectionModeReader reader = (ConnectionModeReader)state;
  113. bool completeSelf = false;
  114. Exception completionException = null;
  115. try
  116. {
  117. if (reader.GetReadResult())
  118. {
  119. completeSelf = reader.ContinueReading();
  120. }
  121. }
  122. #pragma warning suppress 56500 // [....], transferring exception to caller
  123. catch (Exception e)
  124. {
  125. if (Fx.IsFatal(e))
  126. {
  127. throw;
  128. }
  129. completeSelf = true;
  130. completionException = e;
  131. }
  132. if (completeSelf)
  133. {
  134. reader.Complete(completionException);
  135. }
  136. }
  137. bool GetReadResult()
  138. {
  139. offset = 0;
  140. size = Connection.EndRead();
  141. if (size == 0)
  142. {
  143. if (this.decoder.StreamPosition == 0) // client timed out a cached connection
  144. {
  145. base.Close(GetRemainingTimeout());
  146. return false;
  147. }
  148. else
  149. {
  150. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(decoder.CreatePrematureEOFException());
  151. }
  152. }
  153. // restore ExceptionEventType to Error after the initial read for cached connections
  154. Connection.ExceptionEventType = TraceEventType.Error;
  155. if (buffer == null)
  156. {
  157. buffer = Connection.AsyncReadBuffer;
  158. }
  159. return true;
  160. }
  161. public FramingMode GetConnectionMode()
  162. {
  163. if (readException != null)
  164. {
  165. throw DiagnosticUtility.ExceptionUtility.ThrowHelper(readException, Connection.ExceptionEventType);
  166. }
  167. return decoder.Mode;
  168. }
  169. public void StartReading(TimeSpan receiveTimeout, Action connectionDequeuedCallback)
  170. {
  171. this.decoder = new ServerModeDecoder();
  172. this.receiveTimeoutHelper = new TimeoutHelper(receiveTimeout);
  173. this.ConnectionDequeuedCallback = connectionDequeuedCallback;
  174. bool completeSelf = false;
  175. Exception completionException = null;
  176. try
  177. {
  178. completeSelf = ContinueReading();
  179. }
  180. #pragma warning suppress 56500 // [....], transferring exception to caller
  181. catch (Exception e)
  182. {
  183. if (Fx.IsFatal(e))
  184. {
  185. throw;
  186. }
  187. completeSelf = true;
  188. completionException = e;
  189. }
  190. if (completeSelf)
  191. {
  192. Complete(completionException);
  193. }
  194. }
  195. }
  196. }