/// <summary> /// Opens a connection to a server specified in the config file retrived from <paramref name="configURI"/>. /// Fragment part of the <paramref name="configURI"/> may be used to select the server by its index, e.g. /// <c>"http://www.example.org/config.json#3"</c>. If no fragment is provided, or index is invalid, first server /// with supported protocol is chosen. Upon connection <paramref name="onConnected"/> is called with the /// constructed Connection object. /// </summary> /// <param name="configURI"> /// URI where config is to be found. Data URIs starting with <c>"data:text/json;base64,"</c> are supported. /// </param> /// <param name="onConnected">Handler to be invoked when connection is established.</param> public void OpenConnection(string configURI, Action<Connection> onConnected) { string fragment = ""; Config ServerConfiguration = RetrieveConfig(configURI, out fragment); ServiceDescription server = SelectServer(fragment, ServerConfiguration); string protocolName = server.protocol.name; string transportName = server.transport.name; string transportUrl = server.transport.url; string host; int port; GetHostAndPortFromUrl(transportUrl, out host, out port); ITransportConnectionFactory transportConnectionFactory = TransportRegistry.Instance .GetTransport(transportName) .TransportConnectionFactory; IProtocol protocol = protocolRegistry.GetProtocol(protocolName); ITransportConnection transportConnection = transportConnectionFactory.OpenConnection(host, port, this, onConnected); transportConnection.Opened += (sender, e) => { Connection establishedConnection = new Connection(transportConnection, protocol); establishedConnection.LoadIDL(ServerConfiguration); onConnected(establishedConnection); }; }
/// <summary> /// Creates a server specified in the config file retrieved from <paramref name="configURI"/>. Fragment part of /// the <paramref name="configURI"/> may be used to select the server by its index, e.g. /// <c>"http://www.example.org/config.json#3"</c>. If no fragment is provided, or index is invalid, first server /// with supported protocol is chosen. For each connected client <paramref name="onNewClient"/> is called with /// constructed Connection object. /// </summary> /// <remarks> /// Note that <paramref name="onNewClient"/> may be executed on a different thread than the one you are calling /// from, depending on the implementation of the protocol specified in the config file. /// </remarks> public ServiceDescription StartServer(string uri, int port, string transportName, string protocolName, Config ServerConfig, Action<Connection> onNewClient) { IProtocol protocol = protocolRegistry.GetProtocol(protocolName); ITransportConnectionFactory transportConnectionFactory = TransportRegistry.Instance .GetTransport(transportName) .TransportConnectionFactory; ITransportListener transportListener = transportConnectionFactory.StartConnectionListener(uri, port); transportListener.NewClientConnected += (object sender, NewConnectionEventArgs e) => { Connection newConnection = new Connection(e.Connection, protocol); newConnection.LoadIDL(ServerConfig); onNewClient(newConnection); }; var server = new ServiceDescription(); server.protocol = new ProtocolConfig { name = protocolName }; server.transport = new TransportConfig { name = transportName, url = transportName + "://" + uri + ":" +port }; server.implementedServices = "*"; return server; }