private void SendAck(byte[] data) { //发送ACK报文 MyDataGram pro = MyDataGram.DecodeMessage(data); MyDataGram ack = new MyDataGram(); ack.DstID = pro.SrcID; ack.SrcID = pro.DstID; ack.Text = "__ack__" + pro.Text; ack.Type = MessageType.Text; string tempIP = ""; var resp = CSCore_instance.Query("q" + pro.SrcID); if (resp == "n") { MessageBox.Show("当前好友不在线!"); return; } else { tempIP = resp; } SendUDPData(MyDataGram.EncodeMessage(ack), tempIP, ttPort);//danger! }
private void ReceiveListener() { while (listening) { Thread.Sleep(500); byte[] msg = null; if (inter.messages.Count > 0) { //have new tcp message! msg = inter.messages.Dequeue(); } if (msg != null && msg.Length > 0) { MyDataGram pro = MyDataGram.DecodeMessage(msg); if (pro.Type != MessageType.Disable) { //DistributeMessage(pro); Dispatcher.BeginInvoke(new Dis(DistributeMessage), pro); } } } }
private void AccecptUdp() { bool OnGoing = true; while (OnGoing) { //todo:加功能,发送端的计时器 //收到udp Thread.Sleep(50); try { var buff = Udplistener.Receive(ref endpoint); int flag = 0; lock (inter) { MyDataGram pro = MyDataGram.DecodeMessage(buff); if (pro.Text.Length > 7) { if (pro.Text.Substring(0, 7) == "__ack__") { for (int i = 0; i < UdpMessageList.Count; i++) { var tempudp = MyDataGram.DecodeMessage(UdpMessageList[i].data); if (tempudp.SrcID == pro.DstID && tempudp.DstID == pro.SrcID) { if (tempudp.Text == pro.Text.Substring(7)) { //成功收到了对应的ack MessageBox.Show("收到了ACK报文."); UdpMessageList.RemoveAt(i); flag = 1; } } } } } if (pro.Type != MessageType.Disable) { if (pro.Text.Length < 7) { SendAck(buff); } else if (pro.Text.Substring(0, 7) != "__ack__") { SendAck(buff); } } if (flag == 0) { inter.messages.Enqueue(buff); } } } catch { //do nothing } lock (inter) { OnGoing = inter.listening; } } }