* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / System.Runtime.Remoting / System.Runtime.Remoting.Channels / BinaryServerFormatterSink.cs
index 6e75b8e1055a6577081aa85e55571a687ba1d525..a7ba64956e5dd881467a686b4d7df95aeae1f4d9 100644 (file)
@@ -118,6 +118,36 @@ namespace System.Runtime.Remoting.Channels {
                                                        IMessage requestMsg, ITransportHeaders requestHeaders, Stream requestStream,
                                                        out IMessage responseMsg, out ITransportHeaders responseHeaders, out Stream responseStream)
                {
+                       // Check whether the request was already processed by another
+                       // formatter sink and pass the request to the next sink if so.
+                       if (requestMsg != null)
+                               return next_sink.ProcessMessage (sinkStack,
+                                                                requestMsg,
+                                                                requestHeaders,
+                                                                requestStream,
+                                                                out responseMsg,
+                                                                out responseHeaders,
+                                                                out responseStream);
+
+                       // Check whether the request is suitable for this formatter
+                       // and pass the request to the next sink if not.
+                       // Note that a null content-type is handled as suitable,
+                       // otherwise no other sink will be able to handle the request.
+                       string contentType = requestHeaders["Content-Type"] as string;
+                       if (contentType != null && contentType != "application/octet-stream") {
+                               try {
+                                       return next_sink.ProcessMessage (sinkStack,
+                                               requestMsg,
+                                               requestHeaders,
+                                               requestStream,
+                                               out responseMsg,
+                                               out responseHeaders,
+                                               out responseStream);
+                               } catch {
+                                       // Let this formatter handle the exception.
+                               }
+                       }
+
                        sinkStack.Push (this, null);
                        ServerProcessing res;
 
@@ -140,17 +170,27 @@ namespace System.Runtime.Remoting.Channels {
                                responseHeaders = null;
                                responseStream = null;
                        }
-
+                       
                        if (res == ServerProcessing.Complete)
                        {
-                               responseStream = null;
-                               responseHeaders = new TransportHeaders();
-
-                               if (sinkStack != null) responseStream = sinkStack.GetResponseStream (responseMsg, responseHeaders);
-                               if (responseStream == null) responseStream = new MemoryStream();
-
-                               _binaryCore.Serializer.Serialize (responseStream, responseMsg);
+                               for (int n=0; n<3; n++) {
+                                       responseStream = null;
+                                       responseHeaders = new TransportHeaders();
+
+                                       if (sinkStack != null) responseStream = sinkStack.GetResponseStream (responseMsg, responseHeaders);
+                                       if (responseStream == null) responseStream = new MemoryStream();
+
+                                       try {
+                                               _binaryCore.Serializer.Serialize (responseStream, responseMsg);
+                                               break;
+                                       } catch (Exception ex) {
+                                               if (n == 2) throw ex;
+                                               else responseMsg = new ReturnMessage (ex, (IMethodCallMessage)requestMsg);
+                                       }
+                               }
+                               
                                if (responseStream is MemoryStream) responseStream.Position = 0;
+                               
 
                                sinkStack.Pop (this);
                        }