[runtime] Updates comments.
[mono.git] / mcs / class / System.Runtime.Remoting / System.Runtime.Remoting.Channels / BinaryClientFormatterSink.cs
1 //
2 // System.Runtime.Remoting.Channels.BinaryClientFormatterSink.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.Collections;
33 using System.IO;
34 using System.Runtime.Remoting.Messaging;
35 using System.Runtime.Serialization;
36 using System.Runtime.Serialization.Formatters.Binary;
37
38 namespace System.Runtime.Remoting.Channels
39 {
40         public class BinaryClientFormatterSink : IClientFormatterSink,
41                 IMessageSink, IClientChannelSink, IChannelSinkBase
42         {
43                 BinaryCore _binaryCore = BinaryCore.DefaultInstance;
44                 IClientChannelSink _nextInChain;
45
46                 public BinaryClientFormatterSink (IClientChannelSink nextSink)
47                 {
48                         _nextInChain = nextSink;
49                 }
50
51                 internal BinaryCore BinaryCore
52                 {
53                         get { return _binaryCore; }
54                         set { _binaryCore = value; }
55                 }
56
57                 public IClientChannelSink NextChannelSink
58                 {
59                         get {
60                                 return _nextInChain;
61                         }
62                 }
63
64                 public IMessageSink NextSink
65                 {
66                         get {
67                                 // This is the last sink in the IMessageSink sink chain
68                                 return null;
69                         }
70                 }
71
72                 public IDictionary Properties
73                 {
74                         get {
75                                 return null;
76                         }
77                 }
78
79                 public void AsyncProcessRequest (IClientChannelSinkStack sinkStack,
80                                                  IMessage msg,
81                                                  ITransportHeaders headers,
82                                                  Stream stream)
83                 {
84                         // never called because the formatter sink is
85                         // always the first in the chain
86                         throw new NotSupportedException("BinaryClientFormatterSink must be the first sink in the IClientChannelSink chain");
87                 }
88
89                 public void AsyncProcessResponse (IClientResponseChannelSinkStack sinkStack,
90                                                   object state,
91                                                   ITransportHeaders headers,
92                                                   Stream stream)
93                 {
94                         IMessage replyMessage = (IMessage)_binaryCore.Deserializer.DeserializeMethodResponse (stream, null, (IMethodCallMessage)state);
95                         sinkStack.DispatchReplyMessage (replyMessage);
96                 }
97
98                 public Stream GetRequestStream (IMessage msg,
99                                                 ITransportHeaders headers)
100                 {
101                         // never called
102                         throw new NotSupportedException ();
103                 }
104
105                 public void ProcessMessage (IMessage msg,
106                                             ITransportHeaders requestHeaders,
107                                             Stream requestStream,
108                                             out ITransportHeaders responseHeaders,
109                                             out Stream responseStream)
110                 {
111                         // never called because the formatter sink is
112                         // always the first in the chain
113                         throw new NotSupportedException ();
114                 }
115
116                 public IMessageCtrl AsyncProcessMessage (IMessage msg,
117                         IMessageSink replySink)
118                 {
119                         ITransportHeaders transportHeaders = new TransportHeaders();
120                         transportHeaders[CommonTransportKeys.RequestUri] = ((IMethodCallMessage)msg).Uri;
121                         transportHeaders["Content-Type"] = "application/octet-stream";
122
123                         Stream stream = _nextInChain.GetRequestStream(msg, transportHeaders);
124                         if (stream == null) stream = new MemoryStream ();
125
126                         _binaryCore.Serializer.Serialize (stream, msg, null);
127                         if (stream is MemoryStream) stream.Position = 0;
128
129                         ClientChannelSinkStack stack = new ClientChannelSinkStack(replySink);
130                         stack.Push (this, msg);
131
132                         _nextInChain.AsyncProcessRequest (stack, msg, transportHeaders, stream);
133
134                         // FIXME: No idea about how to implement IMessageCtrl
135                         return null;    
136                 }
137
138                 public IMessage SyncProcessMessage (IMessage msg)
139                 {
140                         try {
141
142                                 ITransportHeaders call_headers = new TransportHeaders();
143                                 call_headers[CommonTransportKeys.RequestUri] = ((IMethodCallMessage)msg).Uri;
144                                 call_headers["Content-Type"] = "application/octet-stream";
145
146                                 Stream call_stream = _nextInChain.GetRequestStream(msg, call_headers);
147                                 if (call_stream == null) call_stream = new MemoryStream ();
148
149                                 // Serialize msg to the stream
150
151                                 _binaryCore.Serializer.Serialize (call_stream, msg, null);
152                                 if (call_stream is MemoryStream) call_stream.Position = 0;
153
154                                 Stream response_stream;
155                                 ITransportHeaders response_headers;
156
157                                 _nextInChain.ProcessMessage (msg, call_headers, call_stream, out response_headers,
158                                                             out response_stream);
159
160                                 // Deserialize response_stream
161
162                                 return (IMessage) _binaryCore.Deserializer.DeserializeMethodResponse (response_stream, null, (IMethodCallMessage)msg);
163                                 
164                         } catch (Exception e) {
165                                  return new ReturnMessage (e, (IMethodCallMessage)msg);
166                         }
167                 }
168         }
169 }