private bool handleFileIO(byte[] buffer, int length, string filepath, int chunk, string ip, string fullname) { string fileData = StringCompressor.ToHexString(buffer, length); string m = "<OBJECT>" + "FILE*" + chunk.ToString() + "*" + Path.GetFileName(filepath).Replace("?", "_") + "*" + fileData; string chk = Sock.Checksum(Sock.Checksum(m)); // do it twice, trust me _e = null; AckDone.Reset(); Sock.SendToBuddy(userName, false, ip, fullname, m, null); // wait for confirmations AckDone.WaitOne(5000); if (!isExit && (_e == null || !_e.Valid || _e.Checksum != chk)) { // try to repeat once _e = null; AckDone.Reset(); Sock.SendToBuddy(userName, false, ip, fullname, m, null); // wait for confirmations AckDone.WaitOne(5000); if (_e == null || !_e.Valid || _e.Checksum != chk) { return(false); } } return(true); }
private void buttonConnect_Click(object sender, EventArgs e) { if (this.comboBoxUsers.SelectedIndex != 0) { Sock.SendToBuddy(userName, true, buddyList[this.comboBoxUsers.Text], this.comboBoxUsers.Text, "Reconnected!", null); } }
private void richTextBoxChatIn_TextChanged(object sender, EventArgs e) { Sock.MyTypingStatus(true); updateToolStrip(); _lastTyped.Stop(); // resets timer _lastTyped.Start(); GC.Collect(); // just helps ok }
private void buttonGoConnection_Click(object sender, EventArgs e) { CSavedIPs.ChangeSubList(this.textBoxSubs.Text); Sock.SetSock(this.userName, this.myIP, this.textBoxSubs.Text); Sock.StartSearching(this); appendText(richTextBoxChatOut, "Searching..." + Environment.NewLine, Color.LightGreen); this.buttonGoConnection.Enabled = false; }
private void richTextBoxChatIn_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode.Equals(Keys.Enter)) { if (Control.ModifierKeys != Keys.Shift) { e.Handled = true; Sock.MyTypingStatus(false); processTypedMessage(this.richTextBoxChatIn.Text); } } }
public MessageEventArgs(string data) { if (data != null && data.Contains("|")) { data = data.Trim(); // important trim int loc, loc1, loc2, loc3; if (find4backwards(data, out loc, out loc1, out loc2, out loc3)) { id = data.Substring(loc + 1, loc1 - loc - 1); friendName = data.Substring(loc1 + 1, loc2 - loc1 - 1); friendIP = data.Substring(loc2 + 1, loc3 - loc2 - 1); textFromFriend = data.Substring(loc3 + 1, data.Length - loc3 - 1); checksum = Sock.Checksum(textFromFriend.Replace("<EOF>", "")); // sanity checks if (friendIP.Split('.').Count() != 4 || id.Length > 4 || id.Length == 0) { valid = false; } else if (textFromFriend.EndsWith("<EOF>")) { if (textFromFriend.StartsWith("<OBJECT>FILE")) { isFile = true; try { string[] splitObj = textFromFriend.Split('*'); this.chunkId = Convert.ToInt32(splitObj[1]); this.fileName = splitObj[2]; this.fileData = (splitObj[3].Substring(0, splitObj[3].Length - 4 - 1)); valid = true; } catch { valid = false; } textFromFriend = textFromFriend.Replace("<EOF>", ""); return; } textFromFriend = textFromFriend.Replace("<EOF>", ""); valid = true; } else { textFromFriend = ""; } } } }
private void Chatter_FormClosing(object sender, FormClosingEventArgs e) { CSavedIPs.ChangeSubList(this.textBoxSubs.Text); this.isExit = true; // temp file cleanup try { foreach (string key in linkList.Keys) { if (File.Exists(linkList[key])) { File.Delete(linkList[key]); } } } catch { } Sock.KillTasks(); }
public Chatter() { InitializeComponent(); CSavedIPs.ReadFile(); this.comboBoxUsers.SelectedIndex = 0; userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name; this.groupBoxTop.Text = "Name: " + userName; this.textBoxSubs.Text = Sock.StartServer(); string[] split = this.textBoxSubs.Text.Split(','); string ipFirst = (split != null && split.Count() > 0) ? split[0] : "10.0.0"; Sock.NewData += Sock_NewDataReceivedEventHandler; Sock.Ack += Sock_Ack; myIP = Sock.GetIPAddress(ipFirst); this.groupBoxBottom.Text = "My IP: " + myIP; }
public MsgState ProcessStates(out MessageEventArgs mea) { mea = processMea(); switch (this._state) { case MsgState.Idle: if (this._msgs2Send.Count > 0) { lock (this._msgs2Send) { this._msg2Go = this._msgs2Send.Dequeue(); } if (!String.IsNullOrWhiteSpace(this._msg2Go)) { this._msgProcessed = false; this._attempt = 0; this._imSendingAck = false; this._state = MsgState.ReadyForRemote; } } break; case MsgState.ReadyForRemote: this._startTime = DateTime.Now; if (this._attempt >= 3) { this._state = MsgState.Idle; } else if (this._msgProcessed) { this._state = MsgState.WaitingResponse; } break; case MsgState.WaitingResponse: // message was already sent out, check timeout DateTime endTime = DateTime.Now; if (endTime.Subtract(this._startTime).Milliseconds >= 799) { Sock.debug(_name + ": timeout, repeating"); this._msgProcessed = false; this._attempt++; this._state = MsgState.ReadyForRemote; } break; case MsgState.ReadyForAck: this._msgProcessed = false; this._msg2Go = this._lastAckMsg; this._state = MsgState.SendAck; break; case MsgState.SendAck: if (this._msgProcessed) { this._msgProcessed = false; this._imSendingAck = false; this._state = MsgState.Done; } break; case MsgState.Done: this._state = MsgState.Idle; break; default: break; } return(this._state); }
private MessageEventArgs processMea() { string localMsg; int myIndex; lock (_buildMsg) { localMsg = _buildMsg.ToString(); myIndex = localMsg.IndexOf("<EOF>"); if (myIndex >= 0) { localMsg = localMsg.Substring(0, myIndex + 1 + 4).Trim(); _buildMsg.Remove(0, myIndex + 1 + 4); } } if (myIndex >= 0) { MessageEventArgs mea = new MessageEventArgs(localMsg); if (mea.Valid) { // for safety _buddyIp = mea.FriendIP; if (this._state == MsgState.WaitingResponse || this._state == MsgState.SendAck) { Sock.debug(_name + ":got ACK:" + this._state.ToString() + ":Id=" + mea.Id); mea.IsAck = true; if (this._state == MsgState.WaitingResponse) { this._state = MsgState.Done; } return(mea); } if (this._imSendingAck || mea.FriendIP == myip) { if (this._state == MsgState.WaitingResponse) { this._state = MsgState.Done; } return(null); } else if (this._state == MsgState.Idle || this._state == MsgState.ReadyForRemote) { this._imSendingAck = true; _lastAckMsg = generate(mea.Id, mea.FriendName, mea.FriendIP, Sock.Checksum(mea.TextFromFriend)); // no need to send entire msg payload _lastAckIP = mea.FriendIP; Sock.debug(_name + ":Display(sendAck)" + this._state.ToString() + ":Id=" + mea.Id); this._state = MsgState.ReadyForAck; } else { return(mea); } } else { Sock.debug(_name + ": oops, invalid message received"); } return(mea); } else { return(null); } }
private void _lastTyped_Tick(object sender, EventArgs e) { Sock.MyTypingStatus(false); updateToolStrip(); }
private void processTypedMessage(string message) { if (String.IsNullOrWhiteSpace(message)) { richTextBoxChatIn.Clear(); return; } string m = message.TrimEnd(); // cmd's if (m.StartsWith("<CMD>")) { m = m.Replace("<CMD>", "").ToLower(); if (m == "clear") { this.richTextBoxChatOut.Clear(); } else if (m == "max") { this.WindowState = FormWindowState.Maximized; } else if (m == "hide") { this.WindowState = FormWindowState.Minimized; } else if (m == "pop") { ActivateMe(); } else if (m == "debug") { Sock.InDebug = true; } else if (m == "normal") { Sock.InDebug = false; } else if (m == "exit") { this.Close(); } else if (m == "look") { this.buttonGoConnection_Click(null, null); } else if (m == "all") { this.comboBoxUsers.SelectedIndex = 0; } richTextBoxChatIn.Clear(); return; } if (this.comboBoxUsers.Items.Count == 1) { appendText(richTextBoxChatOut, "No friends can be found." + Environment.NewLine, Color.LightGreen); // scroll it automatically richTextBoxChatIn.Text = m; richTextBoxChatIn.SelectionStart = richTextBoxChatIn.Text.Length; richTextBoxChatIn.ScrollToCaret(); return; } // handle multi lines nicely if (m.Contains('\n')) { string[] splits = m.Split(new char[] { '\n' }, StringSplitOptions.None); m = ""; foreach (string s in splits) { m += s + "\n\t\t"; } m = m.TrimEnd(); } string buddyName = this.comboBoxUsers.Text; // ready to send message to a buddy if (this.comboBoxUsers.SelectedIndex != 0 && !Sock.SendToBuddy(userName, false, buddyList[buddyName], buddyName, m, null)) { appendText(richTextBoxChatOut, "Me:\t\t", Color.LightGreen); appendText(richTextBoxChatOut, m + " <remote ignored>" + Environment.NewLine, Color.LightSalmon); // scroll it automatically richTextBoxChatIn.Text = m; richTextBoxChatIn.SelectionStart = richTextBoxChatIn.Text.Length; richTextBoxChatIn.ScrollToCaret(); return; } else { appendText(richTextBoxChatOut, "Me:\t\t", Color.LightGreen); appendText(richTextBoxChatOut, m + Environment.NewLine, Color.LightSalmon); //richTextBoxChatOut.SelectionStart = richTextBoxChatOut.Text.Length; // scroll it automatically richTextBoxChatOut.ScrollToCaret(); } this.richTextBoxChatIn.Text = ""; // send message to each buddy if (this.comboBoxUsers.SelectedIndex == 0) { foreach (string key in this.buddyList.Keys) { Sock.SendToBuddy(userName, false, buddyList[key], key, m, null); } } }