/** 解析单次消息内容 */ 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); } }
public void parseMessage(ByteBuffer socketbuffer) { int num = socketbuffer.readByte (); bool flag = (num & 8) != 0; bool flag2 = (num & 4) != 0; bool flag3 = (num & 2) != 0; ByteBuffer data = new ByteBuffer (this.length - 1); data.write (socketbuffer.toArray (), 0, this.length - 1); if (base.socket.Available >= 2) { byte[] buffer = new byte[2]; base.socket.Receive (buffer, SocketFlags.None); this.length = ByteKit.readUnsignedShort (buffer, 0); } else { this.length = 0; } if (flag) { data = this.encryptionCode (data, this._receiveChallengeCode); } if (flag3) { data = new ByteBuffer (ZIPUtil.Decompress (data.toArray ())); } if (flag2) { int num2 = data.readInt (); ByteBuffer buffer4 = new ByteBuffer (); buffer4.writeBytes (data.toArray (), 0, data.top - data.position); int num3 = (int)ChecksumUtil.Adler32 (buffer4); if (num2 != num3) { //MonoBehaviour.print(string.Concat(new object[] { "crc is err,crcValue", num2, ",nowCrc=", num3 })); // Log.info return; } } ErlKVMessage message = new ErlKVMessage (null); message.bytesRead (data); if (base._portHandler != null) { base._portHandler.erlReceive (this, message); } }
public void TransParseMessage(ByteBuffer socketbuffer , bool isServer , ByteBuffer src) { try{ int num = socketbuffer.readByte (); bool flag = (num & 8) != 0; bool flag2 = (num & 4) != 0; bool flag3 = (num & 2) != 0; ByteBuffer data = new ByteBuffer (this.length - 1); data.write (socketbuffer.toArray (), 0, this.length - 1); if ((this.dataBuffer.bytesAvailable) >= 2) { byte[] buffer = new byte[2]; //base.socket.Receive (buffer, SocketFlags.None); this.dataBuffer.readBytes(buffer,0,2); this.length = ByteKit.readUnsignedShort (buffer, 0); } else { this.length = 0; } if (flag) { if (this.isServer) { data = this.encryptionCode (data, this._sendChallengeCode); } else { data = this.encryptionCode (data, this._receiveChallengeCode); } } if (flag3) { data = new ByteBuffer (ZIPUtil.Decompress (data.toArray ())); } if (flag2) { int num2 = data.readInt (); ByteBuffer buffer4 = new ByteBuffer (); buffer4.writeBytes (data.toArray (), 0, (int)data.bytesAvailable); int num3 = (int)ChecksumUtil.Adler32 (buffer4); if (num2 != num3) { Log.Error(string.Concat(new object[] { "crc is err,crcValue", num2, ",nowCrc=", num3 })); // Log.info //if (this.transCallBack != null) { // this.transCallBack.Invoke (); // } int len = (int)this.dataBuffer.bytesAvailable; int pos = this.dataBuffer.position; byte[] tmp = new byte[len]; byte[] bak = new byte[pos]; this.dataBuffer.position = 0; this.dataBuffer.readBytes (bak, 0, pos); this.dataBuffer.readBytes (tmp, 0, len); this.dataBuffer.clear (); this.dataBuffer = new ByteBuffer (tmp); if(!this.isServer){ base.socket.Send (bak);} if (this.dataBuffer.bytesAvailable > 0) { this.TransReceive (null, this.isServer); } return; } } ErlKVMessage message = new ErlKVMessage (null); this.tmpBuffer = data; message.bytesRead (data); Log.Debug(string.Concat( data.getArray())+"++++++"); this.myLen = this.dataBuffer.top; if (base._portHandler != null) { base._portHandler.erlReceive (this, message); } } catch(Exception e) { Log.Error (e.Message); if (this.dataBuffer.bytesAvailable > 0) { this.TransReceive (null, this.isServer); } else { int len = (int)this.dataBuffer.bytesAvailable; int pos = this.dataBuffer.position; byte[] tmp = new byte[len]; byte[] bak = new byte[pos]; this.dataBuffer.position = 0; this.dataBuffer.readBytes (bak, 0, pos); this.dataBuffer.readBytes (tmp, 0, len); this.dataBuffer.clear (); this.dataBuffer = new ByteBuffer (tmp); if (!this.isServer) { base.socket.Send (bak); } if (this.dataBuffer.bytesAvailable > 0) { this.TransReceive (null, this.isServer); } } } }