Package.cpp
上传用户:dzyhzl
上传日期:2019-04-29
资源大小:56270k
文件大小:4k
源码类别:

模拟服务器

开发平台:

C/C++

  1. // Package.cpp: implementation of the Package class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "Package.h"
  6. //////////////////////////////////////////////////////////////////////
  7. // Construction/Destruction
  8. //////////////////////////////////////////////////////////////////////
  9. namespace PackageCQ
  10. {
  11. Package::~Package()
  12. {
  13. m_DataStream.clear(); 
  14. m_DataStream.close();
  15. }
  16. Package::Package(string PackageName)
  17. {
  18. try
  19. {
  20. m_DataStream.open(PackageName.c_str(),ios::in|ios::binary );
  21. if(!m_DataStream.is_open())
  22. {
  23. throw exception("File Open Error!!");
  24. }
  25. }
  26. catch(exception& error)
  27. {
  28. strstream ErrorInfo;
  29. ErrorInfo<<"Open Package File Error! System Error Info :"<< error.what()<<endl<<ends;
  30. throw exception(ErrorInfo.str()  );
  31. }
  32. PackageFileHead FileHead;
  33. FileHead<<m_DataStream;
  34. if(!FileHeadCheck(FileHead))
  35. {
  36. throw (exception("FILE Type Error:File isn't Packge Type"));
  37. }
  38. unsigned long  BlockCount = FileHead.GetBlockCount();
  39. unsigned long  IndexOffset = FileHead.GetIndexOffset();
  40. m_DataStream.seekg(IndexOffset,ios::beg);
  41. BlockSource Block; 
  42. for(int i=0;i<BlockCount;i++)
  43. {
  44. Block<<m_DataStream;
  45. int Pos = m_DataStream.tellg(); 
  46. m_DataBlock.InsertBlock(Block);
  47. }
  48. }
  49. bool Package::FileHeadCheck(const PackageFileHead& FileHead) const
  50. {
  51. if(FileHead.GetFileType() == string(FileID))
  52. {
  53. return true;
  54. }
  55. else
  56. {
  57. return false;
  58. }
  59. }
  60. DataSource& Package::GetDataSource()
  61. {
  62. return m_DataBlock;
  63. }
  64. Package::Package(DataSource &Source,const string FileName)
  65. {
  66. m_DataStream.open(FileName.c_str(),ios::out|ios::binary );
  67.         
  68. if(!m_DataStream.is_open())
  69. {
  70. throw exception("File Open Error!!");
  71. }
  72. Interval Interval1;
  73.         
  74. Interval1.StartRecord (); 
  75. unsigned long  BlockStartPosition = sizeof(pack_header);
  76.         
  77.  
  78. ConstructionDataBlock(Source,BlockStartPosition);
  79. Interval1.EndRecord();
  80. int IntervalSecond = Interval1.GetIntervalBySecond(); 
  81. unsigned long IndexOffset = m_DataStream.tellg();
  82. m_DataBlock >> m_DataStream;
  83. m_DataStream.seekg(0,ios::beg);
  84. unsigned long CRC32value = 0;
  85. PackageFileHead FileHead(Source.GetBlockCount(), IndexOffset,FileID,BlockStartPosition,CRC32value) ;
  86. FileHead >> m_DataStream;
  87.     
  88. m_DataStream.clear(); 
  89. m_DataStream.close(); 
  90. m_DataStream.seekg(0,ios::beg ); 
  91. m_DataStream.open(FileName.c_str(),ios::in|ios::binary ); 
  92. }
  93. void Package::ConstructionDataBlock(DataSource &Source,unsigned long ConstructionPosition)
  94. {
  95. int BlockCount = Source.GetBlockCount();
  96. int CurrentPoint = ConstructionPosition;
  97. /* Interval Interval1;
  98. Interval1.StartRecord (); 
  99. */
  100. for( int i=0;i<BlockCount;i++)
  101. {
  102. DataPointFromStream FilePoint(m_DataStream,CurrentPoint);
  103. BlockSource         Block(FilePoint);
  104. // Interval Interval2;
  105. // Interval2.StartRecord ();
  106. Block.CloneBlockSourceToMe(Source[i]) ;
  107. // Interval2.EndRecord();
  108. // cout <<"部分耗时"<<(Interval2.GetInterval() )<<endl; 
  109. m_DataBlock.InsertBlock(Block);
  110.  
  111. CurrentPoint = CurrentPoint + Block.GetLength(); 
  112. }
  113. // Interval1.EndRecord();
  114. // cout <<"总循环耗时"<<(Interval1.GetInterval() )<<endl; 
  115. }
  116. }