public void sendHttpTask(SSHttpTask task) { lock (_locker) { sendingQueue.Enqueue(task); // We must pulse because we're Monitor.Pulse(_locker); // changing a blocking condition. } }
//网络下载任务 private void RunDownloadTask(SSHttpTask task) { bool failure = httpClient.doDownload(task.request, task.filePath); if (!failure) { AsyncTask.QueueOnMainThread(() => { task.handleMainThreadCompleted(); }); } else { AsyncTask.QueueOnMainThread(() => { task.handleErrorOcurr(); }); } }
private void Run() { // Keep consuming until told otherwise. while (Loop) { SSHttpTask task = getHttpTask(); if (task != null && task.request != null) { if (task.reqType == SSTaskType.Common) { RunCommon(task); } else if (task.reqType == SSTaskType.Download) { RunDownloadTask(task); } } } }
private void RunCommon(SSHttpTask task) { string acknowlege = httpClient.doRequest(task.request); //Check if we need response or not. if (!string.IsNullOrEmpty(acknowlege)) { task.response = acknowlege.Trim(); AsyncTask.QueueOnMainThread(() => { task.handleMainThreadCompleted(); }); } else { // error ocurr task.response = UNABLE_GET_RESPONSE; AsyncTask.QueueOnMainThread(() => { task.handleErrorOcurr(); }); } }
// void http_onReceieve (SSHttpTask task, string acknowlege) // { // if (task.request is HttpRequest) { // if (Utils.checkJsonFormat (acknowlege)) { // HttpResponseFactory.createResponse (task, acknowlege); // AsyncTask.QueueOnMainThread( () => { task.handleMainThreadCompleted(); } ); // // // } else // { // http_onException (task, acknowlege); // } // // } else if (task.request is ThirdPartyHttpRequest) { // task.response = new ThirdPartyResponse (acknowlege); // AsyncTask.QueueOnMainThread( () => { task.handleMainThreadCompleted(); } ); // // // } else if (task.request is HttpDownloadRequest) { // // task.response = new HttpDownloadResponse (); // AsyncTask.QueueOnMainThread( () => { task.handleMainThreadCompleted(); } ); // // } // // } // All http error will go through this routine. // Handle exception void http_onException(SSHttpTask task, string message) { task.response = message; AsyncTask.QueueOnMainThread(() => { task.handleErrorOcurr(); }); }