int messageType = 1; //默认会显示按钮 public MessageShow(MessageDone m) { InitializeComponent(); mm = m; if (mm.Status == 1 || mm.Status == -1) { button_ok.Visibility = Visibility.Hidden; button_cancel.Visibility = Visibility.Hidden; showlabel.Visibility = Visibility.Visible; this.button_ok.Visibility = Visibility.Collapsed; if (mm.Status == -1) { showlabel.Content = "已拒绝"; } if (mm.Status == 1) { showlabel.Content = "已同意"; } } else if (mm.Status == 2) { button_ok.Visibility = Visibility.Hidden; button_cancel.Visibility = Visibility.Hidden; button_check.Visibility = Visibility.Collapsed; showlabel.Visibility = Visibility.Visible; showlabel.Content = "已阅"; } }
private async Task <MessageDone> DelayedAnswer(MessageDone done, int afterMs) { await Task.Delay(afterMs); return(done); }
/// <summary> /// 核心: 解析报文 /// </summary> /// <param name="rawPacket"></param> private void handlePacket(RawCapture rawPacket) { try { // 构建通用数据包 Packet generalPacket = Packet.ParsePacket(rawPacket.LinkLayerType, rawPacket.Data); // 只要以太网包 if (rawPacket.LinkLayerType == LinkLayers.Ethernet) { // 封装以太网包 EthernetPacket e = EthernetPacket.GetEncapsulated(generalPacket); // 只要IPV4 if (e.Type == EthernetPacketType.IpV4) { IPv4Packet ipv4 = (IPv4Packet)IpPacket.GetEncapsulated(generalPacket); // 只要TCP包 if (ipv4.NextHeader == IPProtocolType.TCP) { // 将通用数据包封装为TCP包 TcpPacket tcp = TcpPacket.GetEncapsulated(generalPacket); byte[] tmp = tcp.PayloadData;// 数据 if (tmp != null && tmp.Length > 4) { // 处理粘包 int i = 0; do { int len = -1; if (i + 4 > tmp.Length) { //len = 1; break; } else if (tmp[i] == 0xC1) { len = tmp[i + 1]; // C1 0D包长度必须大于13 if (tmp[i + 2] == 0x0D && len >= 13) { // 0D包第3字节后有10字节0x00 直接跳过 // 这10字节应该是发送者名字, 服务器消息则没有 byte[] msg = new byte[len - 13]; Buffer.BlockCopy(tmp, i + 13, msg, 0, msg.Length); string text = Encoding.Default.GetString(msg).Split('\0')[0]; // 处理获取到的服务器消息 MessageDone?.Invoke(this, text); } } else if (tmp[i] == 0xC2 || tmp[i] == 0xC4) { // TODO: 这里有个半包异常 懒得处理了 // C2和C4 第2-3字节规定了长度 len = tmp[i + 1] << 8 | tmp[i + 2]; } else if (tmp[i] == 0xC3) { // C1和C3 第2字节规定了长度 len = tmp[i + 1]; } else { len = 1; } // 防止半包或网络不稳收到C100 造成死循环 if (len == 0) { len = 1; } i += len;// 跳过len字节 } while (i < tmp.Length); } } } } } catch (Exception e) { } }