2010-04-06 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System.Runtime.Remoting / System.Runtime.Remoting.Channels.Ipc.Unix / IpcClientChannel.cs
blobb207217f452d1fc110b41f0f95c7a77efa3bd48a
1 //
2 // System.Runtime.Remoting.Channels.Ipc.Unix.IpcClientChannel.cs
3 //
4 // Author: Robert Jordan (robertj@gmx.net)
5 //
6 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
7 //
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 #if NET_2_0
31 using System;
32 using System.Collections;
33 using System.IO;
34 using System.Runtime.Remoting;
35 using System.Runtime.Remoting.Messaging;
36 using Win32 = System.Runtime.Remoting.Channels.Ipc.Win32;
38 namespace System.Runtime.Remoting.Channels.Ipc.Unix
40 internal class IpcClientChannel : IChannelSender, IChannel
42 object _innerChannel;
44 public IpcClientChannel ()
46 _innerChannel = Activator.CreateInstance(UnixChannelLoader.LoadClientChannel ());
49 public IpcClientChannel (IDictionary properties,
50 IClientChannelSinkProvider sinkProvider)
52 _innerChannel = Activator.CreateInstance(UnixChannelLoader.LoadClientChannel (), new object [] {properties, sinkProvider});
55 public IpcClientChannel (string name,
56 IClientChannelSinkProvider sinkProvider)
58 _innerChannel = Activator.CreateInstance(UnixChannelLoader.LoadClientChannel (), new object [] {name, sinkProvider});
61 public string ChannelName
63 get { return ((IChannel)_innerChannel).ChannelName; }
66 public int ChannelPriority
68 get { return ((IChannel)_innerChannel).ChannelPriority; }
71 public string Parse (string url, out string objectUri)
73 return Win32.IpcChannelHelper.Parse (url, out objectUri);
78 // Converts an ipc URL to a unix URL.
79 // Returns the URL unchanged if it was not ipc.
81 internal static string IpcToUnix (string url)
83 if (url == null)
84 return null;
86 string portName;
87 string objectUri;
88 Win32.IpcChannelHelper.Parse (url, out portName, out objectUri);
89 if (objectUri != null)
90 url = "unix://" + Path.Combine (Path.GetTempPath (), portName) + "?" + objectUri;
91 return url;
94 public IMessageSink CreateMessageSink(string url,
95 object remoteChannelData,
96 out string objectUri)
98 url = IpcToUnix (url);
99 IMessageSink sink = ((IChannelSender)_innerChannel).CreateMessageSink (url, remoteChannelData, out objectUri);
101 if (sink != null)
102 return new UrlMapperSink (sink);
103 else
104 return null;
110 // Simple message sink that changes ipc URLs to unix URLs.
112 sealed class UrlMapperSink : IMessageSink
114 readonly IMessageSink _sink;
116 public UrlMapperSink (IMessageSink sink)
118 _sink = sink;
121 public IMessageSink NextSink
123 get { return _sink.NextSink; }
126 static void ChangeUri (IMessage msg)
128 string uri = msg.Properties ["__Uri"] as string;
129 if (uri != null) {
130 msg.Properties ["__Uri"] = IpcClientChannel.IpcToUnix (uri);
134 public IMessage SyncProcessMessage(IMessage msg)
136 ChangeUri (msg);
137 return _sink.SyncProcessMessage (msg);
140 public IMessageCtrl AsyncProcessMessage(IMessage msg,
141 IMessageSink replySink)
143 ChangeUri (msg);
144 return _sink.AsyncProcessMessage (msg, replySink);
150 #endif