BYTE SD_ReadBytes(DWORD sector,DWORD offset,BYTE* buf,WORD num){//读取若干字节 WORD SecReadCnt=0; while(offset>=512){//计算偏移跳过的扇区 offset-=512; sector++; } if(ReadBegin(sector++)!=0) return 1;//进入读状态失败 while(offset--){//跳过offset个字节 SecReadCnt++; SPI_ByteRW(0xFF); } while(num){//还有数据要读 while(num>0 && SecReadCnt<512){//还有数据要读并且本扇区未读完 *(buf++)=SPI_ByteRW(0xFF); SecReadCnt++; num--; } if(num==0){//读完毕 break; } else{//数据未读完,本扇区读结束 ReadEnd(); if(ReadBegin(sector++)!=0) return 1;//进入下一扇区读状态失败 SecReadCnt=0;//本扇区字节计数清零 } } while((SecReadCnt++)<512){//跳过本扇区最后的字节 SPI_ByteRW(0xFF); } ReadEnd(); return 0; }
MemoryData NetworkBuffer::ReadData(size_t len, DataReadingMode mode/* = DataReadingMode::AlwaysCopy*/) { assert(len <= ReadableCount()); if (mode == DataReadingMode::AlwaysCopy) { MemoryData result = MemoryData::CopyFrom(ReadBegin(), len); Retrieve(len); return result; } else { MemoryData result = MemoryData::FromStatic(ReadBegin(), len); Retrieve(len); return result; } }
BYTE SD_ReadOneSector(DWORD sector,BYTE* buf){//读一个扇区(buf为512字节),成功返回0 WORD i; if(ReadBegin(sector++)!=0) return 1;//进入读状态失败 for(i=0;i<512;i++){//读512个数据 *(buf++)=SPI_ByteRW(0xFF); } ReadEnd();//退出读状态 return 0; }
void NetworkBuffer::TryAdjust() { if (IsFull()) { mStream.ReserveSize(Size() * 2); } else if (mReadIndex>= Size() / 2) //write { // move readable data to the front, make space inside buffer size_t readable = ReadableCount(); Memory::SafeCopy(mStream.MutablePtr(), mStream.Length(), ReadBegin(), readable); mReadIndex = 0; mWriteIndex = mReadIndex + readable; assert(readable == ReadableCount()); } }
HeapString NetworkBuffer::ReadString(size_t len) { HeapString result((const char*)ReadBegin(), len); Retrieve(len); return result; }
StringRef NetworkBuffer::ReadStringRef(size_t len) { StringRef result((const char*)ReadBegin(), len); Retrieve(len); return result; }
void NetworkBuffer::RetrieveUntil(const byte* end) { assert(ReadBegin() <= end); assert(end <= WriteBegin()); Retrieve(end - ReadBegin()); }