// udp发送包 private void udpSendPack(byte[] send) { try { UnUdpState ao = new UnUdpState(); ao.send = send; ao.socket = this.udpSocket; ao.socket.BeginSendTo(ao.send, 0, ao.send.Length, SocketFlags.None, udpHost, new AsyncCallback(udpBeginSendBack), ao); } catch(Exception e) { UnFile.writeLog("udpSendPack",e.ToString()); } }
// 监听线程 private void udpListenHandle() { while (isTrue) { try { UnUdpState ao = new UnUdpState(); ao.socket = udpSocket; //uint IOC_IN = 0x80000000; //uint IOC_VENDOR = 0x18000000; //uint SIO_UDP_CONNRESET = IOC_IN | IOC_VENDOR | 12; //ao.socket.IOControl((int)SIO_UDP_CONNRESET, new byte[] { Convert.ToByte(false) }, null); // 接收首位的数据报 ao.socket.BeginReceiveFrom(ao.receive, 0, ao.receive.Length, SocketFlags.None, ref ao.remote, new AsyncCallback(udpBeginReceiveFromBack), ao); udpListenHandle_log = true; udpAllDone.WaitOne(); } catch { if (udpListenHandle_log) { udpListenHandle_log = false; //UnFile.writeLog("udpListenHandle", ex.ToString()); } } } }
// 监听线程 private void udpListenHandle() { bool isLog = false; while (!_isFinish) { if (!_isPause) { try { UnUdpState ao = new UnUdpState(); ao.socket = udpSocket; ao.socket.BeginReceiveFrom(ao.receive, 0, ao.receive.Length, SocketFlags.None, ref ao.remote, new AsyncCallback(udpBeginReceiveFromBack), ao); udpAllDone.WaitOne(); isLog = false; } catch (Exception ex) { udpAllDone.Set(); if (!isLog) { isLog = true; UnFile.writeLog("udpListenHandle", ex.ToString()); } } } else { Thread.Sleep(1000); } } }
// 处理数据包 private byte[] proPackage(UnUdpState ao) { if (intServer != null) { UnAttrRst rst = intServer.proPackage(ao.remote, null); if (rst != null && rst.code < 0) { return null; } } // 解析包数据 UnUdpEntity entity = UnUdpHelp.analyzePackage(ao.receive); if (entity == null || ((entity.HashCode + "").Length < 16 && (entity.PackMD5 + "").Length < 16)) { return null; } if (intServer != null) { UnAttrRst rst = intServer.proPackage(ao.remote, entity); if (rst != null && rst.code < 0) { return null; } } ss.addReceiveNum(1); byte[] data = null; // 处理包数据 switch (entity.Event.getUnUdpEveEnum()) { // 上传文件 case UnUdpEveEnum.upFilePackage: case UnUdpEveEnum.upFileQuery: data = this.proUpFilePackage(ao.remote, entity); break; case UnUdpEveEnum.msgPackage: data = this.proMsgPackage(ao.remote, entity); break; case UnUdpEveEnum.downFile: case UnUdpEveEnum.downFileQuery: data = this.proDownFilePackage(ao.remote, entity); break; } ss.addProLength(entity.PackSize); return data; }