/// <summary> /// Saves the given Server information into a bookmark file in the /// currently configured bookmark directory. /// </summary> /// <param name="server">The Server object to save.</param> /// <returns>Status result code.</returns> public static Status AddBookmark(Server server) { if (server == null) return Status.GetFailure("Internal error (null server object provided)"); string fullPath = null; try { s_log.InfoFormat("Saving bookmark for server '{0}'", server.ServerName); string filename = server.ServerName + s_bookmarkExtension; fullPath = Path.Combine(GetBookmarkDirectory(), filename); if (File.Exists(fullPath)) { s_log.WarnFormat("Bookmark already exists: {0}", fullPath); return Status.GetFailure("A bookmark with that name already exists."); } CustomSerialize(server, fullPath); return Status.Success; } catch (Exception e) { s_log.ErrorFormat("Error saving bookmark: {0}", e.Message); try { File.Delete(fullPath); } catch { } return Status.GetFailure(String.Format("Error while saving bookmark: {0}", e.Message)); } }
public Status Connect(Server server) { if (server == null) return Status.Failure; // If connected to another server, disconnect first. if (m_server != null) m_server.Disconnect(); // If we're connecting to a new server, set up the delegates. // This is typically only the same server during the "auto-reconnect" cycle. if (m_server != server) { m_server = server; // Set up the event delegates for this Server connection. m_server.ProgressUpdated += HandleProgressUpdated; m_server.Connected += HandleConnected; m_server.Disconnected += HandleDisconnected; m_server.ChatReceived += HandleChatReceived; m_server.UserListUpdate += HandleUserListUpdate; m_server.PmReceived += HandlePmReceived; m_server.UserInfoReceived += HandleUserInfoReceived; } // Initiate server connection. if (m_server.Connect() == Status.Failure) { string message = String.Format("Connecting to {0} failed.", m_server.Address); s_log.ErrorFormat(message); LocalChatMessage(message); m_server = null; return Status.Failure; } // Print a message indicating successful connection. LocalChatMessage(String.Format("Connected to '{0}'", m_server.ServerName)); // Send initial transactions. Login login = new Login(m_server.LoginName, m_server.Password, m_server.Nick, m_server.Icon); if (m_server.SendTransaction(login) == Status.Failure) { string message = String.Format("Sending login to server failed."); s_log.ErrorFormat(message); LocalChatMessage(message); m_server = null; return Status.Failure; } // Store initial user settings. m_localUser = new User(); m_localUser.Username = m_server.Nick; m_localUser.IconId = m_server.Icon; return Status.Success; }
internal Status Connect(Server target) { if (target == null) return Status.Failure; m_server = target; try { // Parse address information from the server target. IPAddress ipAddress; AddressFamily targetFamily; int targetPort; if (ParseAddress(target.Address, out ipAddress, out targetFamily, out targetPort) == Status.Failure) return Status.Failure; // If the socket is already connected, disconnect it. if (m_socket != null && m_socket.Connected == true) m_socket.Disconnect(true); // If the socket has not been created or will be changing address families, create a new socket. if (m_socket == null || m_socket.AddressFamily != targetFamily) { // Release the system resources before we create a new socket. if (m_socket != null) m_socket.Close(); m_socket = new Socket(targetFamily, SocketType.Stream, ProtocolType.Tcp); } m_socket.Connect(ipAddress, targetPort); s_log.InfoFormat("Connected to '{0}' at '{1}' on port {2}", target.ServerName, target.Address, targetPort); return Status.Success; } catch (Exception e) { s_log.ErrorFormat("Exception opening socket for {0}: {1}", target.ServerName, e.Message); m_server = null; return Status.Failure; } }
private Status InitiateConnectDelegate(Server server) { if (server == null) return Status.Failure; // Update the Chat window state. m_chatWindow.SetConnecting(server.ServerName); // Connect to the server. Status result = m_controller.Connect(server); // Hide the progress bar when finished. // Reset the window title if connecting failed. if (result != Status.Success) { m_chatWindow.SetConnectFailed(); } return result; }
internal void InitiateConnect(Server server) { m_backgroundActions.Add(() => InitiateConnectDelegate(server)); }
private Server MakeServerFromControls() { try { Server server = new Server(); server.ServerName = m_serverName.Text; server.Address = m_addressText.Text; server.Nick = m_nickText.Text; server.Icon = 31337; server.LoginName = m_usernameText.Text; server.Password = m_passwordText.Password; return server; } catch (Exception e) { s_log.ErrorFormat("Exception setting values from window: {0}", e.Message); return null; } }
private void ConnectButton_Click(object sender, RoutedEventArgs e) { // Persist the settings and close the window. this.ConfiguredServer = MakeServerFromControls(); this.Close(); }
private void CancelButton_Click(object sender, RoutedEventArgs e) { this.ConfiguredServer = null; this.Close(); }
// Even though the FileUtils class can be accessed directly, the View // should generally use the Controller class for every operation. public Status AddBookmark(Server server) { return FileUtils.AddBookmark(server); }
private static Server CustomDeserialize(string fullPath) { Server server = new Server(); using (StreamReader sr = new StreamReader(fullPath)) { //TODO: allow the lines in any order server.ServerName = CopyAfterEquals(sr.ReadLine()); server.Address = CopyAfterEquals(sr.ReadLine()); server.LoginName = CopyAfterEquals(sr.ReadLine()); server.Password = Decode(CopyAfterEquals(sr.ReadLine())); server.Nick = CopyAfterEquals(sr.ReadLine()); server.Icon = int.Parse(CopyAfterEquals(sr.ReadLine())); } return server; }
/// <summary> /// Uses [Serializable] attribute to serialize via BinaryFormatter. /// </summary> //private static void BinarySerialize(Server server, string fullPath) //{ // using (FileStream fs = new FileStream(fullPath, FileMode.Create)) // { // BinaryFormatter serializer = new BinaryFormatter(); // serializer.Serialize(fs, server); // } //} /// <summary> /// Uses [Serializable] attribute to deserialize via BinaryFormatter. /// </summary> //private static Server BinaryDeserialize(string fullPath) //{ // using (FileStream fs = new FileStream(fullPath, FileMode.Open)) // { // BinaryFormatter serializer = new BinaryFormatter(); // return (Server)serializer.Deserialize(fs); // } //} //private static void XmlSerialize(Server server, string fullPath) //{ // using (FileStream fs = new FileStream(fullPath, FileMode.Create)) // { // SoapFormatter serializer = new SoapFormatter(); // serializer.Serialize(fs, server); // } //} //private static Server XmlDeserialize(string fullPath) //{ // using (FileStream fs = new FileStream(fullPath, FileMode.Open)) // { // SoapFormatter serializer = new SoapFormatter(); // return (Server)serializer.Deserialize(fs); // } //} private static void CustomSerialize(Server server, string fullPath) { using (StreamWriter sw = new StreamWriter(fullPath, false)) { //TODO: write generically via reflection sw.WriteLine(String.Format("ServerName={0}", server.ServerName)); sw.WriteLine(String.Format("Address={0}", server.Address)); sw.WriteLine(String.Format("LoginName={0}", server.LoginName)); sw.WriteLine(String.Format("Password={0}", Encode(server.Password))); sw.WriteLine(String.Format("Nick={0}", server.Nick)); sw.WriteLine(String.Format("Icon={0}", server.Icon)); } }
private Status ConnectToServer(Server server) { if (server == null) return Status.NoResult; m_screen.WriteLine("Connecting to: {0}", server.ToString()); Status result = m_controller.Connect(server); if (result == Status.Failure) m_screen.WriteLine("Failed to connect"); return result; }
private Server ParseServerArguments(string[] args) { Server server = new Server(); for (int i = 0; i < 5; i++) { if (args.Length < i + 1) break; switch (i) { case 0: server.Address = args[i]; break; case 1: server.Nick = args[i]; break; case 2: server.LoginName = args[i]; break; case 3: server.Password = args[i]; break; case 4: int.TryParse(args[i], out server.Icon); break; } } return server; }
private Status ParseBookmark(string text, out Server bookmark) { bookmark = null; string[] args = text.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); // There must be at least two parameters. if (args.Length < 2) return Status.GetFailure("A bookmark name and server address are required to create a bookmark."); // The first token is the new bookmark name (server name), then normal server parameters. string bookmarkName = args[0]; // Copy the server parameters to a new array so we can re-use the parsing method. string[] serverArgs = new string[args.Length - 1]; for (int i = 1; i < args.Length; i++) { serverArgs[i - 1] = args[i]; } // Parse the parameters, and set the bookmark name as the server name. bookmark = ParseServerArguments(serverArgs); bookmark.ServerName = bookmarkName; return Status.Success; }