上一次說到通過管道把接收到的
HTTP
數據通知另一個線程處理,它不是直接發送數據過去,而是把數據在共享內存里的句柄發送過去,達到高效通訊的目的。下面就來分析資源處理進程里,接收到這個消息之后,做些什么處理。這個消息的處理代碼如下:
#001
?
void ResourceDispatcher::OnReceivedData(int request_id,
#002
?????????????????????????????????????????
SharedMemoryHandle shm_handle,
#003
?????????????????????????????????????????
int data_len) {
#004
???
// Acknowlegde the reception of this data.
回應這個消息,說已經收到數據了。
#005
???
IPC::Message::Sender* sender = message_sender();
#006
???
if (sender)
#007
?????
sender->Send(
#008
?????????
new ViewHostMsg_DataReceived_ACK(MSG_ROUTING_NONE, request_id));
#009
?
#010
???
DCHECK((shm_handle && data_len > 0) || (!shm_handle && !data_len));
打開共享內存文件,使用只讀的方式。
#011
???
SharedMemory shared_mem(shm_handle, true);
?
// read only
#012
?
查找到請求下載的資源的請求標識號。
#013
???
PendingRequestList::iterator it = pending_requests_.find(request_id);
如果沒有找到相應的請求標識號,就直接返回,不用處理這些數據。
#014
???
if (it == pending_requests_.end()) {
#015
?????
// this might happen for kill()ed requests on the webkit end, so perhaps
#016
?????
// it shouldn't be a warning...
#017
?????
DLOG(WARNING) << "Got data for a nonexistant or finished request";
#018
?????
return;
#019
???
}
#020
?
這里找到相應的請求標識號,就把數據放到請求信息里處理。
#021
???
PendingRequestInfo& request_info = it->second;
#022
?
#023
???
if (data_len > 0 && shared_mem.Map(data_len)) {
#024
?????
RESOURCE_LOG("Dispatching " << data_len << " bytes for " <<
#025
??????????????????
request_info.peer->GetURLForDebugging());
#026
?????
const char* data = static_cast<char*>(shared_mem.memory());
#027
?????
request_info.peer->OnReceivedData(data, data_len);
#028
???
}
#029
?
}
上面這個函數實現接收到
HTTP
數據,并且把數據放到請求的緩沖區里,但它沒有知道什么時候接收數據完成,顯然有另外一個消息來做這些的工作,就是下面類
ResourceDispatcherHost
的函數:
#001
???
bool OnResponseCompleted(int request_id, const URLRequestStatus& status) {
#002
?????
receiver_->Send(new ViewMsg_Resource_RequestComplete(
#003
?????????
routing_id_, request_id, status));
#004
?
#005
?????
// If we still have a read buffer, then see about caching it for later...
#006
?????
if (spare_read_buffer_) {
#007
???????
read_buffer_.reset();
#008
?????
} else if (read_buffer_.get() && read_buffer_->memory()) {
#009
???????
spare_read_buffer_ = read_buffer_.release();
#010
?????
}
#011
?????
return true;
#012
???
}
這個函數里通過發送消息
ViewMsg_Resource_RequestComplete
來通知資源進程已經把網絡的數據接收完成了,可以進入下一步處理。然后在資源進程里就會處理這個消息,下一次再來分析這方面的代碼。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
