Ejemplo n.º 1
0
		WWW GetForm (MiniTuple<string, HttpType, object> req)
		{
			WWW www;
			if (req.field1 == HttpType.POST) {
				if (m_form == null)
				{
					LogMgr.LogError ("Form is Null May Cause Erros while Use Http Post!");
				}
				www = new WWW (req.field0, m_form);
			} 
			else 
			{

				www = new WWW (req.field0.Split(';')[0]);
			}

			return www;
		}
Ejemplo n.º 2
0
		void Read(Stream DataIntStream,Stream DataOutStream,MiniTuple<string, int, object> req,long oldSize,long totalSize,Action<byte[], float, bool> callback)
		{
			bool bvalue = false;
			try
			{
				byte[] bys = new byte[req.field1];
				int readLen = DataOutStream.Read(bys, 0, bys.Length);
				int maxLen = readLen;
				int total = readLen;
				while (readLen > 0  )
				{
					if(DownLoadPause)
						continue;
					
					if (DataIntStream != null)
					{
						DataIntStream.Write(bys, 0, readLen);
					}
					bvalue = total == totalSize;
					if (callback != null)
					{
						float value = (float)(oldSize + total) / (float)(totalSize+ oldSize);
						callback(bys, value, bvalue);
					}
					readLen = DataOutStream.Read(bys, 0, bys.Length);
					
					total += readLen;
					
					maxLen = Math.Max(readLen, maxLen);
				}

				bvalue = true;
			}
			catch (Exception ex)
			{
				if (m_OthersErrorEvent != null)
				{
					m_OthersErrorEvent(ex);
				}
				if (callback != null)
                {
                    callback(null, 0, true);
                }
            }
            
        }
Ejemplo n.º 3
0
        void ThreadDownLoad()
        {
			MiniTuple<string, HttpType, object> curRequest = new MiniTuple<string, HttpType, object>();

			bool isEmpty =false;
			lock(m_lock)
			{
				if(requestList.Count >0)
				{
					curRequest  =requestList.Pop();
				}
				else
					isEmpty =true;
			}
			if(isEmpty)
				return;

			HttpWebRequest request = null;
			HttpWebResponse response = null;
			HttpType curHttpType = curRequest.field1;
            //path,flag,limitspeed,ACTION
			var req = (MiniTuple<string, int, object>)curRequest.field2;
			Stream DataStream = null ;
			try
			{

				long oldSize =0;
				if(curHttpType == HttpType.DOWNLOADFILE_TOMEMORY)
				{
					DataStream = new MemoryStream();
				}
				else if (curHttpType == HttpType.DOWNLOADFILE)
				{
					DataStream = new FileStream(req.field0, FileMode.OpenOrCreate, FileAccess.ReadWrite);
					oldSize= DataStream.Length;
				}
				else
				{
					LogMgr.LogError("Http Download State Error");
					return;
				}

				Action<byte[], float, bool> callback = (Action<byte[], float, bool>)req.field2;
				//req
				
				request= (HttpWebRequest)HttpWebRequest.Create(curRequest.field0);
				request.AddRange("bytes",(int)oldSize);
				request.Timeout = Config.mIns.Http_TimeOut;
				request.ReadWriteTimeout = 20 * 1000;
				request.KeepAlive = false;
				request.AllowWriteStreamBuffering = false;
				request.AllowAutoRedirect = true;
				request.AutomaticDecompression = DecompressionMethods.None;
                
                response = (HttpWebResponse)request.GetResponse();

				if (response.Headers["Content-Range"] == null)
				{
					oldSize = 0;
				}
				//long to float   may cause error
				long totalSize = response.ContentLength + oldSize;

				using ( Stream stream = response.GetResponseStream())
				{
					if(curHttpType == HttpType.DOWNLOADFILE)
					{
						if (DataStream != null && oldSize > 0)
						{
							DataStream.Seek(0, SeekOrigin.End);
						}
						
                        Read(DataStream,stream,req,oldSize,response.ContentLength,callback);
					}
					else if(curHttpType == HttpType.DOWNLOADFILE_TOMEMORY)
					{
						
						Read(DataStream,stream,req,oldSize,response.ContentLength,delegate(byte[] arg1, float arg2, bool arg3) {
							if(!arg3)
							{
								callback(arg1,arg2,arg3);
							}
							else
							{
								byte[] totalBys = new byte[DataStream.Length];
								DataStream.Read(totalBys,0,totalBys.Length);
								callback(totalBys,(float)totalBys.Length /(float)totalSize,arg3);
							}
						});
						
					}
				}

				if(DataStream != null)
					DataStream.Close();

				System.GC.Collect();
				
				
			}
			catch (WebException ex)
			{
				DealWithEx(ex);
				
			}
			finally
			{

				if(request != null)
				{
					request.Abort();
				}

				if(response != null)
				{
					response.Close();
				}

				if(DataStream != null)
				{
					DataStream.Close();
				}

				System.GC.Collect();
			}

        }
Ejemplo n.º 4
0
        void HttpThread()
        {
			MiniTuple<string, HttpType, object> curRequest = new MiniTuple<string, HttpType, object>();
			bool isEmpty =false;
			lock(m_lock)
			{
				if(requestList.Count >0)
				{
					curRequest  =requestList.Pop();
				}
				else
					isEmpty =true;
			}
            if (isEmpty)
            {
                LogMgr.LogError("Is Empty");
                return;
            }

            int trytimes = 2;
            MiniTuple<string, System.Action<string>> temp = (MiniTuple<string, System.Action<string>>)curRequest.field2;
            string strRequest = temp.field0;
            HttpWebRequest req = null;
            HttpWebResponse Response = null;
            Action postevent = delegate()
            {
				if(req != null)
				{
					req.Abort();
				}


                if (curRequest.field1 == HttpType.POST)
                {
					req = (HttpWebRequest)WebRequest.Create(curRequest.field0);
					req.Timeout = Config.mIns.Http_TimeOut;
                    req.Method = "POST";
					req.Accept ="text/html, application/xhtml+xml, */*";

					byte[] btBodys = System.Text.Encoding.UTF8.GetBytes(strRequest);
					
					if ( btBodys.Length >0)
					{
						req.ContentLength = btBodys.Length;
						using (Stream respSb = req.GetRequestStream())
						{
							
							respSb.Write(btBodys, 0, btBodys.Length);
						}
					}
                }
                else if (curRequest.field1 == HttpType.GET)
                {

					req = (HttpWebRequest)WebRequest.Create(curRequest.field0 +"?"+strRequest);
                    req.Timeout = Config.mIns.Http_TimeOut;
					req.ContentType = "application/x-www-form-urlencoded,application/json";
					req.Accept ="*/*";
                    req.Method = "GET";
                }

//				LogMgr.Log("HttpWebRequest -1");

                using (Response = (HttpWebResponse)req.GetResponse())
                {
//					LogMgr.Log("HttpWebRequest -2");
                    var sr = new StreamReader(Response.GetResponseStream());
//					LogMgr.Log("HttpWebRequest -3");
                    string responseContent = sr.ReadToEnd();
//					LogMgr.Log("HttpWebRequest -4");
                    if (temp.field1 != null)
                    {
                        temp.field1(responseContent);
                    }
//					LogMgr.Log("HttpWebRequest 5");
//                    LogMgr.Log("收到请求");
                    trytimes = 0;
                    req.Abort();
                    sr.Close();
                }
            };
			Action httpCall;

			httpCall = delegate() {
				try
				{
					postevent();
//					LogMgr.Log("HttpWebRequest 6");
				}
				catch (Exception webEx)
				{
					LogMgr.LogError(webEx);
					if (webEx is WebException)
					{
						var ex = webEx as WebException;
						if (ex.Status == WebExceptionStatus.Timeout && trytimes > 0)
						{
							trytimes--;
							
							httpCall();
						}
						else
						{
							DealWithEx(webEx);
						}
						
					}
					else
						DealWithEx(webEx);
					
					
				}
				finally
				{
					if (req != null)
					{
						req.Abort();
					}
					
					if (Response != null)
					{
						Response.Close();
					}
					
				}
			};

			httpCall();

        }
Ejemplo n.º 5
0
        public void StartConnect()
        {
#if USE_COR

			if (!m_task.Running) 
			{

				m_task.Start ();
			}
			else
			{
				LogMgr.Log ("Http Coroutine is Runing");
			}
#else
            try
            {


                HttpType m_state = m_curRequest.field1;
                if (m_state == HttpType.GET || m_state == HttpType.POST)
                {

					MiniTuple<string, System.Action<string>> value = new MiniTuple<string, System.Action<string>>();
                    value.field0 = m_requestSR.ToString();
                    value.field1 = m_SuccessEvent;
                    m_curRequest.field2 = value;

                    m_requestSR.Length = 0;
                    requestList.Push(m_curRequest);

                    m_thread.Start();
                }
                else if (m_state == HttpType.DOWNLOADFILE || m_state == HttpType.DOWNLOADFILE_TOMEMORY)
                {
                    file.field2 = onProcess;
                    m_curRequest.field2 = file;
                    requestList.Push(m_curRequest);
                    m_thread.Start();
                }

            }
            catch (Exception ex)
            {
				if(m_OthersErrorEvent != null)
				{
					m_OthersErrorEvent(ex);
				}
            }

#endif
        }
Ejemplo n.º 6
0
        public HttpUnit()
        {

			requestList = new Stack<MiniTuple<string, HttpType, object>>();
			this.m_curRequest = new MiniTuple<string, HttpType, object>();
			ErrorManager.mIns.Register<HttpUnit>(this);
			System.Net.ServicePointManager.DefaultConnectionLimit=20;
#if USE_COR
#else
			this.m_lock = new object();
			this.file = new MiniTuple<string, int, object>();

            this.file.field1 = Config.mIns.HttpSpeedLimit;

            this.m_requestSR = new StringBuilder(1024);

            this.m_varUtils = new MiniTuple<short, int>();
            this.m_varUtils.field0 = 0;
            this.m_varUtils.field1 = Config.mIns.HttpSpeedLimit;
#endif
        }