private void m_reconnectTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { try { m_parent.OnStatusMessage("Attempting to restart data channel..."); this.DataChannel = null; UdpServer dataChannel = new UdpServer(m_configurationString); dataChannel.Start(); this.DataChannel = dataChannel; m_parent.OnStatusMessage("Data channel successfully restarted."); } catch (Exception ex) { m_parent.OnStatusMessage("Failed to restart data channel due to exception: {0}", ex.Message); m_reconnectTimer.Start(); } }
/// <summary> /// Create a communications server /// </summary> /// <remarks> /// Note that typical configuration string should be prefixed with a "protocol=tcp" or a "protocol=udp" /// </remarks> public static IServer CreateCommunicationServer(string configurationString) { Dictionary<string, string> configurationData = configurationString.ParseKeyValuePairs(); IServer server = null; string protocol; if (configurationData.TryGetValue("protocol", out protocol)) { configurationData.Remove("protocol"); StringBuilder settings = new StringBuilder(); foreach (string key in configurationData.Keys) { settings.Append(key); settings.Append("="); settings.Append(configurationData[key]); settings.Append(";"); } switch (protocol.ToLower()) { case "tcp": server = new TcpServer(settings.ToString()); break; case "udp": server = new UdpServer(settings.ToString()); break; default: throw new ArgumentException("Transport protocol \'" + protocol + "\' is not valid."); } } else { throw new ArgumentException("Transport protocol must be specified."); } return server; }
/// <summary> /// Create a communications server /// </summary> /// <remarks> /// Note that typical configuration string should be prefixed with a "protocol=tcp" or a "protocol=udp" /// </remarks> /// <param name="configurationString">The configuration string for the server.</param> /// <returns>A communications server.</returns> public static IServer Create(string configurationString) { Dictionary<string, string> configurationSettings = configurationString.ParseKeyValuePairs(); IServer server = null; string protocol; if (configurationSettings.TryGetValue("protocol", out protocol)) { configurationSettings.Remove("protocol"); StringBuilder settings = new StringBuilder(); foreach (string key in configurationSettings.Keys) { settings.Append(key); settings.Append("="); settings.Append(configurationSettings[key]); settings.Append(";"); } // Create a server instance for the specified protocol. switch (protocol.Trim().ToLower()) { case "tcp": server = new TcpServer(settings.ToString()); break; case "udp": server = new UdpServer(settings.ToString()); break; default: throw new ArgumentException("Transport protocol \'" + protocol + "\' is not valid"); } // Apply server settings from the connection string to the client. foreach (KeyValuePair<string, string> setting in configurationSettings) { PropertyInfo property = server.GetType().GetProperty(setting.Key); if (property != null) property.SetValue(server, Convert.ChangeType(setting.Value, property.PropertyType), null); } } else { throw new ArgumentException("Transport protocol must be specified"); } return server; }