public static void initNet(int bitLen = 128, string charset = "utf-8") { bool flag = false; int[] array = NetParams.bitList; for (int i = 0; i < array.Length; i++) { int num = array[i]; if (num == bitLen) { flag = true; break; } } if (!flag) { NetLog.info("NetParams-initNet()", "指定的位元:" + bitLen + "值不是32、64、128其中的一个!"); } else { NetParams.bitmapLen = (short)(bitLen / 8); NetLog.info("NetParams-initNet()", "bitmapLen:" + NetParams.bitmapLen); if (charset != null && charset.Trim().Length > 0) { NetParams.encode = charset; } } }
//关闭连接 public void close() { stopReceive(); if (tcpClient != null && tcpClient.Client != null) { tcpClient.Client.Close(); tcpClient.Close(); tcpClient = null; } NetLog.info(this, "-close() 已关闭与服务器连接!"); setNetStatus(NetParams.LINK_FAIL); bodyBuffer = null; msgLen = 0; }
private void ConnectCallBack(IAsyncResult asyncresult) { var net = asyncresult.AsyncState as NetSocket; try { if (net.tcpClient.Client != null && net.tcpClient.Client.Connected) { //连接成功 net.tcpClient.EndConnect(asyncresult); NetLog.info(net, "-ConnectCallBack()" + "第" + net.tryNum + "次连接服务器 [" + net.ip + "] [OK]"); net.startReceive(); net.tryNum = 0; net.msgLen = 0; //重连时,先将缓存的数据清空 lock (dataList) { dataList.Clear(); } net.setNetStatus(NetParams.LINK_OK); //首先发送 var netdata = new MemoryStream(); send(netdata, 1, 0); } else { NetLog.info(net, "-ConnectCallBack()" + "第" + net.tryNum + "次连接服务器 [" + net.ip + "] [Fail]"); net.retryConnect(); } } catch (Exception ex) { NetLog.error(net, "-ConnectCallBack() 连接服务器异常:" + ex); net.retryConnect(); } }
public byte[] encoder(IP8583Msg p8583Msg, bool isMac = false) { int macIndex = p8583Msg.getMacIndex(); p8583Msg.setBitmap(this.createBitmap(p8583Msg)); if (isMac) { BitUtils.generateBitmap(p8583Msg.getBitmap(), (short)macIndex); } byte[] array = new byte[0]; byte[] bitmap = p8583Msg.getBitmap(); if (this.isEncoderTrace) { BitUtils.println(bitmap); } for (short num = 0; num < NetParams.bitmapLen; num += 1) { for (short num2 = 0; num2 < 8; num2 += 1) { if ((bitmap[(int)num] >> (int)(7 - num2) & 1) == 1) { short num3 = (short)(num * 8 + num2 + 1); if ((int)num3 < macIndex) { P8583Tep p8583Tep = NetParams.tepMap[num3]; byte[] array3; if (p8583Tep.maxLen > 0) { byte[] array2 = MiscUtils.fill(p8583Msg.getField((int)num3), (int)p8583Tep.maxLen, ' '); array3 = new byte[(int)array.Length + (int)p8583Tep.maxLen + (int)((num3 == 1) ? (int)NetParams.bitmapLen : 0)]; Array.Copy(array, 0, array3, 0, array.Length); Array.Copy(array2, 0, array3, array.Length, (int)p8583Tep.maxLen); if (num3 == 1) { Array.Copy(bitmap, 0, array3, array.Length + (int)p8583Tep.maxLen, bitmap.Length); } if (this.isEncoderTrace) { NetLog.info(this, string.Concat(new object[] { "-encoder() no=", num3, ",maxLen=", p8583Tep.maxLen, ",msg=", MiscUtils.bytes2String(array2) })); } } else { int num4 = p8583Msg.getField((int)num3).Length; byte[] array2 = MiscUtils.number2Bytes(num4, (int)p8583Tep.lenFlag); array3 = new byte[array.Length + array2.Length + num4]; Array.Copy(array, 0, array3, 0, array.Length); Array.Copy(array2, 0, array3, array.Length, array2.Length); Array.Copy(p8583Msg.getField((int)num3), 0, array3, array.Length + array2.Length, num4); if (this.isEncoderTrace) { NetLog.info(this, string.Concat(new object[] { "-encoder() no=", num3, ",lenFlag=", p8583Tep.lenFlag, ",msg len=", MiscUtils.bytes2Int(array2), ",msg=", MiscUtils.bytes2String(p8583Msg.getField((int)num3)) })); } } array = array3; } } } } byte[] result; if (isMac) { byte[] array4 = MiscUtils.string2Bytes("CMAC_000"); byte[] array3 = new byte[array.Length + array4.Length]; Array.Copy(array, 0, array3, 0, array.Length); Array.Copy(array4, 0, array3, array.Length, array4.Length); result = array3; } else { result = array; } return(result); }
public IP8583Msg decoder(byte[] bytes) { int num = 0; byte[] array = new byte[(int)NetParams.bitmapLen]; P8583Msg p8583Msg = new P8583Msg(); Array.Copy(bytes, 6, array, 0, array.Length); p8583Msg.setBitmap(array); if (this.isDecoderTrace) { BitUtils.println(array); } for (short num2 = 0; num2 < NetParams.bitmapLen; num2 += 1) { for (short num3 = 0; num3 < 8; num3 += 1) { if ((array[(int)num2] >> (int)(7 - num3) & 1) == 1) { short num4 = (short)(num2 * 8 + num3 + 1); P8583Tep p8583Tep = NetParams.tepMap[num4]; byte[] array2; if (p8583Tep.maxLen > 0) { array2 = new byte[(int)p8583Tep.maxLen]; Array.Copy(bytes, num, array2, 0, (int)p8583Tep.maxLen); num += (int)p8583Tep.maxLen; if (num4 == 1) { num += (int)NetParams.bitmapLen; } if (this.isDecoderTrace) { NetLog.info(this, string.Concat(new object[] { "-decoder() no=", num4, ",maxLen=", p8583Tep.maxLen, ",msg=", MiscUtils.bytes2String(array2) })); } } else { byte[] array3 = new byte[(int)p8583Tep.lenFlag]; Array.Copy(bytes, num, array3, 0, (int)p8583Tep.lenFlag); num += (int)p8583Tep.lenFlag; array2 = new byte[MiscUtils.bytes2Int(array3)]; Array.Copy(bytes, num, array2, 0, array2.Length); num += array2.Length; if (this.isDecoderTrace) { NetLog.info(this, string.Concat(new object[] { "-decoder() no=", num4, ",lenFlag=", p8583Tep.lenFlag, ",msg=", MiscUtils.bytes2String(array2) })); } } p8583Msg.setField((int)num4, array2); } } } return(p8583Msg); }