public static ResourceRecord Parse(byte [] buffer, ref int offset) { string name; if ((buffer[offset] & 0xc0) == 0xc0) { // Detected compression. Name is at offset in // lower 14 bits of next 2 bytes. int start = NetworkBitConverter.ToInt16(buffer, offset); if (LabelEncoding.GetString(buffer, start & 0x3fff, out name) == 0) { throw new InvalidDnsFormatException("name"); } offset += 2; } else { int used = LabelEncoding.GetString(buffer, offset, out name); if (used == 0) { throw new InvalidDnsFormatException("name"); } offset += used; } ushort rType = NetworkBitConverter.ToUInt16(buffer, offset); offset += 2; ushort rClass = NetworkBitConverter.ToUInt16(buffer, offset); offset += 2; uint ttlSeconds = NetworkBitConverter.ToUInt32(buffer, offset); offset += 4; ushort rdataLength = NetworkBitConverter.ToUInt16(buffer, offset); offset += 2; byte [] rdata = new byte[rdataLength]; Array.Copy(buffer, offset, rdata, 0, rdataLength); offset += rdataLength; return(new ResourceRecord(name, (Type)rType, (Class)rClass, ttlSeconds, rdata)); }
/// <summary> /// 将客户端转来的byte[]转换成指定对象(自定义协议) /// </summary> /// <param name="connection"></param> /// <param name="buffer"></param> /// <param name="maxMessageSize"></param> /// <param name="readlength"></param> /// <returns></returns> /// <exception cref="BadProtocolException">bad async binary protocl</exception> public DSSBinaryCommandInfo FindCommandInfo(IConnection connection, ArraySegment <byte> buffer, int maxMessageSize, out int readlength) { if (buffer.Count < 4) { readlength = 0; return(null); } var payload = buffer.Array; //获取message length var messageLength = NetworkBitConverter.ToInt32(payload, buffer.Offset); if (messageLength < 7) { throw new BadProtocolException("bad async binary protocl"); } if (messageLength > maxMessageSize) { throw new BadProtocolException("message is too long"); } readlength = messageLength + 4; if (buffer.Count < readlength) { readlength = 0; return(null); } var seqID = NetworkBitConverter.ToInt32(payload, buffer.Offset + 4); var projectID = NetworkBitConverter.ToInt16(payload, buffer.Offset + 8); var extProperty = NetworkBitConverter.ToInt16(payload, buffer.Offset + 10); var cmdNameLength = NetworkBitConverter.ToInt16(payload, buffer.Offset + 12); var versionNumberLength = NetworkBitConverter.ToInt16(payload, buffer.Offset + 14); var projectNameLength = NetworkBitConverter.ToInt16(payload, buffer.Offset + 16); var strName = Encoding.UTF8.GetString(payload, buffer.Offset + 18, cmdNameLength); var versionNumber = Encoding.UTF8.GetString(payload, buffer.Offset + 18 + cmdNameLength, versionNumberLength); var projectName = Encoding.UTF8.GetString(payload, buffer.Offset + 18 + cmdNameLength + versionNumberLength, projectNameLength); var dataLength = messageLength - 12 - cmdNameLength - versionNumberLength - projectNameLength; byte[] data = null; if (dataLength > 0) { data = new byte[dataLength]; Buffer.BlockCopy(payload, buffer.Offset + 18 + cmdNameLength + versionNumberLength + projectNameLength, data, 0, dataLength); } return(new DSSBinaryCommandInfo(seqID, projectID, projectName, extProperty, strName, versionNumber, data)); }
/// <summary> /// find response /// </summary> /// <param name="connection"></param> /// <param name="buffer"></param> /// <param name="readlength"></param> /// <returns></returns> /// <exception cref="BadProtocolException">bad async binary protocl</exception> public DSSBinaryResponse FindResponse(IConnection connection, ArraySegment <byte> buffer, out int readlength) { if (buffer.Count < 4) { readlength = 0; return(null); } //获取message length var messageLength = NetworkBitConverter.ToInt32(buffer.Array, buffer.Offset); if (messageLength < 7) { throw new BadProtocolException("bad async binary protocl"); } readlength = messageLength + 4; if (buffer.Count < readlength) { readlength = 0; return(null); } var seqID = NetworkBitConverter.ToInt32(buffer.Array, buffer.Offset + 4); var projectID = NetworkBitConverter.ToInt16(buffer.Array, buffer.Offset + 8); var extProperty = NetworkBitConverter.ToInt16(buffer.Array, buffer.Offset + 10); var flagLength = NetworkBitConverter.ToInt16(buffer.Array, buffer.Offset + 12); var versonLength = NetworkBitConverter.ToInt16(buffer.Array, buffer.Offset + 14); var projectNameLength = NetworkBitConverter.ToInt16(buffer.Array, buffer.Offset + 16); var strName = Encoding.UTF8.GetString(buffer.Array, buffer.Offset + 18, flagLength); var versionNumber = Encoding.UTF8.GetString(buffer.Array, buffer.Offset + 18 + flagLength, versonLength); var projectName = Encoding.UTF8.GetString(buffer.Array, buffer.Offset + 18 + flagLength + versonLength, projectNameLength); var dataLength = messageLength - 16 - flagLength - versonLength - projectNameLength; byte[] data = null; if (dataLength > 0) { data = new byte[dataLength]; Buffer.BlockCopy(buffer.Array, buffer.Offset + 18 + flagLength + versonLength + projectNameLength, data, 0, dataLength); } return(new DSSBinaryResponse(seqID, projectID, projectName, extProperty, strName, versionNumber, data)); }