Verify good connection before using it.

This commit is contained in:
jmjatlanta 2017-08-03 13:36:53 -05:00
parent d1d4d19fa8
commit ef53c886a0

View file

@ -82,23 +82,27 @@ void* ipfs_bitswap_engine_peer_request_processor_start(void* ctx) {
break; break;
} }
if (current_peer_entry->connection_type == CONNECTION_TYPE_CONNECTED) { if (current_peer_entry->connection_type == CONNECTION_TYPE_CONNECTED) {
libp2p_logger_debug("bitswap_engine", "We're connected to this peer. Lets see if there is a message waiting for us.\n"); if (current_peer_entry->sessionContext == NULL || current_peer_entry->sessionContext->default_stream == NULL) {
int retVal = current_peer_entry->sessionContext->default_stream->peek(current_peer_entry->sessionContext); current_peer_entry->connection_type = CONNECTION_TYPE_NOT_CONNECTED;
if (retVal < 0) { } else {
libp2p_logger_debug("bitswap_engine", "We thought we were connected, but Peek reported an error.\n"); libp2p_logger_debug("bitswap_engine", "We're connected to this peer. Lets see if there is a message waiting for us.\n");
libp2p_peer_handle_connection_error(current_peer_entry); int retVal = current_peer_entry->sessionContext->default_stream->peek(current_peer_entry->sessionContext);
} else if (retVal > 0) { if (retVal < 0) {
libp2p_logger_debug("bitswap_engine", "Something waiting on network for peer %s.\n", current_peer_entry->id); libp2p_logger_debug("bitswap_engine", "We thought we were connected, but Peek reported an error.\n");
unsigned char* buffer = NULL; libp2p_peer_handle_connection_error(current_peer_entry);
size_t buffer_len = 0; } else if (retVal > 0) {
if (current_peer_entry->sessionContext->default_stream->read(current_peer_entry->sessionContext, &buffer, &buffer_len, 1)) { libp2p_logger_debug("bitswap_engine", "Something waiting on network for peer %s.\n", current_peer_entry->id);
// handle it unsigned char* buffer = NULL;
int retVal = ipfs_multistream_marshal(buffer, buffer_len, current_peer_entry->sessionContext, context->ipfsNode); size_t buffer_len = 0;
did_some_processing = 1; if (current_peer_entry->sessionContext->default_stream->read(current_peer_entry->sessionContext, &buffer, &buffer_len, 1)) {
if (retVal == -1) { // handle it
// there was a problem. Clean up int retVal = ipfs_multistream_marshal(buffer, buffer_len, current_peer_entry->sessionContext, context->ipfsNode);
current_peer_entry->connection_type = CONNECTION_TYPE_NOT_CONNECTED; did_some_processing = 1;
libp2p_session_context_free(current_peer_entry->sessionContext); if (retVal == -1) {
// there was a problem. Clean up
current_peer_entry->connection_type = CONNECTION_TYPE_NOT_CONNECTED;
libp2p_session_context_free(current_peer_entry->sessionContext);
}
} }
} }
} }