mirror of
				https://github.com/ciphervance/supercell-wx.git
				synced 2025-10-31 02:50:04 +00:00 
			
		
		
		
	Optimize reading of repeating level 3 packet data
This commit is contained in:
		
							parent
							
								
									f1472275bc
								
							
						
					
					
						commit
						bf56680d85
					
				
					 5 changed files with 80 additions and 81 deletions
				
			
		|  | @ -97,20 +97,18 @@ bool LinkedContourVectorPacket::Parse(std::istream& is) | |||
| 
 | ||||
|    // The number of vectors is equal to the size divided by the number of bytes
 | ||||
|    // in a vector coordinate
 | ||||
|    int     vectorCount = vectorSize / 4; | ||||
|    int16_t endI; | ||||
|    int16_t endJ; | ||||
|    int vectorCount = vectorSize / 4; | ||||
| 
 | ||||
|    p->endI_.resize(vectorCount); | ||||
|    p->endJ_.resize(vectorCount); | ||||
| 
 | ||||
|    for (int v = 0; v < vectorCount && !is.eof(); v++) | ||||
|    { | ||||
|       is.read(reinterpret_cast<char*>(&endI), 2); | ||||
|       is.read(reinterpret_cast<char*>(&endJ), 2); | ||||
|       is.read(reinterpret_cast<char*>(&p->endI_[v]), 2); | ||||
|       is.read(reinterpret_cast<char*>(&p->endJ_[v]), 2); | ||||
| 
 | ||||
|       endI = ntohs(endI); | ||||
|       endJ = ntohs(endJ); | ||||
| 
 | ||||
|       p->endI_.push_back(endI); | ||||
|       p->endJ_.push_back(endJ); | ||||
|       p->endI_[v] = ntohs(p->endI_[v]); | ||||
|       p->endJ_[v] = ntohs(p->endJ_[v]); | ||||
|    } | ||||
| 
 | ||||
|    if (is.eof()) | ||||
|  |  | |||
|  | @ -110,20 +110,18 @@ bool LinkedVectorPacket::Parse(std::istream& is) | |||
| 
 | ||||
|    // The number of vectors is equal to the size divided by the number of bytes
 | ||||
|    // in a vector coordinate
 | ||||
|    int     vectorCount = vectorSize / 4; | ||||
|    int16_t endI; | ||||
|    int16_t endJ; | ||||
|    int vectorCount = vectorSize / 4; | ||||
| 
 | ||||
|    p->endI_.resize(vectorCount); | ||||
|    p->endJ_.resize(vectorCount); | ||||
| 
 | ||||
|    for (int v = 0; v < vectorCount && !is.eof(); v++) | ||||
|    { | ||||
|       is.read(reinterpret_cast<char*>(&endI), 2); | ||||
|       is.read(reinterpret_cast<char*>(&endJ), 2); | ||||
|       is.read(reinterpret_cast<char*>(&p->endI_[v]), 2); | ||||
|       is.read(reinterpret_cast<char*>(&p->endJ_[v]), 2); | ||||
| 
 | ||||
|       endI = ntohs(endI); | ||||
|       endJ = ntohs(endJ); | ||||
| 
 | ||||
|       p->endI_.push_back(endI); | ||||
|       p->endJ_.push_back(endJ); | ||||
|       p->endI_[v] = ntohs(p->endI_[v]); | ||||
|       p->endJ_[v] = ntohs(p->endJ_[v]); | ||||
|    } | ||||
| 
 | ||||
|    if (is.eof()) | ||||
|  |  | |||
|  | @ -80,26 +80,15 @@ bool TextAndSpecialSymbolPacket::Parse(std::istream& is) | |||
| { | ||||
|    bool blockValid = true; | ||||
| 
 | ||||
|    std::streampos isBegin = is.tellg(); | ||||
| 
 | ||||
|    is.read(reinterpret_cast<char*>(&p->packetCode_), 2); | ||||
|    is.read(reinterpret_cast<char*>(&p->lengthOfBlock_), 2); | ||||
| 
 | ||||
|    p->packetCode_    = ntohs(p->packetCode_); | ||||
|    p->lengthOfBlock_ = ntohs(p->lengthOfBlock_); | ||||
| 
 | ||||
|    int vectorSize = static_cast<int>(p->lengthOfBlock_) - 4; | ||||
| 
 | ||||
|    if (is.eof()) | ||||
|    { | ||||
|       BOOST_LOG_TRIVIAL(debug) << logPrefix_ << "Reached end of file"; | ||||
|       blockValid = false; | ||||
|    } | ||||
|    else if (p->packetCode_ == 8) | ||||
|    { | ||||
|       is.read(reinterpret_cast<char*>(&p->valueOfText_), 2); | ||||
|       p->valueOfText_ = ntohs(p->valueOfText_); | ||||
| 
 | ||||
|       vectorSize -= 2; | ||||
|    } | ||||
|    int textLength = static_cast<int>(p->lengthOfBlock_) - 4; | ||||
| 
 | ||||
|    is.read(reinterpret_cast<char*>(&p->startI_), 2); | ||||
|    is.read(reinterpret_cast<char*>(&p->startJ_), 2); | ||||
|  | @ -107,17 +96,6 @@ bool TextAndSpecialSymbolPacket::Parse(std::istream& is) | |||
|    p->startI_ = ntohs(p->startI_); | ||||
|    p->startJ_ = ntohs(p->startJ_); | ||||
| 
 | ||||
|    // The number of vectors is equal to the size divided by the number of bytes
 | ||||
|    // in a vector coordinate
 | ||||
|    int  vectorCount = vectorSize; | ||||
|    char c; | ||||
| 
 | ||||
|    for (int v = 0; v < vectorCount && !is.eof(); v++) | ||||
|    { | ||||
|       is.get(c); | ||||
|       p->characters_.push_back(c); | ||||
|    } | ||||
| 
 | ||||
|    if (is.eof()) | ||||
|    { | ||||
|       BOOST_LOG_TRIVIAL(debug) << logPrefix_ << "Reached end of file"; | ||||
|  | @ -131,6 +109,39 @@ bool TextAndSpecialSymbolPacket::Parse(std::istream& is) | |||
|             << logPrefix_ << "Invalid packet code: " << p->packetCode_; | ||||
|          blockValid = false; | ||||
|       } | ||||
|       else if (p->lengthOfBlock_ < 1 || p->lengthOfBlock_ > 32767) | ||||
|       { | ||||
|          BOOST_LOG_TRIVIAL(warning) | ||||
|             << logPrefix_ << "Invalid length of block: " << p->packetCode_; | ||||
|          blockValid = false; | ||||
|       } | ||||
|       else if (p->packetCode_ == 8) | ||||
|       { | ||||
|          is.read(reinterpret_cast<char*>(&p->valueOfText_), 2); | ||||
|          p->valueOfText_ = ntohs(p->valueOfText_); | ||||
| 
 | ||||
|          textLength -= 2; | ||||
|       } | ||||
|    } | ||||
| 
 | ||||
|    if (blockValid && textLength < 0) | ||||
|    { | ||||
|       BOOST_LOG_TRIVIAL(warning) | ||||
|          << logPrefix_ << "Too few bytes in block: " << p->lengthOfBlock_; | ||||
|       blockValid = false; | ||||
|    } | ||||
| 
 | ||||
|    if (blockValid) | ||||
|    { | ||||
|       p->characters_.resize(textLength); | ||||
|       is.read(reinterpret_cast<char*>(p->characters_.data()), textLength); | ||||
|    } | ||||
| 
 | ||||
|    std::streampos isEnd = is.tellg(); | ||||
| 
 | ||||
|    if (!ValidateMessage(is, isEnd - isBegin)) | ||||
|    { | ||||
|       blockValid = false; | ||||
|    } | ||||
| 
 | ||||
|    return blockValid; | ||||
|  |  | |||
|  | @ -80,28 +80,24 @@ bool UnlinkedContourVectorPacket::Parse(std::istream& is) | |||
| 
 | ||||
|    // The number of vectors is equal to the size divided by the number of bytes
 | ||||
|    // in a vector
 | ||||
|    int     vectorCount = vectorSize / 8; | ||||
|    int16_t beginI; | ||||
|    int16_t beginJ; | ||||
|    int16_t endI; | ||||
|    int16_t endJ; | ||||
|    int vectorCount = vectorSize / 8; | ||||
| 
 | ||||
|    p->beginI_.resize(vectorCount); | ||||
|    p->beginJ_.resize(vectorCount); | ||||
|    p->endI_.resize(vectorCount); | ||||
|    p->endJ_.resize(vectorCount); | ||||
| 
 | ||||
|    for (int v = 0; v < vectorCount && !is.eof(); v++) | ||||
|    { | ||||
|       is.read(reinterpret_cast<char*>(&beginI), 2); | ||||
|       is.read(reinterpret_cast<char*>(&beginJ), 2); | ||||
|       is.read(reinterpret_cast<char*>(&endI), 2); | ||||
|       is.read(reinterpret_cast<char*>(&endJ), 2); | ||||
|       is.read(reinterpret_cast<char*>(&p->beginI_[v]), 2); | ||||
|       is.read(reinterpret_cast<char*>(&p->beginJ_[v]), 2); | ||||
|       is.read(reinterpret_cast<char*>(&p->endI_[v]), 2); | ||||
|       is.read(reinterpret_cast<char*>(&p->endJ_[v]), 2); | ||||
| 
 | ||||
|       beginI = ntohs(beginI); | ||||
|       beginJ = ntohs(beginJ); | ||||
|       endI   = ntohs(endI); | ||||
|       endJ   = ntohs(endJ); | ||||
| 
 | ||||
|       p->beginI_.push_back(beginI); | ||||
|       p->beginJ_.push_back(beginJ); | ||||
|       p->endI_.push_back(endI); | ||||
|       p->endJ_.push_back(endJ); | ||||
|       p->beginI_[v] = ntohs(p->beginI_[v]); | ||||
|       p->beginJ_[v] = ntohs(p->beginJ_[v]); | ||||
|       p->endI_[v]   = ntohs(p->endI_[v]); | ||||
|       p->endJ_[v]   = ntohs(p->endJ_[v]); | ||||
|    } | ||||
| 
 | ||||
|    if (is.eof()) | ||||
|  |  | |||
|  | @ -105,28 +105,24 @@ bool UnlinkedVectorPacket::Parse(std::istream& is) | |||
| 
 | ||||
|    // The number of vectors is equal to the size divided by the number of bytes
 | ||||
|    // in a vector
 | ||||
|    int     vectorCount = vectorSize / 8; | ||||
|    int16_t beginI; | ||||
|    int16_t beginJ; | ||||
|    int16_t endI; | ||||
|    int16_t endJ; | ||||
|    int vectorCount = vectorSize / 8; | ||||
| 
 | ||||
|    p->beginI_.resize(vectorCount); | ||||
|    p->beginJ_.resize(vectorCount); | ||||
|    p->endI_.resize(vectorCount); | ||||
|    p->endJ_.resize(vectorCount); | ||||
| 
 | ||||
|    for (int v = 0; v < vectorCount && !is.eof(); v++) | ||||
|    { | ||||
|       is.read(reinterpret_cast<char*>(&beginI), 2); | ||||
|       is.read(reinterpret_cast<char*>(&beginJ), 2); | ||||
|       is.read(reinterpret_cast<char*>(&endI), 2); | ||||
|       is.read(reinterpret_cast<char*>(&endJ), 2); | ||||
|       is.read(reinterpret_cast<char*>(&p->beginI_[v]), 2); | ||||
|       is.read(reinterpret_cast<char*>(&p->beginJ_[v]), 2); | ||||
|       is.read(reinterpret_cast<char*>(&p->endI_[v]), 2); | ||||
|       is.read(reinterpret_cast<char*>(&p->endJ_[v]), 2); | ||||
| 
 | ||||
|       beginI = ntohs(beginI); | ||||
|       beginJ = ntohs(beginJ); | ||||
|       endI   = ntohs(endI); | ||||
|       endJ   = ntohs(endJ); | ||||
| 
 | ||||
|       p->beginI_.push_back(beginI); | ||||
|       p->beginJ_.push_back(beginJ); | ||||
|       p->endI_.push_back(endI); | ||||
|       p->endJ_.push_back(endJ); | ||||
|       p->beginI_[v] = ntohs(p->beginI_[v]); | ||||
|       p->beginJ_[v] = ntohs(p->beginJ_[v]); | ||||
|       p->endI_[v]   = ntohs(p->endI_[v]); | ||||
|       p->endJ_[v]   = ntohs(p->endJ_[v]); | ||||
|    } | ||||
| 
 | ||||
|    if (is.eof()) | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Dan Paulat
						Dan Paulat