[bcl] Remove NET_2_0 defines from the class libs. This has been done using: unifdef...
[mono.git] / mcs / class / System.Runtime.Remoting / System.Runtime.Remoting.Channels.Tcp / TcpServerTransportSink.cs
1 //
2 // System.Runtime.Remoting.Channels.Tcp.TcpServerTransportSink.cs
3 //
4 // Author: Rodrigo Moya (rodrigo@ximian.com)
5 //         Lluis Sanchez Gual (lsg@ctv.es)
6 //
7 // 2002 (C) Copyright, Ximian, Inc.
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.Collections;
33 using System.Runtime.Remoting.Messaging;
34 using System.Net.Sockets;
35 using System.IO;
36
37 namespace System.Runtime.Remoting.Channels.Tcp
38 {
39         internal class TcpServerTransportSink : IServerChannelSink, IChannelSinkBase
40         {
41                 IServerChannelSink next_sink;
42                 
43                 public TcpServerTransportSink (IServerChannelSink next)
44                 {
45                         next_sink = next;
46                 }
47                 
48                 public IServerChannelSink NextChannelSink 
49                 {
50                         get 
51                         {
52                                 return next_sink;
53                         }
54                 }
55
56                 public IDictionary Properties 
57                 {
58                         get 
59                         {
60                                 if (next_sink != null) return next_sink.Properties;
61                                 else return null;
62                         }
63                 }
64
65                 public void AsyncProcessResponse (IServerResponseChannelSinkStack sinkStack, object state,
66                         IMessage msg, ITransportHeaders headers, Stream responseStream)
67                 {
68                         ClientConnection connection = (ClientConnection)state;
69                         
70                         NetworkStream ns = new NetworkStream (connection.Socket);
71                         TcpMessageIO.SendMessageStream (ns, responseStream, headers, connection.Buffer);
72                         ns.Flush ();
73                         ns.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 TcpServerChannel 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 = TcpMessageIO.ReceiveMessageStream (stream, out requestHeaders, connection.Buffer);
102                         requestHeaders [CommonTransportKeys.IPAddress] = connection.ClientAddress;
103                         requestHeaders [CommonTransportKeys.ConnectionId] = connection.Id;
104
105                         string uri = (string) requestHeaders [CommonTransportKeys.RequestUri];
106                         TcpChannel.ParseChannelUrl (uri, out uri);
107                         
108                         if (uri != null)
109                                 requestHeaders [CommonTransportKeys.RequestUri] = uri;
110                         
111                         // Pushes the connection object together with the sink. This information
112                         // will be used for sending the response in an async call.
113
114                         ServerChannelSinkStack sinkStack = new ServerChannelSinkStack();
115                         sinkStack.Push(this, connection);
116
117                         ITransportHeaders responseHeaders;
118                         Stream responseStream;
119                         IMessage responseMsg;
120
121                         ServerProcessing proc = next_sink.ProcessMessage(sinkStack, null, requestHeaders, requestStream, out responseMsg, out responseHeaders, out responseStream);
122
123                         switch (proc)
124                         {
125                                 case ServerProcessing.Complete:
126                                         TcpMessageIO.SendMessageStream (stream, responseStream, responseHeaders, connection.Buffer);
127                                         stream.Flush ();
128                                         break;
129
130                                 case ServerProcessing.Async:
131                                 case ServerProcessing.OneWay:
132                                         break;
133                         }
134                 }
135         }
136 }
137