/** * * 使用方法: * 1,入bytebuf后 把bytebuf发送出去,并释放bytebuf * 2,判断返回值是否为null,如果不为null发送出去并释放它 * * headerOffset +6字节fectHead + 2字节bodylenth(lenth-headerOffset-6) * * 1,对数据写入头标记为数据类型 markData * 2,写入消息长度 * 3,获得缓存数据中最大长度,其他的缓存进行扩容到同样长度 * 4,去掉头长度,进行fec编码 * 5,对冗余字节数组进行标记为fec makefec * 6,返回完整长度 * * 注意: 传入的bytebuf如果需要释放在传入后手动释放。 * 返回的bytebuf 也需要自己释放 * @param byteBuf * @return */ public IByteBuffer[] encode(IByteBuffer byteBuf) { markData(byteBuf, headerOffset); int sz = byteBuf.WriterIndex; byteBuf.SetShort(payloadOffset, sz - headerOffset - Fec.fecHeaderSizePlus2); this.shardCache[shardCount] = byteBuf.RetainedDuplicate(); this.shardCount++; if (sz > this.maxSize) { this.maxSize = sz; } if (shardCount != dataShards) { return(null); } //填充parityShards for (int i = 0; i < parityShards; i++) { IByteBuffer parityByte = PooledByteBufferAllocator.Default.DirectBuffer(this.maxSize); shardCache[i + dataShards] = parityByte; encodeCache[i] = parityByte; markParity(parityByte, headerOffset); parityByte.SetWriterIndex(this.maxSize); } //按着最大长度不足补充0 for (var i = 0; i < this.dataShards; i++) { var shard = shardCache[i]; var left = this.maxSize - shard.WriterIndex; if (left <= 0) { continue; } //是否需要扩容 会出现吗?? //if(shard.capacity()<this.maxSize){ // ByteBuf newByteBuf = ByteBufAllocator.DEFAULT.buffer(this.maxSize); // newByteBuf.writeBytes(shard); // shard.release(); // shard = newByteBuf; // shardCache[i] = shard; //} shard.WriteBytes(zeros, left); zeros.SetReaderIndex(0); } codec.encodeParity(shardCache, payloadOffset, this.maxSize - payloadOffset); //释放dataShards for (int i = 0; i < dataShards; i++) { this.shardCache[i].Release(); this.shardCache[i] = null; } this.shardCount = 0; this.maxSize = 0; return(this.encodeCache); }