/// <summary> /// Initialize InSim asynchronously. /// </summary> /// <param name="settings">The InSim settings.</param> /// <returns>A task object.</returns> public async Task InitializeAsync(InSimSettings settings) { PreInitialize(settings); try { if (Settings.IsRelayHost) { await TcpSocket.ConnectAsync(RelayHost, RelayPort); } else { await TcpSocket.ConnectAsync(Settings.Host, Settings.Port); // Initialize InSim. await SendAsync(GetInSimInitPacket()); PostInitialize(); } OnInitialized(new InitializeEventArgs(Settings)); } catch (SocketException ex) { if (ex.SocketErrorCode == SocketError.ConnectionRefused) { throw new InSimException(StringResources.InSimCouldNotConnectMessage, ex); } throw; } }
private void PreInitialize(InSimSettings settings) { if (settings == null) { throw new ArgumentNullException("settings"); } ThrowIfDisposed(); ThrowIfConnected(); InitializeSockets(); Settings = new ReadOnlyInSimSettings(settings); }
/// <summary> /// Initializes the connection with LFS. /// </summary> /// <param name="settings">A <see cref="InSimSettings"/> object containing information to initialize the connection with.</param> public void Initialize(InSimSettings settings) { if (settings == null) { throw new ArgumentNullException("settings"); } ThrowIfDisposed(); ThrowIfConnected(); Settings = new ReadOnlyInSimSettings(settings); try { if (Settings.IsRelayHost) { TcpSocket.Connect(RelayHost, RelayPort); } else { TcpSocket.Connect(Settings.Host, Settings.Port); Send(new IS_ISI { Admin = Settings.Admin, Flags = Settings.Flags, IName = Settings.IName, Interval = Settings.Interval, Prefix = Settings.Prefix, ReqI = 1, // Request version. UDPPort = Settings.UdpPort, }); if (Settings.UdpPort > 0) { UdpSocket.Bind(Settings.Host, Settings.UdpPort); } } OnInitialized(new InitializeEventArgs(Settings)); } catch (SocketException ex) { if (ex.SocketErrorCode == SocketError.ConnectionRefused) { throw new InSimException(StringResources.InSimCouldNotConnectMessage, ex); } throw; } }
/// <summary> /// Saves an instance of <see cref="InSimSettings"/> to an XML file. /// </summary> /// <param name="settings">The instance of <see cref="InSimSettings"/> to save.</param> /// <param name="path">The full path to where the XML file will be written.</param> public static void SaveToXml(InSimSettings settings, string path) { if (settings == null) { throw new ArgumentNullException("settings"); } XElement xSettings = new XElement("Settings"); xSettings.Add(new XElement("Host", settings.Host)); xSettings.Add(new XElement("Port", settings.Port)); xSettings.Add(new XElement("Admin", settings.Admin)); xSettings.Add(new XElement("UdpPort", settings.UdpPort)); xSettings.Add(new XElement("Flags", settings.Flags)); // There are a bunch of chars XML cannot encode, so we use this little hack to make // sure none of them get saved. Maybe should just throw an error? xSettings.Add(new XElement("Prefix", IsLegalXmlChar(settings.Prefix) ? settings.Prefix.ToString() : null)); xSettings.Add(new XElement("Interval", settings.Interval)); xSettings.Add(new XElement("IName", settings.IName)); xSettings.Add(new XElement("IsRelayHost", settings.IsRelayHost)); xSettings.Save(path); }
/// <summary> /// Creates a new instance of the <see cref="ReadOnlyInSimSettings"/> class. /// </summary> /// <param name="settings">The InSimSettings to make readonly.</param> public ReadOnlyInSimSettings(InSimSettings settings) { this.settings = settings; }