Merge branch 'master' of github.com:tgiphil/mono
[mono.git] / mcs / class / Mono.Posix / Mono.Remoting.Channels.Unix / UnixBinaryClientFormatterSink.cs
1 //
2 // Mono.Remoting.Channels.Unix.UnixBinaryClientFormatterSink.cs
3 //
4 // Author: Rodrigo Moya (rodrigo@ximian.com)
5 //         Dietmar Maurer (dietmar@ximian.com)
6 //         Lluis Sanchez Gual (lluis@ideary.com)
7 //
8 // 2002 (C) Copyright, Ximian, Inc.
9 //
10
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System;
33 using System.Collections;
34 using System.IO;
35 using System.Runtime.Remoting.Messaging;
36 using System.Runtime.Remoting.Channels;
37 using System.Runtime.Serialization;
38 using System.Runtime.Serialization.Formatters.Binary;
39
40 namespace Mono.Remoting.Channels.Unix
41 {
42         internal class UnixBinaryClientFormatterSink : IClientFormatterSink, IMessageSink, IClientChannelSink, IChannelSinkBase
43         {
44                 UnixBinaryCore _binaryCore = UnixBinaryCore.DefaultInstance;
45                 IClientChannelSink _nextInChain;
46
47                 public UnixBinaryClientFormatterSink (IClientChannelSink nextSink)
48                 {
49                         _nextInChain = nextSink;
50                 }
51
52                 internal UnixBinaryCore BinaryCore
53                 {
54                         get { return _binaryCore; }
55                         set { _binaryCore = value; }
56                 }
57
58                 public IClientChannelSink NextChannelSink
59                 {
60                         get {
61                                 return _nextInChain;
62                         }
63                 }
64
65                 public IMessageSink NextSink
66                 {
67                         get {
68                                 // This is the last sink in the IMessageSink sink chain
69                                 return null;
70                         }
71                 }
72
73                 public IDictionary Properties
74                 {
75                         get {
76                                 return null;
77                         }
78                 }
79
80                 public void AsyncProcessRequest (IClientChannelSinkStack sinkStack,
81                                                  IMessage msg,
82                                                  ITransportHeaders headers,
83                                                  Stream stream)
84                 {
85                         // never called because the formatter sink is
86                         // always the first in the chain
87                         throw new NotSupportedException("UnixBinaryClientFormatterSink must be the first sink in the IClientChannelSink chain");
88                 }
89
90                 public void AsyncProcessResponse (IClientResponseChannelSinkStack sinkStack,
91                                                   object state,
92                                                   ITransportHeaders headers,
93                                                   Stream stream)
94                 {
95                         IMessage replyMessage = (IMessage)_binaryCore.Deserializer.DeserializeMethodResponse (stream, null, (IMethodCallMessage)state);
96                         sinkStack.DispatchReplyMessage (replyMessage);
97                 }
98
99                 public Stream GetRequestStream (IMessage msg,
100                                                 ITransportHeaders headers)
101                 {
102                         // never called
103                         throw new NotSupportedException ();
104                 }
105
106                 public void ProcessMessage (IMessage msg,
107                                             ITransportHeaders requestHeaders,
108                                             Stream requestStream,
109                                             out ITransportHeaders responseHeaders,
110                                             out Stream responseStream)
111                 {
112                         // never called because the formatter sink is
113                         // always the first in the chain
114                         throw new NotSupportedException ();
115                 }
116
117                 public IMessageCtrl AsyncProcessMessage (IMessage msg,
118                         IMessageSink replySink)
119                 {
120                         ITransportHeaders transportHeaders = new TransportHeaders();
121                         Stream stream = _nextInChain.GetRequestStream(msg, transportHeaders);
122                         if (stream == null) stream = new MemoryStream ();
123
124                         _binaryCore.Serializer.Serialize (stream, msg, null);
125                         if (stream is MemoryStream) stream.Position = 0;
126
127                         ClientChannelSinkStack stack = new ClientChannelSinkStack(replySink);
128                         stack.Push (this, msg);
129
130                         _nextInChain.AsyncProcessRequest (stack, msg, transportHeaders, stream);
131
132                         // FIXME: No idea about how to implement IMessageCtrl
133                         return null;    \r
134                 }
135
136                 public IMessage SyncProcessMessage (IMessage msg)
137                 {
138                         try {
139
140                                 ITransportHeaders call_headers = new TransportHeaders();
141                                 call_headers["__RequestUri"] = ((IMethodCallMessage)msg).Uri;
142                                 call_headers["Content-Type"] = "application/octet-stream";
143
144                                 Stream call_stream = _nextInChain.GetRequestStream(msg, call_headers);
145                                 if (call_stream == null) call_stream = new MemoryStream ();
146
147                                 // Serialize msg to the stream
148
149                                 _binaryCore.Serializer.Serialize (call_stream, msg, null);
150                                 if (call_stream is MemoryStream) call_stream.Position = 0;
151
152                                 Stream response_stream;
153                                 ITransportHeaders response_headers;
154
155                                 _nextInChain.ProcessMessage (msg, call_headers, call_stream, out response_headers,
156                                                             out response_stream);
157
158                                 // Deserialize response_stream
159
160                                 return (IMessage) _binaryCore.Deserializer.DeserializeMethodResponse (response_stream, null, (IMethodCallMessage)msg);
161                                 
162                         } catch (Exception e) {
163                                  return new ReturnMessage (e, (IMethodCallMessage)msg);
164                         }
165                 }
166         }
167 }