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