本文整理汇总了C++中StreamBuffer::ReceiveDate方法的典型用法代码示例。如果您正苦于以下问题:C++ StreamBuffer::ReceiveDate方法的具体用法?C++ StreamBuffer::ReceiveDate怎么用?C++ StreamBuffer::ReceiveDate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类StreamBuffer
的用法示例。
在下文中一共展示了StreamBuffer::ReceiveDate方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main()
{
char srcfileName[500]="E:\\c&c++\\CPP2014\\homework\\2014-7-15\\media\\in.mp3";
char dstfileName[500]="E:\\c&c++\\CPP2014\\homework\\2014-7-15\\media\\out.mp3";
FILE* fpSrcFile = NULL;
FILE* fpDstFile = NULL;
const int MTU = 1500;//最大传输单元,网络术语,表示一个数据包大最大尺寸,单位:字节
unsigned int iOffset;
int iReadBytes = 0, iWantReadBytes;
int iContinueBytes;
int iUseBytes;
unsigned int iOutDataOffset;
char *pOutData;
StreamBuffer MyBuffer;
const int iMaxPacketNum = 20; //每次读入20个数据包,然后以乱序的形式发给排序模块(StreamBuffer类)
int iSendOrder[iMaxPacketNum];//记录下发数据包的顺序
unsigned int iPacketOffset[iMaxPacketNum];//记录每个数据包中第一个字节数据的偏移量
unsigned int iPacketLen[iMaxPacketNum];//记录每个数据包中的数据长度
char (*pDataBuf)[MTU]; //数据包缓冲区区。创建方法1:指向一位数组的指针,下一步会为它一次性申请一个二维数组
//char *pDataBuf[iMaxPacketNum];// 创建方法2,指针数组,一步会为它多次申请空间,每次申请一个一维数组,释放也要多次
int iPacketNum;
int i;
int iPackNo;
//-------------- added for experiment 03, begin ------------------------------
int iTotalPacketNum = 0;
const int DiscardPacketInterval = 100;//每DiscardPacketInterval个数据包丢一个,建议数值:30~100 (srcfileName的文件尺寸足够大,会有丢失产生)
//-------------- added for experiment 03, end ------------------------------
srand(100);//用固定值初始化,会生成固定的随机数序列,方便测试程序,否则用srand( (unsigned)time( NULL ) );
pDataBuf = new char[iMaxPacketNum][MTU];//方法1
/*for( i=0;i<iMaxPacketNum;i++)//方法2
{
pDataBuf[i] = new char[MTU];
}*/
fpSrcFile = fopen(srcfileName, "rb");
if( fpSrcFile == NULL )
{
cout<<"Cann't open file: "<<srcfileName<<endl;
return 1;
}
fpDstFile = fopen(dstfileName, "wb");
if( fpDstFile == NULL )
{
cout<<"Cann't create file: "<< dstfileName <<endl;
return 2;
}
iWantReadBytes = 1024;
do
{
iPacketNum = 0;
for( i=0;i<iMaxPacketNum;i++)//初始化数据包长度为0,表示没有读入数据
iPacketLen[i] = 0;//2014.07.11 iPacketNum->i
// iReadBytes = fread(pDataBuf[iPacketNum], 1, iWantReadBytes, fpSrcFile);
do
{
iPacketOffset[iPacketNum] = ftell(fpSrcFile);
iReadBytes = fread(pDataBuf[iPacketNum], 1, iWantReadBytes, fpSrcFile);
iPacketLen[iPacketNum] = iReadBytes; //当前数据包读取成功,记录数据包长度,否则依旧是0
iWantReadBytes = (iPacketOffset[iPacketNum]+iPacketNum*iPacketNum)%500+400;//下一个数据包读取长度
iPacketNum++;
} while( iReadBytes > 0 && iPacketNum < iMaxPacketNum );//读入一组数据包,如果文件结束:iReadBytes<1
//把刚刚已经读入一组数据包,乱序下发给排序模块
GenDisOrder(iSendOrder,iMaxPacketNum);
for(i=0;i<iMaxPacketNum;i++)//只要长度不为0,就发给排序模块
{
iPackNo = iSendOrder[i];
if(iPacketLen[iPackNo] > 0 )//有数据,给给排序模块
{
//-------------- added for experiment 03, begin ------------------------------
iTotalPacketNum++;
if( iTotalPacketNum%DiscardPacketInterval == DiscardPacketInterval-1)
continue;//不给缓冲区发送,相当于丢失
//-------------- added for experiment 03, end ------------------------------
#ifdef TEST_FRAME //正常情况,代码框架会产生数据有缺失的结果文件
iUseBytes = iPacketLen[iPackNo];
pOutData = pDataBuf[iPackNo];
iOutDataOffset = iPacketOffset[iPackNo];
#else
MyBuffer.ReceiveDate(iPacketOffset[iPackNo],iPacketLen[iPackNo],pDataBuf[iPackNo]);
iContinueBytes = MyBuffer.ContinueBytes(iOutDataOffset, pOutData);
iUseBytes = iContinueBytes - 100;//假设用了一部分
if( iContinueBytes > 1024) //示例数值,可以调整
#endif
{
fseek(fpDstFile,iOutDataOffset,SEEK_SET);
fwrite(pOutData,iUseBytes,1,fpDstFile);
MyBuffer.RemoveData(iUseBytes);
}
//.........这里部分代码省略.........