public static DataHandle getInstance() { if (instance == null) { instance = new DataHandle(); } return(instance); }
public static void SSendNote(Socket other, Notes nt) { string msg = "NOTE_" + nt.id.ToString() + "_" + nt.Tittle + "_" + nt.user_id.ToString() + "_" + nt.Content + "_" + DataHandle.getInstance().getRecentAccount().username; other.Send(SerializeMsg(msg)); }
public static void SSendReminder(Socket other, Reminders r) { string msg = "REMINDER_" + r.ID.ToString() + "_" + r.Tittle + "_" + r.Content + "_" + r.Time + "_" + r.Check.ToString() + "_" + r.User_id.ToString() + "_" + DataHandle.getInstance().getRecentAccount().username; other.Send(SerializeMsg(msg)); }
private void Close() { if (others != null && others.Count != 0) { foreach (Socket other in others) { SendStatus("DISCARD CONNECTION_" + DataHandle.getInstance().getRecentAccount().username); } others.Clear(); p.Clear(); } else { SendStatus("DISCARD CONNECTION_" + DataHandle.getInstance().getRecentAccount().username); } clientStatus = -1; peer.Close(); }
public int startClient(IPAddress svIP) { endpoint = new IPEndPoint(svIP, port); peer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP); try { peer.Connect(endpoint); SendStatus("CREATE CONNECTION_" + DataHandle.getInstance().getRecentAccount().username); } catch { MessageBox.Show("Cannot connect to server", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return(0); } Thread listen = new Thread(CReceive); listen.IsBackground = true; listen.Start(); clientStatus = 1; return(1); }
public void SReceive(Object dest) { Socket other = dest as Socket; try { while (true) { byte[] data = new byte[1024 * 5000]; other.Receive(data); string msg = (string)DeserializeMsg(data); string[] tokens = msg.Split('_'); if (tokens[0] == "STATUS") { if (tokens[1] == "DISCARD CONNECTION") { p.Remove(tokens[2].ToString().Trim()); Form1.getInstance().networkSubWindow1.UpdateClientList(p, others); Form1.getInstance().UpdateConnectionStatus("Connected to " + tokens[2].ToString().Trim()); } else { p.Add(tokens[2]); Form1.getInstance().networkSubWindow1.UpdateClientList(p, others); Form1.getInstance().UpdateConnectionStatus("Connected to " + tokens[2].ToString().Trim()); } } else if (tokens[0] == "ACCOUNT") { Account a = new Account() { username = tokens[3].ToString().Trim(), id = Convert.ToInt32(tokens[1]), password = tokens[2].ToString().Trim(), created = Convert.ToDateTime(tokens[4].ToString().Trim()), }; Form1.getInstance().UpdateConnectionStatus("Received information from account " + tokens[3].ToString().Trim()); if (DataHandle.getInstance().doesAccountExit(a)) { Form1.getInstance().UpdateConnectionStatus("Account " + tokens[3].ToString().Trim() + " has already existed"); } else { DataHandle.getInstance().saveAccount(a); } } else if (tokens[0] == "DETAILS") { PersonalDetail d = new PersonalDetail() { id = Convert.ToInt32(tokens[1]), account = Convert.ToInt32(tokens[5]), category = tokens[4].ToString().Trim(), subcategory = tokens[2].ToString().Trim(), content = tokens[3].ToString().Trim(), }; DataHandle.getInstance().createDetail(d); } else if (tokens[0] == "REMINDER") { Reminders r = new Reminders() { ID = Convert.ToInt32(tokens[1]), Tittle = tokens[2].ToString().Trim(), Content = tokens[3].ToString().Trim(), Time = tokens[4].ToString().Trim(), Check = Convert.ToInt32(tokens[5]), User_id = Convert.ToInt32(tokens[6]), }; Form1.getInstance().UpdateConnectionStatus("Received reminder from account " + tokens[tokens.Length - 1]); DataHandle.getInstance().CreateNewReminder(r); } else if (tokens[0] == "NOTE") { Notes n = new Notes() { id = Convert.ToInt32(tokens[1]), Tittle = tokens[2].ToString().Trim(), Content = tokens[4].ToString().Trim(), user_id = Convert.ToInt32(tokens[3]), }; Form1.getInstance().UpdateConnectionStatus("Received reminder from account " + tokens[tokens.Length - 1]); DataHandle.getInstance().CreateNewNote(n); } } } catch { others.Remove(other); other.Close(); } }
public void CReceive() { try { while (true) { byte[] data = new byte[1024 * 5000]; peer.Receive(data); string msg = (string)DeserializeMsg(data); string[] tokens = msg.Split('_'); if (tokens[0] == "STATUS") { if (tokens[1] == "DISCARD CONNECTION") { Form1.getInstance().UpdateConnectionStatus("Disconnected to account " + tokens[tokens.Length - 1]); Close(); } else { Form1.getInstance().UpdateConnectionStatus("Connected to account " + tokens[tokens.Length - 1]); } /*p.Add(tokens[2]);*/ } else if (tokens[0] == "ACCOUNT") { Account a = new Account() { username = tokens[3].ToString().Trim(), id = Convert.ToInt32(tokens[1]), password = tokens[2].ToString().Trim(), created = Convert.ToDateTime(tokens[4].ToString().Trim()), }; DataHandle.getInstance().saveAccount(a); /* Form1.getInstance().networkSubWindow1.UpdateClientList(p, others); */ } else if (tokens[0] == "DETAILS") { PersonalDetail d = new PersonalDetail() { id = Convert.ToInt32(tokens[1]), account = Convert.ToInt32(tokens[5]), category = tokens[4].ToString().Trim(), subcategory = tokens[2].ToString().Trim(), content = tokens[3].ToString().Trim(), }; DataHandle.getInstance().createDetail(d); } else if (tokens[0] == "REMINDER") { Reminders r = new Reminders() { ID = Convert.ToInt32(tokens[1]), Tittle = tokens[2].ToString().Trim(), Content = tokens[3].ToString().Trim(), Time = tokens[4].ToString().Trim(), Check = Convert.ToInt32(tokens[5]), User_id = Convert.ToInt32(tokens[6]), }; DataHandle.getInstance().CreateNewReminder(r); } else if (tokens[0] == "NOTE") { Notes n = new Notes() { id = Convert.ToInt32(tokens[1]), Tittle = tokens[2].ToString().Trim(), Content = tokens[4].ToString().Trim(), user_id = Convert.ToInt32(tokens[3]), }; DataHandle.getInstance().CreateNewNote(n); } } } catch { Close(); } }