上一次說(shuō)到通過(guò)管道把接收到的
HTTP
數(shù)據(jù)通知另一個(gè)線程處理,它不是直接發(fā)送數(shù)據(jù)過(guò)去,而是把數(shù)據(jù)在共享內(nèi)存里的句柄發(fā)送過(guò)去,達(dá)到高效通訊的目的。下面就來(lái)分析資源處理進(jìn)程里,接收到這個(gè)消息之后,做些什么處理。這個(gè)消息的處理代碼如下:
#001
?
void ResourceDispatcher::OnReceivedData(int request_id,
#002
?????????????????????????????????????????
SharedMemoryHandle shm_handle,
#003
?????????????????????????????????????????
int data_len) {
#004
???
// Acknowlegde the reception of this data.
回應(yīng)這個(gè)消息,說(shuō)已經(jīng)收到數(shù)據(jù)了。
#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));
打開(kāi)共享內(nèi)存文件,使用只讀的方式。
#011
???
SharedMemory shared_mem(shm_handle, true);
?
// read only
#012
?
查找到請(qǐng)求下載的資源的請(qǐng)求標(biāo)識(shí)號(hào)。
#013
???
PendingRequestList::iterator it = pending_requests_.find(request_id);
如果沒(méi)有找到相應(yīng)的請(qǐng)求標(biāo)識(shí)號(hào),就直接返回,不用處理這些數(shù)據(jù)。
#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
?
這里找到相應(yīng)的請(qǐng)求標(biāo)識(shí)號(hào),就把數(shù)據(jù)放到請(qǐng)求信息里處理。
#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
?
}
上面這個(gè)函數(shù)實(shí)現(xiàn)接收到
HTTP
數(shù)據(jù),并且把數(shù)據(jù)放到請(qǐng)求的緩沖區(qū)里,但它沒(méi)有知道什么時(shí)候接收數(shù)據(jù)完成,顯然有另外一個(gè)消息來(lái)做這些的工作,就是下面類
ResourceDispatcherHost
的函數(shù):
#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
???
}
這個(gè)函數(shù)里通過(guò)發(fā)送消息
ViewMsg_Resource_RequestComplete
來(lái)通知資源進(jìn)程已經(jīng)把網(wǎng)絡(luò)的數(shù)據(jù)接收完成了,可以進(jìn)入下一步處理。然后在資源進(jìn)程里就會(huì)處理這個(gè)消息,下一次再來(lái)分析這方面的代碼。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元

