/// <summary> /// a factory method which creates the listener and makes it start listening. /// </summary> /// <param name="appName"></param> /// <returns></returns> public static void StartListening(string appName, LinkEventHandler handler) { s_appName = appName; FwBroker broker = FwBroker.GetBroker(); s_port = broker.RegisterAndGetPort(appName); FwLinkListener.OnLinkRequest += handler; ChannelServices.RegisterChannel(new TcpChannel(s_port)); RemotingConfiguration.ApplicationName = appName; RemotingConfiguration.RegisterWellKnownServiceType(typeof(FwLinkListener), "listener", WellKnownObjectMode.Singleton ); //although we don't really need to use it, we create this object now //so that we can preload with appropriate properties. string path = FwLinkListener.GetPortPath(s_port, appName); FwLinkListener listener = (FwLinkListener)Activator.GetObject(typeof(FwLinkListener), path); if (listener == null) { throw new ApplicationException("Could not create the initial listener for this application"); } listener.Test(); }
/// <summary> /// Attempts to find an application which matches the link, and passes the link to that application. /// Does not attempt to launch new applications. /// </summary> /// <param name="link"></param> /// <returns>true if it successfully linked to a running application</returns> public static bool AttemptLink(FwLink link) { try { FwBroker broker = FwBroker.GetBroker(); int port = broker.GetRunningApplicationPort(link.ApplicationName); //note that this will not fail, even if no one is listening on that channel FwLinkListener listener = (FwLinkListener)Activator.GetObject(typeof(FwLinkListener), GetPortPath(port, link.ApplicationName)); //but this will fail if we did not really connect to a listener listener.Request(link); return(true); } catch (Exception error) { } return(false); }
/* /// <summary> * /// * /// </summary> * /// <param name="applicationName"></param> * /// <returns></returns> * protected static int RegisterNextAvailablePortForApplication (string applicationName) * { * string[] paths= new string[kMaxApplicationsOfOneType]; * //find a port that is not in use * int start =GetStartingPortForApplication(applicationName); * for(int port = start; port< start + kMaxApplicationsOfOneType;port++) * { * try * { * ChannelServices.RegisterChannel(new TcpChannel(port)); * } * catch(System.Net.Sockets.SocketException) * { * continue; * } * return port; * } * throw new ApplicationException("Could not find an open port for this application to use."); * } * * protected static string[] GetPossiblePathsToApplication(string applicationName) * { * int start = GetStartingPortForApplication(applicationName); * string[] paths= new string[kMaxApplicationsOfOneType]; * for(int port = start; port < start + kMaxApplicationsOfOneType; port++) * { * paths[port-start] = GetPortPath(port, applicationName); * } * return paths; * } */ public static void StopListening() { FwBroker broker = FwBroker.GetBroker(); broker.Unregister(s_appName, s_port); }