public void DataReceived(Packet p) { if (p.SourceIP == Listener.Instance.LocalIP) return; // ignore my self if (StructuralComparisons.StructuralEqualityComparer.Equals(p.Header ,Headers.Init.WHO_IS_THERE)) { //var pp = new Packet(Headers.Init.I_AM_HERE, txtMyUser.Text, Listener.Instance.LocalIP, new byte[] { 0 }); //var s = new MySender(ViewModel.UserName); //s.BroadcastRaw(pp.ToRaw()); } else if (StructuralComparisons.StructuralEqualityComparer.Equals(p.Header, Headers.Init.I_AM_HERE)) { ViewModel.CheckNewContact(p.UserIdText, p.SourceIP); } else if (StructuralComparisons.StructuralEqualityComparer.Equals(p.Header, Headers.Message.MESSAGE)) { var guidBytes = p.Data.Take(16).ToArray(); var messageBytes = p.Data.Skip(16).ToArray(); ViewModel.GotMessage(p.UserIdText, p.SourceIP, Encoding.Unicode.GetString(messageBytes), new Guid(guidBytes)); } else if (StructuralComparisons.StructuralEqualityComparer.Equals(p.Header, Headers.Message.MESSAGE_ACCEPTED)) { var guidBytes = p.Data.Take(16).ToArray(); ViewModel.GotMessageConfirm(new Guid(guidBytes)); } }
void ListenerAction() { var l = Listener.Instance; while (System.Threading.Thread.CurrentThread.IsAlive) { var b = l.Receive(); if (b.Length > 0) { var p = new Packet(b); Dispatcher.Invoke(() => { DataReceived(p); }); } System.Threading.Thread.Sleep(50); } }
protected void SendMessage(string targetAddress, byte[] message, string myUser) { var pp = new Packet(Headers.Message.MESSAGE, myUser, Listener.Instance.LocalIP, message); SendRaw(pp.ToRaw(), targetAddress); }
private void btnSendWhoIsThere_Click(object sender, EventArgs e) { var s = GetSender(); var pp = new Packet(Headers.Init.WHO_IS_THERE, s.UserName, Listener.Instance.LocalIP, new byte[] { 0 }); s.BroadcastRaw(pp.ToRaw()); }