clean up peer request queue after message send

This commit is contained in:
jmjatlanta 2017-08-02 10:52:35 -05:00
parent e5e565272e
commit 17dbad3bce
3 changed files with 27 additions and 4 deletions

View file

@ -672,13 +672,30 @@ int ipfs_bitswap_message_add_wantlist_items(struct BitswapMessage* message, stru
return 1;
}
/***
* Look through vector for specific Cid, then mark it cancel
* @param vector the vector of CidEntrys
* @param incoming_cid the cid to look for
* @returns true(1) if found one, false(0) if not
*/
int ipfs_bitswap_message_cancel_cid(struct Libp2pVector* vector, struct Cid* incoming_cid) {
for(int i = 0; i < vector->total; i++) {
struct CidEntry* entry = (struct CidEntry*)libp2p_utils_vector_get(vector, i);
if (ipfs_cid_compare(entry->cid, incoming_cid) == 0) {
entry->cancel = 1;
return 1;
}
}
return 0;
}
/***
* Add the blocks to the BitswapMessage
* @param message the message
* @param blocks the requested blocks
* @returns true(1) on success, false(0) otherwise
*/
int ipfs_bitswap_message_add_blocks(struct BitswapMessage* message, struct Libp2pVector* blocks) {
int ipfs_bitswap_message_add_blocks(struct BitswapMessage* message, struct Libp2pVector* blocks, struct Libp2pVector* cids_they_want) {
// bitswap 1.0 uses blocks, bitswap 1.1 uses payload
if (message == NULL)
@ -690,9 +707,15 @@ int ipfs_bitswap_message_add_blocks(struct BitswapMessage* message, struct Libp2
if (message->payload == NULL)
return 0;
}
for(int i = 0; i < blocks->total; i++) {
int tot_blocks = blocks->total;
for(int i = 0; i < tot_blocks; i++) {
const struct Block* current = (const struct Block*) libp2p_utils_vector_get(blocks, i);
libp2p_utils_vector_add(message->payload, current);
ipfs_bitswap_message_cancel_cid(cids_they_want, current->cid);
}
for (int i = 0; i < tot_blocks; i++) {
libp2p_utils_vector_delete(blocks, 0);
}
return 1;
}

View file

@ -321,7 +321,7 @@ int ipfs_bitswap_peer_request_process_entry(const struct BitswapContext* context
struct BitswapMessage* msg = ipfs_bitswap_message_new();
// see if we can fulfill any of their requests. If so, fill in msg->payload
ipfs_bitswap_peer_request_get_blocks_they_want(context, request);
ipfs_bitswap_message_add_blocks(msg, request->blocks_we_want_to_send);
ipfs_bitswap_message_add_blocks(msg, request->blocks_we_want_to_send, request->cids_they_want);
// add requests that we would like
ipfs_bitswap_message_add_wantlist_items(msg, request->cids_we_want);
// send message

View file

@ -215,4 +215,4 @@ int ipfs_bitswap_message_add_wantlist_items(struct BitswapMessage* message, stru
* @param blocks the requested blocks
* @returns true(1) on success, false(0) otherwise
*/
int ipfs_bitswap_message_add_blocks(struct BitswapMessage* message, struct Libp2pVector* blocks);
int ipfs_bitswap_message_add_blocks(struct BitswapMessage* message, struct Libp2pVector* blocks, struct Libp2pVector* cids_they_want);