multithreaded download resources on a server

Source: Internet
Author: User

1. Code implementation

Using Unityengine;
Using System.Collections;
Using System;
Using System.Text;
Using System.Net;
Using System.IO;


Internal class Webreqstate
{
Public byte[] Buffer;

Public FileStream FS;

public const int buffersize = 1024;

Public Stream Orginalstream;

Public HttpWebResponse WebResponse;

Public webreqstate (String path)
{
Buffer = new byte[1024];
FS = new FileStream (path, filemode.create);
}

}

public class Httphelper
{

string path = null;
String Assetname;
Public Httphelper (String path)
{
This.path = path;
}

public void asydownload (string url)
{
Debug.Log ("url is:+" +url);
Assetname = URL. Split ('/') [4];
Debug.Log ("Assetname is: +" +assetname);
HttpWebRequest HttpRequest = webrequest.create (URL) as HttpWebRequest;
Httprequest.begingetresponse (New AsyncCallback (Responsecallback), HttpRequest);
}

void Responsecallback (IAsyncResult ar)
{
HttpWebRequest req = ar. AsyncState as HttpWebRequest;
if (req = = null) return;
HttpWebResponse response = req. EndGetResponse (AR) as HttpWebResponse;
if (response. StatusCode! = Httpstatuscode.ok)
{
Response. Close ();
Return
}
Debug.Log (path + "/" + assetname);
Webreqstate st = new Webreqstate (path + "/" + assetname);
St. WebResponse = response;
Stream Responsestream = Response. GetResponseStream ();
St.orginalstream = Responsestream;
Responsestream.beginread (St. Buffer, 0, Webreqstate.buffersize, new AsyncCallback (Readdatacallback), ST);
}

void Readdatacallback (IAsyncResult ar)
{
Webreqstate rs = ar. AsyncState as Webreqstate;
int read = Rs.OrginalStream.EndRead (AR);
if (Read > 0)
{
Rs.fs.Write (Rs. Buffer, 0, read);
Rs.fs.Flush ();
Rs.OrginalStream.BeginRead (Rs. Buffer, 0, Webreqstate.buffersize, new AsyncCallback (Readdatacallback), RS);
}
Else
{
Rs.fs.Close ();
Rs.OrginalStream.Close ();
Rs. WebResponse.Close ();
Debug.Log (Assetname + "::: Success");
}
}
}

2. Download resources from server to local (PC side tested successfully)

void Ongui ()
{
string[] str = new String[2] {"Sphere", "Cube"};

Download
if (GUI. button (new Rect (10, 10, 200, 45), "Download"))
{
String rootpath = Application.persistentdatapath;//android saved to the/storage/sdcard0/android/data/package name ( Example: Com.example.test)/files
for (int i = 0; i < str. Length; i++)//str is a string array that holds some assetbundle names
{
Thread thread = new Thread (new Parameterizedthreadstart (Downasset)); Parameterizedthreadstart multi-line Path parameter
Thread. Start (RootPath + "|" + Str[i]); With only one object argument, so use string concatenation
}
}

void Downasset (System.Object file)
{
string[] FileName = file. ToString (). Split (' | ');
Debug.Log ("filename 000000 is: +" + filename[0]);
Debug.Log ("filename is 1111:+" + filename[1]);
Httphelper help = new Httphelper (filename[0]);
Help. Asydownload ("http://192.168.0.98/MyItems/Test/" + filename[1] + ". Assetbundle");//Note test the IP address on the phone
Debug.Log ("Download address is: +" + "http://192.168.0.98/MyItems/Test/" + filename[1] + ". Assetbundle");

}

multithreaded download resources on a server

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.