ContextInputChannelBase.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Channels
  5. {
  6. using System;
  7. using System.Collections.Generic;
  8. using System.ServiceModel;
  9. abstract class ContextInputChannelBase<TChannel> : LayeredChannel<TChannel> where TChannel : class, IInputChannel
  10. {
  11. ContextExchangeMechanism contextExchangeMechanism;
  12. ServiceContextProtocol contextProtocol;
  13. protected ContextInputChannelBase(ChannelManagerBase channelManager, TChannel innerChannel, ContextExchangeMechanism contextExchangeMechanism)
  14. : base(channelManager, innerChannel)
  15. {
  16. this.contextExchangeMechanism = contextExchangeMechanism;
  17. this.contextProtocol = new ServiceContextProtocol(contextExchangeMechanism);
  18. }
  19. public EndpointAddress LocalAddress
  20. {
  21. get { return this.InnerChannel.LocalAddress; }
  22. }
  23. public IAsyncResult BeginReceive(TimeSpan timeout, AsyncCallback callback, object state)
  24. {
  25. return this.InnerChannel.BeginReceive(timeout, callback, state);
  26. }
  27. public IAsyncResult BeginReceive(AsyncCallback callback, object state)
  28. {
  29. return this.InnerChannel.BeginReceive(callback, state);
  30. }
  31. public IAsyncResult BeginTryReceive(TimeSpan timeout, AsyncCallback callback, object state)
  32. {
  33. return this.InnerChannel.BeginTryReceive(timeout, callback, state);
  34. }
  35. public IAsyncResult BeginWaitForMessage(TimeSpan timeout, AsyncCallback callback, object state)
  36. {
  37. return this.InnerChannel.BeginWaitForMessage(timeout, callback, state);
  38. }
  39. public Message EndReceive(IAsyncResult result)
  40. {
  41. Message message = this.InnerChannel.EndReceive(result);
  42. ProcessContextHeader(message);
  43. return message;
  44. }
  45. public bool EndTryReceive(IAsyncResult result, out Message message)
  46. {
  47. if (this.InnerChannel.EndTryReceive(result, out message))
  48. {
  49. ProcessContextHeader(message);
  50. return true;
  51. }
  52. else
  53. {
  54. return false;
  55. }
  56. }
  57. public bool EndWaitForMessage(IAsyncResult result)
  58. {
  59. return this.InnerChannel.EndWaitForMessage(result);
  60. }
  61. public Message Receive(TimeSpan timeout)
  62. {
  63. Message message = this.InnerChannel.Receive(timeout);
  64. ProcessContextHeader(message);
  65. return message;
  66. }
  67. public Message Receive()
  68. {
  69. Message message = this.InnerChannel.Receive();
  70. ProcessContextHeader(message);
  71. return message;
  72. }
  73. public bool TryReceive(TimeSpan timeout, out Message message)
  74. {
  75. if (this.InnerChannel.TryReceive(timeout, out message))
  76. {
  77. ProcessContextHeader(message);
  78. return true;
  79. }
  80. else
  81. {
  82. return false;
  83. }
  84. }
  85. public bool WaitForMessage(TimeSpan timeout)
  86. {
  87. return this.InnerChannel.WaitForMessage(timeout);
  88. }
  89. void ProcessContextHeader(Message message)
  90. {
  91. if (message != null)
  92. {
  93. contextProtocol.OnIncomingMessage(message);
  94. }
  95. }
  96. }
  97. }