/mcs/class/System.ServiceModel/System.ServiceModel/ClientRealProxy.cs

https://github.com/t-ashula/mono · C# · 142 lines · 95 code · 15 blank · 32 comment · 22 complexity · 76a0cf1d3b93e7338ca098e0e499fce0 MD5 · raw file

  1. //
  2. // ClientRealProxy.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <atsushi@ximian.com>
  6. //
  7. // Copyright (C) 2011 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. #if !DISABLE_REAL_PROXY
  29. using System;
  30. using System.Collections.Generic;
  31. using System.Linq;
  32. using System.Reflection;
  33. using System.Runtime.Remoting;
  34. using System.Runtime.Remoting.Messaging;
  35. using System.Runtime.Remoting.Proxies;
  36. using System.ServiceModel.Channels;
  37. using System.ServiceModel.Description;
  38. using System.ServiceModel.Dispatcher;
  39. using System.ServiceModel.MonoInternal;
  40. using System.Threading;
  41. namespace System.ServiceModel
  42. {
  43. class ClientRealProxy : RealProxy, IRemotingTypeInfo
  44. {
  45. public ClientRealProxy (Type type, IInternalContextChannel channel, bool isDuplex)
  46. : base (type)
  47. {
  48. this.channel = channel;
  49. this.isDuplex = isDuplex;
  50. }
  51. bool isDuplex;
  52. IInternalContextChannel channel;
  53. Dictionary<object,object[]> saved_params = new Dictionary<object,object[]> ();
  54. // It is used for such case that EndProcess() gets invoked
  55. // before storing params is done after BeginProcess().
  56. ManualResetEvent wait = new ManualResetEvent (false);
  57. #region IRemotingTypeInfo
  58. public virtual string TypeName { get; set; }
  59. static bool CanCastTo<T> (Type type)
  60. {
  61. return typeof (T) == type || typeof (T).GetInterfaces ().Contains (type);
  62. }
  63. public virtual bool CanCastTo (Type t, object o)
  64. {
  65. if (CanCastTo<IClientChannel> (t))
  66. return true;
  67. #if !NET_2_1
  68. if (isDuplex && CanCastTo<IDuplexContextChannel> (t))
  69. return true;
  70. #endif
  71. return false;
  72. }
  73. #endregion
  74. public override IMessage Invoke (IMessage inputMessage)
  75. {
  76. try {
  77. return DoInvoke (inputMessage);
  78. } catch (TargetInvocationException ex) {
  79. if (ex.InnerException != null)
  80. throw ex.InnerException;
  81. throw;
  82. }
  83. }
  84. IMessage DoInvoke (IMessage inputMessage)
  85. {
  86. var inmsg = (IMethodCallMessage) inputMessage;
  87. var od = channel.Contract.Operations.FirstOrDefault (o => inmsg.MethodBase.Equals (o.SyncMethod) || inmsg.MethodBase.Equals (o.BeginMethod) || inmsg.MethodBase.Equals (o.EndMethod));
  88. if (od == null) {
  89. // Then IContextChannel methods.
  90. var ret = inmsg.MethodBase.Invoke (channel, inmsg.InArgs);
  91. return new ReturnMessage (ret, null, 0, null, inmsg);
  92. } else {
  93. object [] pl;
  94. MethodBase method = null;
  95. List<object> outArgs = null;
  96. object ret;
  97. if (inmsg.MethodBase.Equals (od.SyncMethod)) {
  98. // sync invocation
  99. pl = new object [inmsg.MethodBase.GetParameters ().Length];
  100. Array.Copy (inmsg.Args, pl, inmsg.ArgCount);
  101. ret = channel.Process (inmsg.MethodBase, od.Name, pl, OperationContext.Current);
  102. method = od.SyncMethod;
  103. } else if (inmsg.MethodBase.Equals (od.BeginMethod)) {
  104. // async invocation
  105. pl = new object [inmsg.ArgCount - 2];
  106. Array.Copy (inmsg.Args, 0, pl, 0, pl.Length);
  107. ret = channel.BeginProcess (inmsg.MethodBase, od.Name, pl, (AsyncCallback) inmsg.Args [inmsg.ArgCount - 2], inmsg.Args [inmsg.ArgCount - 1]);
  108. saved_params [ret] = pl;
  109. wait.Set ();
  110. } else {
  111. var result = (IAsyncResult) inmsg.InArgs [0];
  112. wait.WaitOne ();
  113. pl = saved_params [result];
  114. wait.Reset ();
  115. saved_params.Remove (result);
  116. ret = channel.EndProcess (inmsg.MethodBase, od.Name, pl, result);
  117. method = od.BeginMethod;
  118. }
  119. if (method != null && method.GetParameters ().Any (pi => pi.IsOut || pi.ParameterType.IsByRef))
  120. return new ReturnMessage (ret, pl, pl.Length, null, inmsg);
  121. else
  122. return new ReturnMessage (ret, outArgs != null ? outArgs.ToArray () : null, outArgs != null ? outArgs.Count : 0, null, inmsg);
  123. }
  124. }
  125. }
  126. }
  127. #endif