/** 解析单次消息内容 */ public void parseMessage(ByteBuffer socketbuffer) { int versionInfo = socketbuffer.readByte(); bool encryption = ((versionInfo & 8) != 0); bool crc = ((versionInfo & 4) != 0); bool compress = ((versionInfo & 2) != 0); //if(!MiniConnectManager.IsRobot) //MonoBehaviour.print ("length=>" + length + " versionInfo=>" + versionInfo + " encryption=>" + encryption + " crc=>" + crc + " compress=>" + compress); ByteBuffer data = new ByteBuffer(length - 1); data.write(socketbuffer.toArray(), 0, length - 1); //为下次数据处理做判断 if (socket.Available >= 2) { byte[] b = new byte[2]; socket.Receive(b, SocketFlags.None); length = ByteKit.readUnsignedShort(b, 0); } else { length = 0; } if (encryption) { data = encryptionCode(data, _receiveChallengeCode); } if (compress) { byte[] bb = ZIPUtil.Decompress(data.toArray()); data = new ByteBuffer(bb); } if (crc) { int crcValue = data.readInt(); ByteBuffer data1 = new ByteBuffer(); data1.writeBytes(data.toArray(), 0, (data.top - data.position)); int nowCrc = (int)ChecksumUtil.Adler32(data1); if (crcValue != nowCrc) { MonoBehaviour.print("crc is err,crcValue" + crcValue + ",nowCrc=" + nowCrc); return; } } ErlKVMessage message = new ErlKVMessage(null); message.bytesRead(data); if (_portHandler != null) // _portHandler可以是DataAccess或者ErlTransmitPort,如果要保存funcUid就要设置为DataAccess { _portHandler.erlReceive(this, message); } }
/** 发送方法 * @param data 数据 * @param isEncryption 是否加密 * @param isCrc 是否crc * @param isCompress 是否压缩 * @param kv kv类型 为0表示消息为二进制数据,为1表示消息为KeyValue类型,key为字符串,Value为标准格式的数据 * */ public void sendErl(ByteBuffer data, int encryption, int crc, int compress, int kv) { //没有得到pk码,一般出现在连接有,但是接不到后台消息 if (_sendChallengeCode == null || _sendChallengeCode.Length < 0) { return; } _encryption = encryption; _crc = crc; _compress = compress; _kv = kv; int crcValue = 0; ByteBuffer data1 = new ByteBuffer(); if (_compress == COMPRESS && data.length() >= 64) // 根据参数和数据长度判断是否执行压缩 { byte[] bb = ZIPUtil.Compress(data.toArray()); data = new ByteBuffer(bb); } else { _compress = 0; } if (_crc == 1 && _compress == 0) { crcValue = (int)ChecksumUtil.Adler32(data); data1.writeInt(crcValue); } else { _crc = 0; } data1.writeBytes(data.toArray()); if (_encryption == 1) { data1 = encryptionCode(data1, _sendChallengeCode); // 执行加密 } send(data1); _encryption = ENCRYPTION; _crc = CRC; _compress = COMPRESS; _kv = KV; }