/// <summary>LoadContent /// </summary> public override void LoadContent(ClusterConfiguration configuration, byte[] data) { int bytesRead = 0; byte[] groupNameBuffer = new byte[Consts.FDFS_GROUP_NAME_MAX_LEN]; Array.Copy(data, bytesRead, groupNameBuffer, 0, Consts.FDFS_GROUP_NAME_MAX_LEN); GroupName = EndecodeUtil.DecodeString(groupNameBuffer, configuration.Charset); //Util.ByteToString(option.Charset, groupNameBuffer).TrimEnd('\0'); bytesRead += Consts.FDFS_GROUP_NAME_MAX_LEN; byte[] ipAddressBuffer = new byte[Consts.IP_ADDRESS_SIZE - 1]; Array.Copy(data, bytesRead, ipAddressBuffer, 0, Consts.IP_ADDRESS_SIZE - 1); IPAddresses.Add(EndecodeUtil.DecodeString(ipAddressBuffer, configuration.Charset)); //IPAddresses.Add(new string(option.Charset.GetChars(ipAddressBuffer)).TrimEnd('\0')); bytesRead += Consts.IP_ADDRESS_SIZE - 1; byte[] portBuffer = new byte[Consts.FDFS_PROTO_PKG_LEN_SIZE]; Array.Copy(data, bytesRead, portBuffer, 0, Consts.FDFS_PROTO_PKG_LEN_SIZE); Port = (int)ByteUtil.BufferToLong(portBuffer, 0); bytesRead += Consts.FDFS_PROTO_PKG_LEN_SIZE; while (data.Length - bytesRead >= Consts.IP_ADDRESS_SIZE - 1) { ipAddressBuffer = new byte[Consts.IP_ADDRESS_SIZE - 1]; Array.Copy(data, bytesRead, ipAddressBuffer, 0, Consts.IP_ADDRESS_SIZE - 1); IPAddresses.Add(EndecodeUtil.DecodeString(ipAddressBuffer, configuration.Charset)); // IPAddresses.Add(new string(option.Charset.GetChars(ipAddressBuffer)).TrimEnd('\0')); bytesRead += Consts.IP_ADDRESS_SIZE - 1; } }
/// <summary>LoadContent /// </summary> public override void LoadContent(ClusterConfiguration configuration, byte[] data) { byte[] fileSizeBuffer = new byte[Consts.FDFS_PROTO_PKG_LEN_SIZE]; byte[] createTimeBuffer = new byte[Consts.FDFS_PROTO_PKG_LEN_SIZE]; byte[] crcBuffer = new byte[Consts.FDFS_PROTO_PKG_LEN_SIZE]; Array.Copy(data, 0, fileSizeBuffer, 0, fileSizeBuffer.Length); Array.Copy(data, Consts.FDFS_PROTO_PKG_LEN_SIZE, createTimeBuffer, 0, createTimeBuffer.Length); Array.Copy(data, Consts.FDFS_PROTO_PKG_LEN_SIZE + Consts.FDFS_PROTO_PKG_LEN_SIZE, crcBuffer, 0, crcBuffer.Length); FileSize = ByteUtil.BufferToLong(data, 0); CreateTime = new DateTime(1970, 1, 1).AddSeconds(ByteUtil.BufferToLong(data, Consts.FDFS_PROTO_PKG_LEN_SIZE)); Crc32 = ByteUtil.BufferToLong(data, Consts.FDFS_PROTO_PKG_LEN_SIZE + Consts.FDFS_PROTO_PKG_LEN_SIZE); }
/// <summary>LoadContent /// </summary> public override void LoadContent(FastDFSOption option, byte[] data) { byte[] groupNameBuffer = new byte[Consts.FDFS_GROUP_NAME_MAX_LEN]; Array.Copy(data, groupNameBuffer, Consts.FDFS_GROUP_NAME_MAX_LEN); GroupName = ByteUtil.ByteToString(groupNameBuffer, option.Charset).TrimEnd('\0'); byte[] ipAddressBuffer = new byte[Consts.IP_ADDRESS_SIZE - 1]; Array.Copy(data, Consts.FDFS_GROUP_NAME_MAX_LEN, ipAddressBuffer, 0, Consts.IP_ADDRESS_SIZE - 1); IPAddress = new string(Encoding.GetEncoding(option.Charset).GetChars(ipAddressBuffer)).TrimEnd('\0'); byte[] portBuffer = new byte[Consts.FDFS_PROTO_PKG_LEN_SIZE]; Array.Copy(data, Consts.FDFS_GROUP_NAME_MAX_LEN + Consts.IP_ADDRESS_SIZE - 1, portBuffer, 0, Consts.FDFS_PROTO_PKG_LEN_SIZE); Port = (int)ByteUtil.BufferToLong(portBuffer, 0); }
/// <summary>LoadContent /// </summary> public override void LoadContent(ClusterConfiguration configuration, byte[] data) { byte[] groupNameBuffer = new byte[Consts.FDFS_GROUP_NAME_MAX_LEN]; Array.Copy(data, groupNameBuffer, Consts.FDFS_GROUP_NAME_MAX_LEN); GroupName = EndecodeUtil.DecodeString(groupNameBuffer); byte[] ipAddressBuffer = new byte[Consts.IP_ADDRESS_SIZE - 1]; Array.Copy(data, Consts.FDFS_GROUP_NAME_MAX_LEN, ipAddressBuffer, 0, Consts.IP_ADDRESS_SIZE - 1); IPAddress = EndecodeUtil.DecodeString(ipAddressBuffer); //new string(option.Charset.GetChars(ipAddressBuffer)).TrimEnd('\0'); byte[] portBuffer = new byte[Consts.FDFS_PROTO_PKG_LEN_SIZE]; Array.Copy(data, Consts.FDFS_GROUP_NAME_MAX_LEN + Consts.IP_ADDRESS_SIZE - 1, portBuffer, 0, Consts.FDFS_PROTO_PKG_LEN_SIZE); Port = (int)ByteUtil.BufferToLong(portBuffer, 0); }
public override ReceivedPackage Filter(ref SequenceReader <byte> reader) { var transportContext = _getCtx(); var package = new ReceivedPackage() { IsComplete = false, IsOutputStream = transportContext.IsOutputStream, OutputFilePath = transportContext.OutputFilePath }; if (transportContext.IsOutputStream) { if (!_foundHeader) { //未读取到头部,并且长度不足头部 if (reader.Length < headerLength) { return(null); } //长度 var packLength = ByteUtil.BufferToLong(reader.Sequence.Slice(0, Consts.FDFS_PROTO_PKG_LEN_SIZE).ToArray()); var frameLength = packLength + headerLength; if (reader.Length < frameLength) { //缓存中没有把整个文件数据缓存下来,就需要读一部分写一步 var readLength = reader.Length; //长度 reader.TryReadBigEndian(out _length); //命令 reader.TryRead(out _command); //状态 reader.TryRead(out _status); package.Length = _length; package.Command = _command; package.Status = _status; var bodyLength = readLength - headerLength; package.Body = reader.Sequence.Slice(headerLength, bodyLength).ToArray(); reader.Advance(bodyLength); //设置读取的进度 _readPosition += bodyLength; _foundHeader = true; } else { //全部读完,则直接完成 //长度 reader.TryReadBigEndian(out _length); //命令 reader.TryRead(out _command); //状态 reader.TryRead(out _status); package.Length = _length; package.Command = _command; package.Status = _status; package.Body = reader.Sequence.Slice(headerLength, packLength).ToArray(); reader.Advance(package.Body.Length); _foundHeader = true; //重置 ReceiveReset(); package.IsComplete = true; } } else { //不是第一次读 package.Length = _length; package.Command = _command; package.Status = _status; //未读的长度 var unreadLength = _length - _readPosition; //需要写的长度,剩余长度与当前接收包体两者取小值 var chunkSize = Math.Min(unreadLength, reader.Length); package.Body = reader.Sequence.Slice(0, chunkSize).ToArray(); reader.Advance(package.Body.Length); //判断是否完成 if (_readPosition + chunkSize >= _length) { //完成 ReceiveReset(); package.IsComplete = true; } else { //设置读取进度 _readPosition += chunkSize; } } } else { //如果是整包读取,但是还读取不到头,等待下次接收 if (reader.Length < headerLength) { return(null); } //整个数据包的长度,应该是包长度+2+body //包的数据长度 var packLength = ByteUtil.BufferToLong(reader.Sequence.Slice(0, Consts.FDFS_PROTO_PKG_LEN_SIZE).ToArray()); var frameLength = packLength + headerLength; if (reader.Length < frameLength) { return(null); } //长度 reader.TryReadBigEndian(out _length); //命令 reader.TryRead(out _command); //状态 reader.TryRead(out _status); package.Length = _length; package.Command = _command; package.Status = _status; package.Body = reader.Sequence.Slice(headerLength, packLength).ToArray(); reader.Advance(package.Body.Length); //重置 ReceiveReset(); package.IsComplete = true; } return(package); }
public override ReceivedPackage Filter(ref SequenceReader <byte> reader) { var transportContext = _getContextAction(); var package = new ReceivedPackage() { IsComplete = false, IsOutputStream = transportContext.IsOutputStream, OutputFilePath = transportContext.OutputFilePath }; if (transportContext.IsOutputStream) { if (!_foundHeader) { //未读取到头部,并且长度不足头部 if (reader.Length < lengthFieldEndOffset) { return(null); } //整个数据包的长度,应该是包长度+2+body var frameLengthBuffer = reader.Sequence.Slice(0, Consts.FDFS_PROTO_PKG_LEN_SIZE).ToArray(); var frameLength = ByteUtil.BufferToLong(frameLengthBuffer) + lengthFieldEndOffset; if (reader.Length < frameLength) { //读取全部的数据 var readLength = reader.Length; //长度 reader.TryReadBigEndian(out long _length); //命令 reader.TryRead(out byte _command); //状态 reader.TryRead(out byte _status); package.Length = _length; package.Command = _command; package.Status = _status; var bodyLength = readLength - lengthFieldEndOffset; package.Body = reader.Sequence.Slice(bodyLength).ToArray(); reader.Advance(bodyLength); //设置读取的进度 _readPosition += bodyLength; _foundHeader = true; } else { //全部读完,则直接完成 // extract frame var readerLength = reader.Length; //IByteBuffer frame = ExtractFrame(context, input, readerIndex, frameLengthInt); //input.SetReaderIndex(readerIndex + frameLengthInt); //长度 reader.TryReadBigEndian(out long _length); //命令 reader.TryRead(out byte _command); //状态 reader.TryRead(out byte _status); package.Length = _length; package.Command = _command; package.Status = _status; package.Body = reader.Sequence.Slice(0, frameLength).ToArray(); reader.Advance(package.Body.Length); //重置 Reset(); package.IsComplete = true; } } else { //不是第一次读 package.Length = _length; package.Command = _command; package.Status = _status; //未读的长度 var unreadLength = _length - _readPosition; //需要写的长度,剩余长度与当前接收包体两者取小值 var chunkSize = Math.Min(unreadLength, reader.Length); package.Body = reader.Sequence.Slice(0, chunkSize).ToArray(); reader.Advance(package.Body.Length); //判断是否完成 if (_readPosition + chunkSize >= _length) { //完成 Reset(); package.IsComplete = true; } else { //设置读取进度 _readPosition += chunkSize; } } } else { //读取不到头部 if (reader.Length < lengthFieldEndOffset) { return(null); } //整个数据包的长度,应该是包长度+2+body var frameLengthBuffer = reader.Sequence.Slice(0, Consts.FDFS_PROTO_PKG_LEN_SIZE).ToArray(); var frameLength = ByteUtil.BufferToLong(frameLengthBuffer) + lengthFieldEndOffset; if (reader.Length < frameLength) { return(null); } //长度 reader.TryReadBigEndian(out long _length); //命令 reader.TryRead(out byte _command); //状态 reader.TryRead(out byte _status); package.Length = _length; package.Command = _command; package.Status = _status; package.Body = reader.Sequence.Slice(package.Length).ToArray(); reader.Advance(package.Body.Length); //重置 Reset(); package.IsComplete = true; } return(package); }