Browse Source

* TcpServerChannel.cs: Changed default formatter sink to BinaryFormatterSink
* TcpMessageIO.cs: warning corrected.
* TcpConnectionPool.cs: changed default value of MaxOpenConnections.
* TcpClientTransportSink.cs: implemented support for one way calls.
* TcpClientChannel.cs: Changed default formatter sink to BinaryFormatterSink.
* TcpChannel.cs: corrected Parse method. Now can parse channel uris.
* BinaryServerFormatterSink.cs: Implemented.
* BinaryClientFormatterSink.cs: Implemented.

svn path=/trunk/mcs/; revision=10925

Lluis Sanchez 23 years ago
parent
commit
913fe3e70b

+ 12 - 1
mcs/class/System.Runtime.Remoting/ChangeLog

@@ -1,4 +1,15 @@
-2002-12-29  Lluis Sanchez Gual <[email protected]>
+2002-12-29  Lluis Sanchez Gual <[email protected]>
+
+	* TcpServerChannel.cs: Changed default formatter sink to BinaryFormatterSink
+	* TcpMessageIO.cs: warning corrected.
+	* TcpConnectionPool.cs: changed default value of MaxOpenConnections.
+	* TcpClientTransportSink.cs: implemented support for one way calls.
+	* TcpClientChannel.cs: Changed default formatter sink to BinaryFormatterSink.
+	* TcpChannel.cs: corrected Parse method. Now can parse channel uris.
+	* BinaryServerFormatterSink.cs: Implemented.
+	* BinaryClientFormatterSink.cs: Implemented.
+	
+2002-12-29  Lluis Sanchez Gual <[email protected]>
 
 	* CommonTransportKeys.cs: Implemented and added to CVS
 	* TcpChannel.cs: Added. It is a more ore less complete implementation

+ 4 - 2
mcs/class/System.Runtime.Remoting/System.Runtime.Remoting.Channels.Tcp/TcpChannel.cs

@@ -2,7 +2,7 @@
 // System.Runtime.Remoting.Channels.Tcp.TcpChannel.cs
 //
 // Author: Rodrigo Moya ([email protected])
-//         Lluis Sanchez Gual (l[email protected])
+//         Lluis Sanchez Gual (l[email protected])
 //
 // 2002 (C) Copyright, Ximian, Inc.
 //
@@ -114,7 +114,7 @@ namespace System.Runtime.Remoting.Channels.Tcp
 			objectURI = null;
 			port = 0;
 			
-			Match m = Regex.Match (url, "tcp://([^:]+):([0-9]+)[/](.*)");
+			Match m = Regex.Match (url, "tcp://([^:]+):([0-9]+)/?(.*)");
 
 			if (!m.Success)
 				return null;
@@ -123,6 +123,8 @@ namespace System.Runtime.Remoting.Channels.Tcp
 			string port_str = m.Groups[2].Value;
 			objectURI = m.Groups[3].Value;
 			port = Convert.ToInt32 (port_str);
+
+			if (objectURI == string.Empty) objectURI = null;
 				
 			return host;
 		}

+ 8 - 6
mcs/class/System.Runtime.Remoting/System.Runtime.Remoting.Channels.Tcp/TcpClientChannel.cs

@@ -2,7 +2,7 @@
 // System.Runtime.Remoting.Channels.Tcp.TcpClientChannel.cs
 //
 // Author: Dietmar Maurer ([email protected])
-//         Lluis Sanchez Gual (l[email protected])
+//         Lluis Sanchez Gual (l[email protected])
 //
 // 2002 (C) Copyright, Ximian, Inc.
 //
@@ -12,7 +12,6 @@ using System.IO;
 using System.Net.Sockets;
 using System.Runtime.Remoting.Messaging;
 using System.Runtime.Remoting.Channels;
-using System.Runtime.Remoting.Channels.Simple;
 using System.Threading;
 
 namespace System.Runtime.Remoting.Channels.Tcp
@@ -45,8 +44,7 @@ namespace System.Runtime.Remoting.Channels.Tcp
 			}
 			else
 			{
-				// FIXME: change soap to binary
-				_sinkProvider = new SimpleClientFormatterSinkProvider ();
+				_sinkProvider = new BinaryClientFormatterSinkProvider ();
 				_sinkProvider.Next = new TcpClientTransportSinkProvider ();
 			}
 
@@ -84,10 +82,14 @@ namespace System.Runtime.Remoting.Channels.Tcp
 	    {
 			if (url == null && remoteChannelData != null) {
 				IChannelDataStore ds = remoteChannelData as IChannelDataStore;
-				if (ds != null)
+				if (ds != null && ds.ChannelUris.Length > 0)
 					url = ds.ChannelUris [0];
+				else {
+					objectURI = null;
+					return null;
+				}
 			}
-			
+
 			if (Parse (url, out objectURI) == null)
 				return null;
 

+ 22 - 10
mcs/class/System.Runtime.Remoting/System.Runtime.Remoting.Channels.Tcp/TcpClientTransportSink.cs

@@ -2,7 +2,7 @@
 // System.Runtime.Remoting.Channels.Tcp.TcpClientTransportSink.cs
 //
 // Author: Dietmar Maurer ([email protected])
-//         Lluis Sanchez ([email protected])
+//         Lluis Sanchez Gual ([email protected])
 //
 // 2002 (C) Copyright, Ximian, Inc.
 //
@@ -56,8 +56,12 @@ namespace System.Runtime.Remoting.Channels.Tcp
 
 				connection = TcpConnectionPool.GetConnection (_host, _port);
 				TcpMessageIO.SendMessageStream (connection.Stream, requestStream, headers, connection.Buffer);
-				sinkStack.Push (this, connection);
-				ThreadPool.QueueUserWorkItem (new WaitCallback(ReadAsyncTcpMessage), sinkStack);
+
+				if (!RemotingServices.IsOneWay (((IMethodMessage)msg).MethodBase)) 
+				{
+					sinkStack.Push (this, connection);
+					ThreadPool.QueueUserWorkItem (new WaitCallback(ReadAsyncTcpMessage), sinkStack);
+				}
 			}
 			catch
 			{
@@ -131,13 +135,21 @@ namespace System.Runtime.Remoting.Channels.Tcp
 				connection = TcpConnectionPool.GetConnection (_host, _port);
 				TcpMessageIO.SendMessageStream (connection.Stream, requestStream, requestHeaders, connection.Buffer);
 
-				// Reads the response
-				MessageType type = TcpMessageIO.ReceiveMessageType (connection.Stream);
-
-				if (type != MessageType.MethodMessage)
-					throw new RemotingException ("Unknown response message from server");
-
-				responseStream = TcpMessageIO.ReceiveMessageStream (connection.Stream, out responseHeaders, connection.Buffer);
+				if (!RemotingServices.IsOneWay (((IMethodMessage)msg).MethodBase)) 
+				{
+					// Reads the response
+					MessageType type = TcpMessageIO.ReceiveMessageType (connection.Stream);
+
+					if (type != MessageType.MethodMessage)
+						throw new RemotingException ("Unknown response message from server");
+
+					responseStream = TcpMessageIO.ReceiveMessageStream (connection.Stream, out responseHeaders, connection.Buffer);
+				}
+				else
+				{
+					responseHeaders = null;
+					responseStream = null;
+				}
 			}
 			finally
 			{

+ 4 - 2
mcs/class/System.Runtime.Remoting/System.Runtime.Remoting.Channels.Tcp/TcpConnectionPool.cs

@@ -1,7 +1,7 @@
 //
 // System.Runtime.Remoting.Channels.Tcp.TcpConnectionPool.cs
 //
-// Author: Lluis Sanchez ([email protected])
+// Author: Lluis Sanchez Gual ([email protected])
 //
 // 2002 (C) Lluis Sanchez Gual
 //
@@ -29,7 +29,7 @@ namespace System.Runtime.Remoting.Channels.Tcp
 		// instance for each host
 		static Hashtable _pools = new Hashtable();
 
-		static int _maxOpenConnections = 2;
+		static int _maxOpenConnections = 50;
 		static int _keepAliveSeconds = 15;
 
 		static TcpConnectionPool()
@@ -178,7 +178,9 @@ namespace System.Runtime.Remoting.Channels.Tcp
 					// Wait for somewone to release one.
 
 					if (connection == null)
+					{
 						Monitor.Wait(_pool);
+					}
 				} 
 				while (connection == null);
 

+ 2 - 2
mcs/class/System.Runtime.Remoting/System.Runtime.Remoting.Channels.Tcp/TcpMessageIO.cs

@@ -1,6 +1,6 @@
 // System.Runtime.Remoting.Channels.Tcp.TcpMessageIO.cs
 //
-// Author: Lluis Sanchez ([email protected])
+// Author: Lluis Sanchez Gual ([email protected])
 //
 // (C) 2002 Lluis Sanchez Gual
 
@@ -49,7 +49,7 @@ namespace System.Runtime.Remoting.Channels.Tcp
 				}
 				return MessageType.Unknown;
 			}
-			catch (IOException ex)
+			catch (IOException)
 			{
 				// Stream closed
 				return MessageType.CancelSignal;

+ 2 - 3
mcs/class/System.Runtime.Remoting/System.Runtime.Remoting.Channels.Tcp/TcpServerChannel.cs

@@ -2,7 +2,7 @@
 // System.Runtime.Remoting.Channels.Tcp.TcpServerChannel.cs
 //
 // Author: Rodrigo Moya ([email protected])
-//         Lluis Sanchez Gual (l[email protected])
+//         Lluis Sanchez Gual (l[email protected])
 //
 // 2002 (C) Copyright, Ximian, Inc.
 //
@@ -14,7 +14,6 @@ using System.Net.Sockets;
 using System.Net;
 using System.Threading;
 using System.IO;
-using System.Runtime.Remoting.Channels.Simple;
 
 namespace System.Runtime.Remoting.Channels.Tcp
 {
@@ -33,7 +32,7 @@ namespace System.Runtime.Remoting.Channels.Tcp
 		{
 			if (serverSinkProvider == null) {
 				// FIXME: change soap for binary
-				serverSinkProvider = new SimpleServerFormatterSinkProvider ();
+				serverSinkProvider = new BinaryServerFormatterSinkProvider ();
 			}
 
 			host = Dns.GetHostByName(Dns.GetHostName()).HostName;

+ 57 - 29
mcs/class/System.Runtime.Remoting/System.Runtime.Remoting.Channels/BinaryClientFormatterSink.cs

@@ -3,6 +3,7 @@
 //
 // Author: Rodrigo Moya ([email protected])
 //         Dietmar Maurer ([email protected])
+//         Lluis Sanchez Gual ([email protected])
 //
 // 2002 (C) Copyright, Ximian, Inc.
 //
@@ -10,6 +11,7 @@
 using System.Collections;
 using System.IO;
 using System.Runtime.Remoting.Messaging;
+using System.Runtime.Serialization;
 using System.Runtime.Serialization.Formatters.Binary;
 
 namespace System.Runtime.Remoting.Channels
@@ -17,8 +19,20 @@ namespace System.Runtime.Remoting.Channels
 	public class BinaryClientFormatterSink : IClientFormatterSink,
 		IMessageSink, IClientChannelSink, IChannelSinkBase
 	{
+		static BinaryFormatter _serializationFormatter;
+		static BinaryFormatter _deserializationFormatter;
+
 		IClientChannelSink nextInChain;
-		IRemotingFormatter formatter = new BinaryFormatter ();
+
+		static BinaryClientFormatterSink ()
+		{
+			RemotingSurrogateSelector surrogateSelector = new RemotingSurrogateSelector ();
+			StreamingContext context = new StreamingContext(StreamingContextStates.Remoting, null);
+
+			_serializationFormatter = new BinaryFormatter (surrogateSelector, context);
+			_deserializationFormatter = new BinaryFormatter (null, context);
+		}
+
 		
 		public BinaryClientFormatterSink (IClientChannelSink nextSink)
 		{
@@ -35,7 +49,8 @@ namespace System.Runtime.Remoting.Channels
 		public IMessageSink NextSink
 		{
 			get {
-				return (IMessageSink) nextInChain;
+				// This is the last sink in the IMessageSink sink chain
+				return null;
 			}
 		}
 
@@ -46,12 +61,6 @@ namespace System.Runtime.Remoting.Channels
 			}
 		}
 
-		public IMessageCtrl AsyncProcessMessage (IMessage msg,
-							 IMessageSink replySink)
-		{
-			throw new NotImplementedException ();
-		}
-
 		public void AsyncProcessRequest (IClientChannelSinkStack sinkStack,
 						 IMessage msg,
 						 ITransportHeaders headers,
@@ -59,16 +68,16 @@ namespace System.Runtime.Remoting.Channels
 	        {
 			// never called because the formatter sink is
 			// always the first in the chain
-			throw new NotSupportedException ();
+			throw new NotSupportedException("BinaryClientFormatterSink must be the first sink in the IClientChannelSink chain");
 		}
 
-		[MonoTODO]
 		public void AsyncProcessResponse (IClientResponseChannelSinkStack sinkStack,
 						  object state,
 						  ITransportHeaders headers,
 						  Stream stream)
 		{
-			throw new NotImplementedException ();
+			IMessage replyMessage = (IMessage)_deserializationFormatter.DeserializeMethodResponse (stream, null, (IMethodCallMessage)state);
+			sinkStack.DispatchReplyMessage (replyMessage);
 		}
 
 		public Stream GetRequestStream (IMessage msg,
@@ -89,32 +98,51 @@ namespace System.Runtime.Remoting.Channels
 			throw new NotSupportedException ();
 		}
 
-		[MonoTODO]
+		public IMessageCtrl AsyncProcessMessage (IMessage msg,
+			IMessageSink replySink)
+		{
+			ITransportHeaders transportHeaders = new TransportHeaders();
+			transportHeaders[CommonTransportKeys.RequestUri] = ((IMethodCallMessage)msg).Uri;
+
+			Stream stream = nextInChain.GetRequestStream(msg, transportHeaders);
+			if (stream == null) stream = new MemoryStream ();
+
+			_serializationFormatter.Serialize (stream, msg, null);
+			if (stream is MemoryStream) stream.Position = 0;
+
+			ClientChannelSinkStack stack = new ClientChannelSinkStack(replySink);
+			stack.Push (this, msg);
+
+			nextInChain.AsyncProcessRequest (stack, msg, transportHeaders, stream);
+
+			// FIXME: No idea about how to implement IMessageCtrl
+			return null;	
+		}
+
 		public IMessage SyncProcessMessage (IMessage msg)
 		{
 			try {
 
-				ITransportHeaders response_headers;
+				ITransportHeaders call_headers = new TransportHeaders();
+				call_headers[CommonTransportKeys.RequestUri] = ((IMethodCallMessage)msg).Uri;
+
+				Stream call_stream = nextInChain.GetRequestStream(msg, call_headers);
+				if (call_stream == null) call_stream = new MemoryStream ();
+
+				// Serialize msg to the stream
+
+				_serializationFormatter.Serialize (call_stream, msg, null);
+				if (call_stream is MemoryStream) call_stream.Position = 0;
+
 				Stream response_stream;
-			
-				// fixme: use nextInChain.GetRequestStream() ??
-				Stream out_stream = new MemoryStream ();
-				//Stream out_stream = File.Open ("test.bin", FileMode.Create);
-			
-				// serialize msg to the stream
-				//formatter.Serialize (out_stream, msg, null);
-				//formatter.Serialize (out_stream, new Exception ("TEST"), null);
-				//out_stream.Position = 0;			
-				nextInChain.ProcessMessage (msg, null, out_stream, out response_headers,
-							    out response_stream);
+				ITransportHeaders response_headers;
 
-				out_stream.Close ();
+				nextInChain.ProcessMessage (msg, call_headers, call_stream, out response_headers,
+							    out response_stream);
 
-				// deserialize response_stream
-				//IMessage result = (IMessage) formatter.Deserialize (response_stream, null);
-				throw new NotImplementedException ();
+				// Deserialize response_stream
 
-				return null;
+				return (IMessage) _deserializationFormatter.DeserializeMethodResponse (response_stream, null, (IMethodCallMessage)msg);
 				
 			} catch (Exception e) {
 				 return new ReturnMessage (e, (IMethodCallMessage)msg);

+ 55 - 7
mcs/class/System.Runtime.Remoting/System.Runtime.Remoting.Channels/BinaryServerFormatterSink.cs

@@ -2,6 +2,7 @@
 // System.Runtime.Remoting.Channels.BinaryServerFormatterSink.cs
 //
 // Author: Duncan Mak ([email protected])
+//         Lluis Sanchez Gual ([email protected])
 //
 // 2002 (C) Copyright, Ximian, Inc.
 //
@@ -9,6 +10,8 @@
 using System.Collections;
 using System.IO;
 using System.Runtime.Remoting.Messaging;
+using System.Runtime.Serialization;
+using System.Runtime.Serialization.Formatters.Binary;
 
 namespace System.Runtime.Remoting.Channels {
 
@@ -21,9 +24,21 @@ namespace System.Runtime.Remoting.Channels {
 			Other = 1,
 		}
 
+		static BinaryFormatter _serializationFormatter;
+		static BinaryFormatter _deserializationFormatter;
+
 		IServerChannelSink next_sink;
 		Protocol protocol;
 		IChannelReceiver receiver;
+
+		static BinaryServerFormatterSink()
+		{
+			RemotingSurrogateSelector surrogateSelector = new RemotingSurrogateSelector ();
+			StreamingContext context = new StreamingContext(StreamingContextStates.Remoting, null);
+
+			_serializationFormatter = new BinaryFormatter (surrogateSelector, context);
+			_deserializationFormatter = new BinaryFormatter (null, context);
+		}
 		
 		public BinaryServerFormatterSink (BinaryServerFormatterSink.Protocol protocol,
 						  IServerChannelSink nextSink,
@@ -46,26 +61,59 @@ namespace System.Runtime.Remoting.Channels {
 			}
 		}
 
-		[MonoTODO]
 		public void AsyncProcessResponse (IServerResponseChannelSinkStack sinkStack, object state,
-						  IMessage msg, ITransportHeaders headers, Stream stream)
+						  IMessage message, ITransportHeaders headers, Stream stream)
 		{
-			throw new NotImplementedException ();
+			ITransportHeaders responseHeaders = new TransportHeaders();
+
+			if (sinkStack != null) stream = sinkStack.GetResponseStream (message, responseHeaders);
+			if (stream == null) stream = new MemoryStream();
+
+			_serializationFormatter.Serialize (stream, message, null);
+			if (stream is MemoryStream) stream.Position = 0;
+
+			sinkStack.AsyncProcessResponse (message, responseHeaders, stream);
 		}
 
 		public Stream GetResponseStream (IServerResponseChannelSinkStack sinkStack, object state,
 						IMessage msg, ITransportHeaders headers)
 		{
-			// never called 
-			throw new NotSupportedException ();
+			return null;
 		}
 		
-		[MonoTODO]
 		public ServerProcessing ProcessMessage (IServerChannelSinkStack sinkStack,
 							IMessage requestMsg, ITransportHeaders requestHeaders, Stream requestStream,
 							out IMessage responseMsg, out ITransportHeaders responseHeaders, out Stream responseStream)
 		{
-			throw new NotImplementedException ();
+			sinkStack.Push (this, null);
+
+			requestMsg = (IMessage) _deserializationFormatter.Deserialize (requestStream, null);
+
+			string url = (string)requestHeaders[CommonTransportKeys.RequestUri];
+			string uri;
+			receiver.Parse (url, out uri);
+			if (uri == null) uri = url;
+			requestMsg.Properties["__Uri"] = uri;
+
+			// Fixme: check if the message is an async msg
+
+			ServerProcessing res = next_sink.ProcessMessage (sinkStack, requestMsg, requestHeaders, null, out responseMsg, out responseHeaders, out responseStream);
+
+			if (res == ServerProcessing.Complete)
+			{
+				responseStream = null;
+				responseHeaders = new TransportHeaders();
+
+				if (sinkStack != null) responseStream = sinkStack.GetResponseStream (responseMsg, responseHeaders);
+				if (responseStream == null) responseStream = new MemoryStream();
+
+				_serializationFormatter.Serialize (responseStream, responseMsg);
+				if (responseStream is MemoryStream) responseStream.Position = 0;
+
+				sinkStack.Pop (this);
+			}
+			return res;
 		}
+
 	}
 }