fork download
  1. DataChunk chunk;
  2. char tmpPacket[8192];
  3. short dataLength;
  4. bool isHead = true;
  5. unsigned short tmpLength, offset;
  6. recvThreadNum++;
  7. //cout<<"RecvAndSaveInBuf-"<<tmpIterator->second.sockTCP<<": has entered one thread!!"<<endl;
  8. while(1){
  9. dataLength = recv(tmpIterator->second.sockTCP, tmpPacket, isHead ? sizeof(DataChunk) : chunk.length, MSG_WAITALL);
  10. //檢查是否接收到完整封包, 因若其他thread之recv()返回error時會造成其他thread之recv()當下接收資料不完整, 導致後續資料完全錯亂
  11. tmpLength = isHead ? sizeof(DataChunk) - dataLength : chunk.length - dataLength;
  12. offset = dataLength;
  13. while(tmpLength != 0){
  14. dataLength = recv(tmpIterator->second.sockTCP, tmpPacket + offset, tmpLength, MSG_WAITALL);
  15. if(dataLength == 0) break;
  16. tmpLength -= dataLength;
  17. offset += dataLength;
  18. }
  19. //getsockopt(pPair.second.sockTCP, IPPROTO_TCP, TCP_INFO, &info, (socklen_t*)&infolen);
  20. //cout<<"RecvAndSaveInBuf: "<<pPair.second.sockTCP<<": "<<info.tcpi_rcv_rtt<<endl;
  21. if(dataLength <= 0){
  22. shutdown(tmpIterator->second.sockTCP, 2);
  23. close(tmpIterator->second.sockTCP);
  24. cout<<"RecvAndSaveInBuf-"<<tmpIterator->second.sockTCP<<": recv() data failed: "<<strerror(errno)<<endl;
  25. break;
  26. }
  27. if(isHead){
  28. memcpy((char*)&chunk, tmpPacket, sizeof(DataChunk));
  29. isHead = false;
  30. }
  31. else{
  32. chunk.data = tmpPacket;
  33. if(chunk.state == INITPKG){
  34. while(1){
  35. map<sockaddr_in, Link>::iterator index = LinksTable.begin();
  36. for(; index != LinksTable.end(); index++){
  37. if(index->second.inf.inf_name != ""){
  38. index->second.inf.inf_name = CharToInf(*chunk.data);
  39. index->second.timer = *(timeval*)(chunk.data + sizeof(char));
  40. }
  41. }
  42. if(index == LinksTable.end()){
  43. cout<<"RecvAndSaveInBuf-"<<tmpIterator->second.sockTCP<<": Cannot find the interface in LinksTable\n";
  44. continue;
  45. }
  46. else{
  47. break;
  48. }
  49. }
  50. }
  51. else if(chunk.state == CTRLPKG){
  52. unsigned short index, length = chunk.length - sizeof(timeval);
  53. if(TimevalCompare(*(timeval*)(chunk.data + length), existAddrTimeStamp) != 1){
  54. isHead = true;
  55. continue;
  56. }
  57. existAddrTimeStamp = *(timeval*)(chunk.data + length);
  58. cout<<"RecvAndSaveInBuf-"<<tmpIterator->second.sockTCP<<": existAddr: ";
  59. for(int i = 0; i < (int)(chunk.length - sizeof(timeval)); i++){
  60. cout<<CharToInf(chunk.data[i]);
  61. if(i != (chunk.length - sizeof(timeval) - 1)){
  62. cout<<" - ";
  63. }
  64. }
  65. cout<<endl;
  66. pthread_mutex_lock(&hLinks);
  67. for(map<sockaddr_in, Link>::iterator i = LinksTable.begin(); i != LinksTable.end(); i++){
  68. for(index = 0; index < length; index++){
  69. if(i->second.inf.inf_name == CharToInf(chunk.data[index])){
  70. break;
  71. }
  72. }
  73. if(index == length){
  74. shutdown(i->second.sockTCP, 2);
  75. close(i->second.sockTCP);
  76. shutdown(i->second.hbSockUDP, 2);
  77. close(i->second.hbSockUDP);
  78. }
  79. }
  80. pthread_mutex_unlock(&hLinks);
  81. isHead = true;
  82. continue;
  83. }
  84.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty