BinaryCore.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // System.Runtime.Remoting.Channels.BinaryCore.cs
  3. //
  4. // Author: Lluis Sanchez Gual ([email protected])
  5. //
  6. // 2003 (C) Copyright, Novell, Inc.
  7. //
  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.Collections;
  29. using System.Runtime.Remoting.Messaging;
  30. using System.Runtime.Serialization;
  31. using System.Runtime.Serialization.Formatters;
  32. using System.Runtime.Serialization.Formatters.Binary;
  33. namespace System.Runtime.Remoting.Channels
  34. {
  35. internal class BinaryCore
  36. {
  37. BinaryFormatter _serializationFormatter;
  38. BinaryFormatter _deserializationFormatter;
  39. bool _includeVersions = true;
  40. bool _strictBinding = false;
  41. IDictionary _properties;
  42. #if NET_1_1
  43. TypeFilterLevel _filterLevel = TypeFilterLevel.Low;
  44. #endif
  45. public static BinaryCore DefaultInstance = new BinaryCore ();
  46. public BinaryCore (object owner, IDictionary properties, string[] allowedProperties)
  47. {
  48. _properties = properties;
  49. if (_properties == null)
  50. {
  51. _properties = new Hashtable(10);
  52. }
  53. foreach(DictionaryEntry property in properties)
  54. {
  55. string key = (string) property.Key;
  56. if (Array.IndexOf (allowedProperties, key) == -1)
  57. throw new RemotingException (owner.GetType().Name + " does not recognize '" + key + "' configuration property");
  58. switch (key)
  59. {
  60. case "includeVersions":
  61. _includeVersions = Convert.ToBoolean (property.Value);
  62. break;
  63. case "strictBinding":
  64. _strictBinding = Convert.ToBoolean (property.Value);
  65. break;
  66. #if NET_1_1
  67. case "typeFilterLevel":
  68. if (property.Value is TypeFilterLevel)
  69. _filterLevel = (TypeFilterLevel) property.Value;
  70. else {
  71. string s = (string) property.Value;
  72. _filterLevel = (TypeFilterLevel) Enum.Parse (typeof(TypeFilterLevel), s);
  73. }
  74. break;
  75. #endif
  76. }
  77. }
  78. Init ();
  79. }
  80. public BinaryCore ()
  81. {
  82. _properties = new Hashtable ();
  83. Init ();
  84. }
  85. public void Init ()
  86. {
  87. RemotingSurrogateSelector surrogateSelector = new RemotingSurrogateSelector ();
  88. StreamingContext context = new StreamingContext (StreamingContextStates.Remoting, null);
  89. #if !TARGET_JVM
  90. _serializationFormatter = new BinaryFormatter (surrogateSelector, context);
  91. _deserializationFormatter = new BinaryFormatter (null, context);
  92. #else
  93. _serializationFormatter = (BinaryFormatter) vmw.@internal.remoting.BinaryFormatterUtils.CreateBinaryFormatter (surrogateSelector, context, false);
  94. _deserializationFormatter = (BinaryFormatter) vmw.@internal.remoting.BinaryFormatterUtils.CreateBinaryFormatter (null, context, false);
  95. #endif
  96. #if NET_1_1
  97. _serializationFormatter.FilterLevel = _filterLevel;
  98. _deserializationFormatter.FilterLevel = _filterLevel;
  99. #endif
  100. if (!_includeVersions)
  101. {
  102. _serializationFormatter.AssemblyFormat = FormatterAssemblyStyle.Simple;
  103. _deserializationFormatter.AssemblyFormat = FormatterAssemblyStyle.Simple;
  104. }
  105. if (!_strictBinding)
  106. {
  107. _serializationFormatter.Binder = ChannelCore.SimpleBinder;
  108. _deserializationFormatter.Binder = ChannelCore.SimpleBinder;
  109. }
  110. }
  111. public BinaryFormatter Serializer
  112. {
  113. get { return _serializationFormatter; }
  114. }
  115. public BinaryFormatter Deserializer
  116. {
  117. get { return _deserializationFormatter; }
  118. }
  119. public IDictionary Properties
  120. {
  121. get { return _properties; }
  122. }
  123. #if NET_1_1
  124. public TypeFilterLevel TypeFilterLevel
  125. {
  126. get { return _filterLevel; }
  127. }
  128. #endif
  129. }
  130. }