File: ServerUtilities.cs

package info (click to toggle)
mono 4.6.2.7+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (128 lines) | stat: -rw-r--r-- 4,111 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// ****************************************************************
// Copyright 2007, Charlie Poole
// This is free software licensed under the NUnit license. You may
// obtain a copy of the license at http://nunit.org/?p=license&r=2.4
// ****************************************************************
using System;
using System.IO;
using System.Collections;
using System.Collections.Specialized;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Reflection;
using System.Diagnostics;

namespace NUnit.Util
{
	/// <summary>
	/// Summary description for RemotingUtilities.
	/// </summary>
	public class ServerUtilities
	{
		/// <summary>
		///  Create a TcpChannel with a given name, on a given port.
		/// </summary>
		/// <param name="port"></param>
		/// <param name="name"></param>
		/// <returns></returns>
		private static TcpChannel CreateTcpChannel( string name, int port, int limit )
		{
			ListDictionary props = new ListDictionary();
			props.Add( "port", port );
			props.Add( "name", name );
			props.Add( "bindTo", "127.0.0.1" );

			BinaryServerFormatterSinkProvider serverProvider =
				new BinaryServerFormatterSinkProvider();

            // NOTE: TypeFilterLevel and "clientConnectionLimit" property don't exist in .NET 1.0.
			Type typeFilterLevelType = typeof(object).Assembly.GetType("System.Runtime.Serialization.Formatters.TypeFilterLevel");
			if (typeFilterLevelType != null)
			{
				PropertyInfo typeFilterLevelProperty = serverProvider.GetType().GetProperty("TypeFilterLevel");
				object typeFilterLevel = Enum.Parse(typeFilterLevelType, "Full");
				typeFilterLevelProperty.SetValue(serverProvider, typeFilterLevel, null);

                props.Add("clientConnectionLimit", limit);
            }

			BinaryClientFormatterSinkProvider clientProvider =
				new BinaryClientFormatterSinkProvider();

			return new TcpChannel( props, clientProvider, serverProvider );
		}

		public static TcpChannel GetTcpChannel()
		{
			return GetTcpChannel( "", 0, 2 );
		}

		/// <summary>
		/// Get a channel by name, casting it to a TcpChannel.
		/// Otherwise, create, register and return a TcpChannel with
		/// that name, on the port provided as the second argument.
		/// </summary>
		/// <param name="name">The channel name</param>
		/// <param name="port">The port to use if the channel must be created</param>
		/// <returns>A TcpChannel or null</returns>
		public static TcpChannel GetTcpChannel( string name, int port )
		{
			return GetTcpChannel( name, port, 2 );
		}
		
		/// <summary>
		/// Get a channel by name, casting it to a TcpChannel.
		/// Otherwise, create, register and return a TcpChannel with
		/// that name, on the port provided as the second argument.
		/// </summary>
		/// <param name="name">The channel name</param>
		/// <param name="port">The port to use if the channel must be created</param>
		/// <param name="limit">The client connection limit or negative for the default</param>
		/// <returns>A TcpChannel or null</returns>
		public static TcpChannel GetTcpChannel( string name, int port, int limit )
		{
			TcpChannel channel = ChannelServices.GetChannel( name ) as TcpChannel;

			if ( channel == null )
			{
				// NOTE: Retries are normally only needed when rapidly creating
				// and destroying channels, as in running the NUnit tests.
				int retries = 10;
				while( --retries > 0 )
					try
					{
						channel = CreateTcpChannel( name, port, limit );
						ChannelServices.RegisterChannel( channel );
						break;
					}
					catch( Exception e )
					{
                        Trace.WriteLine(e);
						System.Threading.Thread.Sleep(300);
					}
			}

			return channel;
		}

		public static void SafeReleaseChannel( IChannel channel )
		{
			if( channel != null )
				try
				{
					ChannelServices.UnregisterChannel( channel );
				}
				catch( RemotingException )
				{
					// Channel was not registered - ignore
				}
		}

		public static string MakeUrl( string uri, int port )
		{
			return string.Format( "tcp://127.0.0.1:{0}/{1}", port, uri );
		}

	}
}