// assembleMessage is called when the length of a message is greater than the currently set chunkSize.
// It returns the final payload of the message assembled from multiple chunks.
assembleMessage is called when the length of a message is greater than the currently set chunkSize.
It returns the final payload of the message assembled from multiple chunks.
// assembleMessage is called when the length of a message is greater than the currently set chunkSize . // It returns the final payload of the message assembled from multiple chunks .
func (chunkHandler *ChunkHandler) assembleMessage(messageLength uint32) (payload []byte, n int, err error) {
	payload = make([]byte, messageLength)
	n, err = io.ReadFull(chunkHandler.socketr, payload[:chunkHandler.inChunkSize])
	offset := chunkHandler.inChunkSize
	if err != nil {
		return payload, n, err
	}
	for offset < messageLength {
		_, r, err := chunkHandler.ReadChunkHeader()
		n += r
		if err != nil {
			return payload, n, err
		}
		if offset+chunkHandler.inChunkSize < messageLength {
			r, err := io.ReadFull(chunkHandler.socketr, payload[offset:offset+chunkHandler.inChunkSize])
			n += r
			if err != nil {
				return payload, n, err
			}
			offset += chunkHandler.inChunkSize
		} else {
			remainingBytes := messageLength - offset
			r, err := io.ReadFull(chunkHandler.socketr, payload[offset:offset+remainingBytes])
			n += r
			if err != nil {
				return payload, n, err
			}
			offset += remainingBytes
		}
	}
	return payload, n, err
}
func ( chunkHandler * ChunkHandler ) assembleMessage ( messageLength uint32 ) ( payload [ ] byte , n int , err error ) { payload = make ( [ ] byte , messageLength ) n , err = io . ReadFull ( chunkHandler . socketr , payload [ : chunkHandler . inChunkSize ] ) offset := chunkHandler . inChunkSize if err != nil { return payload , n , err } for offset < messageLength { _ , r , err := chunkHandler . ReadChunkHeader ( ) n += r if err != nil { return payload , n , err } if offset + chunkHandler . inChunkSize < messageLength { r , err := io . ReadFull ( chunkHandler . socketr , payload [ offset : offset + chunkHandler . inChunkSize ] ) n += r if err != nil { return payload , n , err } offset += chunkHandler . inChunkSize } else { remainingBytes := messageLength - offset r , err := io . ReadFull ( chunkHandler . socketr , payload [ offset : offset + remainingBytes ] ) n += r if err != nil { return payload , n , err } offset += remainingBytes } } return payload , n , err }
assembleMessage is called when the length of a message is greater than the currently set chunkSize.
assembleMessage is called when the length of a message is greater than the currently set chunkSize .
//fmt.Println("assembling message...") // Read the initial chunk data that was sent with the first chunk header // While there are still more bytes to read // Read the next chunks (header + data) until we complete our message //fmt.Printf("assembling message, encountered chunk type: %d, message type: %d\n", h.BasicHeader.FMT, h.MessageHeader.MessageTypeID) // If this chunk is still not the end of the message, then read the whole chunk // If this is the last chunk of the message, just read the remaining bytes