Add concurrent implementation of an ObjectPool to reuse small objects like Node instances
[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                         if (properties != null) {
55                                 object val = properties ["name"];
56                                 if (val != null) name = val as string;
57                         
58                                 val = properties ["priority"];
59                                 if (val != null) priority = Convert.ToInt32 (val);
60                         }
61                         
62                         if (sinkProvider != null)
63                         {
64                                 _sinkProvider = sinkProvider;
65
66                                 // add the tcp provider at the end of the chain
67                                 IClientChannelSinkProvider prov = sinkProvider;
68                                 while (prov.Next != null) prov = prov.Next;
69                                 prov.Next = new TcpClientTransportSinkProvider ();
70
71                                 // Note: a default formatter is added only when
72                                 // no sink providers are specified in the config file.
73                         }
74                         else
75                         {
76                                 _sinkProvider = new BinaryClientFormatterSinkProvider ();
77                                 _sinkProvider.Next = new TcpClientTransportSinkProvider ();
78                         }
79
80                 }
81
82                 public TcpClientChannel (string name, IClientChannelSinkProvider sinkProvider)
83                 {
84                         this.name = name;
85                         
86                         if (sinkProvider != null) {
87                                 _sinkProvider = sinkProvider;
88                                 
89                                 // add the tcp provider at the end of the chain
90                                 IClientChannelSinkProvider prov = sinkProvider;
91                                 while (prov.Next != null)
92                                         prov = prov.Next;
93                                 prov.Next = new TcpClientTransportSinkProvider ();
94                         } else {
95                                 _sinkProvider = new BinaryClientFormatterSinkProvider ();
96                                 _sinkProvider.Next = new TcpClientTransportSinkProvider ();
97                         }
98                 }
99                 
100                 public string ChannelName
101                 {
102                         get {
103                                 return name;
104                         }
105                 }
106
107                 public int ChannelPriority
108                 {
109                         get {
110                                 return priority;
111                         }
112                 }
113
114                 public virtual IMessageSink CreateMessageSink (string url,
115                                                        object remoteChannelData,
116                                                        out string objectURI)
117             {
118                         if (url != null && Parse (url, out objectURI) != null)
119                                 return (IMessageSink) _sinkProvider.CreateSink (this, url, remoteChannelData);
120                         
121                         if (remoteChannelData != null) {
122                                 IChannelDataStore ds = remoteChannelData as IChannelDataStore;
123                                 if (ds != null && ds.ChannelUris.Length > 0)
124                                         url = ds.ChannelUris [0];
125                                 else {
126                                         objectURI = null;
127                                         return null;
128                                 }
129                         }
130
131                         if (Parse (url, out objectURI) == null)
132                                 return null;
133
134                         return (IMessageSink) _sinkProvider.CreateSink (this, url, remoteChannelData);
135                 }
136
137                 public string Parse (string url, out string objectURI)
138                 {
139                         return TcpChannel.ParseChannelUrl (url, out objectURI);
140                 }
141         }
142 }