private BlkputRet bput(WebClient wc, string url, byte[] bdata) { for (int i = 0; i < RETRYTIMES; i++) { try{ byte[] rawBytes = wc.UploadData(url, bdata); BlkputRet putRet = JsonConvert.DeserializeObject <BlkputRet>(Encoding.UTF8.GetString(rawBytes)); if (CRC32.CheckSumBytes(bdata) != putRet.crc32) { if (i == (RETRYTIMES - 1)) { throw new Exception("crc32 check error"); } } else { return(putRet); } }catch (Exception e) { if (i == (RETRYTIMES - 1)) { throw e; } } } return(null); }
private BlkputRet mkblk(string upToken, byte[] blockdata, int blkidx) { int block_size = blockdata.Length; string url = string.Format("{0}/mkblk/{1}", QiniuHosts.UP_HOST, block_size); using (WebClient wc = new WebClient()) { // wc.UploadProgressChanged += new UploadProgressChangedEventHandler (AsynProgressChangedHandler); wc.Headers.Add("Authorization", "UpToken " + upToken); //first chunk length int fcl = CHUNKSIZE < block_size ? CHUNKSIZE : block_size; //first chunk byte[] fc = new byte[fcl]; Array.Copy(blockdata, 0, fc, 0, fcl); BlkputRet putRet = bput(wc, url, fc); this.bytesSent += fcl; onProgressing(this, new UploadProgressEventArgs(this.fileSize, this.bytesSent)); while (putRet.offset < block_size) { url = string.Format("{0}/bput/{1}/{2}", putRet.host, putRet.ctx, putRet.offset); //chunk length int cl = (CHUNKSIZE < (block_size - putRet.offset)) ? CHUNKSIZE : (int)(block_size - putRet.offset); byte[] chunk = new byte[cl]; Array.Copy(blockdata, putRet.offset, chunk, 0, cl); putRet = bput(wc, url, chunk); this.bytesSent += cl; onProgressing(this, new UploadProgressEventArgs(this.fileSize, this.bytesSent)); } return(putRet); } }
private void ResumbleUp(string upToken, string saveKey = null, string mimeType = null, bool crc32 = false, NameValueCollection xVars = null) { //streamReader has checked long fsize = streamReader.Length; int block_cnt = BLOCK_COUNT(fsize); // int chunks = fsize / CHUNKSIZE; BlkputRet [] resumbleUpRets = new BlkputRet[block_cnt]; Parallel.For(0, block_cnt, (i) => { Console.WriteLine("thread id={0}", i); int blocksize = (i + 1) * BLOCKSIZE > fsize ? (int)(fsize - i * BLOCKSIZE) : BLOCKSIZE; byte[] buf = new byte[blocksize]; lock (streamReader) { streamReader.Seek(i * BLOCKBITS, SeekOrigin.Begin); streamReader.Read(buf, 0, blocksize); } resumbleUpRets [i] = mkblk(upToken, buf, i); }); mkfile(upToken, saveKey, fsize, mimeType, ref resumbleUpRets, xVars); }