Remoting strange phenomenon. If the method is not returned for a long time, no exception is reported.

Source: Internet
Author: User

Function: download an object.

Method: Call the remoting method to obtain the byte array of the entire file and write it to the local file.

Code:

Public static bool downloadfromtestpool (string pfilename, string plocalfullpath)
{
Bool isok = true;
System. Io. filestream write = NULL;
Try
{
System. Io. fileinfo file = new fileinfo (plocalfullpath );
If (! File. Directory. exists)
File. Directory. Create ();

Byte [] buffer = factory. messionfacade. downfromtestpool (pfilename); // factory. messionfacade is the remoting remote proxy
Write = new filestream (plocalfullpath, filemode. Create );
Write. Write (buffer, 0, buffer. Length );
}
Catch (exception ex)
{
Isok = false;
Generalmethod. handleexception (Ex );
}
Finally
{
If (write! = NULL)
Write. Close ();
}
Return isok;
}

The preceding method is correct for multiple times. However, there has been a strange phenomenon recently. When downloading multiple files consecutively, it is generally in the third file. The following line of code will not return for a long time and will not throw an exception, which is quite helpless.

Byte [] buffer = factory. messionfacade. downfromtestpool (pfilename); // factory. messionfacade is the remoting remote proxy

I wonder if the byte array is too large at a time, but is there a problem in some public network situations? Instead, you can use resumable download to perform multiple downloads. Because the method does not return for a long time and there is no exception, we have to use the asynchronous timeout method to detect this situation and re-call the remoting method.

Breakpoint asynchronous Reconnection

Public class syncdownloader
{

Public Delegate byte [] downloadbyteshandle (string pfilename, long pposition, int pmaxsize );

System. Io. filestream write = NULL;
Long Pos = 0;
Int lastreceivesize = 0;
Int bufferversion = 0;
Syncdownloaditem curdown;
Public syncdownloader ()
{

}
Private manualresetevent timeoutobject = new manualresetevent (false );

Public bool startdownload (string filename, string localfullpath)
{
Int buffersize = 50000; // The default 25000 K 12500 6000 3000 1500
Bufferversion = 0;
Pos = 0;
Lastreceivesize = buffersize; // The default value is the maximum value.
Bool isok = false;
Int timeouttimes = 0; // times out 20 times to exit the download.

Try
{
System. Io. fileinfo file = new fileinfo (localfullpath );
If (! File. Directory. exists)
File. Directory. Create ();
Write = new filestream (localfullpath, filemode. Create );
While (true)
{
Timeoutobject. Reset ();
If (lastreceivesize <buffersize)
Break;
Bufferversion ++;

// Asynchronous download
Downloadbyteshandle FH = new downloadbyteshandle (this. dwonloadbytes );
Asynccallback callback = new asynccallback (this. downloadcallback );
Iasyncresult AR = FH. begininvoke (filename, POs, buffersize, callback, bufferversion );

If (! Timeoutobject. waitone (10000) // download completed in 10 seconds
{
Timeouttimes ++;
Console. Write ("_ {0}", timeouttimes); // time-out underline
If (timeouttimes> 20)
Break;
// Timeout
If (buffersize> 3000) // you can download 1.5 K for 10 seconds.
Buffersize = buffersize/2; // the size of the expired download is halved.
}
Else
{
Byte [] buffer = FH. endinvoke (AR );
Console. Write ("."); // Add more
Write. Write (buffer, 0, buffer. Length );
Pos + = buffer. length;
Lastreceivesize = buffer. length;
}
}
Isok = true;
}
Catch (exception ex)
{
Isok = false;
Generalmethod. handleexception (Ex );
}
Finally
{
If (write! = NULL)
Write. Close ();
}
Return isok;
}
Public void downloadcallback (iasyncresult AR)
{
Int dbver = convert. toint32 (AR. asyncstate );
If (dbver = This. bufferversion) // release only when called
Timeoutobject. Set ();
}

Public byte [] dwonloadbytes (string pfilename, long pposition, int pmaxsize)
{
// System. Threading. thread. Sleep (11000); // sleeps for 11 seconds. Determine whether execution is performed again.
Byte [] buffer = NULL;
Try
{
Buffer = factory. areaissuefacade. downfromtestpool (pfilename, pposition, pmaxsize );
}
Catch {} // ignore all errors
Return buffer;
}
}

 

Use the above Code to download four files. The following are process logs. Each vertex indicates a successful call, an underline indicates a timeout, And a number after the underline indicates the number of timeouts.

Start to download files from the Publishing Server ..
Download his. mattgr. UI. dll... download from the publisher
Download his. mattgr. Services. dll ...... download from the publisher successfully
Download his. mattgr. interface. dll_1_2_3 ...... download from the publisher successful
Download his. publicmattgr. Services. dll... download from the publisher

Each log shows that there are three consecutive call times out in the beginning of the third file, and then it will be very fast. Lucky, such a stable exception.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.