retag
[mcs.git] / nunit24 / ClientUtilities / util / ServerUtilities.cs
blob5f779a52f41cfe9a408c87dd240ee8a7b6901544
1 // ****************************************************************
2 // Copyright 2007, Charlie Poole
3 // This is free software licensed under the NUnit license. You may
4 // obtain a copy of the license at http://nunit.org/?p=license&r=2.4
5 // ****************************************************************
6 using System;
7 using System.IO;
8 using System.Collections;
9 using System.Collections.Specialized;
10 using System.Runtime.Remoting;
11 using System.Runtime.Remoting.Channels;
12 using System.Runtime.Remoting.Channels.Tcp;
13 using System.Reflection;
14 using System.Diagnostics;
16 namespace NUnit.Util
18 /// <summary>
19 /// Summary description for RemotingUtilities.
20 /// </summary>
21 public class ServerUtilities
23 /// <summary>
24 /// Create a TcpChannel with a given name, on a given port.
25 /// </summary>
26 /// <param name="port"></param>
27 /// <param name="name"></param>
28 /// <returns></returns>
29 private static TcpChannel CreateTcpChannel( string name, int port, int limit )
31 ListDictionary props = new ListDictionary();
32 props.Add( "port", port );
33 props.Add( "name", name );
34 props.Add( "bindTo", "127.0.0.1" );
36 BinaryServerFormatterSinkProvider serverProvider =
37 new BinaryServerFormatterSinkProvider();
39 // NOTE: TypeFilterLevel and "clientConnectionLimit" property don't exist in .NET 1.0.
40 Type typeFilterLevelType = typeof(object).Assembly.GetType("System.Runtime.Serialization.Formatters.TypeFilterLevel");
41 if (typeFilterLevelType != null)
43 PropertyInfo typeFilterLevelProperty = serverProvider.GetType().GetProperty("TypeFilterLevel");
44 object typeFilterLevel = Enum.Parse(typeFilterLevelType, "Full");
45 typeFilterLevelProperty.SetValue(serverProvider, typeFilterLevel, null);
47 props.Add("clientConnectionLimit", limit);
50 BinaryClientFormatterSinkProvider clientProvider =
51 new BinaryClientFormatterSinkProvider();
53 return new TcpChannel( props, clientProvider, serverProvider );
56 public static TcpChannel GetTcpChannel()
58 return GetTcpChannel( "", 0, 2 );
61 /// <summary>
62 /// Get a channel by name, casting it to a TcpChannel.
63 /// Otherwise, create, register and return a TcpChannel with
64 /// that name, on the port provided as the second argument.
65 /// </summary>
66 /// <param name="name">The channel name</param>
67 /// <param name="port">The port to use if the channel must be created</param>
68 /// <returns>A TcpChannel or null</returns>
69 public static TcpChannel GetTcpChannel( string name, int port )
71 return GetTcpChannel( name, port, 2 );
74 /// <summary>
75 /// Get a channel by name, casting it to a TcpChannel.
76 /// Otherwise, create, register and return a TcpChannel with
77 /// that name, on the port provided as the second argument.
78 /// </summary>
79 /// <param name="name">The channel name</param>
80 /// <param name="port">The port to use if the channel must be created</param>
81 /// <param name="limit">The client connection limit or negative for the default</param>
82 /// <returns>A TcpChannel or null</returns>
83 public static TcpChannel GetTcpChannel( string name, int port, int limit )
85 TcpChannel channel = ChannelServices.GetChannel( name ) as TcpChannel;
87 if ( channel == null )
89 // NOTE: Retries are normally only needed when rapidly creating
90 // and destroying channels, as in running the NUnit tests.
91 int retries = 10;
92 while( --retries > 0 )
93 try
95 channel = CreateTcpChannel( name, port, limit );
96 ChannelServices.RegisterChannel( channel );
97 break;
99 catch( Exception e )
101 Trace.WriteLine(e);
102 System.Threading.Thread.Sleep(300);
106 return channel;
109 public static void SafeReleaseChannel( IChannel channel )
111 if( channel != null )
114 ChannelServices.UnregisterChannel( channel );
116 catch( RemotingException )
118 // Channel was not registered - ignore
122 public static string MakeUrl( string uri, int port )
124 return string.Format( "tcp://127.0.0.1:{0}/{1}", port, uri );