public void Close(HTTP_OP_RET ret, int httpCode, string msg) { this.m_opRet = ret; this.m_httpStatusCode = httpCode; this.m_msg = msg; /* * switch (ret) { * case HTTP_OP_RET.succ: * break; * case HTTP_OP_RET.failed: * break; * case HTTP_OP_RET.time_out: * break; * case HTTP_OP_RET.unknown: * break; * } */ if (m_responseStream != null) { m_responseStream.Close(); m_responseStream = null; } if (m_reponse != null) { m_reponse.Close(); m_reponse = null; } if (m_request != null) { m_request.Abort(); m_request = null; } this.m_buffer = null; m_OPComplete = true; }
public void Start() { m_alive = true; m_OPComplete = false; m_opRet = HTTP_OP_RET.unknown; Uri uri = new Uri(m_url); try { m_opStep = HTTP_OP_STEP.connecting; m_request = (HttpWebRequest)WebRequest.Create(uri); m_request.AllowAutoRedirect = false; IAsyncResult iar = (IAsyncResult)m_request.BeginGetResponse(new AsyncCallback(GetResponseCallback), this); ThreadPool.RegisterWaitForSingleObject(iar.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), this, CONNTECTTIMEOUT, true); } catch (Exception e) { Close(HTTP_OP_RET.failed, -1, e.ToString()); } }
public void Update() { if (!m_alive) { return; } if (m_OPComplete) { if (m_opRet == HTTP_OP_RET.succ) { m_fileBuffStream.Position = 0; string md5Code = UpdateUtils.Md5(this.m_fileBuffStream); m_fileBuffStream.Position = 0; if (!Save(m_fileBuffStream, m_filePath)) { this.m_opRet = HTTP_OP_RET.failed; this.m_httpStatusCode = -1; this.m_msg = "Save File Failed"; return; } if (null != this.m_onProgressHandler) { m_onProgressHandler(this.m_url, this.m_filePath, this.m_downloadSize, this.m_totalSize); } if (null != m_onSuccHandler) { m_onSuccHandler(m_url, m_filePath, (int)m_totalSize, md5Code); } } else { if (m_redirectURL != null) { if (null != this.m_onRedirectHandler) { m_onRedirectHandler(m_url, m_filePath, m_redirectURL, m_httpStatusCode, m_msg); } } else { if (null != this.m_onErrorHandler) { m_onErrorHandler(m_url, m_filePath, m_httpStatusCode, m_msg); } } } m_alive = false; } else { if (m_opStep == HTTP_OP_STEP.downloading) { m_updateDeltaTime += Time.deltaTime; if (m_updateDeltaTime > 0.5) { m_updateDeltaTime = 0; if (null != this.m_onProgressHandler) { m_onProgressHandler(this.m_url, this.m_filePath, this.m_downloadSize, this.m_totalSize); } } } } }