Merge pull request #2023 from juergenhoetzel/master
[mono.git] / mcs / class / Mono.Posix / Mono.Remoting.Channels.Unix / UnixServerTransportSink.cs
1 //
2 // Mono.Remoting.Channels.Unix.UnixServerTransportSink.cs
3 //
4 // Author: Rodrigo Moya (rodrigo@ximian.com)
5 //         Lluis Sanchez Gual (lsg@ctv.es)
6 //
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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;
32 using System.Net.Sockets;
33 using System.Collections;
34 using System.Runtime.Remoting.Messaging;
35 using System.IO;
36 using System.Runtime.Remoting.Channels;
37
38 namespace Mono.Remoting.Channels.Unix
39 {
40     internal class UnixServerTransportSink : IServerChannelSink, IChannelSinkBase
41     {
42         IServerChannelSink next_sink;
43         
44         public UnixServerTransportSink (IServerChannelSink next)
45         {
46             next_sink = next;
47         }
48         
49         public IServerChannelSink NextChannelSink 
50         {
51             get 
52             {
53                 return next_sink;
54             }
55         }
56
57         public IDictionary Properties 
58         {
59             get 
60             {
61                 if (next_sink != null) return next_sink.Properties;
62                 else return null;
63             }
64         }
65
66         public void AsyncProcessResponse (IServerResponseChannelSinkStack sinkStack, object state,
67                                           IMessage msg, ITransportHeaders headers, Stream responseStream)
68         {
69             ClientConnection connection = (ClientConnection)state;
70             NetworkStream stream = new NetworkStream (connection.Client);
71             UnixMessageIO.SendMessageStream (stream, responseStream, headers, connection.Buffer);
72             stream.Flush ();
73             stream.Close ();
74         }
75
76         public Stream GetResponseStream (IServerResponseChannelSinkStack sinkStack, object state,
77                                          IMessage msg, ITransportHeaders headers)
78         {
79             return null;
80         }
81         
82         public ServerProcessing ProcessMessage (IServerChannelSinkStack sinkStack,
83                                                 IMessage requestMsg,
84                                                 ITransportHeaders requestHeaders,
85                                                 Stream requestStream,
86                                                 out IMessage responseMsg,
87                                                 out ITransportHeaders responseHeaders,
88                                                 out Stream responseStream)
89         {
90             // this is the first sink, and UnixServerChannel does not call it.
91             throw new NotSupportedException ();
92         }
93
94         internal void InternalProcessMessage (ClientConnection connection, Stream stream)
95         {
96             // Reads the headers and the request stream
97
98             Stream requestStream;
99             ITransportHeaders requestHeaders;
100
101             requestStream = UnixMessageIO.ReceiveMessageStream (stream, out requestHeaders, connection.Buffer);
102
103 /*            try {
104                 PeerCred cred = connection.Client.PeerCredential;
105                 requestHeaders["__uid"] = cred.UserID;
106             } catch (Exception e) {
107                 Console.WriteLine ("Couldn't get the peer cred: " + e);
108             }
109 */
110             // Pushes the connection object together with the sink. This information
111             // will be used for sending the response in an async call.
112
113             ServerChannelSinkStack sinkStack = new ServerChannelSinkStack();
114             sinkStack.Push(this, connection);
115
116             ITransportHeaders responseHeaders;
117             Stream responseStream;
118             IMessage responseMsg;
119
120             ServerProcessing proc = next_sink.ProcessMessage(sinkStack, null, requestHeaders, requestStream, out responseMsg, out responseHeaders, out responseStream);
121
122             switch (proc)
123             {
124             case ServerProcessing.Complete:
125                 UnixMessageIO.SendMessageStream (stream, responseStream, responseHeaders, connection.Buffer);
126                 stream.Flush ();
127                 break;
128
129             case ServerProcessing.Async:
130             case ServerProcessing.OneWay:
131                 break;
132             }
133         }
134     }
135 }
136