TcpClientChannel.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. using System.Collections;
  10. using System.IO;
  11. using System.Net.Sockets;
  12. using System.Runtime.Remoting.Messaging;
  13. using System.Runtime.Remoting.Channels;
  14. using System.Threading;
  15. namespace System.Runtime.Remoting.Channels.Tcp
  16. {
  17. public class TcpClientChannel : IChannelSender, IChannel
  18. {
  19. int priority = 1;
  20. string name = "tcp";
  21. IClientChannelSinkProvider _sinkProvider;
  22. public TcpClientChannel ()
  23. {
  24. _sinkProvider = new BinaryClientFormatterSinkProvider ();
  25. _sinkProvider.Next = new TcpClientTransportSinkProvider ();
  26. }
  27. public TcpClientChannel (IDictionary properties, IClientChannelSinkProvider sinkProvider)
  28. {
  29. object val = properties ["name"];
  30. if (val != null) name = val as string;
  31. val = properties ["priority"];
  32. if (val != null) priority = Convert.ToInt32 (val);
  33. if (sinkProvider != null)
  34. {
  35. _sinkProvider = sinkProvider;
  36. // add the tcp provider at the end of the chain
  37. IClientChannelSinkProvider prov = sinkProvider;
  38. while (prov.Next != null) prov = prov.Next;
  39. prov.Next = new TcpClientTransportSinkProvider ();
  40. // Note: a default formatter is added only when
  41. // no sink providers are specified in the config file.
  42. }
  43. else
  44. {
  45. _sinkProvider = new BinaryClientFormatterSinkProvider ();
  46. _sinkProvider.Next = new TcpClientTransportSinkProvider ();
  47. }
  48. }
  49. public TcpClientChannel (string name, IClientChannelSinkProvider sinkProvider)
  50. {
  51. this.name = name;
  52. _sinkProvider = sinkProvider;
  53. // add the tcp provider at the end of the chain
  54. IClientChannelSinkProvider prov = sinkProvider;
  55. while (prov.Next != null) prov = prov.Next;
  56. prov.Next = new TcpClientTransportSinkProvider ();
  57. }
  58. public string ChannelName
  59. {
  60. get {
  61. return name;
  62. }
  63. }
  64. public int ChannelPriority
  65. {
  66. get {
  67. return priority;
  68. }
  69. }
  70. public IMessageSink CreateMessageSink (string url,
  71. object remoteChannelData,
  72. out string objectURI)
  73. {
  74. if (url == null && remoteChannelData != null) {
  75. IChannelDataStore ds = remoteChannelData as IChannelDataStore;
  76. if (ds != null && ds.ChannelUris.Length > 0)
  77. url = ds.ChannelUris [0];
  78. else {
  79. objectURI = null;
  80. return null;
  81. }
  82. }
  83. if (Parse (url, out objectURI) == null)
  84. return null;
  85. return (IMessageSink) _sinkProvider.CreateSink (this, url, remoteChannelData);
  86. }
  87. public string Parse (string url, out string objectURI)
  88. {
  89. return TcpChannel.ParseChannelUrl (url, out objectURI);
  90. }
  91. }
  92. }