TcpClientChannel.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //
  2. // System.Runtime.Remoting.Channels.Tcp.TcpClientChannel.cs
  3. //
  4. // Author: Dietmar Maurer ([email protected])
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // 2002 (C) Copyright, Ximian, Inc.
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System.Collections;
  30. using System.IO;
  31. using System.Net.Sockets;
  32. using System.Runtime.Remoting.Messaging;
  33. using System.Runtime.Remoting.Channels;
  34. using System.Threading;
  35. namespace System.Runtime.Remoting.Channels.Tcp
  36. {
  37. public class TcpClientChannel : IChannelSender, IChannel
  38. {
  39. int priority = 1;
  40. string name = "tcp";
  41. IClientChannelSinkProvider _sinkProvider;
  42. public TcpClientChannel ()
  43. {
  44. _sinkProvider = new BinaryClientFormatterSinkProvider ();
  45. _sinkProvider.Next = new TcpClientTransportSinkProvider ();
  46. }
  47. public TcpClientChannel (IDictionary properties, IClientChannelSinkProvider sinkProvider)
  48. {
  49. object val = properties ["name"];
  50. if (val != null) name = val as string;
  51. val = properties ["priority"];
  52. if (val != null) priority = Convert.ToInt32 (val);
  53. if (sinkProvider != null)
  54. {
  55. _sinkProvider = sinkProvider;
  56. // add the tcp provider at the end of the chain
  57. IClientChannelSinkProvider prov = sinkProvider;
  58. while (prov.Next != null) prov = prov.Next;
  59. prov.Next = new TcpClientTransportSinkProvider ();
  60. // Note: a default formatter is added only when
  61. // no sink providers are specified in the config file.
  62. }
  63. else
  64. {
  65. _sinkProvider = new BinaryClientFormatterSinkProvider ();
  66. _sinkProvider.Next = new TcpClientTransportSinkProvider ();
  67. }
  68. }
  69. public TcpClientChannel (string name, IClientChannelSinkProvider sinkProvider)
  70. {
  71. this.name = name;
  72. _sinkProvider = sinkProvider;
  73. // add the tcp provider at the end of the chain
  74. IClientChannelSinkProvider prov = sinkProvider;
  75. while (prov.Next != null) prov = prov.Next;
  76. prov.Next = new TcpClientTransportSinkProvider ();
  77. }
  78. public string ChannelName
  79. {
  80. get {
  81. return name;
  82. }
  83. }
  84. public int ChannelPriority
  85. {
  86. get {
  87. return priority;
  88. }
  89. }
  90. public IMessageSink CreateMessageSink (string url,
  91. object remoteChannelData,
  92. out string objectURI)
  93. {
  94. if (url == null && remoteChannelData != null) {
  95. IChannelDataStore ds = remoteChannelData as IChannelDataStore;
  96. if (ds != null && ds.ChannelUris.Length > 0)
  97. url = ds.ChannelUris [0];
  98. else {
  99. objectURI = null;
  100. return null;
  101. }
  102. }
  103. if (Parse (url, out objectURI) == null)
  104. return null;
  105. return (IMessageSink) _sinkProvider.CreateSink (this, url, remoteChannelData);
  106. }
  107. public string Parse (string url, out string objectURI)
  108. {
  109. return TcpChannel.ParseChannelUrl (url, out objectURI);
  110. }
  111. }
  112. }