Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 04d1d6b

Browse files
committed
fix
1 parent e6e75c7 commit 04d1d6b

File tree

4 files changed

+91
-14
lines changed

4 files changed

+91
-14
lines changed

demo/Program.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class MainClass
1111
public static void Main (string[] args)
1212
{
1313
// 初始化qiniu配置,主要是API Keys
14-
qiniu.Config.ACCESS_KEY = "IT9iP3J9wdXXYsT1p8ns0gWD-CQOdLvIQuyE0FOi";
14+
qiniu.Config.ACCESS_KEY = "IT9iP3J9wdXXYsT1p8ns0gWD-CQOdLvIQuyE0FOk";
1515
qiniu.Config.SECRET_KEY = "zUCzekBtEqTZ4-WJPCGlBrr2PeyYxsYn98LPaivM";
1616

1717
/**********************************************************************
@@ -26,7 +26,7 @@ public static void Main (string[] args)
2626
//======================================================================
2727
{
2828
QiniuFile qfile = new QiniuFile (bucket, qiniukey, localfile);
29-
ResumbleUploadEx puttedCtx = new ResumbleUploadEx (localfile);
29+
// ResumbleUploadEx puttedCtx = new ResumbleUploadEx (localfile);
3030
ManualResetEvent done = new ManualResetEvent (false);
3131
qfile.UploadCompleted += (sender, e) => {
3232
Console.WriteLine (e.key);
@@ -35,16 +35,17 @@ public static void Main (string[] args)
3535
};
3636
qfile.UploadFailed += (sender, e) => {
3737
Console.WriteLine (e.Error.ToString ());
38-
puttedCtx.Save();
38+
// puttedCtx.Save();
3939
done.Set ();
4040
};
41+
4142
qfile.UploadProgressChanged += (sender, e) => {
4243
int percentage = (int)(100 * e.BytesSent / e.TotalBytes);
4344
Console.Write (percentage);
4445
};
4546
qfile.UploadBlockCompleted += (sender, e) => {
46-
puttedCtx.Add(e.Index,e.Ctx);
47-
puttedCtx.Save();
47+
// puttedCtx.Add(e.Index,e.Ctx);
48+
// puttedCtx.Save();
4849
};
4950
qfile.UploadBlockFailed += (sender, e) => {
5051
//
@@ -53,12 +54,14 @@ public static void Main (string[] args)
5354

5455
//上传为异步操作
5556
//上传本地文件到七牛云存储
56-
qfile.Upload (puttedCtx.PuttedCtx);
57+
// qfile.Upload (puttedCtx.PuttedCtx);
58+
qfile.Upload ();
5759
done.WaitOne ();
5860
}
5961

6062
//======================================================================
6163
{
64+
/*
6265
6366
try {
6467
QiniuFile qfile = new QiniuFile (bucket, qiniukey);
@@ -72,6 +75,7 @@ public static void Main (string[] args)
7275
Console.WriteLine (e.Error.HttpCode);
7376
Console.WriteLine (e.Error.ToString ());
7477
}
78+
*/
7579
}
7680
}
7781
}

sdk/QiniuFile.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,26 +278,32 @@ private void uploadBigFile(string token,BlkputRet[] puttedBlk=null){
278278
}
279279
fs.Seek ((long)i * BLOCKSIZE, SeekOrigin.Begin);
280280
fs.Read (buf, 0, readLen);
281-
using (QiniuWebClient client = new QiniuWebClient ()) {
282-
ManualResetEvent done = new ManualResetEvent (false);
281+
using (QiniuWebClient client = new QiniuWebClient (10000.0)) {
282+
bool failed = false;
283283
client.UploadDataCompleted += (sender, e) => {
284284
if (e.Error != null) {
285285
onQiniuUploadBlockFailed (new QiniuUploadBlockFailedEventArgs (i, e.Error));
286+
failed =true;
286287
return;
287288
} else {
288289
blkRets [i] = GetBlkPutRet (e.Result);
289290
onQiniuUploadBlockCompleted (new QiniuUploadBlockCompletedEventArgs (i, blkRets [i]));
290291
}
291-
done.Set ();
292292
};
293293
client.UploadProgressChanged += (sender, e) => {
294294
onQiniuUploadProgressChanged (new QiniuUploadProgressChangedEventArgs (totalSent + e.BytesReceived, finfo.Length));
295295
};
296+
client.Timeout+= (sender, e) => {
297+
onQiniuUploadBlockFailed(new QiniuUploadBlockFailedEventArgs(i,new Exception("QiniuWebClient Timeout.")));
298+
failed = true;
299+
};
296300
client.UpToken = token;
297301
client.Headers.Add("Content-Type", "application/octet-stream");
298302
string url = string.Format("{0}/mkblk/{1}", Config.UP_HOST, readLen);
299-
client.UploadDataAsync (new Uri (url), "POST", buf);
300-
done.WaitOne ();
303+
client.iUploadDataAsync ( url, "POST", buf);
304+
if(failed){
305+
return;
306+
}
301307
totalSent += readLen;
302308
}
303309
}

sdk/QiniuResumbleUploadEx.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public ResumbleUploadEx (string filename, string puttedCtxDir=null){
5959
this.puttedCtxFileName = Path.Combine (this.puttedCtxDir, ctxfile);
6060
if (!File.Exists (this.puttedCtxFileName)) {
6161
File.Open (this.puttedCtxFileName, FileMode.Create);
62+
this.puttedCtx = new Dictionary<int, BlkputRet> ();
6263
} else {
6364
this.puttedCtx = initloadPuttedCtx ();
6465
}

sdk/QiniuWebClient.cs

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.Timers;
3+
using System.Threading;
24
using System.IO;
35
using System.Collections.Specialized;
46
using System.Net;
@@ -7,22 +9,57 @@ namespace qiniu
79
{
810
public class QiniuWebClient:WebClient
911
{
12+
public event EventHandler<EventArgs> Timeout;
13+
/// <summary>
14+
/// Ons the timeout.
15+
/// </summary>
16+
protected virtual void onTimeout(){
17+
if (this.Timeout != null) {
18+
this.Timeout (this,new EventArgs());
19+
}
20+
}
21+
22+
private bool isUploading = true;
23+
private object isUploadinglocker = new object ();
24+
25+
ManualResetEvent done = new ManualResetEvent (false);
26+
27+
System.Timers.Timer timer;
28+
1029
private string uptoken;
1130

1231
/// <summary>
1332
/// Gets or sets upload token.
1433
/// </summary>
1534
/// <value>Up token.</value>
1635
public string UpToken {
17-
get {
18-
return uptoken;
19-
}
36+
get { return uptoken; }
2037
set {
2138
uptoken = value;
2239
this.Headers.Add ("Authorization", "UpToken " + this.uptoken);
2340
}
2441
}
2542

43+
/// <summary>
44+
/// Initializes a new instance of the <see cref="qiniu.QiniuWebClient"/> class.
45+
/// </summary>
46+
/// <param name="timeout">Timeout.</param>
47+
public QiniuWebClient(double timeout = 100000.0){
48+
49+
this.timer = new System.Timers.Timer (timeout);
50+
this.timer.Elapsed+= (object sender, ElapsedEventArgs e) => {
51+
if(!isUploading){
52+
onTimeout();
53+
this.timer.Stop();
54+
done.Set();
55+
return;
56+
}
57+
lock(isUploadinglocker){
58+
isUploading = false;
59+
}
60+
};
61+
}
62+
2663
/// <summary>
2764
/// Posts the form.
2865
/// </summary>
@@ -80,6 +117,35 @@ public string Call(string url,MAC mac){
80117
}
81118
}
82119

120+
protected override void OnUploadDataCompleted (UploadDataCompletedEventArgs e)
121+
{
122+
base.OnUploadDataCompleted (e);
123+
done.Set ();
124+
}
125+
126+
protected override void OnUploadProgressChanged (UploadProgressChangedEventArgs e)
127+
{
128+
lock (isUploadinglocker) {
129+
isUploading = true;
130+
}
131+
base.OnUploadProgressChanged (e);
132+
}
133+
134+
/// <summary>
135+
/// Is the upload data async.
136+
/// </summary>
137+
/// <returns>The upload data async.</returns>
138+
/// <param name="url">URL.</param>
139+
/// <param name="data">Data.</param>
140+
public void iUploadDataAsync(string url,string method,byte[] data){
141+
142+
this.UploadDataAsync (new Uri (url),method, data);
143+
this.timer.Start ();
144+
done.WaitOne ();
145+
return;
146+
}
147+
148+
83149
private Stream GetPostStream (Stream putStream, string fileName, NameValueCollection formData, string boundary)
84150
{
85151
Stream postDataStream = new System.IO.MemoryStream ();

0 commit comments

Comments
 (0)