Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1. uint32 i;
  2. for(i = nCurDate; i <= nEndDate; i = DataTimeUtil::GetNextDate(i))
  3. {
  4. sprintf(strFileName,"%s\\%04d\\%02d\\%d%c.txt", filePath, i/10000, (i%10000)/100, i, prefix);
  5. if(sys::file_exist(strFileName))
  6. {
  7. //打开实时更新文件读取第一个long数据,判断是否包含更新数据
  8. if(nFileHandle == -1 && (nFileHandle = _open(strFileName, _O_RDONLY | _O_BINARY, _S_IREAD)) == -1){
  9. log_error("Fail to open realtimefile %s, errorcode = %d(ScanRealTimeData)", strFileName, GetLastError());
  10. throw "Fail to open file";
  11. }
  12.  
  13. //读取当前实时文件数据总数
  14. uint32 nTotalSize = (i == nEndDate && bOpenFlag) ? nTotalFileSize : _lseek(nFileHandle, 0, SEEK_END);
  15. if((int32)nTotalSize < 0) throw "Fail to _lseek RealTimeFile";
  16.  
  17. if(nCurPos > nTotalFileSize){
  18. log_error(" oops! nTotalFileSize is %u,nCurPos is %u",nTotalFileSize,nCurPos);
  19. if (nFileHandle != -1) _close(nFileHandle);
  20. if((nFileHandle = _open(strFileName, _O_RDONLY | _O_BINARY, _S_IREAD)) == -1) {
  21. log_error("Fail to Reopen realtimefile %s, errorcode = %d(ScanRealTimeData)", strFileName, GetLastError());
  22. throw "Fail to Re open file";
  23. }
  24. nTotalSize = _lseek(nFileHandle, 0, SEEK_END);
  25. if((int32)nTotalSize < 0) throw "Fail to Re _lseek RealTimeFile";
  26. }
  27.  
  28. //当前扫描到的实时文件有数据,但之前有效的实时服务器未读完没有完成切换时,不读取该文件内容
  29. if(i == nNowDate)
  30. {
  31. if (prefix == T_FILE && nRealServerNum != g_TFileRTServerNum)break;
  32. if (prefix == F_FILE && nRealServerNum != g_FFileRTServerNum)break;
  33. if (prefix == W_FILE && nRealServerNum != g_WFileRTServerNum)break;
  34. }
  35.  
  36. //如果需要停机维护,则只同步到指定的位置
  37. if(bSync && i == nDstDate && nDstPos < nTotalSize) nTotalSize = nDstPos;
  38.  
  39. //循环处理每一行实时更新数据
  40. while(nCurPos < nTotalSize){
  41. //寻址到指定的位置, 因为上次读取的数据可能不是正好对齐的,一行数据可能只被读取了一半
  42. if(_lseek(nFileHandle, nCurPos, SEEK_SET) == -1) throw "Fail to _lseek.";
  43.  
  44. //获取需要读取的数据块,缺省读取5w数据,但是最后实际大小不足5w,则读取实际大小
  45. uint32 nSingleReadSize = nSingleMaxCount;
  46. if(nTotalSize - nCurPos < nSingleReadSize) nSingleReadSize = nTotalSize - nCurPos;
  47.  
  48. //读取指定大小的实时更新文件内容到缓冲区
  49. int32 nActualSize = (int32)_read(nFileHandle, strFileBuffer, nSingleReadSize);
  50. if(nActualSize != (int32)nSingleReadSize){
  51. log_error("Fail to _read strFileBuffer, Start:%ld, should read %ld, Actual read %ld", nCurPos, nSingleReadSize, nActualSize);
  52. throw "Fail to read file";
  53. }
  54.  
  55. strFileBuffer[nSingleReadSize] = '\0';
  56.  
  57. // 处理数据
  58. char* pFileData = strFileBuffer;
  59.  
  60. if ( prefix == T_FILE )
  61. {
  62. //对于T文件,先处理新增记录,然后再处理删除记录,这样需要循环两遍
  63. TimeRuler tm;
  64. uint32 nTmpPos = nCurPos;
  65. //处理TFile
  66. int32 nRetT = ProcessLineBuffer(pFileData, T_FILE, nTmpPos);
  67. if(nRetT == 0) throw "Fail to call ProcessLineBuffer TFile";
  68. //新增记录时间
  69. g_RealStat.t_time += (uint32)tm.elapsed_ms();
  70. g_nUpdateRealTimeStatus = T_FILE;
  71.  
  72. //处理DFile
  73. pFileData = strFileBuffer;
  74. int32 nRetD = ProcessLineBuffer(pFileData, D_FILE, nCurPos);
  75. if(nRetD == 0) throw "Fail to call ProcessLineBuffer DFile";
  76. //如果字符串结束,最后可能包含的字符为不全的字符串,下次再处理
  77. if(nRetT == 2 || nRetD == 2) break;
  78. }else{
  79. int32 nRet = ProcessLineBuffer(pFileData, prefix, nCurPos);
  80. if(nRet == 0){
  81. log_error("Fail to call ProcessLineBuffer %cFile ", prefix);
  82. throw "Fail to ProcessLineBuffer";
  83. }
  84. if(nRet == 2)break;
  85. }
  86. }
  87. }else{
  88. // 如果服务器出现异常,无法正常连接时,则关闭原有文件句柄,等网络恢复后自动从断点运行
  89. if(nFileHandle != -1 || i != nCurDate){
  90. if(nFileHandle!=-1) _close(nFileHandle),nFileHandle = -1;
  91. if(bOpenFlag) log_error(" %s is not exists!",strFileName);
  92. break;
  93. }
  94. }
  95. nCurPos = 0, bOpenFlag = false;
  96. if(nFileHandle!=-1) _close(nFileHandle), nFileHandle = -1;
  97. }
  98. nCurDate = i;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement