Преглед изворни кода

more tcp channel code, renamed soap namespace to Soap

svn path=/trunk/mcs/; revision=6990
Dietmar Maurer пре 23 година
родитељ
комит
2634b55731
14 измењених фајлова са 378 додато и 74 уклоњено
  1. 26 2
      mcs/class/System.Runtime.Remoting/System.Runtime.Remoting.Channels.Tcp/TcpChannel.cs
  2. 22 16
      mcs/class/System.Runtime.Remoting/System.Runtime.Remoting.Channels.Tcp/TcpClientChannel.cs
  3. 217 0
      mcs/class/System.Runtime.Remoting/System.Runtime.Remoting.Channels.Tcp/TcpServerChannel.cs
  4. 31 22
      mcs/class/System.Runtime.Remoting/System.Runtime.Remoting.Channels/BinaryClientFormatterSink.cs
  5. 12 7
      mcs/class/System.Runtime.Remoting/System.Runtime.Remoting.Channels/BinaryClientFormatterSinkProvider.cs
  6. 14 12
      mcs/class/System.Runtime.Remoting/System.Runtime.Remoting.Channels/BinaryServerFormatterSink.cs
  7. 18 10
      mcs/class/System.Runtime.Remoting/System.Runtime.Remoting.Channels/BinaryServerFormatterSinkProvider.cs
  8. 1 1
      mcs/class/System.Runtime.Serialization.Formatters.Soap/System.Runtime.Serialization.Formatters.Soap/ObjectDeserializer.cs
  9. 1 1
      mcs/class/System.Runtime.Serialization.Formatters.Soap/System.Runtime.Serialization.Formatters.Soap/ObjectSerializer.cs
  10. 1 1
      mcs/class/System.Runtime.Serialization.Formatters.Soap/System.Runtime.Serialization.Formatters.Soap/SoapFormatter.cs
  11. 1 1
      mcs/class/System.Runtime.Serialization.Formatters.Soap/System.Runtime.Serialization.Formatters.Soap/SoapReader.cs
  12. 1 1
      mcs/class/System.Runtime.Serialization.Formatters.Soap/System.Runtime.Serialization.Formatters.Soap/SoapWriter.cs
  13. 32 0
      mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/BinaryFormatter.cs
  14. 1 0
      mcs/class/library.build

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

@@ -8,6 +8,7 @@
 
 using System.Collections;
 using System.Runtime.Remoting.Messaging;
+using System.Text.RegularExpressions;
 
 namespace System.Runtime.Remoting.Channels.Tcp
 {
@@ -72,10 +73,13 @@ namespace System.Runtime.Remoting.Channels.Tcp
 			throw new NotImplementedException ();
 		}
 
-		[MonoTODO]
 		public string Parse (string url, out string objectURI)
 		{
-			throw new NotImplementedException ();
+			int port;
+			
+			string host = ParseTcpURL (url, out objectURI, out port);
+
+			return "tcp://" + host + ":" + port;
 		}
 
 		[MonoTODO]
@@ -89,5 +93,25 @@ namespace System.Runtime.Remoting.Channels.Tcp
 		{
 			throw new NotImplementedException ();
 		}
+
+		internal static string ParseTcpURL (string url, out string objectURI, out int port)
+		{
+			// format: "tcp://host:port/path/to/object"
+			
+			objectURI = null;
+			port = 0;
+			
+			Match m = Regex.Match (url, "tcp://([^:]+):([0-9]+)(/.*)");
+
+			if (!m.Success)
+				return null;
+			
+			string host = m.Groups[1].Value;
+			string port_str = m.Groups[2].Value;
+			objectURI = m.Groups[3].Value;
+			port = Convert.ToInt32 (port_str);
+				
+			return host;
+		}
 	}
 }

+ 22 - 16
mcs/class/System.Runtime.Remoting/System.Runtime.Remoting.Channels.Tcp/TcpClientChannel.cs

@@ -8,19 +8,26 @@
 
 using System.Collections;
 using System.IO;
+using System.Net.Sockets;
 using System.Runtime.Remoting.Messaging;
 using System.Runtime.Remoting.Channels;
-using System.Text.RegularExpressions;
 
 namespace System.Runtime.Remoting.Channels.Tcp
 {
 
 	public class TcpClientTransportSink : IClientChannelSink
 	{
-
+		string host;
+		string object_uri;
+		int port;
+		
+		TcpClient tcpclient;
+		Stream stream = null;
+		
 		public TcpClientTransportSink (string url)
 		{
-
+			host = TcpChannel.ParseTcpURL (url, out object_uri, out port);
+			tcpclient = new TcpClient ();
 		}
 
 		public IDictionary Properties
@@ -63,7 +70,16 @@ namespace System.Runtime.Remoting.Channels.Tcp
 					    out ITransportHeaders responseHeaders,
 					    out Stream responseStream)
 		{
-			throw new NotImplementedException ();
+			if (stream == null) {
+				tcpclient.Connect (host, port);
+				stream = tcpclient.GetStream ();
+			}
+
+			Console.WriteLine ("Client  ProcessMessage");
+
+			responseHeaders = null;
+			responseStream = null;
+			//throw new NotImplementedException ();
 		}
 			
 	}
@@ -160,21 +176,11 @@ namespace System.Runtime.Remoting.Channels.Tcp
 
 		public string Parse (string url, out string objectURI)
 		{
-			// format: "tcp://host:port/path/to/object"
-			
-			objectURI = null;
+			int port;
 			
-			Match m = Regex.Match (url, "tcp://([^:]+):([0-9]+)(/.*)");
+			string host = TcpChannel.ParseTcpURL (url, out objectURI, out port);
 
-			if (!m.Success)
-				return null;
-			
-			string host = m.Groups[1].Value;
-			string port = m.Groups[2].Value;
-			objectURI = m.Groups[3].Value;
-			
 			return "tcp://" + host + ":" + port;
 		}
-
 	}
 }

+ 217 - 0
mcs/class/System.Runtime.Remoting/System.Runtime.Remoting.Channels.Tcp/TcpServerChannel.cs

@@ -0,0 +1,217 @@
+//
+// System.Runtime.Remoting.Channels.Tcp.TcpChannel.cs
+//
+// Author: Rodrigo Moya ([email protected])
+//
+// 2002 (C) Copyright, Ximian, Inc.
+//
+
+using System.Collections;
+using System.Runtime.Remoting.Messaging;
+using System.Text.RegularExpressions;
+using System.Net.Sockets;
+using System.Net;
+using System.Threading;
+using System.IO;
+
+namespace System.Runtime.Remoting.Channels.Tcp
+{
+	public class TcpServerTransportSink : IServerChannelSink, IChannelSinkBase
+	{
+		IServerChannelSink next_sink;
+		
+		public TcpServerTransportSink (IServerChannelSink next)
+		{
+			next_sink = next;
+		}
+		
+		public IServerChannelSink NextChannelSink {
+			get {
+				return next_sink;
+			}
+		}
+
+		[MonoTODO]
+		public IDictionary Properties {
+			get {
+				throw new NotImplementedException ();
+			}
+		}
+
+		[MonoTODO]
+		public void AsyncProcessResponse (IServerResponseChannelSinkStack sinkStack, object state,
+						  IMessage msg, ITransportHeaders headers, Stream stream)
+		{
+			throw new NotImplementedException ();
+		}
+
+		[MonoTODO]
+		public Stream GetResponseStream (IServerResponseChannelSinkStack sinkStack, object state,
+						IMessage msg, ITransportHeaders headers)
+		{
+			throw new NotImplementedException ();
+		}
+		
+		public ServerProcessing ProcessMessage (IServerChannelSinkStack sinkStack,
+							IMessage requestMsg,
+							ITransportHeaders requestHeaders,
+							Stream requestStream,
+							out IMessage responseMsg,
+							out ITransportHeaders responseHeaders,
+							out Stream responseStream)
+		{
+			// this is the first sink, and TcpServerChannel does not call it.
+			throw new NotSupportedException ();
+		}
+
+		internal void InternalProcessMessage (Stream requestStream)
+		{
+			Console.WriteLine ("ProcessMessageInternal");
+		}
+	}
+
+	public class TcpServerChannel : IChannelReceiver, IChannel
+	{
+		int port = 0;
+		string name = "tcp";
+		string host;
+		int priority = 1;
+		Thread server_thread = null;
+		TcpListener listener;
+		TcpServerTransportSink sink;
+		ChannelDataStore channel_data;
+		
+		void Init (IServerChannelSinkProvider provider) {
+			if (provider == null) {
+				provider = new BinaryServerFormatterSinkProvider ();
+			}
+			IServerChannelSink next_sink = provider.CreateSink (this);
+
+			host = Dns.GetHostByName(Dns.GetHostName()).HostName;
+			
+			string [] uris = null;
+			
+			if (port != 0) {
+				uris = new String [1];
+				uris [0] = GetChannelUri ();
+			}
+			
+			channel_data = new ChannelDataStore (uris);;
+
+			sink = new TcpServerTransportSink (next_sink);
+			
+			listener = new TcpListener (port);
+			StartListening (null);
+		}
+		
+		public TcpServerChannel (int port)
+		{
+			this.port = port;
+			Init (null);
+		}
+
+		public TcpServerChannel (IDictionary properties,
+					 IServerChannelSinkProvider serverSinkProvider)
+		{
+			port = (int)properties ["port"];
+			Init (serverSinkProvider);
+		}
+
+		public TcpServerChannel (string name, int port,
+					 IServerChannelSinkProvider serverSinkProvider)
+		{
+			name = name;
+			this.port = port;
+			Init (serverSinkProvider);
+		}
+		
+		public TcpServerChannel (string name, int port)
+		{
+			name = name;
+			this.port = port;
+			Init (null);
+		}
+		
+		public object ChannelData
+		{
+			get {
+				return channel_data;
+			}
+		}
+
+		public string ChannelName
+		{
+			get {
+				return name;
+			}
+		}
+
+		public int ChannelPriority
+		{
+			get {
+				return priority;
+			}
+		}
+
+		public string GetChannelUri ()
+		{
+			return "tcp://" + host + ":" + port;
+		}
+		
+		public string[] GetUrlsForUri (string uri)
+		{
+			string [] result = new String [1];
+
+			if (uri.IndexOf ('/') != 0)
+				result [0] = GetChannelUri () + "/" + uri;
+			else
+				result [0] = GetChannelUri () + uri;
+
+			return result;
+		}
+
+		public string Parse (string url, out string objectURI)
+		{
+			int port;
+			
+			string host = TcpChannel.ParseTcpURL (url, out objectURI, out port);
+
+			return "tcp://" + host + ":" + port;
+		}
+
+		void WaitForConnections ()
+		{
+			while (true) {
+				TcpClient client = listener.AcceptTcpClient ();
+
+				sink.InternalProcessMessage (client.GetStream ());
+				
+				client.Close ();
+			}
+		}
+		
+		public void StartListening (object data)
+		{
+			if (server_thread == null) {
+				listener.Start ();
+				if (port == 0) {
+					port = ((IPEndPoint)listener.LocalEndpoint).Port;
+					channel_data.ChannelUris = new String [1];
+					channel_data.ChannelUris [0] = GetChannelUri ();
+				}
+
+				server_thread = new Thread (new ThreadStart (WaitForConnections));
+				server_thread.Start ();
+			}
+		}
+
+		public void StopListening (object data)
+		{
+			if (server_thread != null) {
+				server_thread.Abort ();
+				server_thread = null;
+				listener.Stop ();
+			}
+		}
+	}
+}

+ 31 - 22
mcs/class/System.Runtime.Remoting/System.Runtime.Remoting.Channels/BinaryClientFormatterSink.cs

@@ -46,20 +46,20 @@ namespace System.Runtime.Remoting.Channels
 			}
 		}
 
-		[MonoTODO]
 		public IMessageCtrl AsyncProcessMessage (IMessage msg,
 							 IMessageSink replySink)
 		{
 			throw new NotImplementedException ();
 		}
 
-		[MonoTODO]
 		public void AsyncProcessRequest (IClientChannelSinkStack sinkStack,
 						 IMessage msg,
 						 ITransportHeaders headers,
 						 Stream stream)
 	        {
-			throw new NotImplementedException ();
+			// never called because the formatter sink is
+			// always the first in the chain
+			throw new NotSupportedException ();
 		}
 
 		[MonoTODO]
@@ -74,7 +74,8 @@ namespace System.Runtime.Remoting.Channels
 		public Stream GetRequestStream (IMessage msg,
 						ITransportHeaders headers)
 		{
-			return null;
+			// never called
+			throw new NotSupportedException ();
 		}
 
 		public void ProcessMessage (IMessage msg,
@@ -83,33 +84,41 @@ namespace System.Runtime.Remoting.Channels
 					    out ITransportHeaders responseHeaders,
 					    out Stream responseStream)
 		{
-			nextInChain.ProcessMessage (msg, requestHeaders, requestStream,
-						    out responseHeaders, out responseStream);
+			// never called because the formatter sink is
+			// always the first in the chain
+			throw new NotSupportedException ();
 		}
 
 		[MonoTODO]
 		public IMessage SyncProcessMessage (IMessage msg)
 		{
-			ITransportHeaders response_headers;
-			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);
+			try {
+
+				ITransportHeaders response_headers;
+				Stream response_stream;
 			
-			out_stream.Close ();
-			throw new NotImplementedException ();
+				// fixme: use nextInChain.GetRequestStream() ??
+				Stream out_stream = new MemoryStream ();
+				//Stream out_stream = File.Open ("test.bin", FileMode.Create);
 			
-			ProcessMessage (msg, null, out_stream, out response_headers, out response_stream);
+				// 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);
+
+				out_stream.Close ();
 
-			// deserialize response_stream
-			IMessage result = (IMessage) formatter.Deserialize (response_stream, null);
+				// deserialize response_stream
+				//IMessage result = (IMessage) formatter.Deserialize (response_stream, null);
+				throw new NotImplementedException ();
 
-			return null;
+				return null;
+				
+			} catch (Exception e) {
+				 return new ReturnMessage (e, (IMethodCallMessage)msg);
+			}
 		}
 	}
 }

+ 12 - 7
mcs/class/System.Runtime.Remoting/System.Runtime.Remoting.Channels/BinaryClientFormatterSinkProvider.cs

@@ -15,19 +15,19 @@ namespace System.Runtime.Remoting.Channels
 	{
 		IClientChannelSinkProvider next = null;
 
-		// this is not used at the moment ??
-		IDictionary properties;
+		// add any sink properties here (private fields)
 		
 		public BinaryClientFormatterSinkProvider ()
 		{
-			properties = new Hashtable ();
+			// nothing to do
 		}
 
 		public BinaryClientFormatterSinkProvider (IDictionary properties,
 							  ICollection providerData)
 	        {
-			this.properties = properties;
-			// fixme: what shall we do with providerData?
+			// copy the contained properties to private fields
+			
+			// add a check that there is no providerData 
 		}
 
 		public IClientChannelSinkProvider Next
@@ -46,11 +46,16 @@ namespace System.Runtime.Remoting.Channels
 						      object remoteChannelData)
 		{
 			IClientChannelSink next_sink = null;
-
+			BinaryClientFormatterSink result;
+			
 			if (next != null)
 				next_sink = next.CreateSink (channel, url, remoteChannelData);
 			
-			return new BinaryClientFormatterSink (next_sink);
+			result = new BinaryClientFormatterSink (next_sink);
+
+			// set properties on the newly creates sink
+
+			return result;
 		}		
 	}
 }

+ 14 - 12
mcs/class/System.Runtime.Remoting/System.Runtime.Remoting.Channels/BinaryServerFormatterSink.cs

@@ -14,14 +14,24 @@ namespace System.Runtime.Remoting.Channels {
 
 	public class BinaryServerFormatterSink : IServerChannelSink, IChannelSinkBase
 	{
+		[Serializable]
+		public enum Protocol
+		{
+			Http = 0,
+			Other = 1,
+		}
+
 		IServerChannelSink next_sink;
+		Protocol protocol;
+		IChannelReceiver receiver;
 		
-		[MonoTODO]
 		public BinaryServerFormatterSink (BinaryServerFormatterSink.Protocol protocol,
 						  IServerChannelSink nextSink,
 						  IChannelReceiver receiver)
 		{
+			this.protocol = protocol;
 			this.next_sink = nextSink;
+			this.receiver = receiver;
 		}
 
 		public IServerChannelSink NextChannelSink {
@@ -30,10 +40,9 @@ namespace System.Runtime.Remoting.Channels {
 			}
 		}
 
-		[MonoTODO]
 		public IDictionary Properties {
 			get {
-				throw new NotImplementedException ();
+				return null;
 			}
 		}
 
@@ -44,11 +53,11 @@ namespace System.Runtime.Remoting.Channels {
 			throw new NotImplementedException ();
 		}
 
-		[MonoTODO]
 		public Stream GetResponseStream (IServerResponseChannelSinkStack sinkStack, object state,
 						IMessage msg, ITransportHeaders headers)
 		{
-			throw new NotImplementedException ();
+			// never called 
+			throw new NotSupportedException ();
 		}
 		
 		[MonoTODO]
@@ -58,12 +67,5 @@ namespace System.Runtime.Remoting.Channels {
 		{
 			throw new NotImplementedException ();
 		}
-		
-		[Serializable]
-		public enum Protocol
-		{
-			Http = 0,
-			Other = 1,
-		}
 	}
 }

+ 18 - 10
mcs/class/System.Runtime.Remoting/System.Runtime.Remoting.Channels/BinaryServerFormatterSinkProvider.cs

@@ -13,10 +13,10 @@ namespace System.Runtime.Remoting.Channels
 	public class BinaryServerFormatterSinkProvider :
 		IServerFormatterSinkProvider, IServerChannelSinkProvider
 	{
-		[MonoTODO]
+		IServerChannelSinkProvider next = null;
+
 		public BinaryServerFormatterSinkProvider ()
 		{
-			throw new NotImplementedException ();
 		}
 
 		[MonoTODO]
@@ -28,26 +28,34 @@ namespace System.Runtime.Remoting.Channels
 
 		public IServerChannelSinkProvider Next
 		{
-			[MonoTODO]
 			get {
-				throw new NotImplementedException ();
+				return next;
 			}
-			[MonoTODO]
+
 			set {
-				throw new NotImplementedException ();
+				next = value;
 			}
 		}
 
-		[MonoTODO]
 		public IServerChannelSink CreateSink (IChannelReceiver channel)
 		{
-			throw new NotImplementedException ();
+			IServerChannelSink next_sink = null;
+			BinaryServerFormatterSink result;
+			
+			if (next != null)
+				next_sink = next.CreateSink (channel);
+			
+			result = new BinaryServerFormatterSink (BinaryServerFormatterSink.Protocol.Other,
+								next_sink, channel);
+
+			// set properties on result
+			
+			return result;
 		}
 
-		[MonoTODO]
 		public void GetChannelData (IChannelDataStore channelData)
 		{
-			throw new NotImplementedException ();
+			// no idea why we need this
 		}
 	}
 }

+ 1 - 1
mcs/class/System.Runtime.Serialization.Formatters.Soap/System.Runtime.Serialization.Formatters.Soap/ObjectDeserializer.cs

@@ -4,7 +4,7 @@ using System.Xml;
 using System.Reflection;
 using System.Collections;
 
-namespace System.Runtime.Serialization.Formatters.soap
+namespace System.Runtime.Serialization.Formatters.Soap
 {	
 	internal class ObjectDeserializer
 	{

+ 1 - 1
mcs/class/System.Runtime.Serialization.Formatters.Soap/System.Runtime.Serialization.Formatters.Soap/ObjectSerializer.cs

@@ -10,7 +10,7 @@ using System.Xml;
 using System.Reflection;
 using System.Collections;
 
-namespace System.Runtime.Serialization.Formatters.soap
+namespace System.Runtime.Serialization.Formatters.Soap
 {
 	internal class ObjectSerializer
 	{		

+ 1 - 1
mcs/class/System.Runtime.Serialization.Formatters.Soap/System.Runtime.Serialization.Formatters.Soap/SoapFormatter.cs

@@ -13,7 +13,7 @@ using System.Runtime.Serialization;
 using System.Runtime.Serialization.Formatters;
 
 
-namespace System.Runtime.Serialization.Formatters.soap
+namespace System.Runtime.Serialization.Formatters.Soap
 {
 	public class SoapFormatter : IRemotingFormatter, IFormatter
 	{

+ 1 - 1
mcs/class/System.Runtime.Serialization.Formatters.Soap/System.Runtime.Serialization.Formatters.Soap/SoapReader.cs

@@ -2,7 +2,7 @@ using System;
 using System.Xml;
 using System.Reflection;
 
-namespace System.Runtime.Serialization.Formatters.soap
+namespace System.Runtime.Serialization.Formatters.Soap
 {	
 
 	public enum ReferenceTypes {Array_Type, Object_Type, Interface_Type, Delegate_Type, String_Type};

+ 1 - 1
mcs/class/System.Runtime.Serialization.Formatters.Soap/System.Runtime.Serialization.Formatters.Soap/SoapWriter.cs

@@ -10,7 +10,7 @@ using System.Collections;
 using System.Xml;
 using System.IO;
 
-namespace System.Runtime.Serialization.Formatters.soap
+namespace System.Runtime.Serialization.Formatters.Soap
 {
 	
 		internal class SoapWriter

+ 32 - 0
mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/BinaryFormatter.cs

@@ -7,6 +7,8 @@
 
 using System.Runtime.Serialization.Formatters;
 using System.Runtime.Serialization;
+using System.Reflection;
+using System.Collections;
 using System.IO;
 using System.Runtime.Remoting.Messaging;
 
@@ -129,6 +131,33 @@ namespace System.Runtime.Serialization.Formatters.Binary {
 			if(serializationStream==null) {
 				throw new ArgumentNullException("serializationStream is null");
 			}
+
+			ISerializable ser = graph as ISerializable;
+
+			StreamingContext context = new StreamingContext (StreamingContextStates.Remoting);
+			object [] oa;
+			
+			if (ser != null) {
+				SerializationInfo info = new SerializationInfo (graph.GetType (), new FormatterConverter ());
+				ser.GetObjectData (info, context);
+				SerializationInfoEnumerator e = info.GetEnumerator ();
+				oa = new object [info.MemberCount];
+				int i = 0;
+				while (e.MoveNext ()) {
+					oa [i++] = e.Current;
+				}
+				Console.WriteLine ("SERIALIZABLE" + info.MemberCount);
+			} else {
+				MemberInfo [] members = FormatterServices.GetSerializableMembers (graph.GetType (), context);
+				oa = FormatterServices.GetObjectData (graph, members);
+				Console.WriteLine ("NOT SERIALIZABLE" + oa.Length);
+			}
+
+			foreach (object o in oa) {
+				Console.WriteLine ("OBJ" + o);
+			}
+			
+			throw new NotImplementedException ();
 		}
 
 		[MonoTODO]
@@ -137,6 +166,9 @@ namespace System.Runtime.Serialization.Formatters.Binary {
 			if(serializationStream==null) {
 				throw new ArgumentNullException("serializationStream is null");
 			}
+
+			// fixme: what about headers?
+			Serialize (serializationStream, graph);			
 		}
 	}
 }

+ 1 - 0
mcs/class/library.build

@@ -20,6 +20,7 @@
 		<nant basedir="Microsoft.VisualBasic" target="build"/>
 		<nant basedir="System.Configuration.Install" target="build"/>
 		<nant basedir="System.Runtime.Remoting" target="build"/>
+		<nant basedir="System.Runtime.Serialization.Formatters.Soap" target="build"/>
 	</target>
 
 	<target name="test">