bt软件下载开发完整版-01(编辑修改稿)内容摘要:

p = pnext。 } } return 0。 } 454 Linux 系统 下的 C 编程  int read_slice_from_harddisk(Btcache *node) 功能:从硬盘读一个 slice 的数据,存放到缓冲区中,在 peer 需要时发送给 peer。 该函数非常类似于 write_btcache_node_to_harddisk,限于篇幅此处不再列出。 具体代码请参考本书所附源代码。  int write_piece_to_harddisk(int sequence,Peer *peer) 功能:检查下载完的一个 piece 的数据是否正确,若正确则写入文件。 参数: sequence 是存放 piece 的第一个 slice 的 Btcache 结点编号,该值的范围为 0~ 1023 中16 的整数值函数实现代码如下所示: int write_piece_to_harddisk(int sequence,Peer *peer) { Btcache *node_ptr = btcache_head, *p。 unsigned char piece_hash1[20], piece_hash2[20]。 int slice_count = piece_length / (16*1024)。 // 一个 piece所含的 slice数 int index, index_copy。 if(peer==NULL) return 1。 int i = 0。 while(i sequence) { node_ptr = node_ptrnext。 i++。 } p = node_ptr。 // p指针指向 piece的第一个 slice所在的 btcache结点 // 计算刚刚下载到的这个 piece的 hash 值 SHA1_CTX ctx。 SHA1Init(amp。 ctx)。 while(slice_count0 amp。 amp。 node_ptr!=NULL) { SHA1Update(amp。 ctx,node_ptrbuff,16*1024)。 slice_count。 node_ptr = node_ptrnext。 } SHA1Final(piece_hash1,amp。 ctx)。 // 从种子文件中获取该 piece的正确的 hash值 index = pindex * 20。 index_copy = pindex。 // 存放 piece的 index for(i = 0。 i 20。 i++) piece_hash2[i] = pieces[index+i]。 // 比较两个 hash值,若两者一致说明下载了一个正确的 piece int ret = memcmp(piece_hash1,piece_hash2,20)。 if(ret != 0) { printf(piece hash is wrong\n)。 return 1。 } // 将该 piece的所有 slice写入文件 node_ptr = p。 slice_count = piece_length / (16*1024)。 while(slice_count 0) { write_btcache_node_to_harddisk(node_ptr)。 // 在 peer的请求队列中删除 piece请 求 Request_piece *req_p = peerRequest_piece_head。 Request_piece *req_q = peerRequest_piece_head。 while(req_p != NULL) { if(req_pbegin==node_ptrbegin amp。 amp。 req_pindex==node_ptrindex) { if(req_p == peerRequest_piece_head) 455 项目实践: BT 下载软件的开发 第章 13 peerRequest_piece_head = req_pnext。 else req_qnext = req_pnext。 free(req_p)。 req_p = req_q = NULL。 break。 } req_q = req_p。 req_p = req_pnext。 } node_ptrindex = 1。 node_ptrbegin = 1。 node_ptrlength = 1。 node_ptrin_use = 0。 node_ptrread_write = 1。 node_ptris_full = 0。 node_ptris_writed = 0。 node_ptraccess_count = 0。 node_ptr = node_ptrnext。 slice_count。 } // 当前处于终端模式,则在 peer链表中删除所有对该 piece 的请求 if(end_mode == 1) delete_request_end_mode(index_copy)。 // 更新位图 set_bit_value(bitmap,index_copy,1)。 // 保存 piece的 index,准备给所有的 peer 发送 have消息 for(i = 0。 i 64。 i++) { if(have_piece_index[i] == 1) { have_piece_index[i] = index_copy。 break。 } } // 更新 download_piece_num,每下载 10个 piece就将位图写入文件 download_piece_num++。 if(download_piece_num % 10 == 0) restore_bitmap()。 // 打印出提示信息 printf(%%%%%% Total piece download:%d %%%%%%\n,download_piece_num)。 printf(writed piece index:%d \n,index_copy)。 return 0。 }  int read_piece_from_harddisk(Btcache *p, int index) 功能:从硬盘上的文件中读取一个 piece 到 p 指针所指向的缓冲区中。 函数实现的代码如下: int read_piece_from_harddisk(Btcache *p, int index) { Btcache *node_ptr = p。 int begin = 0。 int length = 16*1024。 int slice_count = piece_length / (16*1024)。 int ret。 if(p==NULL || index=pieces_length/20) return 1。 while(slice_count 0) { node_ptrindex = index。 node_ptrbegin = begin。 node_ptrlength = length。 456 Linux 系统 下的 C 编程 ret = read_slice_from_harddisk(node_ptr)。 if(ret 0) return 1。 node_ptrin_use = 1。 node_ptrread_write = 0。 node_ptris_full = 1。 node_ptris_writed = 0。 node_ptraccess_count = 0。 begin += 16*1024。 slice_count。 node_ptr = node_ptrnext。 } return 0。 }  int write_btcache_to_harddisk(Peer *peer) 功能:将整个缓冲区中已下载的 piece 写入硬盘,这样可以释放缓冲区。 函数实现的代码如下: int write_btcache_to_harddisk(Peer *peer) { Btcache *p = btcache_head。 int slice_count = piece_length / (16*1024)。 int index_count = 0。 int full_count = 0。 int first_index。 while(p != NULL) { if(index_count % slice_count == 0) { full_count = 0。 first_index = index_count。 } if( (pin_use == 1) amp。 amp。 (pread_write == 1) amp。 amp。 (pis_full == 1) amp。 amp。 (pis_writed == 0) ) { full_count++。 } if(full_count == slice_count) { write_piece_to_harddisk(first_index,peer)。 } index_count++。 p = pnext。 } return 0。 }  int release_read_btcache_node(int base_count) 功能:当缓冲区不够用时,释放那些从硬盘上读取的 piece。 函数实现代码如下: int release_read_btcache_node(int base_count) { Btcache *p = btcache_head。 Btcache *q = NULL。 int count = 0。 int used_count = 0。 int slice_count = piece_length / (16*1024)。 if(base_count 0) return 1。 while(p != NULL) { 457 项目实践: BT 下载软件的开发 第章 13 if(count % slice_count == 0) { used_count = 0。 q = p。 }。
阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。 用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。