New test.
[mono.git] / mcs / class / System.Runtime.Remoting / System.Runtime.Remoting.Channels.Tcp / TcpClientChannel.cs
index 7844e4d929709d1b3f5635c6f1052af7b66ecea8..c460e2e0b3326b5c51ee3f08090c366a2e2f62cb 100644 (file)
 // System.Runtime.Remoting.Channels.Tcp.TcpClientChannel.cs
 //
 // Author: Dietmar Maurer (dietmar@ximian.com)
+//         Lluis Sanchez Gual (lluis@ideary.com)
 //
 // 2002 (C) Copyright, Ximian, Inc.
 //
 
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
 using System.Collections;
 using System.IO;
 using System.Net.Sockets;
 using System.Runtime.Remoting.Messaging;
 using System.Runtime.Remoting.Channels;
+using System.Threading;
 
 namespace System.Runtime.Remoting.Channels.Tcp
 {
-
-       public class TcpClientTransportSink : IClientChannelSink
-       {
-               string host;
-               string object_uri;
-               int port;
-               
-               TcpClient tcpclient;
-               Stream stream = null;
-               
-               public TcpClientTransportSink (string url)
-               {
-                       host = TcpChannel.ParseTcpURL (url, out object_uri, out port);
-                       tcpclient = new TcpClient ();
-               }
-
-               public IDictionary Properties
-               {
-                       get {
-                               return null;
-                       }
-               }
-
-               public IClientChannelSink NextChannelSink
-               {
-                       get {
-                               // we are the last one
-                               return null;
-                       }
-               }
-
-               public void AsyncProcessRequest (IClientChannelSinkStack sinkStack, IMessage msg,
-                                                ITransportHeaders headers, Stream stream)
-               {
-                       throw new NotImplementedException ();                   
-               }
-
-               public void AsyncProcessResponse (IClientResponseChannelSinkStack sinkStack,
-                                                 object state, ITransportHeaders headers,
-                                                 Stream stream)
-               {
-                       throw new NotImplementedException ();
-               }
-
-               public Stream GetRequestStream (IMessage msg, ITransportHeaders headers)
-               {
-                       // no acces to stream?
-                       return null;
-               }
-               
-               public void ProcessMessage (IMessage msg,
-                                           ITransportHeaders requestHeaders,
-                                           Stream requestStream,
-                                           out ITransportHeaders responseHeaders,
-                                           out Stream responseStream)
-               {
-                       if (stream == null) {
-                               tcpclient.Connect (host, port);
-                               stream = tcpclient.GetStream ();
-                       }
-
-                       Console.WriteLine ("Client  ProcessMessage");
-
-                       responseHeaders = null;
-                       responseStream = null;
-                       //throw new NotImplementedException ();
-               }
-                       
-       }
-       
-       public class TcpClientTransportSinkProvider : IClientChannelSinkProvider
-       {
-               public TcpClientTransportSinkProvider ()
-               {
-                       // what should we do here ?
-               }
-
-               public IClientChannelSinkProvider Next
-               {
-                       get {
-                               return null;
-                       }
-
-                       set {
-                               // ignore, we are always the last in the chain 
-                       }
-               }
-
-               public IClientChannelSink CreateSink (IChannelSender channel, string url,
-                                                     object remoteChannelData)
-               {
-                       return new TcpClientTransportSink (url);
-               }
-       }
-
        public class TcpClientChannel : IChannelSender, IChannel
        {
                int priority = 1;                                       
                string name = "tcp";
-               IClientChannelSinkProvider sink_provider;
+               IClientChannelSinkProvider _sinkProvider;
                
                public TcpClientChannel ()
-               {
-                       sink_provider = new BinaryClientFormatterSinkProvider ();
-                       sink_provider.Next = new TcpClientTransportSinkProvider ();
+               {
+                       _sinkProvider = new BinaryClientFormatterSinkProvider ();
+                       _sinkProvider.Next = new TcpClientTransportSinkProvider ();
                }
 
                public TcpClientChannel (IDictionary properties, IClientChannelSinkProvider sinkProvider)
                {
-                       priority = 1;
-                       sink_provider = sinkProvider;
+                       object val = properties ["name"];
+                       if (val != null) name = val as string;
+                       
+                       val = properties ["priority"];
+                       if (val != null) priority = Convert.ToInt32 (val);
+                       
+                       if (sinkProvider != null)
+                       {
+                               _sinkProvider = sinkProvider;
+
+                               // add the tcp provider at the end of the chain
+                               IClientChannelSinkProvider prov = sinkProvider;
+                               while (prov.Next != null) prov = prov.Next;
+                               prov.Next = new TcpClientTransportSinkProvider ();
+
+                               // Note: a default formatter is added only when
+                               // no sink providers are specified in the config file.
+                       }
+                       else
+                       {
+                               _sinkProvider = new BinaryClientFormatterSinkProvider ();
+                               _sinkProvider.Next = new TcpClientTransportSinkProvider ();
+                       }
 
-                       // add the tcp provider at the end of the chain
-                       IClientChannelSinkProvider prov = sinkProvider;
-                       while (prov.Next != null) prov = prov.Next;
-                       prov.Next = new TcpClientTransportSinkProvider ();
                }
 
                public TcpClientChannel (string name, IClientChannelSinkProvider sinkProvider)
                {
-                       priority = 1;           
                        this.name = name;
-                       sink_provider = sinkProvider;
+                       _sinkProvider = sinkProvider;
 
                        // add the tcp provider at the end of the chain
                        IClientChannelSinkProvider prov = sinkProvider;
@@ -158,29 +102,32 @@ namespace System.Runtime.Remoting.Channels.Tcp
                        }
                }
 
-               public IMessageSink CreateMessageSink (string url,
+               public virtual IMessageSink CreateMessageSink (string url,
                                                       object remoteChannelData,
                                                       out string objectURI)
-               {
-                       if (url == null && remoteChannelData != null) {
+           {
+                       if (url != null && Parse (url, out objectURI) != null)
+                               return (IMessageSink) _sinkProvider.CreateSink (this, url, remoteChannelData);
+                       
+                       if (remoteChannelData != null) {
                                IChannelDataStore ds = remoteChannelData as IChannelDataStore;
-                               if (ds != null)
+                               if (ds != null && ds.ChannelUris.Length > 0)
                                        url = ds.ChannelUris [0];
+                               else {
+                                       objectURI = null;
+                                       return null;
+                               }
                        }
-                       
+
                        if (Parse (url, out objectURI) == null)
                                return null;
 
-                       return (IMessageSink) sink_provider.CreateSink (this, url, remoteChannelData);
+                       return (IMessageSink) _sinkProvider.CreateSink (this, url, remoteChannelData);
                }
 
                public string Parse (string url, out string objectURI)
                {
-                       int port;
-                       
-                       string host = TcpChannel.ParseTcpURL (url, out objectURI, out port);
-
-                       return "tcp://" + host + ":" + port;
+                       return TcpChannel.ParseChannelUrl (url, out objectURI);
                }
        }
 }